Skip to content

[Diagnostics] Skip overloaded locations where all solutions have the same type #66035

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 2 commits into from
Jun 16, 2023
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
33 changes: 29 additions & 4 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5302,12 +5302,18 @@ bool ConstraintSystem::diagnoseAmbiguityWithFixes(
/// provided set.
static unsigned countDistinctOverloads(ArrayRef<OverloadChoice> choices) {
llvm::SmallPtrSet<void *, 4> uniqueChoices;
unsigned result = 0;
for (auto choice : choices) {
if (uniqueChoices.insert(choice.getOpaqueChoiceSimple()).second)
++result;
uniqueChoices.insert(choice.getOpaqueChoiceSimple());
}
return result;
return uniqueChoices.size();
}

static Type getOverloadChoiceType(ConstraintLocator *overloadLoc,
const Solution &solution) {
auto selectedOverload = solution.overloadChoices.find(overloadLoc);
if (selectedOverload == solution.overloadChoices.end())
return Type();
return solution.simplifyType(selectedOverload->second.adjustedOpenedType);
}

/// Determine the name of the overload in a set of overload choices.
Expand Down Expand Up @@ -5389,6 +5395,25 @@ bool ConstraintSystem::diagnoseAmbiguity(ArrayRef<Solution> solutions) {
auto &overload = diff.overloads[i];
auto *locator = overload.locator;

// If there is only one overload difference, it's the best.
if (n == 1) {
bestOverload = i;
break;
}

// If there are multiple overload sets involved, let's pick the
// one that has choices with different types, because that is
// most likely the source of ambiguity.
{
auto overloadTy = getOverloadChoiceType(locator, solutions.front());
if (std::all_of(solutions.begin() + 1, solutions.end(),
[&](const Solution &solution) {
return overloadTy->isEqual(
getOverloadChoiceType(locator, solution));
}))
continue;
}

ASTNode anchor;

// Simplification of member locator would produce a base expression,
Expand Down
50 changes: 50 additions & 0 deletions test/Constraints/ambiguity_diagnostics.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// RUN: %target-typecheck-verify-swift -swift-version 5 -disable-availability-checking

protocol View {
}

extension View {
func title<S>(_ title: S) -> some View where S : StringProtocol {
EmptyView()
}

func title(_ titleKey: LocalizedString) -> some View {
EmptyView()
}
}

extension View {
func background<T: ShapeStyle>(_: T) -> some View {
EmptyView()
}
}

struct EmptyView : View {}

struct Text : View {
init(_: String) {}
}

protocol ShapeStyle {
}

struct AnyShapeStyle : ShapeStyle {}
struct AnyGradient : ShapeStyle {}

struct LocalizedString : ExpressibleByStringLiteral, ExpressibleByExtendedGraphemeClusterLiteral {
init(extendedGraphemeClusterLiteral value: String) {}
init(stringLiteral: String) {}
}

func test() {
func __findValue(_: String, fallback: LocalizedString) -> LocalizedString { fatalError() }
func __findValue<T: ExpressibleByStringLiteral>(_: String, fallback: T) -> T { fatalError() }
func __findValue<T: ExpressibleByExtendedGraphemeClusterLiteral>(_: String, fallback: T) -> T { fatalError() }

func ambiguitySource() -> AnyShapeStyle { fatalError() } // expected-note {{found this candidate}}
func ambiguitySource() -> AnyGradient { fatalError() } // expected-note {{found this candidate}}

Text("Test")
.title(__findValue("someKey", fallback: "<unknown>"))
.background(ambiguitySource()) // expected-error {{ambiguous use of 'ambiguitySource()'}}
}