Skip to content

Commit 2b207e8

Browse files
committed
[Diagnostics] Augment "expected parameter" note with an argument type
Currently ambiguity notes attached to a candidate only mention expected type and its position. To improve clarify of such notes it's useful to print argument type as well since it's not always clear what it is at the first glance at the code. Resolves: SR-14634 Resolves: rdar://78224323
1 parent 5925cb2 commit 2b207e8

File tree

8 files changed

+26
-20
lines changed

8 files changed

+26
-20
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,8 @@ ERROR(cannot_convert_argument_value,none,
361361
(Type,Type))
362362

363363
NOTE(candidate_has_invalid_argument_at_position,none,
364-
"candidate expects %select{|in-out }2value of type %0 for parameter #%1",
365-
(Type, unsigned, bool))
364+
"candidate expects %select{|in-out }2value of type %0 for parameter #%1 (got %3)",
365+
(Type, unsigned, bool, Type))
366366

367367
ERROR(cannot_convert_array_to_variadic,none,
368368
"cannot pass array of type %0 as variadic arguments of type %1",

lib/Sema/CSDiagnostics.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6135,10 +6135,10 @@ bool ArgumentMismatchFailure::diagnoseAsError() {
61356135
bool ArgumentMismatchFailure::diagnoseAsNote() {
61366136
auto *locator = getLocator();
61376137
if (auto *callee = getCallee()) {
6138-
emitDiagnosticAt(
6139-
callee, diag::candidate_has_invalid_argument_at_position, getToType(),
6140-
getParamPosition(),
6141-
locator->isLastElement<LocatorPathElt::LValueConversion>());
6138+
emitDiagnosticAt(callee, diag::candidate_has_invalid_argument_at_position,
6139+
getToType(), getParamPosition(),
6140+
locator->isLastElement<LocatorPathElt::LValueConversion>(),
6141+
getFromType());
61426142
return true;
61436143
}
61446144

@@ -7034,9 +7034,9 @@ bool AbstractRawRepresentableFailure::diagnoseAsNote() {
70347034
}
70357035
} else if (auto argConv =
70367036
locator->getLastElementAs<LocatorPathElt::ApplyArgToParam>()) {
7037-
diagnostic.emplace(
7038-
emitDiagnostic(diag::candidate_has_invalid_argument_at_position,
7039-
RawReprType, argConv->getParamIdx(), /*inOut=*/false));
7037+
diagnostic.emplace(emitDiagnostic(
7038+
diag::candidate_has_invalid_argument_at_position, RawReprType,
7039+
argConv->getParamIdx(), /*inOut=*/false, ExpectedType));
70407040
}
70417041

70427042
if (diagnostic) {

lib/Sema/ConstraintSystem.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3676,10 +3676,16 @@ static bool diagnoseAmbiguity(
36763676
assert(fn);
36773677

36783678
if (fn->getNumParams() == 1) {
3679+
auto argExpr =
3680+
simplifyLocatorToAnchor(solution.Fixes.front()->getLocator());
3681+
assert(argExpr);
3682+
36793683
const auto &param = fn->getParams()[0];
3684+
auto argType = solution.simplifyType(cs.getType(argExpr));
3685+
36803686
DE.diagnose(noteLoc, diag::candidate_has_invalid_argument_at_position,
36813687
solution.simplifyType(param.getPlainType()),
3682-
/*position=*/1, param.isInOut());
3688+
/*position=*/1, param.isInOut(), argType);
36833689
} else {
36843690
DE.diagnose(noteLoc, diag::candidate_partial_match,
36853691
fn->getParamListAsString(fn->getParams()));

test/Constraints/closures.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,8 @@ takeVoidVoidFn { () -> Void in
349349
}
350350

351351
// <rdar://problem/19997471> Swift: Incorrect compile error when calling a function inside a closure
352-
func f19997471(_ x: String) {} // expected-note {{candidate expects value of type 'String' for parameter #1}}
353-
func f19997471(_ x: Int) {} // expected-note {{candidate expects value of type 'Int' for parameter #1}}
352+
func f19997471(_ x: String) {} // expected-note {{candidate expects value of type 'String' for parameter #1 (got 'T')}}
353+
func f19997471(_ x: Int) {} // expected-note {{candidate expects value of type 'Int' for parameter #1 (got 'T')}}
354354

355355
func someGeneric19997471<T>(_ x: T) {
356356
takeVoidVoidFn {

test/Constraints/construction.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ enum Z {
1616

1717
init() { self = .none }
1818
init(_ c: UnicodeScalar) { self = .char(c) }
19-
// expected-note@-1 2 {{candidate expects value of type 'UnicodeScalar' (aka 'Unicode.Scalar') for parameter #1}}
19+
// expected-note@-1 2 {{candidate expects value of type 'UnicodeScalar' (aka 'Unicode.Scalar') for parameter #1 (got 'Z')}}
2020
init(_ s: String) { self = .string(s) }
21-
// expected-note@-1 2 {{candidate expects value of type 'String' for parameter #1}}
21+
// expected-note@-1 2 {{candidate expects value of type 'String' for parameter #1 (got 'Z')}}
2222
init(_ x: Int, _ y: Int) { self = .point(x, y) }
2323
}
2424

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 expects value of type 'Int' for parameter #1}}
402+
// expected-note@-1 {{candidate expects value of type 'Int' for parameter #1 (got 'Double')}}
403403
static func overload(b : Int) -> Color {} // expected-note {{incorrect labels for candidate (have: '(_:)', expected: '(b:)')}}
404-
// expected-note@-1 {{candidate expects value of type 'Int' for parameter #1}}
404+
// expected-note@-1 {{candidate expects value of type 'Int' for parameter #1 (got 'Double')}}
405405

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

test/Constraints/overload.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
func markUsed<T>(_ t: T) {}
44

55
func f0(_: Float) -> Float {}
6-
// expected-note@-1 {{candidate expects value of type 'Float' for parameter #1}}
6+
// expected-note@-1 {{candidate expects value of type 'Float' for parameter #1 (got 'X')}}
77
func f0(_: Int) -> Int {}
8-
// expected-note@-1 {{candidate expects value of type 'Int' for parameter #1}}
8+
// expected-note@-1 {{candidate expects value of type 'Int' for parameter #1 (got 'X')}}
99

1010
func f1(_: Int) {}
1111

test/Sema/diag_ambiguous_overloads.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ array.withUnsafeBufferPointer {
147147
// expected-note@-5 {{found candidate with type '(UnsafeMutableRawPointer) -> UnsafeRawPointer'}}
148148
}
149149

150-
func SR12689_1(_ u: Int) -> String { "" } // expected-note {{found this candidate}} expected-note {{candidate expects value of type 'Int' for parameter #1}}
151-
func SR12689_1(_ u: String) -> Double { 0 } // expected-note {{found this candidate}} expected-note {{candidate expects value of type 'String' for parameter #1}}
150+
func SR12689_1(_ u: Int) -> String { "" } // expected-note {{found this candidate}} expected-note {{candidate expects value of type 'Int' for parameter #1 (got 'Double')}}
151+
func SR12689_1(_ u: String) -> Double { 0 } // expected-note {{found this candidate}} expected-note {{candidate expects value of type 'String' for parameter #1 (got 'Double')}}
152152
func SR12689_2(_ u: Int) {}
153153

154154
SR12689_2(SR12689_1(1 as Double)) // expected-error {{no exact matches in call to global function 'SR12689_1'}}

0 commit comments

Comments
 (0)