From 533c7b78550d0d32f3ccb79956c7e5b2a82a9990 Mon Sep 17 00:00:00 2001 From: Richard Wei Date: Mon, 2 May 2022 16:27:59 -0700 Subject: [PATCH] [Sema] Allow code to shadow definitions in implicit _StringProcessing module. Treat _StringProcessing decls as if it were declared in the Swift module, just like how _Concurrency is treated (#34642). --- lib/AST/NameLookup.cpp | 16 ++++++++++++++++ .../Sema/Inputs/ShadowsStringProcessing.swift | 4 ++++ .../string_processing_module_shadowing.swift | 11 +++++++++++ 3 files changed, 31 insertions(+) create mode 100644 test/StringProcessing/Sema/Inputs/ShadowsStringProcessing.swift create mode 100644 test/StringProcessing/Sema/string_processing_module_shadowing.swift diff --git a/lib/AST/NameLookup.cpp b/lib/AST/NameLookup.cpp index 1453b00e2f351..21813c89178ab 100644 --- a/lib/AST/NameLookup.cpp +++ b/lib/AST/NameLookup.cpp @@ -528,6 +528,22 @@ static void recordShadowedDeclsAfterTypeMatch( } } + // Next, prefer any other module over the _StringProcessing module. + if (auto spModule = ctx.getLoadedModule(ctx.Id_StringProcessing)) { + if ((firstModule == spModule) != (secondModule == spModule)) { + // If second module is _StringProcessing, then it is shadowed by + // first. + if (secondModule == spModule) { + shadowed.insert(secondDecl); + continue; + } + + // Otherwise, the first declaration is shadowed by the second. + shadowed.insert(firstDecl); + break; + } + } + // The Foundation overlay introduced Data.withUnsafeBytes, which is // treated as being ambiguous with SwiftNIO's Data.withUnsafeBytes // extension. Apply a special-case name shadowing rule to use the diff --git a/test/StringProcessing/Sema/Inputs/ShadowsStringProcessing.swift b/test/StringProcessing/Sema/Inputs/ShadowsStringProcessing.swift new file mode 100644 index 0000000000000..8993cb10f4318 --- /dev/null +++ b/test/StringProcessing/Sema/Inputs/ShadowsStringProcessing.swift @@ -0,0 +1,4 @@ + public struct Regex { + public var someProperty: T? + public init() { } + } diff --git a/test/StringProcessing/Sema/string_processing_module_shadowing.swift b/test/StringProcessing/Sema/string_processing_module_shadowing.swift new file mode 100644 index 0000000000000..374d8fba32605 --- /dev/null +++ b/test/StringProcessing/Sema/string_processing_module_shadowing.swift @@ -0,0 +1,11 @@ +// RUN: %empty-directory(%t) +// RUN: %target-swift-frontend -emit-module -emit-module-path %t/ShadowsStringProcessing.swiftmodule -module-name ShadowsStringProcessing %S/Inputs/ShadowsStringProcessing.swift -disable-availability-checking +// RUN: %target-typecheck-verify-swift -I %t -enable-experimental-string-processing -disable-availability-checking + +import ShadowsStringProcessing + +func f(_ t : Regex) -> Bool { + return t.someProperty == "123" +} + +func g(_: _StringProcessing.Regex) {}