Skip to content

Commit b23d24d

Browse files
committed
[flang] Fix name resolution bug
When the current scope is an implied DO loop nested within a derived type declaration, it is possible for name resolution to mistakenly resolve a name to a component rather than to a name in the outer scope. Fix. Fixes #158412.
1 parent 8ae3aea commit b23d24d

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

flang/lib/Semantics/resolve-names.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2994,12 +2994,20 @@ Symbol *ScopeHandler::FindSymbol(const Scope &scope, const parser::Name &name) {
29942994
}
29952995
}
29962996
return FindSymbol(scope.parent(), name);
2997-
} else {
2997+
} else if (scope.kind() == Scope::Kind::ImpliedDos) {
2998+
if (Symbol * symbol{FindInScope(scope, name)}) {
2999+
return Resolve(name, symbol);
3000+
} else {
3001+
// Don't use scope.FindSymbol() as below, since implied DO scopes
3002+
// can be parts of initializers in derived type components.
3003+
return FindSymbol(scope.parent(), name);
3004+
}
3005+
} else if (inEquivalenceStmt_) {
29983006
// In EQUIVALENCE statements only resolve names in the local scope, see
29993007
// 19.5.1.4, paragraph 2, item (10)
3000-
return Resolve(name,
3001-
inEquivalenceStmt_ ? FindInScope(scope, name)
3002-
: scope.FindSymbol(name.source));
3008+
return Resolve(name, FindInScope(scope, name));
3009+
} else {
3010+
return Resolve(name, scope.FindSymbol(name.source));
30033011
}
30043012
}
30053013

@@ -8722,7 +8730,6 @@ const parser::Name *DeclarationVisitor::ResolveName(const parser::Name &name) {
87228730
return &name;
87238731
}
87248732
}
8725-
87268733
if (CheckForHostAssociatedImplicit(name)) {
87278734
NotePossibleBadForwardRef(name);
87288735
return &name;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
!RUN: %flang_fc1 -fdebug-unparse %s 2>&1 | FileCheck %s
2+
double precision, parameter :: x = 1.0d0
3+
type t
4+
!CHECK: REAL :: x(1_4) = [INTEGER(4)::8_4]
5+
real :: x(1) = [(kind(x),j=1,1)]
6+
end type
7+
end

0 commit comments

Comments
 (0)