From ac64c7b987f15bcce09b616c1ee0deaf2b7b587e Mon Sep 17 00:00:00 2001 From: Jean Perier Date: Tue, 14 Jun 2022 14:32:39 +0200 Subject: [PATCH] [flang] Support PDT declaration with initial comp value in internal procedure Lowering was crashing with "fatal internal error: node has not been analyzed" if a PDT with initial component value was defined inside an internal procedure. This is because the related expression cannot be analyzed without the component values (which happens at the instatiation). These expression do not need to be visited (the instantiations, if any will be). Use the form of GetExpr that tolerates the parse tree expression to not be analyzed into an evaluate::Expr when looking through the symbols used in an internal procedure. Note that the PDTs TODO will then fire (it happens after the PFT analysis) as expected if the derived type is used. Differential Revision: https://reviews.llvm.org/D127735 --- flang/lib/Lower/PFTBuilder.cpp | 4 +++- flang/test/Lower/host-associated.f90 | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/flang/lib/Lower/PFTBuilder.cpp b/flang/lib/Lower/PFTBuilder.cpp index ca0df83..2e2e024 100644 --- a/flang/lib/Lower/PFTBuilder.cpp +++ b/flang/lib/Lower/PFTBuilder.cpp @@ -1768,7 +1768,9 @@ struct SymbolVisitor { 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 index 5d57304..aa1f8b0 100644 --- a/flang/test/Lower/host-associated.f90 +++ b/flang/test/Lower/host-associated.f90 @@ -661,3 +661,25 @@ subroutine test_11a 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 -- 2.7.4