New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Diagnostics] Improve diagnostic when using == instead of = for defau… #26139
Conversation
…lt function argument.
cc @rintaro |
lib/Parse/ParsePattern.cpp
Outdated
if (Tok.isBinaryOperator() && Tok.getText() == "==") { | ||
diagnose(Tok, | ||
diag::expected_assignment_instead_of_comparison_operator) | ||
.fixItReplace(Tok.getLoc(), "="); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible for the compiler to check this earlier and recover by parsing the expression normally?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The check could indeed be taken before l.272 for instance, but just to be sure I understand: in this case "recover" means that the token for '==' should be consumed so that the code can continue parsing the default value and so on?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I was thinking you'd essentially have the compiler just pretend that the user wrote =
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it. Will try this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this check and recovery should be performed later, like l.385.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but somehow I need to avoid the parsing logic to get into any of this branches, so a check would be needed before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should work as expected if you consume ==
as if it's =
and parse the default value after ==
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As well, parser recovers from usage of '==' and parses the whole parameter clause.
lib/Parse/ParsePattern.cpp
Outdated
SyntaxParsingContext DefaultArgContext(P.SyntaxContext, | ||
SyntaxKind::InitializerClause); | ||
SourceLoc equalLoc = P.consumeToken(tok::equal); | ||
SourceLoc equalLoc = P.consumeToken(assignmentTok); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if consumption of either '=' or '==' should be taken out of here and be performed on caller site?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consumeToken
also allows not passing a token kind at all, but I still think you might be right. It's weird to consume something when you don't know what it is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm OK with:
assert(Tok.is(tok::equal) ||
(Tok.isBinaryOperator() && Tok.getText() == "=="));
consumeToken();
inside parseDefaultArgument()
. Like
Lines 6632 to 6634 in 26c4ccc
Parser::parseDeclInit(ParseDeclOptions Flags, DeclAttributes &Attributes) { | |
assert(Tok.is(tok::kw_init)); | |
SourceLoc ConstructorLoc = consumeToken(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the feedback .. addressed in 2cfe8ad
@swift-ci Please smoke test |
@swift-ci Please smoke test Linux platform |
Thank you Victor! |
thank you both for the guidance. |
…lt function argument.
Resolves SR-11006.