[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
This commit is contained in:
Peter Klausler 2022-05-02 13:55:29 -07:00
parent 879a47a55f
commit a054c88205
2 changed files with 10 additions and 0 deletions

View File

@ -67,6 +67,7 @@ Expr<SomeDerived> FoldOperation(
FoldingContext &context, StructureConstructor &&structure) { FoldingContext &context, StructureConstructor &&structure) {
StructureConstructor ctor{structure.derivedTypeSpec()}; StructureConstructor ctor{structure.derivedTypeSpec()};
bool isConstant{true}; bool isConstant{true};
auto restorer{context.WithPDTInstance(structure.derivedTypeSpec())};
for (auto &&[symbol, value] : std::move(structure)) { for (auto &&[symbol, value] : std::move(structure)) {
auto expr{Fold(context, std::move(value.value()))}; auto expr{Fold(context, std::move(value.value()))};
if (IsPointer(symbol)) { if (IsPointer(symbol)) {

View File

@ -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