Skip to content

Commit a7802a8

Browse files
committed
[flang] Do not return true for pointer sub-object in IsPointerObject
evaluate::IsPointerObject used to return true for pointer suboject like `pointer(10)` while these object are not pointers. This prevented some checks like 15.5.2.7 to be correctly enforced (e.g., it was possible to pass `pointer(10)` to a non intent(in) dummy pointer). After updating IsPointerObject behavior and adding a test for 15.5.2.7 in call07.f90, a test in call03.f90 for 15.5.2.4(14) was failing. It appeared the related semantics check was relying on IsPointerObject to return true for `pointer(10)`. Adapt the code to detect pointer element in another way. While looking at the code, I also noticed that semantics was rejecting `character(1)` pointer/assumed shape suboject when these are allowed (the standard has a special case for character(1) in 15.5.2.4(14), and I verified that other compilers that enforce 15.5.2.4(14) do accept this). Differential Revision: https://reviews.llvm.org/D121377
1 parent 3ed643e commit a7802a8

File tree

4 files changed

+43
-26
lines changed

4 files changed

+43
-26
lines changed

flang/lib/Evaluate/tools.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ bool IsObjectPointer(const Expr<SomeType> &expr, FoldingContext &context) {
767767
return false;
768768
} else if (const auto *funcRef{UnwrapProcedureRef(expr)}) {
769769
return IsVariable(*funcRef);
770-
} else if (const Symbol * symbol{GetLastSymbol(expr)}) {
770+
} else if (const Symbol * symbol{UnwrapWholeSymbolOrComponentDataRef(expr)}) {
771771
return IsPointer(symbol->GetUltimate());
772772
} else {
773773
return false;

flang/lib/Semantics/check-call.cpp

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -309,29 +309,34 @@ static void CheckExplicitDataArg(const characteristics::DummyDataObject &dummy,
309309
"Coindexed scalar actual argument must be associated with a scalar %s"_err_en_US,
310310
dummyName);
311311
}
312-
if (!IsArrayElement(actual) &&
313-
!(actualType.type().category() == TypeCategory::Character &&
314-
actualType.type().kind() == 1) &&
315-
!(dummy.type.type().IsAssumedType() && dummyIsAssumedSize) &&
316-
!dummyIsAssumedRank) {
317-
messages.Say(
318-
"Whole scalar actual argument may not be associated with a %s array"_err_en_US,
319-
dummyName);
320-
}
321-
if (actualIsPolymorphic) {
322-
messages.Say(
323-
"Polymorphic scalar may not be associated with a %s array"_err_en_US,
324-
dummyName);
325-
}
326-
if (actualIsPointer) {
327-
messages.Say(
328-
"Scalar POINTER target may not be associated with a %s array"_err_en_US,
329-
dummyName);
330-
}
331-
if (actualLastSymbol && IsAssumedShape(*actualLastSymbol)) {
332-
messages.Say(
333-
"Element of assumed-shape array may not be associated with a %s array"_err_en_US,
334-
dummyName);
312+
bool actualIsArrayElement{IsArrayElement(actual)};
313+
bool actualIsCKindCharacter{
314+
actualType.type().category() == TypeCategory::Character &&
315+
actualType.type().kind() == 1};
316+
if (!actualIsCKindCharacter) {
317+
if (!actualIsArrayElement &&
318+
!(dummy.type.type().IsAssumedType() && dummyIsAssumedSize) &&
319+
!dummyIsAssumedRank) {
320+
messages.Say(
321+
"Whole scalar actual argument may not be associated with a %s array"_err_en_US,
322+
dummyName);
323+
}
324+
if (actualIsPolymorphic) {
325+
messages.Say(
326+
"Polymorphic scalar may not be associated with a %s array"_err_en_US,
327+
dummyName);
328+
}
329+
if (actualIsArrayElement && actualLastSymbol &&
330+
IsPointer(*actualLastSymbol)) {
331+
messages.Say(
332+
"Element of pointer array may not be associated with a %s array"_err_en_US,
333+
dummyName);
334+
}
335+
if (actualLastSymbol && IsAssumedShape(*actualLastSymbol)) {
336+
messages.Say(
337+
"Element of assumed-shape array may not be associated with a %s array"_err_en_US,
338+
dummyName);
339+
}
335340
}
336341
}
337342
if (actualLastObject && actualLastObject->IsCoarray() &&

flang/test/Semantics/call03.f90

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,21 +196,28 @@ subroutine test08(x) ! 15.5.2.4(13)
196196
subroutine charray(x)
197197
character :: x(10)
198198
end subroutine
199-
subroutine test09(ashape, polyarray, c) ! 15.5.2.4(14), 15.5.2.11
199+
subroutine test09(ashape, polyarray, c, assumed_shape_char) ! 15.5.2.4(14), 15.5.2.11
200200
real :: x, arr(10)
201201
real, pointer :: p(:)
202+
real, pointer :: p_scalar
203+
character(10), pointer :: char_pointer(:)
204+
character(*) :: assumed_shape_char(:)
202205
real :: ashape(:)
203206
class(t) :: polyarray(*)
204207
character(10) :: c(:)
205208
!ERROR: Whole scalar actual argument may not be associated with a dummy argument 'x=' array
206209
call assumedsize(x)
207-
!ERROR: Scalar POINTER target may not be associated with a dummy argument 'x=' array
210+
!ERROR: Whole scalar actual argument may not be associated with a dummy argument 'x=' array
211+
call assumedsize(p_scalar)
212+
!ERROR: Element of pointer array may not be associated with a dummy argument 'x=' array
208213
call assumedsize(p(1))
209214
!ERROR: Element of assumed-shape array may not be associated with a dummy argument 'x=' array
210215
call assumedsize(ashape(1))
211216
!ERROR: Polymorphic scalar may not be associated with a dummy argument 'x=' array
212217
call polyassumedsize(polyarray(1))
213218
call charray(c(1:1)) ! not an error if character
219+
call charray(char_pointer(1)) ! not an error if character
220+
call charray(assumed_shape_char(1)) ! not an error if character
214221
call assumedsize(arr(1)) ! not an error if element in sequence
215222
call assumedrank(x) ! not an error
216223
call assumedtypeandsize(x) ! not an error

flang/test/Semantics/call07.f90

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ subroutine s02(p)
1414
subroutine s03(p)
1515
real, pointer, intent(in) :: p(:)
1616
end subroutine
17+
subroutine s04(p)
18+
real, pointer :: p
19+
end subroutine
1720

1821
subroutine test
1922
!ERROR: CONTIGUOUS POINTER must be an array
@@ -30,6 +33,8 @@ subroutine test
3033
call s03(a03) ! ok
3134
!ERROR: Actual argument associated with POINTER dummy argument 'p=' must also be POINTER unless INTENT(IN)
3235
call s02(a03)
36+
!ERROR: Actual argument associated with POINTER dummy argument 'p=' must also be POINTER unless INTENT(IN)
37+
call s04(a02(1))
3338
!ERROR: An array section with a vector subscript may not be a pointer target
3439
call s03(a03([1,2,4]))
3540
!ERROR: A coindexed object may not be a pointer target

0 commit comments

Comments
 (0)