Skip to content

Allow code to shadow definitions in the Foundation module #61642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ EXPERIMENTAL_FEATURE(BuiltinMacros, false)
/// declare an attribute which is discoverable and constructable at runtime.
EXPERIMENTAL_FEATURE(RuntimeDiscoverableAttrs, false)

/// Whether to allow types from Foundation to be implicitly shadowed
EXPERIMENTAL_FEATURE(ShadowFoundation, false)

#undef EXPERIMENTAL_FEATURE
#undef UPCOMING_FEATURE
#undef SUPPRESSIBLE_LANGUAGE_FEATURE
Expand Down
4 changes: 4 additions & 0 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3156,6 +3156,10 @@ static bool usesFeatureBuiltinMacros(Decl *decl) {
return false;
}

static bool usesFeatureShadowFoundation(Decl *decl) {
return false;
}

static void
suppressingFeatureNoAsyncAvailability(PrintOptions &options,
llvm::function_ref<void()> action) {
Expand Down
73 changes: 46 additions & 27 deletions lib/AST/NameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,37 +660,56 @@ static void recordShadowedDeclsAfterTypeMatch(
}
}

// 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
// latter rather than the former, which be the consequence of a more
// significant change to name shadowing in the future.
if (auto owningStruct1
= firstDecl->getDeclContext()->getSelfStructDecl()) {
if (auto owningStruct2
= secondDecl->getDeclContext()->getSelfStructDecl()) {
if (owningStruct1 == owningStruct2 &&
owningStruct1->getName().is("Data") &&
isa<FuncDecl>(firstDecl) && isa<FuncDecl>(secondDecl) &&
firstDecl->getName() == secondDecl->getName() &&
firstDecl->getBaseName().userFacingName() == "withUnsafeBytes") {
// If the second module is the Foundation module and the first
// is the NIOFoundationCompat module, the second is shadowed by the
// first.
if (firstDecl->getModuleContext()->getName()
.is("NIOFoundationCompat") &&
secondDecl->getModuleContext()->getName().is("Foundation")) {
if (ctx.LangOpts.hasFeature(Feature::ShadowFoundation)) {
// If `ShadowFoundation` is enabled, allow all types from Foundation
// to be implicitly shadowed.
if (auto foundationModule = ctx.getLoadedModule(ctx.Id_Foundation)) {
if ((firstModule == foundationModule) !=
(secondModule == foundationModule)) {
// If second module is Foundation, then it is shadowed by first
if (secondModule == foundationModule) {
shadowed.insert(secondDecl);
continue;
}

// If it's the other way around, the first declaration is shadowed
// by the second.
if (secondDecl->getModuleContext()->getName()
.is("NIOFoundationCompat") &&
firstDecl->getModuleContext()->getName().is("Foundation")) {
shadowed.insert(firstDecl);
break;
// Otherwise, the first declaration is shadowed by the second.
shadowed.insert(firstDecl);
break;
}
}
} else {
// 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
// latter rather than the former, which be the consequence of a more
// significant change to name shadowing in the future.
if (auto owningStruct1
= firstDecl->getDeclContext()->getSelfStructDecl()) {
if (auto owningStruct2
= secondDecl->getDeclContext()->getSelfStructDecl()) {
if (owningStruct1 == owningStruct2 &&
owningStruct1->getName().is("Data") &&
isa<FuncDecl>(firstDecl) && isa<FuncDecl>(secondDecl) &&
firstDecl->getName() == secondDecl->getName() &&
firstDecl->getBaseName().userFacingName() == "withUnsafeBytes") {
// If the second module is the Foundation module and the first
// is the NIOFoundationCompat module, the second is shadowed by the
// first.
if (firstDecl->getModuleContext()->getName()
.is("NIOFoundationCompat") &&
secondDecl->getModuleContext()->getName().is("Foundation")) {
shadowed.insert(secondDecl);
continue;
}

// If it's the other way around, the first declaration is shadowed
// by the second.
if (secondDecl->getModuleContext()->getName()
.is("NIOFoundationCompat") &&
firstDecl->getModuleContext()->getName().is("Foundation")) {
shadowed.insert(firstDecl);
break;
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions test/Constraints/Inputs/FakeFoundation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#import <Foundation/Foundation.h>
1 change: 1 addition & 0 deletions test/Constraints/Inputs/FakeFoundation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// empty
2 changes: 1 addition & 1 deletion test/Constraints/Inputs/imported_type.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#import <Foundation/Foundation.h>
#import "FakeFoundation.h"

@interface Data
@end
5 changes: 3 additions & 2 deletions test/Constraints/invalid_decl_ref.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// RUN: %target-swift-frontend -module-name SomeModule -typecheck -verify -dump-ast -import-objc-header %S/Inputs/imported_type.h %s | %FileCheck %s
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/FakeFoundation.swiftmodule -module-name FakeFoundation %S/Inputs/FakeFoundation.swift -import-objc-header %S/Inputs/FakeFoundation.h -disable-availability-checking

// REQUIRES: objc_interop

import Foundation
Copy link
Member

@kavon kavon Jan 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite sure what the changes in this Constraints test does for us, especially now that the shadowing change is behind a feature flag.

I think any test changes other than for the new foundation-module-shadowing.swift should be reverted so they cover what they did previously.

import FakeFoundation

// CHECK: declref_expr type='module<SomeModule>'
// CHECK-NEXT: type_expr type='Data.Type'
Expand Down
3 changes: 3 additions & 0 deletions test/Frontend/Inputs/ShadowFoundation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public struct UUID {
public let uuidString = "FakeUUID"
}
13 changes: 13 additions & 0 deletions test/Frontend/foundation-module-shadowing.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// REQUIRES: objc_interop
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/ShadowFoundation.swiftmodule -module-name ShadowFoundation %S/Inputs/ShadowFoundation.swift -disable-availability-checking
// RUN: %target-typecheck-verify-swift -I %t -disable-availability-checking -enable-experimental-feature ShadowFoundation

import Foundation
import ShadowFoundation

func f(_ uuid: UUID) -> Bool {
return uuid.uuidString == "FakeUUID"
}

func g(_ uuid: Foundation.UUID) { }