Skip to content

Commit 6d73ed3

Browse files
authored
Merge pull request #26139 from vguerra/SR-11006
[Diagnostics] Improve diagnostic when using == instead of = for defau…
2 parents 2fdae64 + 2cfe8ad commit 6d73ed3

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,8 @@ ERROR(expected_parameter_name,PointsToFirstBadToken,
861861
"expected parameter name followed by ':'", ())
862862
ERROR(expected_parameter_colon,PointsToFirstBadToken,
863863
"expected ':' following argument label and parameter name", ())
864+
ERROR(expected_assignment_instead_of_comparison_operator,none,
865+
"expected '=' instead of '==' to assign default value for parameter", ())
864866
ERROR(missing_parameter_type,PointsToFirstBadToken,
865867
"parameter requires an explicit type", ())
866868
ERROR(multiple_parameter_ellipsis,none,

lib/Parse/ParsePattern.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ static ParserStatus parseDefaultArgument(
7070
Parser::ParameterContextKind paramContext) {
7171
SyntaxParsingContext DefaultArgContext(P.SyntaxContext,
7272
SyntaxKind::InitializerClause);
73-
SourceLoc equalLoc = P.consumeToken(tok::equal);
73+
assert(P.Tok.is(tok::equal) ||
74+
(P.Tok.isBinaryOperator() && P.Tok.getText() == "=="));
75+
SourceLoc equalLoc = P.consumeToken();
7476

7577
if (P.SF.Kind == SourceFileKind::Interface) {
7678
// Swift module interfaces don't synthesize inherited intializers and
@@ -381,13 +383,20 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc,
381383
param.EllipsisLoc = consumeToken();
382384
}
383385

384-
// ('=' expr)?
385-
if (Tok.is(tok::equal)) {
386+
// ('=' expr) or ('==' expr)?
387+
bool isEqualBinaryOperator =
388+
Tok.isBinaryOperator() && Tok.getText() == "==";
389+
if (Tok.is(tok::equal) || isEqualBinaryOperator) {
386390
SourceLoc EqualLoc = Tok.getLoc();
387-
status |= parseDefaultArgument(*this, defaultArgs, defaultArgIndex,
388-
param.DefaultArg,
389-
param.hasInheritedDefaultArg,
390-
paramContext);
391+
392+
if (isEqualBinaryOperator) {
393+
diagnose(Tok, diag::expected_assignment_instead_of_comparison_operator)
394+
.fixItReplace(EqualLoc, "=");
395+
}
396+
397+
status |= parseDefaultArgument(
398+
*this, defaultArgs, defaultArgIndex, param.DefaultArg,
399+
param.hasInheritedDefaultArg, paramContext);
391400

392401
if (param.EllipsisLoc.isValid() && param.DefaultArg) {
393402
// The range of the complete default argument.

test/Parse/recovery.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,3 +843,8 @@ func postfixDot(a : String) {
843843
func f() {
844844
_ = ClassWithStaticDecls. // expected-error {{expected member name following '.'}}
845845
}
846+
847+
848+
// <rdar://problem/22478168> | SR-11006
849+
// expected-error@+1 {{expected '=' instead of '==' to assign default value for parameter}} {{21-23==}}
850+
func SR11006(a: Int == 0) {}

0 commit comments

Comments
 (0)