[flang-commits] [flang] [flang] Use instantiated PDT for structure constructor in default init (PR #167409)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Mon Nov 10 14:53:34 PST 2025


https://github.com/klausler created https://github.com/llvm/llvm-project/pull/167409

A structure constructor used in (or as) the default component initializer for a PDT derived type component needs to traverse the scope of the right PDT instantiation.

Fixes https://github.com/llvm/llvm-project/issues/167337.

>From e2503bb7ef8651163a842f8a414854212edddde7 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Mon, 10 Nov 2025 14:49:54 -0800
Subject: [PATCH] [flang] Use instantiated PDT for structure constructor in
 default init

A structure constructor used in (or as) the default component initializer
for a PDT derived type component needs to traverse the scope of the right PDT
instantiation.

Fixes https://github.com/llvm/llvm-project/issues/167337.
---
 flang/lib/Semantics/expression.cpp     | 10 +++++++---
 flang/test/Semantics/structconst12.f90 | 21 +++++++++++----------
 2 files changed, 18 insertions(+), 13 deletions(-)

diff --git a/flang/lib/Semantics/expression.cpp b/flang/lib/Semantics/expression.cpp
index ac58dfc005f17..18a724473e7be 100644
--- a/flang/lib/Semantics/expression.cpp
+++ b/flang/lib/Semantics/expression.cpp
@@ -2103,17 +2103,21 @@ static MaybeExpr ImplicitConvertTo(const Symbol &sym, Expr<SomeType> &&expr,
 }
 
 MaybeExpr ExpressionAnalyzer::CheckStructureConstructor(
-    parser::CharBlock typeName, const semantics::DerivedTypeSpec &spec,
+    parser::CharBlock typeName, const semantics::DerivedTypeSpec &spec0,
     std::list<ComponentSpec> &&componentSpecs) {
+  semantics::Scope &scope{context_.FindScope(typeName)};
+  const semantics::DeclTypeSpec &instantiatedType{
+      semantics::FindOrInstantiateDerivedType(scope,
+          semantics::DerivedTypeSpec{spec0},
+          semantics::DeclTypeSpec::TypeDerived)};
+  const semantics::DerivedTypeSpec &spec{instantiatedType.derivedTypeSpec()};
   const Symbol &typeSymbol{spec.typeSymbol()};
   if (!spec.scope() || !typeSymbol.has<semantics::DerivedTypeDetails>()) {
     return std::nullopt; // error recovery
   }
-  const semantics::Scope &scope{context_.FindScope(typeName)};
   const semantics::Scope *pureContext{FindPureProcedureContaining(scope)};
   const auto &typeDetails{typeSymbol.get<semantics::DerivedTypeDetails>()};
   const Symbol *parentComponent{typeDetails.GetParentComponent(*spec.scope())};
-
   if (typeSymbol.attrs().test(semantics::Attr::ABSTRACT)) { // C796
     AttachDeclaration(
         Say(typeName,
diff --git a/flang/test/Semantics/structconst12.f90 b/flang/test/Semantics/structconst12.f90
index 345016b236c8a..f56c5ec2108f4 100644
--- a/flang/test/Semantics/structconst12.f90
+++ b/flang/test/Semantics/structconst12.f90
@@ -1,12 +1,13 @@
 !RUN: %flang_fc1 -fdebug-unparse %s 2>&1 | FileCheck %s
-!CHECK: TYPE(t) :: x = t(pp=f)
-!CHECK-NOT: error:
-interface
-  function f()
-  end
-end interface
-type t
-  procedure(f), nopass, pointer :: pp
+type t1(k1a,k1b)
+  integer, kind :: k1a, k1b
+  integer :: j = -666
+  integer(k1b) :: c1 = k1a
 end type
-type(t) :: x = t(pp=f)
-end
+type t2(k2a,k2b)
+  integer, kind:: k2a, k2b
+  type(t1(k2a+1,k2b*2)) :: c2 = t1(k2a+1,k2b*2)(j=777)
+end type
+type (t2(122,4)), parameter :: x = t2(122,4)()
+!CHECK: TYPE(t2(122_4,4_4)), PARAMETER :: x = t2(k2a=122_4,k2b=4_4)(c2=t1(k1a=123_4,k1b=8_4)(j=777_4,c1=123_8))
+END



More information about the flang-commits mailing list