Skip to content

[SR-510] diagnose enum raw value in closure parsing #987

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

Merged
merged 2 commits into from
Jan 26, 2016

Conversation

dduan
Copy link
Contributor

@dduan dduan commented Jan 15, 2016

This reverts the previous fix "complain for invalid enum raw value" and fixes
SR-510 with a different approach:

enum raw value is parsed as a normal expression. When a closure is encountered
as the expression, a proper context will be absent. If we pass a diagnosis
from raw value to parseExprClosure, it will get properly issued.

enum raw value is parsed as a normal expression using parseExpr(). However,
for a closure, the parser expects a local context that doesn't exist for raw
values.

We create a temporary context to ensure the closure gets parsed as normal.
As a consequence, parseExpr() returns normally for closure and correct
diagnosis for raw value gets issued.

@jrose-apple

@jrose-apple
Copy link
Contributor

Hm. First request: please split the revert out as a separate commit. :-) (Same pull request is fine, so we don't regress on master, but it is logically separate.)

Beyond that, the error message doesn't really apply here. There is an expression there; it's just not one that belongs there.

and all changes introduced in the same pull request
@dduan
Copy link
Contributor Author

dduan commented Jan 15, 2016

@jrose-apple done and done.

@dduan
Copy link
Contributor Author

dduan commented Jan 15, 2016

The error message is now raw value for enum case must be a literal.

@jrose-apple
Copy link
Contributor

This approach seems reasonable, although the hard-coding under the case '{' makes me a little sad.

Do you really think it's better to throw away the closure, rather than to create a dummy local context, parse it, and then complain?

@jrose-apple
Copy link
Contributor

I guess we can add a very coarse assertion that some error was emitted if we end up in the "enum case was null" path. We can't guarantee that it happened while trying to parse the case, but if there haven't been any other errors, we won't let this one slip by.

@dduan dduan force-pushed the SR-510-3 branch 2 times, most recently from 3807d29 to 8ec8bf4 Compare January 16, 2016 00:04
@dduan
Copy link
Contributor Author

dduan commented Jan 16, 2016

@jrose-apple is only exists for swift::Diagnostic, so I can't use it on swift::Diag 🙁

@dduan dduan force-pushed the SR-510-3 branch 5 times, most recently from bc0713d to 7bd34ca Compare January 16, 2016 09:08
@dduan
Copy link
Contributor Author

dduan commented Jan 16, 2016

Update for @jrose-apple:

I implemented your suggestion. Adding a local context made the patch much simpler. The closure gets parsed normally and errors related to both the closure itself and raw value gets issued.

An assertion for the "enum case was null" path is also added. Turns out adding this assertion break tests in test/IDE/complete_in_enum_decl.swift.

All tests are passing.

@@ -4413,7 +4412,7 @@ ParserStatus Parser::parseDeclEnumCase(ParseDeclOptions Flags,
return Status;
}
if (RawValueExpr.isNull()) {
diagnose(NextLoc, diag::nonliteral_enum_case_raw_value);
assert("No error emmited for invalid enum raw value expression");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emmited emitted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank Erik. Fixed.

@dduan dduan force-pushed the SR-510-3 branch 2 times, most recently from 8271f27 to e242ad7 Compare January 17, 2016 20:30
@jrose-apple
Copy link
Contributor

Um. We can work out the assertion failures separately (it still seems valuable). I actually meant creating a local context ahead of time, so that we wouldn't have to hardcode a particular ID in the main expr-parsing loop. I guess this is cheaper, though, since it only comes up on failure. Would it at least make sense to move it into the closure-parsing logic, though?

Also, how does that local context ever get cleaned up? Seems like it's just new'ed into space.

@dduan dduan force-pushed the SR-510-3 branch 2 times, most recently from 9100bdd to cf045f7 Compare January 19, 2016 21:34
@dduan
Copy link
Contributor Author

dduan commented Jan 19, 2016

@jrose-apple Ok, I removed the hard-coded diagnosis from parseExpr, tests are passing.

@jrose-apple
Copy link
Contributor

That still leaks the local context, though, doesn't it?

@dduan
Copy link
Contributor Author

dduan commented Jan 20, 2016

@jrose-apple Oops, fixed.

@dduan
Copy link
Contributor Author

dduan commented Jan 23, 2016

@jrose-apple anything else I can do to make this one better?

@jrose-apple
Copy link
Contributor

Sorry I hadn't gotten back to this! I think this is pretty good. It's not a completely beautiful solution but it does mean we're parsing the same way as everything else. I have nitpicks on your comments but I'll merge it after that.

enum raw value is parsed as a normal expression using `parseExpr()`. However,
for a closure, the parser expects a local context that doesn't exist for raw
values.

We create a temporary context to ensure the closure gets parsed as normal.
As a consequence, `parseExpr()` returns normally for closure and correct
diagnosis for raw value gets issued.
@dduan
Copy link
Contributor Author

dduan commented Jan 26, 2016

Done. Thank you for all the help 😬.

jrose-apple added a commit that referenced this pull request Jan 26, 2016
[SR-510] Diagnose closures used as enum raw values.
@jrose-apple jrose-apple merged commit ebcabd7 into swiftlang:master Jan 26, 2016
@dduan dduan deleted the SR-510-3 branch December 23, 2019 21:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants