Index: flang/lib/Semantics/resolve-names.cpp =================================================================== --- flang/lib/Semantics/resolve-names.cpp +++ flang/lib/Semantics/resolve-names.cpp @@ -2938,21 +2938,29 @@ bool ModuleVisitor::BeginSubmodule( const parser::Name &name, const parser::ParentIdentifier &parentId) { - auto &ancestorName{std::get(parentId.t)}; - auto &parentName{std::get>(parentId.t)}; + const auto &ancestorName{std::get(parentId.t)}; + Scope *parentScope{nullptr}; Scope *ancestor{FindModule(ancestorName, false /*not intrinsic*/)}; - if (!ancestor) { - return false; + if (ancestor) { + if (const auto &parentName{ + std::get>(parentId.t)}) { + parentScope = FindModule(*parentName, false /*not intrinsic*/, ancestor); + } else { + parentScope = ancestor; + } } - Scope *parentScope{parentName - ? FindModule(*parentName, false /*not intrinsic*/, ancestor) - : ancestor}; - if (!parentScope) { - return false; + if (parentScope) { + PushScope(*parentScope); + } else { + // Error recovery: there's no ancestor scope, so create a dummy one to + // hold the submodule's scope. + SourceName dummyName{context().GetTempName(currScope())}; + Symbol &dummySymbol{MakeSymbol(dummyName, Attrs{}, ModuleDetails{false})}; + PushScope(Scope::Kind::Module, &dummySymbol); + parentScope = &currScope(); } - PushScope(*parentScope); // submodule is hosted in parent BeginModule(name, true); - if (!ancestor->AddSubmodule(name.source, currScope())) { + if (ancestor && !ancestor->AddSubmodule(name.source, currScope())) { Say(name, "Module '%s' already has a submodule named '%s'"_err_en_US, ancestorName.source, name.source); }