Skip to content

Commit ec92be4

Browse files
committed
Support module selectors for cross-import overlays
1 parent 42fdd1d commit ec92be4

File tree

9 files changed

+85
-14
lines changed

9 files changed

+85
-14
lines changed

include/swift/AST/Module.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,11 @@ class ModuleDecl
481481
void getDeclaredCrossImportBystanders(
482482
SmallVectorImpl<Identifier> &bystanderNames);
483483

484+
/// Returns the name that should be used for this module in a module
485+
/// selector. For separately-imported overlays, this will be the declaring
486+
/// module's name.
487+
Identifier getNameForModuleSelector();
488+
484489
/// Retrieve the ABI name of the module, which is used for metadata and
485490
/// mangling.
486491
Identifier getABIName() const;

lib/AST/Module.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2564,6 +2564,12 @@ bool ModuleDecl::getRequiredBystandersIfCrossImportOverlay(
25642564
return false;
25652565
}
25662566

2567+
Identifier ModuleDecl::getNameForModuleSelector() {
2568+
if (auto declaring = getDeclaringModuleIfCrossImportOverlay())
2569+
return declaring->getName();
2570+
return this->getName();
2571+
}
2572+
25672573
bool ModuleDecl::isClangHeaderImportModule() const {
25682574
auto importer = getASTContext().getClangModuleLoader();
25692575
if (!importer)

lib/AST/NameLookup.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -372,27 +372,33 @@ bool swift::removeOutOfModuleDecls(SmallVectorImpl<ValueDecl*> &decls,
372372
// FIXME: Should we look this up relative to dc?
373373
// We'd need a new ResolutionKind.
374374
// FIXME: How can we diagnose this?
375-
ModuleDecl *visibleFrom = ctx.getLoadedModule(moduleSelector);
376-
if (!visibleFrom && moduleSelector == ctx.TheBuiltinModule->getName())
377-
visibleFrom = ctx.TheBuiltinModule;
378-
if (!visibleFrom) {
375+
ModuleDecl *visibleFromRoot = ctx.getLoadedModule(moduleSelector);
376+
if (!visibleFromRoot && moduleSelector == ctx.TheBuiltinModule->getName())
377+
visibleFromRoot = ctx.TheBuiltinModule;
378+
if (!visibleFromRoot) {
379379
LLVM_DEBUG(llvm::dbgs() << "no module " << moduleSelector << "\n");
380380
bool clearedAny = !decls.empty();
381381
decls.clear();
382382
return clearedAny;
383383
}
384384

385+
SmallVector<ModuleDecl *, 4> visibleFrom;
386+
dc->getSeparatelyImportedOverlays(visibleFromRoot, visibleFrom);
387+
if (visibleFrom.empty())
388+
visibleFrom.push_back(visibleFromRoot);
389+
385390
size_t initialCount = decls.size();
386391
decls.erase(
387392
std::remove_if(decls.begin(), decls.end(), [&](ValueDecl *decl) -> bool {
388-
bool inScope = ctx.getImportCache().isImportedBy(decl->getModuleContext(),
389-
visibleFrom);
393+
bool inScope = llvm::any_of(visibleFrom, [&](ModuleDecl *visibleFromMod) {
394+
return ctx.getImportCache().isImportedBy(decl->getModuleContext(),
395+
visibleFromMod);
396+
});
390397

391398
LLVM_DEBUG(decl->dumpRef(llvm::dbgs()));
392399
LLVM_DEBUG(llvm::dbgs() << ": " << decl->getModuleContext()->getName()
393400
<< (inScope ? " is " : " is NOT ")
394-
<< "selected by " << visibleFrom->getName()
395-
<< "\n");
401+
<< "selected by " << moduleSelector << "\n");
396402

397403
return !inScope;
398404
}),

lib/Sema/CSDiagnostics.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6378,7 +6378,8 @@ bool MemberFromWrongModuleFailure::diagnoseAsError() {
63786378
emitDiagnosticAt(loc, diag::wrong_module_selector, Member->getName(),
63796379
Name.getModuleSelector());
63806380

6381-
Identifier actualModuleName = Member->getModuleContext()->getName();
6381+
Identifier actualModuleName =
6382+
Member->getModuleContext()->getNameForModuleSelector();
63826383
ASSERT(actualModuleName != Name.getModuleSelector() &&
63836384
"Module selector failure on member in same module?");
63846385
emitDiagnosticAt(loc, diag::note_change_module_selector, actualModuleName)

lib/Sema/TypeCheckNameLookup.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,7 +1176,7 @@ ModuleSelectorCorrection(const LookupResult &candidates) {
11761176

11771177
auto owningModule = decl->getModuleContext();
11781178
candidateModules.insert(
1179-
{ owningModule->getName(), kind });
1179+
{ owningModule->getNameForModuleSelector(), kind });
11801180
}
11811181
}
11821182

@@ -1187,7 +1187,7 @@ ModuleSelectorCorrection(const LookupTypeResult &candidates) {
11871187
for (auto result : candidates) {
11881188
auto owningModule = result.Member->getModuleContext();
11891189
candidateModules.insert(
1190-
{ owningModule->getName(), CandidateKind::ContextFree });
1190+
{ owningModule->getNameForModuleSelector(), CandidateKind::ContextFree });
11911191
}
11921192
}
11931193

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
%YAML 1.2
2+
---
3+
version: 1
4+
modules:
5+
- name: _ModuleSelectorTestingKit_ctypes
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// swift-interface-format-version: 1.0
2+
// swift-module-flags: -module-name _ModuleSelectorTestingKit_ctypes -swift-version 5
3+
4+
import Swift
5+
@_exported import ModuleSelectorTestingKit
6+
import ctypes
7+
8+
public struct E {
9+
public init(value: Int)
10+
public dynamic mutating func negate()
11+
public var magnitude: Swift.UInt
12+
}
13+
14+
public func e() -> Void

test/NameLookup/module_selector.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// * Whether X::foo finds foos in X's re-exports
99
// * Whether we handle access paths correctly
1010
// * Interaction with ClangImporter
11-
// * Cross-import overlays
1211
// * Key path dynamic member lookup
1312
// * Custom type attributes (and coverage of type attrs generally is sparse)
1413
//
@@ -107,7 +106,6 @@ extension B: @retroactive main::Equatable {
107106
(main::+)
108107
// expected-error@-1 {{'+' is not imported through module 'main'}}
109108
// expected-note@-2 {{did you mean module 'Swift'?}} {{8-12=Swift}}
110-
// expected-note@-3 {{did you mean module '_Concurrency'?}} {{8-12=_Concurrency}} FIXME: Accept and suggest 'Swift::' instead?
111109

112110
let magnitude: Int.main::Magnitude = main::magnitude
113111
// expected-error@-1 {{'Magnitude' is not imported through module 'main'}}
@@ -195,7 +193,6 @@ extension C: @retroactive ModuleSelectorTestingKit::Equatable {
195193
(ModuleSelectorTestingKit::+)
196194
// expected-error@-1 {{'+' is not imported through module 'ModuleSelectorTestingKit'}}
197195
// expected-note@-2 {{did you mean module 'Swift'?}} {{8-32=Swift}}
198-
// expected-note@-3 {{did you mean module '_Concurrency'?}} {{8-32=_Concurrency}} FIXME: Accept and suggest 'Swift::' instead?
199196

200197
let magnitude: Int.ModuleSelectorTestingKit::Magnitude = ModuleSelectorTestingKit::magnitude
201198
// expected-error@-1 {{'Magnitude' is not imported through module 'ModuleSelectorTestingKit'}}
@@ -417,3 +414,17 @@ func badModuleNames() {
417414
func builtinModuleLookups(_ int: Builtin::Int64) -> Builtin::Int64 {
418415
return Builtin::int_bswap_Int64(int)
419416
}
417+
418+
func concurrencyModuleLookups(
419+
_: any Swift::Clock,
420+
_: any _Concurrency::Clock,
421+
_: any ModuleSelectorTestingKit::Clock
422+
// expected-error@-1 {{'Clock' is not imported through module 'ModuleSelectorTestingKit'}}
423+
// expected-note@-2 {{did you mean module 'Swift'?}} {{10-34=Swift}}
424+
) async {
425+
await Swift::withTaskCancellationHandler {} onCancel: {}
426+
await _Concurrency::withTaskCancellationHandler {} onCancel: {}
427+
await ModuleSelectorTestingKit::withTaskCancellationHandler {} onCancel: {}
428+
// expected-error@-1 {{'withTaskCancellationHandler' is not imported through module 'ModuleSelectorTestingKit'}}
429+
// expected-note@-2 {{did you mean module 'Swift'?}} {{9-33=Swift}}
430+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %target-typecheck-verify-swift -sdk %clang-importer-sdk -module-name main -I %S/Inputs -enable-experimental-feature ModuleSelector -enable-cross-import-overlays
2+
// RUN: %target-typecheck-verify-swift -sdk %clang-importer-sdk -module-name main -I %S/Inputs -enable-experimental-feature ModuleSelector -enable-experimental-feature ParserASTGen -enable-cross-import-overlays
3+
4+
// REQUIRES: swift_feature_ModuleSelector, swift_feature_ParserASTGen
5+
6+
import ModuleSelectorTestingKit
7+
import ctypes
8+
9+
func crossImportLookups(
10+
_: E,
11+
_: ModuleSelectorTestingKit::E,
12+
_: _ModuleSelectorTestingKit_ctypes::E,
13+
_: Swift::E
14+
// expected-error@-1 {{'E' is not imported through module 'Swift'}}
15+
// expected-note@-2 {{did you mean module 'ModuleSelectorTestingKit'?}} {{6-11=ModuleSelectorTestingKit}}
16+
) {
17+
e()
18+
ModuleSelectorTestingKit::e()
19+
_ModuleSelectorTestingKit_ctypes::e()
20+
Swift::e()
21+
// expected-error@-1 {{'e' is not imported through module 'Swift'}}
22+
// expected-note@-2 {{did you mean module 'ModuleSelectorTestingKit'?}} {{3-8=ModuleSelectorTestingKit}}
23+
}

0 commit comments

Comments
 (0)