Skip to content

Commit 233f750

Browse files
authored
[flang] Catch more bad pointer initialization targets (llvm#83731)
A pointer variable initialization or pointer component default initialization cannot reference another pointer. Fixes llvm#82944.
1 parent 9f67f19 commit 233f750

File tree

2 files changed

+76
-18
lines changed

2 files changed

+76
-18
lines changed

flang/lib/Evaluate/check-expression.cpp

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ class IsInitialDataTargetHelper
250250
}
251251
}
252252
return false;
253+
} else if (!CheckVarOrComponent(ultimate)) {
254+
return false;
253255
} else if (!ultimate.attrs().test(semantics::Attr::TARGET)) {
254256
if (messages_) {
255257
messages_->Say(
@@ -267,7 +269,7 @@ class IsInitialDataTargetHelper
267269
}
268270
return false;
269271
} else {
270-
return CheckVarOrComponent(ultimate);
272+
return true;
271273
}
272274
}
273275
bool operator()(const StaticDataObject &) const { return false; }
@@ -318,24 +320,23 @@ class IsInitialDataTargetHelper
318320
private:
319321
bool CheckVarOrComponent(const semantics::Symbol &symbol) {
320322
const Symbol &ultimate{symbol.GetUltimate()};
321-
if (IsAllocatable(ultimate)) {
322-
if (messages_) {
323-
messages_->Say(
324-
"An initial data target may not be a reference to an ALLOCATABLE '%s'"_err_en_US,
325-
ultimate.name());
326-
emittedMessage_ = true;
327-
}
328-
return false;
329-
} else if (ultimate.Corank() > 0) {
330-
if (messages_) {
331-
messages_->Say(
332-
"An initial data target may not be a reference to a coarray '%s'"_err_en_US,
333-
ultimate.name());
334-
emittedMessage_ = true;
335-
}
336-
return false;
323+
const char *unacceptable{nullptr};
324+
if (ultimate.Corank() > 0) {
325+
unacceptable = "a coarray";
326+
} else if (IsAllocatable(ultimate)) {
327+
unacceptable = "an ALLOCATABLE";
328+
} else if (IsPointer(ultimate)) {
329+
unacceptable = "a POINTER";
330+
} else {
331+
return true;
337332
}
338-
return true;
333+
if (messages_) {
334+
messages_->Say(
335+
"An initial data target may not be a reference to %s '%s'"_err_en_US,
336+
unacceptable, ultimate.name());
337+
emittedMessage_ = true;
338+
}
339+
return false;
339340
}
340341

341342
parser::ContextualMessages *messages_;

flang/test/Semantics/init01.f90

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ subroutine objectpointers(j)
88
real, save :: x3
99
real, target :: x4
1010
real, target, save :: x5(10)
11+
real, pointer :: x6
12+
type t1
13+
real, allocatable :: c1
14+
real, allocatable, codimension[:] :: c2
15+
real :: c3
16+
real :: c4(10)
17+
real, pointer :: c5
18+
end type
19+
type(t1), target, save :: o1
20+
type(t1), save :: o2
21+
type(t1), target :: o3
1122
!ERROR: An initial data target may not be a reference to an ALLOCATABLE 'x1'
1223
real, pointer :: p1 => x1
1324
!ERROR: An initial data target may not be a reference to a coarray 'x2'
@@ -20,6 +31,52 @@ subroutine objectpointers(j)
2031
real, pointer :: p5 => x5(j)
2132
!ERROR: Pointer has rank 0 but target has rank 1
2233
real, pointer :: p6 => x5
34+
!ERROR: An initial data target may not be a reference to a POINTER 'x6'
35+
real, pointer :: p7 => x6
36+
!ERROR: An initial data target may not be a reference to an ALLOCATABLE 'c1'
37+
real, pointer :: p1o => o1%c1
38+
!ERROR: An initial data target may not be a reference to a coarray 'c2'
39+
real, pointer :: p2o => o1%c2
40+
!ERROR: An initial data target may not be a reference to an object 'o2' that lacks the TARGET attribute
41+
real, pointer :: p3o => o2%c3
42+
!ERROR: An initial data target may not be a reference to an object 'o3' that lacks the SAVE attribute
43+
real, pointer :: p4o => o3%c3
44+
!ERROR: An initial data target must be a designator with constant subscripts
45+
real, pointer :: p5o => o1%c4(j)
46+
!ERROR: Pointer has rank 0 but target has rank 1
47+
real, pointer :: p6o => o1%c4
48+
!ERROR: An initial data target may not be a reference to a POINTER 'c5'
49+
real, pointer :: p7o => o1%c5
50+
type t2
51+
!ERROR: An initial data target may not be a reference to an ALLOCATABLE 'x1'
52+
real, pointer :: p1 => x1
53+
!ERROR: An initial data target may not be a reference to a coarray 'x2'
54+
real, pointer :: p2 => x2
55+
!ERROR: An initial data target may not be a reference to an object 'x3' that lacks the TARGET attribute
56+
real, pointer :: p3 => x3
57+
!ERROR: An initial data target may not be a reference to an object 'x4' that lacks the SAVE attribute
58+
real, pointer :: p4 => x4
59+
!ERROR: An initial data target must be a designator with constant subscripts
60+
real, pointer :: p5 => x5(j)
61+
!ERROR: Pointer has rank 0 but target has rank 1
62+
real, pointer :: p6 => x5
63+
!ERROR: An initial data target may not be a reference to a POINTER 'x6'
64+
real, pointer :: p7 => x6
65+
!ERROR: An initial data target may not be a reference to an ALLOCATABLE 'c1'
66+
real, pointer :: p1o => o1%c1
67+
!ERROR: An initial data target may not be a reference to a coarray 'c2'
68+
real, pointer :: p2o => o1%c2
69+
!ERROR: An initial data target may not be a reference to an object 'o2' that lacks the TARGET attribute
70+
real, pointer :: p3o => o2%c3
71+
!ERROR: An initial data target may not be a reference to an object 'o3' that lacks the SAVE attribute
72+
real, pointer :: p4o => o3%c3
73+
!ERROR: An initial data target must be a designator with constant subscripts
74+
real, pointer :: p5o => o1%c4(j)
75+
!ERROR: Pointer has rank 0 but target has rank 1
76+
real, pointer :: p6o => o1%c4
77+
!ERROR: An initial data target may not be a reference to a POINTER 'c5'
78+
real, pointer :: p7o => o1%c5
79+
end type
2380

2481
!TODO: type incompatibility, non-deferred type parameter values, contiguity
2582

0 commit comments

Comments
 (0)