Skip to content

Commit 3b1e881

Browse files
committed
[Diagnostics] Improve diagnostic when using == instead of = for default function argument.
1 parent 21365cc commit 3b1e881

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,8 @@ ERROR(expected_parameter_name,PointsToFirstBadToken,
848848
"expected parameter name followed by ':'", ())
849849
ERROR(expected_parameter_colon,PointsToFirstBadToken,
850850
"expected ':' following argument label and parameter name", ())
851+
ERROR(expected_assignment_instead_of_comparison_operator,none,
852+
"expected '=' instead of '==' to assign default value for parameter", ())
851853
ERROR(missing_parameter_type,PointsToFirstBadToken,
852854
"parameter requires an explicit type", ())
853855
ERROR(multiple_parameter_ellipsis,none,

lib/Parse/ParsePattern.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,13 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc,
366366
} else {
367367
// Otherwise, we're not sure what is going on, but this doesn't smell
368368
// like a parameter.
369-
diagnose(Tok, diag::expected_parameter_name);
369+
if (Tok.isBinaryOperator() && Tok.getText() == "==") {
370+
diagnose(Tok,
371+
diag::expected_assignment_instead_of_comparison_operator)
372+
.fixItReplace(Tok.getLoc(), "=");
373+
} else {
374+
diagnose(Tok, diag::expected_parameter_name);
375+
}
370376
param.isInvalid = true;
371377
param.FirstNameLoc = Tok.getLoc();
372378
TokReceiver->registerTokenKindChange(param.FirstNameLoc,

test/Parse/recovery.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,3 +814,9 @@ func postfixDot(a : String) {
814814
func f() {
815815
_ = ClassWithStaticDecls. // expected-error {{expected member name following '.'}}
816816
}
817+
818+
819+
// <rdar://problem/22478168> | SR-11006
820+
// expected-error@+2 {{expected '=' instead of '==' to assign default value for parameter}} {{21-23==}}
821+
// expected-error@+1 {{expected ',' separator}}
822+
func SR11006(a: Int == 0) {}

0 commit comments

Comments
 (0)