Skip to content

Commit 4dc981f

Browse files
committed
[flang][runtime] Check SOURCE= conformability on ALLOCATE
The SOURCE= expression of an ALLOCATE statement, when present and not scalar, must conform to the shape of the allocated objects. Check this at runtime, and return a recoverable error, or crash, when appropriate. Fixes #143900.
1 parent 52a6492 commit 4dc981f

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

flang-rt/lib/runtime/allocatable.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,26 @@ int RTDEF(AllocatableAllocateSource)(Descriptor &alloc,
165165
alloc, /*asyncObject=*/nullptr, hasStat, errMsg, sourceFile, sourceLine)};
166166
if (stat == StatOk) {
167167
Terminator terminator{sourceFile, sourceLine};
168+
if (alloc.rank() != source.rank() && source.rank() != 0) {
169+
terminator.Crash("ALLOCATE object has rank %d while SOURCE= has rank %d",
170+
alloc.rank(), source.rank());
171+
}
172+
if (int rank{source.rank()}; rank > 0) {
173+
SubscriptValue allocExtent[maxRank], sourceExtent[maxRank];
174+
alloc.GetShape(allocExtent);
175+
source.GetShape(sourceExtent);
176+
for (int j{0}; j < rank; ++j) {
177+
if (allocExtent[j] != sourceExtent[j]) {
178+
if (!hasStat) {
179+
terminator.Crash("ALLOCATE object has extent %jd on dimension %d, "
180+
"but SOURCE= has extent %jd",
181+
static_cast<std::intmax_t>(allocExtent[j]), j + 1,
182+
static_cast<std::intmax_t>(sourceExtent[j]));
183+
}
184+
return StatInvalidExtent;
185+
}
186+
}
187+
}
168188
DoFromSourceAssign(alloc, source, terminator);
169189
}
170190
return stat;

0 commit comments

Comments
 (0)