Skip to content

Commit f854cc6

Browse files
committed
[ConstraintSystem] Attach candidate notes either to decl (if it has valid loc) or AST node
1 parent 82fcee7 commit f854cc6

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3229,6 +3229,9 @@ static bool diagnoseAmbiguity(
32293229
if (!distinctChoices.insert(decl).second)
32303230
continue;
32313231

3232+
auto noteLoc =
3233+
decl->getLoc().isInvalid() ? getLoc(commonAnchor) : decl->getLoc();
3234+
32323235
if (solution.Fixes.size() == 1) {
32333236
diagnosed &=
32343237
solution.Fixes.front()->diagnose(solution, /*asNote*/ true);
@@ -3241,15 +3244,23 @@ static bool diagnoseAmbiguity(
32413244
// lists.
32423245
auto *fn = type->getAs<AnyFunctionType>();
32433246
assert(fn);
3244-
DE.diagnose(decl->getLoc(), diag::candidate_partial_match,
3245-
fn->getParamListAsString(fn->getParams()));
3247+
3248+
if (fn->getNumParams() == 1) {
3249+
const auto &param = fn->getParams()[0];
3250+
DE.diagnose(noteLoc, diag::candidate_has_invalid_argument_at_position,
3251+
solution.simplifyType(param.getPlainType()),
3252+
/*position=*/1, param.isInOut());
3253+
} else {
3254+
DE.diagnose(noteLoc, diag::candidate_partial_match,
3255+
fn->getParamListAsString(fn->getParams()));
3256+
}
32463257
} else {
32473258
// Emit a general "found candidate" note
32483259
if (decl->getLoc().isInvalid()) {
32493260
if (candidateTypes.insert(type->getCanonicalType()).second)
32503261
DE.diagnose(getLoc(commonAnchor), diag::found_candidate_type, type);
32513262
} else {
3252-
DE.diagnose(decl->getLoc(), diag::found_candidate);
3263+
DE.diagnose(noteLoc, diag::found_candidate);
32533264
}
32543265
}
32553266
}

test/Constraints/diagnostics.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,9 @@ enum Color {
399399
static func rainbow() -> Color {}
400400

401401
static func overload(a : Int) -> Color {} // expected-note {{incorrect labels for candidate (have: '(_:)', expected: '(a:)')}}
402-
// expected-note@-1 {{candidate has partially matching parameter list (a: Int)}}
402+
// expected-note@-1 {{candidate expects value of type 'Int' for parameter #1}}
403403
static func overload(b : Int) -> Color {} // expected-note {{incorrect labels for candidate (have: '(_:)', expected: '(b:)')}}
404-
// expected-note@-1 {{candidate has partially matching parameter list (b: Int)}}
404+
// expected-note@-1 {{candidate expects value of type 'Int' for parameter #1}}
405405

406406
static func frob(_ a : Int, b : inout Int) -> Color {}
407407
static var svar: Color { return .Red }

test/Constraints/super_constructor.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ class B {
3939
init() {
4040
}
4141

42-
init(x:Int) { // expected-note{{candidate has partially matching parameter list (x: Int)}}
42+
init(x:Int) { // expected-note{{candidate expects value of type 'Int' for parameter #1}}
4343
}
4444

45-
init(a:UnicodeScalar) { // expected-note {{candidate has partially matching parameter list (a: UnicodeScalar)}}
45+
init(a:UnicodeScalar) { // expected-note {{candidate expects value of type 'UnicodeScalar' (aka 'Unicode.Scalar') for parameter #1}}
4646
}
47-
init(b:UnicodeScalar) { // expected-note{{candidate has partially matching parameter list (b: UnicodeScalar)}}
47+
init(b:UnicodeScalar) { // expected-note {{candidate expects value of type 'UnicodeScalar' (aka 'Unicode.Scalar') for parameter #1}}
4848
}
4949

50-
init(z:Float) { // expected-note{{candidate has partially matching parameter list (z: Float)}}
50+
init(z:Float) { // expected-note{{candidate expects value of type 'Float' for parameter #1}}
5151
super.init() // expected-error{{'super' members cannot be referenced in a root class}}
5252
}
5353
}

test/expr/postfix/dot/init_ref_delegation.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,12 @@ class TestOverloadSets {
323323
self.init(5, 5) // expected-error{{extra argument in call}}
324324
}
325325

326-
convenience init(a : Z0) { // expected-note{{candidate has partially matching parameter list (a: Z0)}}
326+
convenience init(a : Z0) { // expected-note{{candidate expects value of type 'Z0' for parameter #1}}
327327
self.init(42 as Int8) // expected-error{{no exact matches in call to initializer}}
328328
}
329329

330-
init(value: Int) { /* ... */ } // expected-note{{candidate has partially matching parameter list (value: Int)}}
331-
init(value: Double) { /* ... */ } // expected-note{{candidate has partially matching parameter list (value: Double)}}
330+
init(value: Int) { /* ... */ } // expected-note{{candidate expects value of type 'Int' for parameter #1}}
331+
init(value: Double) { /* ... */ } // expected-note{{candidate expects value of type 'Double' for parameter #1}}
332332
}
333333

334334
class TestNestedExpr {

0 commit comments

Comments
 (0)