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 @@ -1768,7 +1768,9 @@ template bool Pre(const A &x) { if constexpr (Fortran::parser::HasTypedExpr::value) - if (const auto *expr = Fortran::semantics::GetExpr(x)) + // Some parse tree Expr may legitimately be un-analyzed after semantics + // (for instance PDT component initial value in the PDT definition body). + if (const auto *expr = Fortran::semantics::GetExpr(nullptr, x)) visitExpr(*expr); return true; } diff --git a/flang/test/Lower/host-associated.f90 b/flang/test/Lower/host-associated.f90 --- a/flang/test/Lower/host-associated.f90 +++ b/flang/test/Lower/host-associated.f90 @@ -661,3 +661,25 @@ external test_11b call test_11c(test_11b, 3) end subroutine test_11a + +subroutine test_PDT_with_init_do_not_crash_host_symbol_analysis() + integer :: i + call sub() +contains + subroutine sub() + ! PDT definition symbols maps to un-analyzed expression, + ! check this does not crash the visit of the internal procedure + ! parse-tree to get the list of captured host variables. + type type1 (k) + integer, KIND :: k + integer :: x = k + end type + type type2 (k, l) + integer, KIND :: k = 4 + integer, LEN :: l = 2 + integer :: x = 10 + real :: y = 20 + end type + print *, i + end subroutine +end subroutine