Skip to content

Commit af3dcf0

Browse files
committed
[Diagnostics] Add a tailored note for passing nil to incompatible argument position
New note mentions both expected argument type and its position and anchors to the affected overload choice.
1 parent 2f44f5b commit af3dcf0

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,9 @@ ERROR(cannot_convert_argument_value_anyobject,none,
425425
(Type, Type))
426426
ERROR(cannot_convert_argument_value_nil,none,
427427
"'nil' is not compatible with expected argument type %0", (Type))
428+
NOTE(note_incompatible_argument_value_nil_at_pos,none,
429+
"'nil' is not compatible with expected argument type %0 at position #%1",
430+
(Type, unsigned))
428431

429432
ERROR(cannot_convert_condition_value,none,
430433
"cannot convert value of type %0 to expected condition type %1",

lib/Sema/CSDiagnostics.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2375,11 +2375,26 @@ bool ContextualFailure::diagnoseAsError() {
23752375
}
23762376

23772377
bool ContextualFailure::diagnoseAsNote() {
2378-
auto overload = getCalleeOverloadChoiceIfAvailable(getLocator());
2378+
auto *locator = getLocator();
2379+
2380+
auto overload = getCalleeOverloadChoiceIfAvailable(locator);
23792381
if (!(overload && overload->choice.isDecl()))
23802382
return false;
23812383

23822384
auto *decl = overload->choice.getDecl();
2385+
2386+
if (auto *anchor = getAsExpr(getAnchor())) {
2387+
anchor = anchor->getSemanticsProvidingExpr();
2388+
2389+
if (isa<NilLiteralExpr>(anchor)) {
2390+
auto argLoc =
2391+
locator->castLastElementTo<LocatorPathElt::ApplyArgToParam>();
2392+
emitDiagnosticAt(decl, diag::note_incompatible_argument_value_nil_at_pos,
2393+
getToType(), argLoc.getArgIdx() + 1);
2394+
return true;
2395+
}
2396+
}
2397+
23832398
emitDiagnosticAt(decl, diag::found_candidate_type, getFromType());
23842399
return true;
23852400
}

0 commit comments

Comments
 (0)