diff --git a/flang/lib/Evaluate/fold.cpp b/flang/lib/Evaluate/fold.cpp --- a/flang/lib/Evaluate/fold.cpp +++ b/flang/lib/Evaluate/fold.cpp @@ -67,6 +67,7 @@ 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 --- /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