Skip to content

[CSOptimizer] MemberImportVisibility: Don't consider overloads that c… #79004

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

Merged
merged 1 commit into from
Jan 29, 2025
Merged
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
12 changes: 12 additions & 0 deletions lib/Sema/CSOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,18 @@ void forEachDisjunctionChoice(
if (!decl)
continue;

// Ignore declarations that come from implicitly imported modules
// when `MemberImportVisibility` feature is enabled otherwise
// we might end up favoring an overload that would be diagnosed
// as unavailable later.
if (cs.getASTContext().LangOpts.hasFeature(
Feature::MemberImportVisibility)) {
if (auto *useDC = constraint->getOverloadUseDC()) {
if (!useDC->isDeclImported(decl))
continue;
}
}

// If disjunction choice is unavailable or disfavored we cannot
// do anything with it.
if (decl->getAttrs().hasAttribute<DisfavoredOverloadAttr>() ||
Expand Down
39 changes: 39 additions & 0 deletions test/Constraints/member_import_visibility.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// RUN: %empty-directory(%t)
// RUN: %empty-directory(%t/src)
// RUN: split-file %s %t/src

/// Build the library A
// RUN: %target-swift-frontend -emit-module %t/src/A.swift \
// RUN: -module-name A \
// RUN: -emit-module-path %t/A.swiftmodule

/// Build the library B
// RUN: %target-swift-frontend -I %t -emit-module %t/src/B.swift \
// RUN: -module-name B \
// RUN: -emit-module-path %t/B.swiftmodule

// RUN: %target-swift-frontend -typecheck -I %t %t/src/Main.swift %t/src/Other.swift -enable-upcoming-feature MemberImportVisibility

// REQUIRES: swift_feature_MemberImportVisibility

//--- A.swift
public struct Test {
public init(a: Double) { }
}

//--- B.swift
import A

extension Test {
public init(a: Int) { fatalError() }
}

//--- Main.swift
import A

func test() {
_ = Test(a: 0) // Ok, selects the overload that takes Double.
}

//--- Other.swift
import B