Skip to content

Commit d82dd84

Browse files
committed
Moving check for '==' close to that of '=' and diagnose there.
As well, parser recovers from usage of '==' and parses the whole parameter clause.
1 parent 3b1e881 commit d82dd84

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

lib/Parse/ParsePattern.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ void Parser::DefaultArgumentInfo::setFunctionContext(
6767
static ParserStatus parseDefaultArgument(
6868
Parser &P, Parser::DefaultArgumentInfo *defaultArgs, unsigned argIndex,
6969
Expr *&init, bool &hasInheritedDefaultArg,
70-
Parser::ParameterContextKind paramContext) {
70+
Parser::ParameterContextKind paramContext, tok assignmentTok) {
7171
SyntaxParsingContext DefaultArgContext(P.SyntaxContext,
7272
SyntaxKind::InitializerClause);
73-
SourceLoc equalLoc = P.consumeToken(tok::equal);
73+
SourceLoc equalLoc = P.consumeToken(assignmentTok);
7474

7575
if (P.SF.Kind == SourceFileKind::Interface) {
7676
// Swift module interfaces don't synthesize inherited intializers and
@@ -366,13 +366,7 @@ 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-
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-
}
369+
diagnose(Tok, diag::expected_parameter_name);
376370
param.isInvalid = true;
377371
param.FirstNameLoc = Tok.getLoc();
378372
TokReceiver->registerTokenKindChange(param.FirstNameLoc,
@@ -387,13 +381,20 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc,
387381
param.EllipsisLoc = consumeToken();
388382
}
389383

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

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

test/Parse/recovery.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,5 @@ func f() {
817817

818818

819819
// <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}}
820+
// expected-error@+1 {{expected '=' instead of '==' to assign default value for parameter}} {{21-23==}}
822821
func SR11006(a: Int == 0) {}

0 commit comments

Comments
 (0)