Skip to content

Commit 2472b68

Browse files
committed
[flang] Add one semantic check for masked array assignment
As Fortran 2018 states, in each where-assignment-stmt, the mask-expr and the variable being defined shall be arrays of the same shape. The previous check does not consider checking if it is an array. Reviewed By: klausler Differential Revision: https://reviews.llvm.org/D125022
1 parent ffc7f9d commit 2472b68

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

flang/lib/Semantics/assignment.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,13 @@ bool AssignmentContext::CheckForPureContext(const SomeExpr &lhs,
216216
return true;
217217
}
218218

219-
// 10.2.3.1(2) The masks and LHS of assignments must all have the same shape
219+
// 10.2.3.1(2) The masks and LHS of assignments must be arrays of the same shape
220220
void AssignmentContext::CheckShape(parser::CharBlock at, const SomeExpr *expr) {
221221
if (auto shape{evaluate::GetShape(foldingContext(), expr)}) {
222222
std::size_t size{shape->size()};
223+
if (size == 0) {
224+
Say(at, "The mask or variable must not be scalar"_err_en_US);
225+
}
223226
if (whereDepth_ == 0) {
224227
whereExtents_.resize(size);
225228
} else if (whereExtents_.size() != size) {

flang/test/Semantics/assign01.f90

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,26 @@ subroutine s3
5252
end where
5353
end where
5454
end
55+
56+
subroutine s4
57+
integer :: x1 = 0, x2(2) = 0
58+
logical :: l1 = .false., l2(2) = (/.true., .false./), l3 = .false.
59+
!ERROR: The mask or variable must not be scalar
60+
where (l1)
61+
!ERROR: The mask or variable must not be scalar
62+
x1 = 1
63+
end where
64+
!ERROR: The mask or variable must not be scalar
65+
where (l1)
66+
!ERROR: The mask or variable must not be scalar
67+
where (l3)
68+
!ERROR: The mask or variable must not be scalar
69+
x1 = 1
70+
end where
71+
end where
72+
!ERROR: The mask or variable must not be scalar
73+
where (l2(2))
74+
!ERROR: The mask or variable must not be scalar
75+
x2(2) = 1
76+
end where
77+
end

flang/test/Semantics/assign04.f90

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,25 @@ subroutine s13()
185185
where ([1==1]) x='*'
186186
where ([1==1]) n='*' ! fine
187187
forall (j=1:1)
188+
!ERROR: The mask or variable must not be scalar
188189
where (j==1)
189190
!ERROR: Defined assignment in WHERE must be elemental, but 'ctor' is not
191+
!ERROR: The mask or variable must not be scalar
190192
x(j)='?'
193+
!ERROR: The mask or variable must not be scalar
191194
n(j)='?' ! fine
195+
!ERROR: The mask or variable must not be scalar
192196
elsewhere (.false.)
193197
!ERROR: Defined assignment in WHERE must be elemental, but 'ctor' is not
198+
!ERROR: The mask or variable must not be scalar
194199
x(j)='1'
200+
!ERROR: The mask or variable must not be scalar
195201
n(j)='1' ! fine
196202
elsewhere
197203
!ERROR: Defined assignment in WHERE must be elemental, but 'ctor' is not
204+
!ERROR: The mask or variable must not be scalar
198205
x(j)='9'
206+
!ERROR: The mask or variable must not be scalar
199207
n(j)='9' ! fine
200208
end where
201209
end forall

0 commit comments

Comments
 (0)