Index: flang/lib/Semantics/resolve-names.cpp =================================================================== --- flang/lib/Semantics/resolve-names.cpp +++ flang/lib/Semantics/resolve-names.cpp @@ -5188,7 +5188,6 @@ CHECK(scope.symbol()); CHECK(scope.symbol()->scope() == &scope); auto &details{scope.symbol()->get()}; - details.set_isForwardReferenced(false); std::set paramNames; for (auto ¶mName : std::get>(stmt.statement.t)) { details.add_paramName(paramName.source); @@ -5240,6 +5239,7 @@ } Walk(std::get>(x.t)); Walk(std::get>(x.t)); + details.set_isForwardReferenced(false); derivedTypeInfo_ = {}; PopScope(); return false; @@ -5255,7 +5255,13 @@ auto *extendsName{derivedTypeInfo_.extends}; std::optional extendsType{ ResolveExtendsType(name, extendsName)}; - auto &symbol{MakeSymbol(name, GetAttrs(), DerivedTypeDetails{})}; + DerivedTypeDetails derivedTypeDetails; + if (Symbol *typeSymbol{FindInScope(currScope(), name)}; typeSymbol && + typeSymbol->has() && + typeSymbol->get().isForwardReferenced()) { + derivedTypeDetails.set_isForwardReferenced(true); + } + auto &symbol{MakeSymbol(name, GetAttrs(), std::move(derivedTypeDetails))}; symbol.ReplaceName(name.source); derivedTypeInfo_.type = &symbol; PushScope(Scope::Kind::DerivedType, &symbol); Index: flang/test/Semantics/typeinfo05.f90 =================================================================== --- /dev/null +++ flang/test/Semantics/typeinfo05.f90 @@ -0,0 +1,16 @@ +!RUN: bbc --dump-symbols %s | FileCheck %s +!RUN: %flang_fc1 -fdebug-dump-symbols %s | FileCheck %s +! Ensure that cycles via POINTERs do not instantiate incomplete derived +! types that would lead to types whose sizeinbytes=0 +program main + implicit none + type t1 + type(t2), pointer :: b + end type t1 +!CHECK: .dt.t1, SAVE, TARGET (CompilerCreated, ReadOnly): ObjectEntity type: TYPE(derivedtype) init:derivedtype(binding=NULL(),name=.n.t1,sizeinbytes=40_8,uninstantiated=NULL(),kindparameter=NULL(),lenparameterkind=NULL(),component=.c.t1,procptr=NULL(),special=NULL(),specialbitset=0_4,hasparent=0_1,noinitializationneeded=0_1,nodestructionneeded=1_1,nofinalizationneeded=1_1) + type :: t2 + type(t1) :: a + end type t2 +! CHECK: .dt.t2, SAVE, TARGET (CompilerCreated, ReadOnly): ObjectEntity type: TYPE(derivedtype) init:derivedtype(binding=NULL(),name=.n.t2,sizeinbytes=40_8,uninstantiated=NULL(),kindparameter=NULL(),lenparameterkind=NULL(),component=.c.t2,procptr=NULL(),special=NULL(),specialbitset=0_4,hasparent=0_1,noinitializationneeded=0_1,nodestructionneeded=1_1,nofinalizationneeded=1_1) +end program main + Index: flang/test/Semantics/typeinfo06.f90 =================================================================== --- /dev/null +++ flang/test/Semantics/typeinfo06.f90 @@ -0,0 +1,16 @@ +!RUN: bbc --dump-symbols %s | FileCheck %s +!RUN: %flang_fc1 -fdebug-dump-symbols %s | FileCheck %s +! Ensure that cycles via ALLOCATABLEs do not instantiate incomplete derived +! types that would lead to types whose sizeinbytes=0 +program main + implicit none + type t1 + type(t2), allocatable :: b + end type t1 +!CHECK: .dt.t1, SAVE, TARGET (CompilerCreated, ReadOnly): ObjectEntity type: TYPE(derivedtype) init:derivedtype(binding=NULL(),name=.n.t1,sizeinbytes=40_8,uninstantiated=NULL(),kindparameter=NULL(),lenparameterkind=NULL(),component=.c.t1,procptr=NULL(),special=NULL(),specialbitset=0_4,hasparent=0_1,noinitializationneeded=0_1,nodestructionneeded=0_1,nofinalizationneeded=1_1) + type :: t2 + type(t1) :: a + end type t2 +! CHECK: .dt.t2, SAVE, TARGET (CompilerCreated, ReadOnly): ObjectEntity type: TYPE(derivedtype) init:derivedtype(binding=NULL(),name=.n.t2,sizeinbytes=40_8,uninstantiated=NULL(),kindparameter=NULL(),lenparameterkind=NULL(),component=.c.t2,procptr=NULL(),special=NULL(),specialbitset=0_4,hasparent=0_1,noinitializationneeded=0_1,nodestructionneeded=0_1,nofinalizationneeded=1_1) +end program main +