Skip to content

Commit 2618f27

Browse files
committed
Allow code to shadow definitions in the Foundation module
1 parent a2ec07c commit 2618f27

File tree

3 files changed

+27
-32
lines changed

3 files changed

+27
-32
lines changed

lib/AST/NameLookup.cpp

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -660,39 +660,19 @@ static void recordShadowedDeclsAfterTypeMatch(
660660
}
661661
}
662662

663-
// The Foundation overlay introduced Data.withUnsafeBytes, which is
664-
// treated as being ambiguous with SwiftNIO's Data.withUnsafeBytes
665-
// extension. Apply a special-case name shadowing rule to use the
666-
// latter rather than the former, which be the consequence of a more
667-
// significant change to name shadowing in the future.
668-
if (auto owningStruct1
669-
= firstDecl->getDeclContext()->getSelfStructDecl()) {
670-
if (auto owningStruct2
671-
= secondDecl->getDeclContext()->getSelfStructDecl()) {
672-
if (owningStruct1 == owningStruct2 &&
673-
owningStruct1->getName().is("Data") &&
674-
isa<FuncDecl>(firstDecl) && isa<FuncDecl>(secondDecl) &&
675-
firstDecl->getName() == secondDecl->getName() &&
676-
firstDecl->getBaseName().userFacingName() == "withUnsafeBytes") {
677-
// If the second module is the Foundation module and the first
678-
// is the NIOFoundationCompat module, the second is shadowed by the
679-
// first.
680-
if (firstDecl->getModuleContext()->getName()
681-
.is("NIOFoundationCompat") &&
682-
secondDecl->getModuleContext()->getName().is("Foundation")) {
683-
shadowed.insert(secondDecl);
684-
continue;
685-
}
686-
687-
// If it's the other way around, the first declaration is shadowed
688-
// by the second.
689-
if (secondDecl->getModuleContext()->getName()
690-
.is("NIOFoundationCompat") &&
691-
firstDecl->getModuleContext()->getName().is("Foundation")) {
692-
shadowed.insert(firstDecl);
693-
break;
694-
}
663+
// Next, prefer any other module over the Foundation module
664+
if (auto foundationModule = ctx.getLoadedModule(ctx.Id_Foundation)) {
665+
if ((firstModule == foundationModule) !=
666+
(secondModule == foundationModule)) {
667+
// If second module is Foundation, then it is shadowed by first
668+
if (secondModule == foundationModule) {
669+
shadowed.insert(secondDecl);
670+
continue;
695671
}
672+
673+
// Otherwise, the first declaration is shadowed by the second.
674+
shadowed.insert(firstDecl);
675+
break;
696676
}
697677
}
698678

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public struct UUID {
2+
public let uuidString = "FakeUUID"
3+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/ShadowFoundation.swiftmodule -module-name ShadowFoundation %S/Inputs/ShadowFoundation.swift -disable-availability-checking
3+
// RUN: %target-typecheck-verify-swift -I %t -disable-availability-checking
4+
5+
import Foundation
6+
import ShadowFoundation
7+
8+
func f(_ uuid: UUID) -> Bool {
9+
return uuid.uuidString == "FakeUUID"
10+
}
11+
12+
func g(_ uuid: Foundation.UUID) { }

0 commit comments

Comments
 (0)