diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp --- a/flang/lib/Semantics/resolve-names.cpp +++ b/flang/lib/Semantics/resolve-names.cpp @@ -594,7 +594,7 @@ protected: // Apply the implicit type rules to this symbol. void ApplyImplicitRules(Symbol &); - const DeclTypeSpec *GetImplicitType(Symbol &); + const DeclTypeSpec *GetImplicitType(Symbol &, const Scope &); bool ConvertToObjectEntity(Symbol &); bool ConvertToProcEntity(Symbol &); @@ -2111,7 +2111,12 @@ void ScopeHandler::ApplyImplicitRules(Symbol &symbol) { if (NeedsType(symbol)) { - if (const DeclTypeSpec * type{GetImplicitType(symbol)}) { + const Scope *scope{&symbol.owner()}; + if (scope->IsGlobal()) { + scope = &currScope(); + } + if (const DeclTypeSpec * + type{GetImplicitType(symbol, GetInclusiveScope(*scope))}) { symbol.set(Symbol::Flag::Implicit); symbol.SetType(*type); return; @@ -2137,9 +2142,9 @@ } } -const DeclTypeSpec *ScopeHandler::GetImplicitType(Symbol &symbol) { - const auto *type{implicitRulesMap_->at(&GetInclusiveScope(symbol.owner())) - .GetType(symbol.name())}; +const DeclTypeSpec *ScopeHandler::GetImplicitType( + Symbol &symbol, const Scope &scope) { + const auto *type{implicitRulesMap_->at(&scope).GetType(symbol.name())}; if (type) { if (const DerivedTypeSpec * derived{type->AsDerived()}) { // Resolve any forward-referenced derived type; a quick no-op else.