diff --git a/flang/lib/Semantics/expression.cpp b/flang/lib/Semantics/expression.cpp --- a/flang/lib/Semantics/expression.cpp +++ b/flang/lib/Semantics/expression.cpp @@ -953,19 +953,11 @@ std::move(designator.u)); } -// Components of parent derived types are explicitly represented as such. static std::optional CreateComponent( DataRef &&base, const Symbol &component, const semantics::Scope &scope) { - if (&component.owner() == &scope) { + if (scope.FindComponent(component.name())) { return Component{std::move(base), component}; } - if (const semantics::Scope * parentScope{scope.GetDerivedTypeParent()}) { - if (const Symbol * parentComponent{parentScope->GetSymbol()}) { - return CreateComponent( - DataRef{Component{std::move(base), *parentComponent}}, component, - *parentScope); - } - } return std::nullopt; } diff --git a/flang/test/Evaluate/folding12.f90 b/flang/test/Evaluate/folding12.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Evaluate/folding12.f90 @@ -0,0 +1,23 @@ +! RUN: %S/test_folding.sh %s %t %f18 +! Test folding of structure constructors +module m + type parent_type + integer :: parent_field + end type parent_type + type, extends(parent_type) :: child_type + integer :: child_field + end type child_type + type(child_type), parameter :: child_var1 = child_type(343, 2) + logical, parameter :: test_direct = child_var1%child_field == 2 + logical, parameter :: test_parent = child_var1%parent_field == 343 + type(child_type), parameter :: array_var(2) = & + [child_type(343, 2), child_type(256, 4)] + logical, parameter :: test_array_child = array_var(2)%child_field == 4 + logical, parameter :: test_array_parent = array_var(2)%parent_field == 256 + type array_type + real, dimension(3) :: real_field + end type array_type + type(array_type), parameter :: array_var2 = & + array_type([(real(i*i), i = 1,3)]) + logical, parameter :: test_array_var = array_var2%real_field(2) == 4.0 +end module