[flang] Fix crash in semantics after PDT instantiation
authorPeter Klausler <pklausler@nvidia.com>
Thu, 19 May 2022 00:48:34 +0000 (17:48 -0700)
committerPeter Klausler <pklausler@nvidia.com>
Tue, 24 May 2022 18:06:12 +0000 (11:06 -0700)
The code in semantics that reinitializes symbol table pointers in
the parse tree of a parameterized derived type prior to a new
instantiation of the type was processing the symbols of the
derived type instantiation scope in arbitrary address order,
which could fail if a reference to a type parameter inherited from
an ancestor type was processed prior to the parent component sequence.
Fix by instantiating components of PDT instantiations in declaration
order.

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

flang/lib/Semantics/type.cpp

index f6888a7..98ad4fd 100644 (file)
@@ -373,8 +373,11 @@ void DerivedTypeSpec::Instantiate(Scope &containingScope) {
 }
 
 void InstantiateHelper::InstantiateComponents(const Scope &fromScope) {
-  for (const auto &pair : fromScope) {
-    InstantiateComponent(*pair.second);
+  // Instantiate symbols in declaration order; this ensures that
+  // parent components and type parameters of ancestor types exist
+  // by the time that they're needed.
+  for (SymbolRef ref : fromScope.GetSymbols()) {
+    InstantiateComponent(*ref);
   }
   ComputeOffsets(context(), scope_);
 }
@@ -396,7 +399,7 @@ public:
 
   void Post(const parser::Name &name) {
     if (name.symbol && name.symbol->has<TypeParamDetails>()) {
-      name.symbol = scope_.FindSymbol(name.source);
+      name.symbol = scope_.FindComponent(name.source);
     }
   }