Skip to content

Commit a51d92a

Browse files
authored
[flang] Fix crash in semantics on error case (#91482)
An erroneous statement function declaration exposed an unhandled situation in a utility routine in semantics. Patch that hole and add a test. Fixes #91429.
1 parent 19b41f4 commit a51d92a

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

flang/lib/Semantics/tools.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,15 +256,17 @@ static const Symbol &FollowHostAssoc(const Symbol &symbol) {
256256
}
257257

258258
bool IsHostAssociated(const Symbol &symbol, const Scope &scope) {
259-
return DoesScopeContain(
260-
&GetProgramUnitOrBlockConstructContaining(FollowHostAssoc(symbol)),
261-
GetProgramUnitOrBlockConstructContaining(scope));
259+
const Symbol &base{FollowHostAssoc(symbol)};
260+
return base.owner().IsTopLevel() ||
261+
DoesScopeContain(&GetProgramUnitOrBlockConstructContaining(base),
262+
GetProgramUnitOrBlockConstructContaining(scope));
262263
}
263264

264265
bool IsHostAssociatedIntoSubprogram(const Symbol &symbol, const Scope &scope) {
265-
return DoesScopeContain(
266-
&GetProgramUnitOrBlockConstructContaining(FollowHostAssoc(symbol)),
267-
GetProgramUnitContaining(scope));
266+
const Symbol &base{FollowHostAssoc(symbol)};
267+
return base.owner().IsTopLevel() ||
268+
DoesScopeContain(&GetProgramUnitOrBlockConstructContaining(base),
269+
GetProgramUnitContaining(scope));
268270
}
269271

270272
bool IsInStmtFunction(const Symbol &symbol) {

flang/test/Semantics/stmt-func01.f90

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,11 @@ subroutine s4
8383
!ERROR: VOLATILE attribute may apply only to a variable
8484
sf(x) = 1.
8585
end
86+
87+
subroutine s5
88+
!ERROR: Invalid specification expression: reference to impure function 'k'
89+
real x(k())
90+
!WARNING: Name 'k' from host scope should have a type declaration before its local statement function definition
91+
!ERROR: 'k' is already declared in this scoping unit
92+
k() = 0.0
93+
end

0 commit comments

Comments
 (0)