From a054c882053e63e6fce7b412a93dc7fc228f11fd Mon Sep 17 00:00:00 2001 From: Peter Klausler Date: Mon, 2 May 2022 13:55:29 -0700 Subject: [PATCH] [flang] Ensure that structure constructors fold parameter references Structure contructors for instances of parameterized derived types must have their components' values folded in the context of the values of the type parameters. Differential Revision: https://reviews.llvm.org/D125116 --- flang/lib/Evaluate/fold.cpp | 1 + flang/test/Semantics/structconst05.f90 | 9 +++++++++ 2 files changed, 10 insertions(+) create mode 100644 flang/test/Semantics/structconst05.f90 diff --git a/flang/lib/Evaluate/fold.cpp b/flang/lib/Evaluate/fold.cpp index ae8b586f4717..92ea4f130df6 100644 --- a/flang/lib/Evaluate/fold.cpp +++ b/flang/lib/Evaluate/fold.cpp @@ -67,6 +67,7 @@ Expr FoldOperation( FoldingContext &context, StructureConstructor &&structure) { StructureConstructor ctor{structure.derivedTypeSpec()}; bool isConstant{true}; + auto restorer{context.WithPDTInstance(structure.derivedTypeSpec())}; for (auto &&[symbol, value] : std::move(structure)) { auto expr{Fold(context, std::move(value.value()))}; if (IsPointer(symbol)) { diff --git a/flang/test/Semantics/structconst05.f90 b/flang/test/Semantics/structconst05.f90 new file mode 100644 index 000000000000..236a890660d9 --- /dev/null +++ b/flang/test/Semantics/structconst05.f90 @@ -0,0 +1,9 @@ +! RUN: %python %S/test_errors.py %s %flang_fc1 +! Ensure that PDT instance structure constructors can be folded to constants +module m1 + type :: pdt(k) + integer, len :: k + character(len=k) :: x, y = "def" + end type + type(pdt(4)) :: v = pdt(4)("abc") +end module