Skip to content

Commit

Permalink
Moving check for '==' close to that of '=' and diagnose there.
Browse files Browse the repository at this point in the history
As well, parser recovers from usage of '==' and parses the whole
parameter clause.
  • Loading branch information
vguerra committed Jul 16, 2019
1 parent 3b1e881 commit d82dd84
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
31 changes: 16 additions & 15 deletions lib/Parse/ParsePattern.cpp
Expand Up @@ -67,10 +67,10 @@ void Parser::DefaultArgumentInfo::setFunctionContext(
static ParserStatus parseDefaultArgument(
Parser &P, Parser::DefaultArgumentInfo *defaultArgs, unsigned argIndex,
Expr *&init, bool &hasInheritedDefaultArg,
Parser::ParameterContextKind paramContext) {
Parser::ParameterContextKind paramContext, tok assignmentTok) {
SyntaxParsingContext DefaultArgContext(P.SyntaxContext,
SyntaxKind::InitializerClause);
SourceLoc equalLoc = P.consumeToken(tok::equal);
SourceLoc equalLoc = P.consumeToken(assignmentTok);

if (P.SF.Kind == SourceFileKind::Interface) {
// Swift module interfaces don't synthesize inherited intializers and
Expand Down Expand Up @@ -366,13 +366,7 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc,
} else {
// Otherwise, we're not sure what is going on, but this doesn't smell
// like a parameter.
if (Tok.isBinaryOperator() && Tok.getText() == "==") {
diagnose(Tok,
diag::expected_assignment_instead_of_comparison_operator)
.fixItReplace(Tok.getLoc(), "=");
} else {
diagnose(Tok, diag::expected_parameter_name);
}
diagnose(Tok, diag::expected_parameter_name);
param.isInvalid = true;
param.FirstNameLoc = Tok.getLoc();
TokReceiver->registerTokenKindChange(param.FirstNameLoc,
Expand All @@ -387,13 +381,20 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc,
param.EllipsisLoc = consumeToken();
}

// ('=' expr)?
if (Tok.is(tok::equal)) {
// ('=' expr) or ('==' expr)?
bool isEqualBinaryOperator =
Tok.isBinaryOperator() && Tok.getText() == "==";
if (Tok.is(tok::equal) || isEqualBinaryOperator) {
SourceLoc EqualLoc = Tok.getLoc();
status |= parseDefaultArgument(*this, defaultArgs, defaultArgIndex,
param.DefaultArg,
param.hasInheritedDefaultArg,
paramContext);

if (isEqualBinaryOperator) {
diagnose(Tok, diag::expected_assignment_instead_of_comparison_operator)
.fixItReplace(EqualLoc, "=");
}

status |= parseDefaultArgument(
*this, defaultArgs, defaultArgIndex, param.DefaultArg,
param.hasInheritedDefaultArg, paramContext, Tok.getKind());

if (param.EllipsisLoc.isValid() && param.DefaultArg) {
// The range of the complete default argument.
Expand Down
3 changes: 1 addition & 2 deletions test/Parse/recovery.swift
Expand Up @@ -817,6 +817,5 @@ func f() {


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

0 comments on commit d82dd84

Please sign in to comment.