Skip to content

Commit 834eee6

Browse files
committed
[Diagnostics] Implement MissingArgumentsFailure::diagnoseAsNote in order
to diagnose ambiguities due to missing arguments.
1 parent 42c7333 commit 834eee6

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3809,6 +3809,21 @@ bool MissingArgumentsFailure::diagnoseAsError() {
38093809
return true;
38103810
}
38113811

3812+
bool MissingArgumentsFailure::diagnoseAsNote() {
3813+
auto *locator = getLocator();
3814+
if (auto overload = getChoiceFor(locator)) {
3815+
auto *fn = resolveType(overload->openedType)->getAs<AnyFunctionType>();
3816+
auto loc = overload->choice.getDecl()->getLoc();
3817+
if (loc.isInvalid())
3818+
loc = getAnchor()->getLoc();
3819+
emitDiagnostic(loc, diag::candidate_partial_match,
3820+
fn->getParamListAsString(fn->getParams()));
3821+
return true;
3822+
}
3823+
3824+
return false;
3825+
}
3826+
38123827
bool MissingArgumentsFailure::diagnoseSingleMissingArgument() const {
38133828
auto &ctx = getASTContext();
38143829

lib/Sema/CSDiagnostics.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,8 @@ class MissingArgumentsFailure final : public FailureDiagnostic {
12101210

12111211
bool diagnoseAsError() override;
12121212

1213+
bool diagnoseAsNote() override;
1214+
12131215
bool diagnoseSingleMissingArgument() const;
12141216

12151217
private:

test/decl/subscript/subscripting.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,12 +398,13 @@ func testSubscript1(_ s1 : SubscriptTest1) {
398398
struct SubscriptTest2 {
399399
subscript(a : String, b : Int) -> Int { return 0 } // expected-note {{candidate expects value of type 'Int' for parameter #2}}
400400
// expected-note@-1 {{declared here}}
401+
// expected-note@-2 {{candidate has partially matching parameter list (String, Int)}}
401402
subscript(a : String, b : String) -> Int { return 0 } // expected-note {{candidate expects value of type 'String' for parameter #2}}
403+
// expected-note@-1 {{candidate has partially matching parameter list (String, String)}}
402404
}
403405

404406
func testSubscript1(_ s2 : SubscriptTest2) {
405-
_ = s2["foo"] // expected-error {{cannot subscript a value of type 'SubscriptTest2' with an argument of type 'String'}}
406-
// expected-note @-1 {{overloads for 'subscript' exist with these partially matching parameter lists: (String, Int), (String, String)}}
407+
_ = s2["foo"] // expected-error {{no exact matches in call to subscript}}
407408

408409
let a = s2["foo", 1.0] // expected-error {{no exact matches in call to subscript}}
409410

test/type/types.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ var d3 : () -> Float = { 4 }
1919
var d4 : () -> Int = { d2 } // expected-error{{function produces expected type 'Int'; did you mean to call it with '()'?}} {{26-26=()}}
2020

2121
var e0 : [Int]
22-
e0[] // expected-error {{cannot subscript a value of type '[Int]' with an argument of type '()'}}
23-
// expected-note @-1 {{overloads for 'subscript' exist with these partially matching parameter lists: ((UnboundedRange_) -> ()), (Int), (R), (Range<Int>), (Range<Self.Index>)}}
22+
e0[] // expected-error {{no exact matches in call to subscript}}
23+
// expected-note@-1 {{candidate has partially matching parameter list (Int)}}
24+
// expected-note@-2 {{candidate has partially matching parameter list (Range<Int>)}}
25+
// expected-note@-3 {{candidate has partially matching parameter list ((UnboundedRange_) -> ())}}
26+
// expected-note@-4 {{candidate has partially matching parameter list (Range<Array<Int>.Index>)}}
27+
// expected-note@-5 {{candidate has partially matching parameter list ((UnboundedRange_) -> ())}}
2428

2529
var f0 : [Float]
2630
var f1 : [(Int,Int)]

0 commit comments

Comments
 (0)