Skip to content

Commit a4c5d00

Browse files
committed
[flang] Fix scope in which undeclared symbols are created
Don't create new symbols in FORALL, implied DO, or other construct scopes when an undeclared name appears; use the innermost enclosing program unit's scope. This clears up a pending TODO in name resolution, and also exposes (& fixes) an unnoticed name resolution problem in a module file test. Differential Revision: https://reviews.llvm.org/D109095
1 parent f411be0 commit a4c5d00

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

flang/lib/Semantics/resolve-names.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -459,9 +459,8 @@ class ScopeHandler : public ImplicitRulesVisitor {
459459
Scope &currScope() { return DEREF(currScope_); }
460460
// The enclosing host procedure if current scope is in an internal procedure
461461
Scope *GetHostProcedure();
462-
// The enclosing scope, skipping blocks and derived types.
463-
// TODO: Will return the scope of a FORALL or implied DO loop; is this ok?
464-
// If not, should call FindProgramUnitContaining() instead.
462+
// The innermost enclosing program unit scope, ignoring BLOCK and other
463+
// construct scopes.
465464
Scope &InclusiveScope();
466465
// The enclosing scope, skipping derived types.
467466
Scope &NonDerivedTypeScope();
@@ -2015,12 +2014,21 @@ void ScopeHandler::Say2(const parser::Name &name, MessageFixedText &&msg1,
20152014
context().SetError(symbol, msg1.isFatal());
20162015
}
20172016

2018-
// T may be `Scope` or `const Scope`
2017+
// This is essentially GetProgramUnitContaining(), but it can return
2018+
// a mutable Scope &, it ignores statement functions, and it fails
2019+
// gracefully for error recovery (returning the original Scope).
20192020
template <typename T> static T &GetInclusiveScope(T &scope) {
20202021
for (T *s{&scope}; !s->IsGlobal(); s = &s->parent()) {
2021-
if (s->kind() != Scope::Kind::Block && !s->IsDerivedType() &&
2022-
!s->IsStmtFunction()) {
2023-
return *s;
2022+
switch (s->kind()) {
2023+
case Scope::Kind::Module:
2024+
case Scope::Kind::MainProgram:
2025+
case Scope::Kind::Subprogram:
2026+
case Scope::Kind::BlockData:
2027+
if (!s->IsStmtFunction()) {
2028+
return *s;
2029+
}
2030+
break;
2031+
default:;
20242032
}
20252033
}
20262034
return scope;

flang/test/Semantics/modfile26.f90

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,15 @@ end module m1
7171
!intrinsic::all
7272
!integer(4),parameter::intpvals(1_8:*)=[INTEGER(4)::0_4,2_4,3_4,4_4,5_4,9_4,10_4,18_4,19_4,38_4,39_4]
7373
!integer(4),parameter::intpkinds(1_8:*)=[INTEGER(4)::1_4,1_4,2_4,2_4,4_4,4_4,8_4,8_4,16_4,16_4,-1_4]
74+
!intrinsic::selected_int_kind
7475
!intrinsic::size
7576
!logical(4),parameter::ipcheck=.true._4
7677
!integer(4),parameter::realprecs(1_8:*)=[INTEGER(4)::3_4,2_4,6_4,15_4,18_4,33_4]
7778
!intrinsic::precision
7879
!logical(4),parameter::rpreccheck=.true._4
7980
!integer(4),parameter::realpvals(1_8:*)=[INTEGER(4)::0_4,3_4,4_4,6_4,7_4,15_4,16_4,18_4,19_4,33_4,34_4]
8081
!integer(4),parameter::realpkinds(1_8:*)=[INTEGER(4)::2_4,2_4,4_4,4_4,8_4,8_4,10_4,10_4,16_4,16_4,-1_4]
82+
!intrinsic::selected_real_kind
8183
!logical(4),parameter::realpcheck=.true._4
8284
!integer(4),parameter::realranges(1_8:*)=[INTEGER(4)::4_4,37_4,37_4,307_4,4931_4,4931_4]
8385
!logical(4),parameter::rrangecheck=.true._4

0 commit comments

Comments
 (0)