Skip to content

Commit

Permalink
[Diagnostics] Improve diagnostic when using == instead of = for defau…
Browse files Browse the repository at this point in the history
…lt function argument.
  • Loading branch information
vguerra committed Jul 14, 2019
1 parent 21365cc commit 3b1e881
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,8 @@ ERROR(expected_parameter_name,PointsToFirstBadToken,
"expected parameter name followed by ':'", ())
ERROR(expected_parameter_colon,PointsToFirstBadToken,
"expected ':' following argument label and parameter name", ())
ERROR(expected_assignment_instead_of_comparison_operator,none,
"expected '=' instead of '==' to assign default value for parameter", ())
ERROR(missing_parameter_type,PointsToFirstBadToken,
"parameter requires an explicit type", ())
ERROR(multiple_parameter_ellipsis,none,
Expand Down
8 changes: 7 additions & 1 deletion lib/Parse/ParsePattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,13 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc,
} else {
// Otherwise, we're not sure what is going on, but this doesn't smell
// like a parameter.
diagnose(Tok, diag::expected_parameter_name);
if (Tok.isBinaryOperator() && Tok.getText() == "==") {
diagnose(Tok,
diag::expected_assignment_instead_of_comparison_operator)
.fixItReplace(Tok.getLoc(), "=");
} else {
diagnose(Tok, diag::expected_parameter_name);
}
param.isInvalid = true;
param.FirstNameLoc = Tok.getLoc();
TokReceiver->registerTokenKindChange(param.FirstNameLoc,
Expand Down
6 changes: 6 additions & 0 deletions test/Parse/recovery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -814,3 +814,9 @@ func postfixDot(a : String) {
func f() {
_ = ClassWithStaticDecls. // expected-error {{expected member name following '.'}}
}


// <rdar://problem/22478168> | SR-11006
// expected-error@+2 {{expected '=' instead of '==' to assign default value for parameter}} {{21-23==}}
// expected-error@+1 {{expected ',' separator}}
func SR11006(a: Int == 0) {}

0 comments on commit 3b1e881

Please sign in to comment.