diff --git a/flang/lib/Semantics/symbol.cpp b/flang/lib/Semantics/symbol.cpp --- a/flang/lib/Semantics/symbol.cpp +++ b/flang/lib/Semantics/symbol.cpp @@ -541,13 +541,12 @@ const Symbol *Symbol::GetParentComponent(const Scope *scope) const { if (const auto *dtDetails{detailsIf()}) { - if (!scope) { - scope = scope_; + const Scope *localScope = scope ? scope : scope_; + if (localScope) { + return dtDetails->GetParentComponent(DEREF(localScope)); } - return dtDetails->GetParentComponent(DEREF(scope)); - } else { - return nullptr; } + return nullptr; } void DerivedTypeDetails::add_component(const Symbol &symbol) { diff --git a/flang/test/Semantics/bad-forward-type.f90 b/flang/test/Semantics/bad-forward-type.f90 --- a/flang/test/Semantics/bad-forward-type.f90 +++ b/flang/test/Semantics/bad-forward-type.f90 @@ -70,3 +70,13 @@ type, extends(undef) :: t end type end subroutine + +subroutine s8 + !ERROR: Derived type 't2' was used but never defined + !ERROR: The derived type 't2' was forward-referenced but not defined + implicit type(t2)(x) + parameter(y=t2(12.3)) + type t2 + real :: c + end type +end subroutine