Skip to content

Commit bbc27fb

Browse files
authored
[flang] Refine checks on assignments to coarrays (#129966)
F'2023 10.2.1.2 paragraph 2 imposes some requirements on the left-hand sides of assignments when they have coindices, and one was not checked while another was inaccurately checked. In short, intrinsic assignment to a coindexed object can't change its type, and neither can it affect allocatable components.
1 parent f6fc29d commit bbc27fb

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

flang/lib/Semantics/expression.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3345,15 +3345,24 @@ const Assignment *ExpressionAnalyzer::Analyze(const parser::AssignmentStmt &x) {
33453345
"in a non-pointer intrinsic assignment statement");
33463346
analyzer.CheckForAssumedRank("in an assignment statement");
33473347
const Expr<SomeType> &lhs{analyzer.GetExpr(0)};
3348-
if (auto dyType{lhs.GetType()};
3349-
dyType && dyType->IsPolymorphic()) { // 10.2.1.2p1(1)
3350-
const Symbol *lastWhole0{UnwrapWholeSymbolOrComponentDataRef(lhs)};
3351-
const Symbol *lastWhole{
3352-
lastWhole0 ? &ResolveAssociations(*lastWhole0) : nullptr};
3353-
if (!lastWhole || !IsAllocatable(*lastWhole)) {
3354-
Say("Left-hand side of assignment may not be polymorphic unless assignment is to an entire allocatable"_err_en_US);
3355-
} else if (evaluate::IsCoarray(*lastWhole)) {
3356-
Say("Left-hand side of assignment may not be polymorphic if it is a coarray"_err_en_US);
3348+
if (auto dyType{lhs.GetType()}) {
3349+
if (dyType->IsPolymorphic()) { // 10.2.1.2p1(1)
3350+
const Symbol *lastWhole0{UnwrapWholeSymbolOrComponentDataRef(lhs)};
3351+
const Symbol *lastWhole{
3352+
lastWhole0 ? &ResolveAssociations(*lastWhole0) : nullptr};
3353+
if (!lastWhole || !IsAllocatable(*lastWhole)) {
3354+
Say("Left-hand side of assignment may not be polymorphic unless assignment is to an entire allocatable"_err_en_US);
3355+
} else if (evaluate::IsCoarray(*lastWhole)) {
3356+
Say("Left-hand side of assignment may not be polymorphic if it is a coarray"_err_en_US);
3357+
}
3358+
}
3359+
if (auto *derived{GetDerivedTypeSpec(*dyType)}) {
3360+
if (auto iter{FindAllocatableUltimateComponent(*derived)}) {
3361+
if (ExtractCoarrayRef(lhs)) {
3362+
Say("Left-hand side of assignment must not be coindexed due to allocatable ultimate component '%s'"_err_en_US,
3363+
iter.BuildResultDesignatorName());
3364+
}
3365+
}
33573366
}
33583367
}
33593368
}

flang/test/Semantics/assign11.f90

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@ program test
44
class(*), allocatable :: pa
55
class(*), pointer :: pp
66
class(*), allocatable :: pac[:]
7+
type t
8+
real, allocatable :: a
9+
end type
10+
type(t) auc[*]
711
pa = 1 ! ok
812
!ERROR: Left-hand side of assignment may not be polymorphic unless assignment is to an entire allocatable
913
pp = 1
1014
!ERROR: Left-hand side of assignment may not be polymorphic if it is a coarray
1115
pac = 1
16+
!ERROR: Left-hand side of assignment must not be coindexed due to allocatable ultimate component '%a'
17+
auc[1] = t()
1218
end

0 commit comments

Comments
 (0)