Skip to content

Commit 5a0382c

Browse files
authored
[flang] Catch non-constant targets for procedure pointer initialization (#86338)
Detect attempts to use non-constant targets, including internal procedures, as initializers for procedure pointers, including components of structure components being used as initializers.
1 parent 3ada883 commit 5a0382c

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

flang/lib/Evaluate/check-expression.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,14 @@ bool IsInitialProcedureTarget(const semantics::Symbol &symbol) {
358358
const auto &ultimate{symbol.GetUltimate()};
359359
return common::visit(
360360
common::visitors{
361-
[](const semantics::SubprogramDetails &subp) {
362-
return !subp.isDummy();
361+
[&](const semantics::SubprogramDetails &subp) {
362+
return !subp.isDummy() && !subp.stmtFunction() &&
363+
symbol.owner().kind() != semantics::Scope::Kind::MainProgram &&
364+
symbol.owner().kind() != semantics::Scope::Kind::Subprogram;
365+
},
366+
[](const semantics::SubprogramNameDetails &x) {
367+
return x.kind() != semantics::SubprogramKind::Internal;
363368
},
364-
[](const semantics::SubprogramNameDetails &) { return true; },
365369
[&](const semantics::ProcEntityDetails &proc) {
366370
return !semantics::IsPointer(ultimate) && !proc.isDummy();
367371
},
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic
2+
! Structure constructors with bad pointer targets
3+
module m
4+
real, target, save :: x
5+
type t
6+
real, pointer :: rp => x
7+
procedure(f), pointer, nopass :: pp => f
8+
end type
9+
contains
10+
real function f()
11+
f = 0.
12+
end
13+
subroutine test(da, dp)
14+
real, target :: y, da
15+
procedure(f) dp
16+
procedure(f), pointer :: lpp
17+
external ext
18+
type(t) :: a1 = t() ! ok
19+
type(t) :: a2 = t(rp=x) ! ok
20+
type(t) :: a3 = t(pp=f) ! ok
21+
type(t) :: a4 = t(pp=ext) ! ok
22+
!ERROR: Must be a constant value
23+
type(t) :: a5 = t(rp=y)
24+
!ERROR: Must be a constant value
25+
type(t) :: a6 = t(rp=da)
26+
!ERROR: Must be a constant value
27+
type(t) :: a7 = t(pp=lpp)
28+
!ERROR: Must be a constant value
29+
type(t) :: a8 = t(pp=internal)
30+
!ERROR: Must be a constant value
31+
type(t) :: a9 = t(pp=dp)
32+
contains
33+
real function internal()
34+
internal = 666.
35+
end
36+
end
37+
end

0 commit comments

Comments
 (0)