Skip to content

Commit 2e2792e

Browse files
committed
[ConstraintSystem] Adjust diagnoseAmbiguity to use a local slice of overloads
Compute a slice of ambiguous overload choices related to an aggregated fix and if such a slice points to a single overload diagnose it as non-ambiguous.
1 parent 15d566d commit 2e2792e

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3399,6 +3399,30 @@ static bool diagnoseAmbiguity(
33993399

34003400
auto &DE = cs.getASTContext().Diags;
34013401

3402+
llvm::SmallPtrSet<ValueDecl *, 4> localAmbiguity;
3403+
{
3404+
for (auto &entry : aggregateFix) {
3405+
const auto &solution = entry.first;
3406+
const auto &overload = solution->getOverloadChoice(ambiguity.locator);
3407+
auto *choice = overload.choice.getDeclOrNull();
3408+
3409+
// It's not possible to diagnose different kinds of overload choices.
3410+
if (!choice)
3411+
return false;
3412+
3413+
localAmbiguity.insert(choice);
3414+
}
3415+
}
3416+
3417+
if (localAmbiguity.empty())
3418+
return false;
3419+
3420+
// If all of the fixes are rooted in the same choice.
3421+
if (localAmbiguity.size() == 1) {
3422+
auto &primaryFix = aggregateFix.front();
3423+
return primaryFix.second->diagnose(*primaryFix.first);
3424+
}
3425+
34023426
{
34033427
auto fixKind = aggregateFix.front().second->getKind();
34043428
if (llvm::all_of(
@@ -3413,10 +3437,7 @@ static bool diagnoseAmbiguity(
34133437
}
34143438
}
34153439

3416-
auto *decl = ambiguity.choices.front().getDeclOrNull();
3417-
if (!decl)
3418-
return false;
3419-
3440+
auto *decl = *localAmbiguity.begin();
34203441
auto *commonCalleeLocator = ambiguity.locator;
34213442

34223443
bool diagnosed = true;

test/Sema/enum_raw_representable.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,7 @@ func sr8150_mutable(obj: SR8150Box) {
204204
sr8150_helper1(opt)
205205
// expected-error@-1 {{cannot convert value of type 'Bar?' to expected argument type 'Double'}} {{23-23=?.rawValue ?? <#default value#>}}
206206
sr8150_helper1(opt ?? Bar.a)
207-
// expected-error@-1 {{no exact matches in call to global function 'sr8150_helper1'}}
208-
// expected-note@-2 {{candidate expects value of type 'Bar' for parameter #0}} {{20-20=(}} {{32-32=).rawValue}}
207+
// expected-error@-1 {{cannot convert value of type 'Bar' to expected argument type 'Double'}} {{20-20=(}} {{32-32=).rawValue}}
209208
let _: Double? = opt
210209
// expected-error@-1 {{cannot convert value of type 'Bar?' to specified type 'Double?'}} {{25-25=?.rawValue}}
211210
}

test/stdlib/KeyPathAppending.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func mismatchedAppends<T, U, V>(readOnlyLeft: KeyPath<T, U>,
6161
// expected-error@-1 {{no exact matches in call to instance method 'appending'}}
6262

6363
_ = writableRight.appending(path: readOnlyLeft)
64-
// expected-error@-1 {{no exact matches in call to instance method 'appending'}}
64+
// expected-error@-1 {{instance method 'appending(path:)' requires that 'KeyPath<U, V>' inherit from 'KeyPath<U, T>'}}
6565

6666
_ = writableRight.appending(path: writableLeft)
6767
// expected-error@-1 {{cannot convert value of type 'WritableKeyPath<T, U>' to expected argument type 'WritableKeyPath<V, U>'}}
@@ -71,7 +71,7 @@ func mismatchedAppends<T, U, V>(readOnlyLeft: KeyPath<T, U>,
7171
// expected-error@-1 {{no exact matches in call to instance method 'appending'}}
7272

7373
_ = referenceRight.appending(path: readOnlyLeft)
74-
// expected-error@-1 {{no exact matches in call to instance method 'appending'}}
74+
// expected-error@-1 {{instance method 'appending(path:)' requires that 'KeyPath<U, V>' inherit from 'KeyPath<U, T>'}}
7575

7676
_ = referenceRight.appending(path: writableLeft)
7777
// expected-error@-1 {{cannot convert value of type 'WritableKeyPath<T, U>' to expected argument type 'WritableKeyPath<V, U>'}}

0 commit comments

Comments
 (0)