[flang] Avoid crash from forward referenced derived type
authorPeter Klausler <pklausler@nvidia.com>
Mon, 11 Jul 2022 17:08:01 +0000 (10:08 -0700)
committerPeter Klausler <pklausler@nvidia.com>
Wed, 13 Jul 2022 23:58:26 +0000 (16:58 -0700)
Fortran permits forward references to derived types in contexts that don't
require knowledge of the derived type definition for semantic analysis,
such as in the declaration of a pointer or allocatable variable or component.
But when the forward-referenced derived type is used later for a component
reference, it is possible for the DerivedTypeSpec in he base variable or component
declaration to still have a null scope pointer even if the type has been defined,
since DerivedTypeSpec and TypeSpec objects are created in scopes of use
rather than in scopes of definition.  The fix is to call
DerivedTypeSpec::Instantiate() in the name resolution of each component
name so that the scope gets filled in if it is still null.

Differential Revision: https://reviews.llvm.org/D129681

flang/lib/Semantics/resolve-names.cpp
flang/test/Semantics/symbol21.f90 [new file with mode: 0644]

index a859073b45152160f41fcb9e1b674cd00e512528..93c6315cf4c9a749346fae403e4afe405c64741f 100644 (file)
@@ -4418,7 +4418,6 @@ void DeclarationVisitor::Post(const parser::DerivedTypeSpec &x) {
       spec->AddRawParamValue(optKeyword, std::move(param));
     }
   }
-
   // The DerivedTypeSpec *spec is used initially as a search key.
   // If it turns out to have the same name and actual parameter
   // value expressions as another DerivedTypeSpec in the current
@@ -6645,7 +6644,8 @@ const parser::Name *DeclarationVisitor::FindComponent(
       MakePlaceholder(component, miscKind);
       return &component;
     }
-  } else if (const DerivedTypeSpec * derived{type->AsDerived()}) {
+  } else if (DerivedTypeSpec * derived{type->AsDerived()}) {
+    derived->Instantiate(currScope()); // in case of forward referenced type
     if (const Scope * scope{derived->scope()}) {
       if (Resolve(component, scope->FindComponent(component.source))) {
         if (auto msg{
diff --git a/flang/test/Semantics/symbol21.f90 b/flang/test/Semantics/symbol21.f90
new file mode 100644 (file)
index 0000000..b2d5af6
--- /dev/null
@@ -0,0 +1,30 @@
+! RUN: %python %S/test_symbols.py %s %flang_fc1
+! Derived type forward reference regression case
+
+ !DEF: /MainProgram1/t2 DerivedType
+ type :: t2
+  !DEF: /MainProgram1/t1 DerivedType
+  !DEF: /MainProgram1/t2/ptr POINTER ObjectEntity TYPE(t1)
+  type(t1), pointer :: ptr
+ end type
+ !REF: /MainProgram1/t1
+ type :: t1
+  !DEF: /MainProgram1/t1/a ObjectEntity REAL(4)
+  real :: a
+  !REF: /MainProgram1/t2
+  !DEF: /MainProgram1/t1/p2 POINTER ObjectEntity TYPE(t2)
+  type(t2), pointer :: p2
+  !REF: /MainProgram1/t1
+  !DEF: /MainProgram1/t1/p1 POINTER ObjectEntity TYPE(t1)
+  type(t1), pointer :: p1
+ end type
+ !REF: /MainProgram1/t1
+ !DEF: /MainProgram1/x1 POINTER ObjectEntity TYPE(t1)
+ !DEF: /MainProgram1/x2 POINTER ObjectEntity TYPE(t1)
+ type(t1), pointer :: x1, x2
+ !REF: /MainProgram1/x2
+ !REF: /MainProgram1/t1/p1
+ !REF: /MainProgram1/t1/a
+ !REF: /MainProgram1/x1
+ x2%p1%a = x1%a
+end program