Skip to content

[flang] More support for anonymous parent components in struct constr… #102642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions flang/lib/Semantics/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2015,6 +2015,15 @@ MaybeExpr ExpressionAnalyzer::Analyze(
// initialize X or A by name, but not both.
auto components{semantics::OrderedComponentIterator{spec}};
auto nextAnonymous{components.begin()};
auto afterLastParentComponentIter{components.end()};
if (parentComponent) {
for (auto iter{components.begin()}; iter != components.end(); ++iter) {
if (iter->test(Symbol::Flag::ParentComp)) {
afterLastParentComponentIter = iter;
++afterLastParentComponentIter;
}
}
}

std::set<parser::CharBlock> unavailable;
bool anyKeyword{false};
Expand Down Expand Up @@ -2060,20 +2069,22 @@ MaybeExpr ExpressionAnalyzer::Analyze(
}
// Here's a regrettably common extension of the standard: anonymous
// initialization of parent components, e.g., T(PT(1)) rather than
// T(1) or T(PT=PT(1)).
if (nextAnonymous == components.begin() && parentComponent &&
valueType == DynamicType::From(*parentComponent) &&
// T(1) or T(PT=PT(1)). There may be multiple parent components.
if (nextAnonymous == components.begin() && parentComponent && valueType &&
context().IsEnabled(LanguageFeature::AnonymousParents)) {
auto iter{
std::find(components.begin(), components.end(), *parentComponent)};
if (iter != components.end()) {
symbol = parentComponent;
nextAnonymous = ++iter;
if (context().ShouldWarn(LanguageFeature::AnonymousParents)) {
Say(source,
"Whole parent component '%s' in structure "
"constructor should not be anonymous"_port_en_US,
symbol->name());
for (auto parent{components.begin()};
parent != afterLastParentComponentIter; ++parent) {
if (auto parentType{DynamicType::From(*parent)}; parentType &&
parent->test(Symbol::Flag::ParentComp) &&
valueType->IsEquivalentTo(*parentType)) {
symbol = &*parent;
nextAnonymous = ++parent;
if (context().ShouldWarn(LanguageFeature::AnonymousParents)) {
Say(source,
"Whole parent component '%s' in structure constructor should not be anonymous"_port_en_US,
symbol->name());
}
break;
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions flang/test/Semantics/structconst10.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -Werror
module m1
type a1
integer ::x1=1
end type a1
type,extends(a1)::a2
integer ::x2=3
end type a2
type,extends(a2)::a3
integer ::x3=3
end type a3
end module m1

program test
use m1
type(a3) v
!PORTABILITY: Whole parent component 'a2' in structure constructor should not be anonymous
v=a3(a2(x1=18,x2=6),x3=6)
!PORTABILITY: Whole parent component 'a1' in structure constructor should not be anonymous
v=a3(a1(x1=18),x2=6,x3=6)
!PORTABILITY: Whole parent component 'a2' in structure constructor should not be anonymous
!PORTABILITY: Whole parent component 'a1' in structure constructor should not be anonymous
v=a3(a2(a1(x1=18),x2=6),x3=6)
v=a3(a2=a2(a1=a1(x1=18),x2=6),x3=6) ! ok
end
Loading