Skip to content

Commit a2d9b10

Browse files
committed
Fix <rdar://problem/22108559> QoI: Confusing error message when calling an instance method as a class method
1 parent d47cded commit a2d9b10

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,11 +1700,7 @@ bool CalleeCandidateInfo::diagnoseAnyStructuralArgumentError(Expr *fnExpr,
17001700
// here.
17011701
if (size() != 1) return false;
17021702

1703-
// We only handle structural errors here.
1704-
if (closeness != CC_ArgumentLabelMismatch &&
1705-
closeness != CC_ArgumentCountMismatch)
1706-
return false;
1707-
1703+
17081704
auto args = decomposeArgParamType(argExpr->getType());
17091705
auto params = decomposeArgParamType(candidates[0].getArgumentType());
17101706

@@ -1731,6 +1727,11 @@ bool CalleeCandidateInfo::diagnoseAnyStructuralArgumentError(Expr *fnExpr,
17311727
}
17321728
}
17331729

1730+
// We only handle structural errors here.
1731+
if (closeness != CC_ArgumentLabelMismatch &&
1732+
closeness != CC_ArgumentCountMismatch)
1733+
return false;
1734+
17341735
SmallVector<Identifier, 4> correctNames;
17351736
unsigned OOOArgIdx = ~0U, OOOPrevArgIdx = ~0U;
17361737
unsigned extraArgIdx = ~0U, missingParamIdx = ~0U;
@@ -3266,6 +3267,15 @@ typeCheckArgumentChildIndependently(Expr *argExpr, Type argType,
32663267
if (candidates.size() == 1 && !argType)
32673268
argType = candidates[0].getArgumentType();
32683269
}
3270+
3271+
// If our candidates are instance members at curry level #0, then the argument
3272+
// being provided is the receiver type for the instance. We produce better
3273+
// diagnostics when we don't force the self type down.
3274+
if (argType && !candidates.empty() &&
3275+
candidates[0].decl->isInstanceMember() && candidates[0].level == 0 &&
3276+
!isa<SubscriptDecl>(candidates[0].decl))
3277+
argType = Type();
3278+
32693279

32703280
// FIXME: This should all just be a matter of getting type type of the
32713281
// sub-expression, but this doesn't work well when typeCheckChildIndependently

test/Constraints/diagnostics.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ c.method2(1.0)(b: 2.0) // expected-error {{cannot convert value of type 'Double'
339339
CurriedClass.method1(c)()
340340
_ = CurriedClass.method1(c)
341341
CurriedClass.method1(c)(1) // expected-error {{argument passed to call that takes no arguments}}
342-
CurriedClass.method1(2.0)(1) // expected-error {{cannot convert value of type 'Double' to expected argument type 'CurriedClass'}}
342+
CurriedClass.method1(2.0)(1) // expected-error {{use of instance member 'method1' on type 'CurriedClass'; did you mean to use a value of type 'CurriedClass' instead?}}
343343

344344
CurriedClass.method2(c)(32)(b: 1)
345345
_ = CurriedClass.method2(c)
@@ -380,7 +380,7 @@ CurriedClass.m1(2, b: 42) // expected-error {{use of instance member 'm1' on t
380380

381381

382382
// <rdar://problem/22108559> QoI: Confusing error message when calling an instance method as a class method
383-
CurriedClass.m2(12) // expected-error {{cannot convert value of type 'Int' to expected argument type 'CurriedClass'}}
383+
CurriedClass.m2(12) // expected-error {{use of instance member 'm2' on type 'CurriedClass'; did you mean to use a value of type 'CurriedClass' instead?}}
384384

385385

386386

0 commit comments

Comments
 (0)