Skip to content

Commit f97e803

Browse files
committed
[Diagnostics] Allow "unknown base" fix to be diagnosed in ambiguity situations
If there are multiple overloads and all of them require explicit base type for a member reference, let's diagnose it as a single error since the problem is the same across the overload choices: ```swift func foo<T>(_: T, defaultT: T? = nil) {} func foo<U>(_: U, defaultU: U? = nil) {} foo(.bar) ``` In this example there is not enough contextual information to determine base type of `.bar` reference and hence both `foo` overloads are a considered valid solutions until explicitly set base type disambiguates them. Resolves: rdar://problem/66891544
1 parent 1345a2b commit f97e803

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

lib/Sema/CSFix.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,6 +1763,10 @@ class SpecifyBaseTypeForContextualMember final : public ConstraintFix {
17631763

17641764
bool diagnose(const Solution &solution, bool asNote = false) const override;
17651765

1766+
bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const override {
1767+
return diagnose(*commonFixes.front().first);
1768+
}
1769+
17661770
static SpecifyBaseTypeForContextualMember *
17671771
create(ConstraintSystem &cs, DeclNameRef member, ConstraintLocator *locator);
17681772
};

test/Constraints/diagnostics.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ enum Color {
409409

410410
let _: (Int, Color) = [1,2].map({ ($0, .Unknown("")) })
411411
// expected-error@-1 {{cannot convert value of type 'Array<(Int, _)>' to specified type '(Int, Color)'}}
412+
// expected-error@-2 {{cannot infer contextual base in reference to member 'Unknown'}}
412413

413414
let _: [(Int, Color)] = [1,2].map({ ($0, .Unknown("")) })// expected-error {{missing argument label 'description:' in call}}
414415

test/Constraints/members.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,3 +718,11 @@ func testSR13359(_ pair: (Int, Int), _ alias: Pair, _ void: Void, labeled: (a: I
718718
_ = labeled[""] // expected-error {{cannot access element using subscript for tuple type '(a: Int, b: Int)'; use '.' notation instead}} {{none}}
719719

720720
}
721+
722+
// rdar://problem/66891544 - incorrect diagnostic ("type is ambiguous") when base type of a reference cannot be determined
723+
func rdar66891544() {
724+
func foo<T>(_: T, defaultT: T? = nil) {}
725+
func foo<U>(_: U, defaultU: U? = nil) {}
726+
727+
foo(.bar) // expected-error {{cannot infer contextual base in reference to member 'bar'}}
728+
}

0 commit comments

Comments
 (0)