diff --git a/flang/lib/Lower/PFTBuilder.cpp b/flang/lib/Lower/PFTBuilder.cpp --- a/flang/lib/Lower/PFTBuilder.cpp +++ b/flang/lib/Lower/PFTBuilder.cpp @@ -1451,6 +1451,14 @@ // in some cases. Non-dummy procedures don't. if (semantics::IsProcedure(sym) && !isProcedurePointerOrDummy) return 0; + // Derived type component symbols may be collected by "CollectSymbols" + // below when processing something like "real :: x(derived%component)". The + // symbol "component" has "ObjectEntityDetails", but it should not be + // instantiated: it is is part of "derived" that should be the only one to + // be instantiated. + if (sym.owner().IsDerivedType()) + return 0; + semantics::Symbol ultimate = sym.GetUltimate(); if (const auto *details = ultimate.detailsIf()) { diff --git a/flang/test/Lower/HLFIR/convert-variable.f90 b/flang/test/Lower/HLFIR/convert-variable.f90 --- a/flang/test/Lower/HLFIR/convert-variable.f90 +++ b/flang/test/Lower/HLFIR/convert-variable.f90 @@ -95,3 +95,16 @@ ! CHECK-LABEL: func.func @_QPscalar_numeric_parameter() { ! CHECK: %[[VAL_0:.*]] = fir.address_of(@_QFscalar_numeric_parameterECp) : !fir.ref ! CHECK: %[[VAL_1:.*]] = hlfir.declare %[[VAL_0]] {fortran_attrs = #fir.var_attrs, uniq_name = "_QFscalar_numeric_parameterECp"} : (!fir.ref) -> (!fir.ref, !fir.ref) + +subroutine test_component_in_spec_expr(x, derived) + type t + integer :: component + end type + type(t) :: derived + ! Test that we do not try to instantiate "component" just because + ! its symbol appears in a specification expression. + real :: x(derived%component) +end subroutine +! CHECK-LABEL: func.func @_QPtest_component_in_spec_expr( +! CHECK-NOT: alloca +! CHECK: return