Skip to content

[Diagnostics] Augment "expected parameter" note with an argument type #37658

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
May 27, 2021
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
4 changes: 2 additions & 2 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,8 @@ ERROR(cannot_convert_argument_value,none,
(Type,Type))

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

ERROR(cannot_convert_array_to_variadic,none,
"cannot pass array of type %0 as variadic arguments of type %1",
Expand Down
14 changes: 7 additions & 7 deletions lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6135,10 +6135,10 @@ bool ArgumentMismatchFailure::diagnoseAsError() {
bool ArgumentMismatchFailure::diagnoseAsNote() {
auto *locator = getLocator();
if (auto *callee = getCallee()) {
emitDiagnosticAt(
callee, diag::candidate_has_invalid_argument_at_position, getToType(),
getParamPosition(),
locator->isLastElement<LocatorPathElt::LValueConversion>());
emitDiagnosticAt(callee, diag::candidate_has_invalid_argument_at_position,
getToType(), getParamPosition(),
locator->isLastElement<LocatorPathElt::LValueConversion>(),
getFromType());
return true;
}

Expand Down Expand Up @@ -7034,9 +7034,9 @@ bool AbstractRawRepresentableFailure::diagnoseAsNote() {
}
} else if (auto argConv =
locator->getLastElementAs<LocatorPathElt::ApplyArgToParam>()) {
diagnostic.emplace(
emitDiagnostic(diag::candidate_has_invalid_argument_at_position,
RawReprType, argConv->getParamIdx(), /*inOut=*/false));
diagnostic.emplace(emitDiagnostic(
diag::candidate_has_invalid_argument_at_position, RawReprType,
argConv->getParamIdx(), /*inOut=*/false, ExpectedType));
}

if (diagnostic) {
Expand Down
8 changes: 7 additions & 1 deletion lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3676,10 +3676,16 @@ static bool diagnoseAmbiguity(
assert(fn);

if (fn->getNumParams() == 1) {
auto argExpr =
simplifyLocatorToAnchor(solution.Fixes.front()->getLocator());
assert(argExpr);

const auto &param = fn->getParams()[0];
auto argType = solution.simplifyType(cs.getType(argExpr));

DE.diagnose(noteLoc, diag::candidate_has_invalid_argument_at_position,
solution.simplifyType(param.getPlainType()),
/*position=*/1, param.isInOut());
/*position=*/1, param.isInOut(), argType);
} else {
DE.diagnose(noteLoc, diag::candidate_partial_match,
fn->getParamListAsString(fn->getParams()));
Expand Down
4 changes: 2 additions & 2 deletions test/Constraints/closures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,8 @@ takeVoidVoidFn { () -> Void in
}

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

func someGeneric19997471<T>(_ x: T) {
takeVoidVoidFn {
Expand Down
4 changes: 2 additions & 2 deletions test/Constraints/construction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ enum Z {

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

Expand Down
4 changes: 2 additions & 2 deletions test/Constraints/diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,9 @@ enum Color {
static func rainbow() -> Color {}

static func overload(a : Int) -> Color {} // expected-note {{incorrect labels for candidate (have: '(_:)', expected: '(a:)')}}
// expected-note@-1 {{candidate expects value of type 'Int' for parameter #1}}
// expected-note@-1 {{candidate expects value of type 'Int' for parameter #1 (got 'Double')}}
static func overload(b : Int) -> Color {} // expected-note {{incorrect labels for candidate (have: '(_:)', expected: '(b:)')}}
// expected-note@-1 {{candidate expects value of type 'Int' for parameter #1}}
// expected-note@-1 {{candidate expects value of type 'Int' for parameter #1 (got 'Double')}}

static func frob(_ a : Int, b : inout Int) -> Color {}
static var svar: Color { return .Red }
Expand Down
4 changes: 2 additions & 2 deletions test/Constraints/overload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
func markUsed<T>(_ t: T) {}

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

func f1(_: Int) {}

Expand Down
4 changes: 2 additions & 2 deletions test/Sema/diag_ambiguous_overloads.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ array.withUnsafeBufferPointer {
// expected-note@-5 {{found candidate with type '(UnsafeMutableRawPointer) -> UnsafeRawPointer'}}
}

func SR12689_1(_ u: Int) -> String { "" } // expected-note {{found this candidate}} expected-note {{candidate expects value of type 'Int' for parameter #1}}
func SR12689_1(_ u: String) -> Double { 0 } // expected-note {{found this candidate}} expected-note {{candidate expects value of type 'String' for parameter #1}}
func SR12689_1(_ u: Int) -> String { "" } // expected-note {{found this candidate}} expected-note {{candidate expects value of type 'Int' for parameter #1 (got 'Double')}}
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')}}
func SR12689_2(_ u: Int) {}

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