Skip to content

[Diagnostics] Diagnose general ambiguity failures in diagnoseAmbiguityWithFixes #29056

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 8, 2020
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
25 changes: 22 additions & 3 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2872,9 +2872,28 @@ bool ConstraintSystem::diagnoseAmbiguityWithFixes(

return true;
}
case AmbiguityKind::General:
// TODO: Handle general ambiguity here.
return false;
case AmbiguityKind::General: {
emitGeneralAmbiguityFailure();

// Notes for operators are diagnosed through emitGeneralAmbiguityFailure
if (name.isOperator())
return true;

llvm::SmallSet<CanType, 4> candidateTypes;
for (const auto &viable: viableSolutions) {
auto overload = viable->getOverloadChoice(commonCalleeLocator);
auto *decl = overload.choice.getDecl();
auto type = viable->simplifyType(overload.openedType);
if (decl->getLoc().isInvalid()) {
if (candidateTypes.insert(type->getCanonicalType()).second)
DE.diagnose(commonAnchor->getLoc(), diag::found_candidate_type, type);
} else {
DE.diagnose(decl->getLoc(), diag::found_candidate);
}
}

return true;
}
}

auto *fix = viableSolutions.front()->Fixes.front();
Expand Down
7 changes: 5 additions & 2 deletions test/Constraints/members.swift
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,11 @@ func rdar50679161() {

func rdar_50467583_and_50909555() {
// rdar://problem/50467583
let _: Set = [Int][]
// expected-error@-1 {{instance member 'subscript' cannot be used on type '[Int]'}}
let _: Set = [Int][] // expected-error {{no exact matches in call to subscript}}
// expected-note@-1 {{found candidate with type '(Int) -> Int'}}
// expected-note@-2 {{found candidate with type '(Range<Int>) -> ArraySlice<Int>'}}
// expected-note@-3 {{found candidate with type '((UnboundedRange_) -> ()) -> ArraySlice<Int>'}}
// expected-note@-4 {{found candidate with type '(Range<Array<Int>.Index>) -> Slice<[Int]>' (aka '(Range<Int>) -> Slice<Array<Int>>')}}

// rdar://problem/50909555
struct S {
Expand Down
1 change: 0 additions & 1 deletion test/Constraints/operator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ func rdar46459603() {

_ = arr.values == [e]
// expected-error@-1 {{binary operator '==' cannot be applied to operands of type 'Dictionary<String, E>.Values' and '[E]'}}
// expected-note@-2 {{expected an argument list of type '(Self, Self)'}}
_ = [arr.values] == [[e]]
// expected-error@-1 {{value of protocol type 'Any' cannot conform to 'Equatable'; only struct/enum/class types can conform to protocols}}
// expected-note@-2 {{requirement from conditional conformance of '[Any]' to 'Equatable'}}
Expand Down
7 changes: 3 additions & 4 deletions test/Constraints/overload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,11 @@ func test20886179(_ handlers: [(Int) -> Void], buttonIndex: Int) {

// The problem here is that the call has a contextual result type incompatible
// with *all* overload set candidates. This is not an ambiguity.
func overloaded_identity(_ a : Int) -> Int {}
func overloaded_identity(_ b : Float) -> Float {}
func overloaded_identity(_ a : Int) -> Int {} // expected-note {{found this candidate}}
func overloaded_identity(_ b : Float) -> Float {} // expected-note {{found this candidate}}

func test_contextual_result_1() {
return overloaded_identity() // expected-error {{cannot invoke 'overloaded_identity' with no arguments}}
// expected-note @-1 {{overloads for 'overloaded_identity' exist with these partially matching parameter lists: (Float), (Int)}}
return overloaded_identity() // expected-error {{no exact matches in call to global function 'overloaded_identity'}}
}

func test_contextual_result_2() {
Expand Down
7 changes: 4 additions & 3 deletions test/Parse/super.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ class B {
var foo: Int
func bar() {}

init() {}
init(x: Int) {}
init() {} // expected-note {{found this candidate}}
init(x: Int) {} // expected-note {{found this candidate}}

subscript(x: Int) -> Int {
get {}
Expand Down Expand Up @@ -38,7 +38,8 @@ class D : B {
super.foo.bar // expected-error {{value of type 'Int' has no member 'bar'}}
super.bar // expected-error {{expression resolves to an unused function}}
super.bar()
super.init // expected-error{{'super.init' cannot be called outside of an initializer}}
// FIXME: should also say "'super.init' cannot be referenced outside of an initializer"
super.init // expected-error{{no exact matches in call to initializer}}
super.init() // expected-error{{'super.init' cannot be called outside of an initializer}}
super.init(0) // expected-error{{'super.init' cannot be called outside of an initializer}} // expected-error {{missing argument label 'x:' in call}}
super[0] // expected-error {{expression resolves to an unused subscript}}
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/type_expr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ protocol P2 {}
protocol P3 {}
func compositionType() {
_ = P1 & P2 // expected-error {{expected member name or constructor call after type name}} expected-note{{use '.self'}} {{7-7=(}} {{14-14=).self}}
_ = P1 & P2.self // expected-error {{binary operator '&' cannot be applied to operands of type 'P1.Protocol' and 'P2.Protocol'}} expected-note {{overloads}}
_ = P1 & P2.self // expected-error {{binary operator '&' cannot be applied to operands of type 'P1.Protocol' and 'P2.Protocol'}}
_ = (P1 & P2).self // Ok.
_ = (P1 & (P2)).self // FIXME: OK? while `typealias P = P1 & (P2)` is rejected.
_ = (P1 & (P2, P3)).self // expected-error {{non-protocol, non-class type '(P2, P3)' cannot be used within a protocol-constrained type}}
Expand Down
12 changes: 6 additions & 6 deletions test/decl/subscript/subscripting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,14 @@ func testUnresolvedMemberSubscriptFixit(_ s0: GenSubscriptFixitTest) {

struct SubscriptTest1 {
subscript(keyword:String) -> Bool { return true }
// expected-note@-1 3 {{found this candidate}} expected-note@-1 {{found candidate with type 'Bool'}}
// expected-note@-1 5 {{found this candidate}} expected-note@-1 {{found candidate with type 'Bool'}}
subscript(keyword:String) -> String? {return nil }
// expected-note@-1 3 {{found this candidate}} expected-note@-1 {{found candidate with type 'String?'}}
// expected-note@-1 5 {{found this candidate}} expected-note@-1 {{found candidate with type 'String?'}}

subscript(arg: SubClass) -> Bool { return true } // expected-note {{declared here}}
// expected-note@-1 2 {{found this candidate}}
subscript(arg: Protocol) -> Bool { return true } // expected-note 2 {{declared here}}
// expected-note@-1 2 {{found this candidate}}

subscript(arg: (foo: Bool, bar: (Int, baz: SubClass)), arg2: String) -> Bool { return true }
// expected-note@-1 3 {{declared here}}
Expand All @@ -379,11 +381,9 @@ func testSubscript1(_ s1 : SubscriptTest1) {
_ = s1.subscript(ClassConformingToRefinedProtocol())
// expected-error@-1 {{value of type 'SubscriptTest1' has no property or method named 'subscript'; did you mean to use the subscript operator?}} {{9-10=}} {{10-19=}} {{19-20=[}} {{54-55=]}}
_ = s1.subscript(true)
// expected-error@-1 {{cannot invoke 'subscript' with an argument list of type '(Bool)'}}
// expected-note@-2 {{overloads for 'subscript' exist with these partially matching parameter lists: (Protocol), (String), (SubClass)}}
// expected-error@-1 {{no exact matches in call to subscript}}
_ = s1.subscript(SuperClass())
// expected-error@-1 {{cannot invoke 'subscript' with an argument list of type '(SuperClass)'}}
// expected-note@-2 {{overloads for 'subscript' exist with these partially matching parameter lists: (Protocol), (String), (SubClass)}}
// expected-error@-1 {{no exact matches in call to subscript}}
_ = s1.subscript("hello")
// expected-error@-1 {{value of type 'SubscriptTest1' has no property or method named 'subscript'; did you mean to use the subscript operator?}}
_ = s1.subscript("hello"
Expand Down