Skip to content

Commit d20f55f

Browse files
authored
[flang] Silence bogus error on local proc pointer initializer (#116663)
A procedure pointer is allowed to be initialized with the subprogram in which it is local, assuming that other requirements are satisfied. Add a good test for local procedure pointer initialization, as no test existed for the error message in question. Fixes #116566.
1 parent 300370c commit d20f55f

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

flang/lib/Semantics/check-declarations.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,8 @@ void CheckHelper::CheckPointerInitialization(const Symbol &symbol) {
11101110
if (proc->init() && *proc->init()) {
11111111
// C1519 - must be nonelemental external or module procedure,
11121112
// or an unrestricted specific intrinsic function.
1113-
const Symbol &ultimate{(*proc->init())->GetUltimate()};
1113+
const Symbol &local{DEREF(*proc->init())};
1114+
const Symbol &ultimate{local.GetUltimate()};
11141115
bool checkTarget{true};
11151116
if (ultimate.attrs().test(Attr::INTRINSIC)) {
11161117
if (auto intrinsic{context_.intrinsics().IsSpecificIntrinsicFunction(
@@ -1123,11 +1124,12 @@ void CheckHelper::CheckPointerInitialization(const Symbol &symbol) {
11231124
ultimate.name(), symbol.name());
11241125
checkTarget = false;
11251126
}
1126-
} else if ((!ultimate.attrs().test(Attr::EXTERNAL) &&
1127-
ultimate.owner().kind() != Scope::Kind::Module) ||
1127+
} else if (!(ultimate.attrs().test(Attr::EXTERNAL) ||
1128+
ultimate.owner().kind() == Scope::Kind::Module ||
1129+
ultimate.owner().IsTopLevel()) ||
11281130
IsDummy(ultimate) || IsPointer(ultimate)) {
1129-
context_.Say("Procedure pointer '%s' initializer '%s' is neither "
1130-
"an external nor a module procedure"_err_en_US,
1131+
context_.Say(
1132+
"Procedure pointer '%s' initializer '%s' is neither an external nor a module procedure"_err_en_US,
11311133
symbol.name(), ultimate.name());
11321134
checkTarget = false;
11331135
} else if (IsElementalProcedure(ultimate)) {

flang/test/Semantics/pointer02.f90

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
! RUN: %python %S/test_errors.py %s %flang_fc1
2+
recursive subroutine sub(dp, dpp)
3+
procedure(inner) dp
4+
procedure(inner), pointer :: dpp
5+
procedure(inner) ext
6+
procedure(sub), pointer :: p1 => sub ! ok
7+
procedure(inner), pointer :: p2 => ext ! ok
8+
!ERROR: Procedure pointer 'p3' initializer 'inner' is neither an external nor a module procedure
9+
procedure(inner), pointer :: p3 => inner
10+
!ERROR: Procedure pointer 'p4' initializer 'dp' is neither an external nor a module procedure
11+
procedure(inner), pointer :: p4 => dp
12+
!ERROR: Procedure pointer 'p5' initializer 'dpp' is neither an external nor a module procedure
13+
procedure(inner), pointer :: p5 => dpp
14+
generic :: generic => ext
15+
!ERROR: 'generic' must be an abstract interface or a procedure with an explicit interface
16+
procedure(generic), pointer :: p6 ! => generic
17+
contains
18+
subroutine inner
19+
end
20+
end
21+
recursive function fun() result(res)
22+
procedure(fun), pointer :: p1 => fun ! ok
23+
!ERROR: Procedure pointer 'p2' initializer 'inner' is neither an external nor a module procedure
24+
procedure(inner), pointer :: p2 => inner
25+
res = 0.
26+
contains
27+
function inner()
28+
inner = 0.
29+
end
30+
end
31+
module m
32+
procedure(msub), pointer :: ps1 => msub ! ok
33+
procedure(mfun), pointer :: pf1 => mfun ! ok
34+
contains
35+
recursive subroutine msub
36+
procedure(msub), pointer :: ps2 => msub ! ok
37+
!ERROR: Procedure pointer 'ps3' initializer 'inner' is neither an external nor a module procedure
38+
procedure(inner), pointer :: ps3 => inner
39+
contains
40+
subroutine inner
41+
end
42+
end
43+
recursive function mfun() result(res)
44+
procedure(mfun), pointer :: pf2 => mfun ! ok
45+
!ERROR: Procedure pointer 'pf3' initializer 'inner' is neither an external nor a module procedure
46+
procedure(inner), pointer :: pf3 => inner
47+
res = 0.
48+
contains
49+
function inner()
50+
inner = 0.
51+
end
52+
end
53+
end

0 commit comments

Comments
 (0)