Skip to content
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

Is a semicolon part of statement or not? Is a semicolon really never part of Statement nonterminal expansion containing ExpressionWithBlock? #773

Closed
steffahn opened this issue Mar 4, 2020 · 2 comments · Fixed by #776
Labels
A-grammar Area: Syntax and parsing

Comments

@steffahn
Copy link
Member

steffahn commented Mar 4, 2020

According to this page, “The semicolon following a statement is not a part of the statement itself.” While this view may be true and relevant for using the stmt parser in procedural macros, the grammar, in particular a nonterminal literally named Statement, tells a different story. I believe this is confusing.

Apart from the above, there is a problem / inaccuracy in the nonterminal ExpressionStatement. The respective page accurately conveys that an ExpressionWithoutBlock must be followed by ; and an ExpressionWithBlock usually isn’t. However, an ExpressionWithBlock most definitely can have a ; following it. This might syntactically be well covered by the fact that ; in by itself is a valid Statement, and I wouldn’t complain if this kind of ; was always redundant and like a no-op statement following the ExpressionWithoutBlock-statement, but this isn’t the case by means of what is stated below the grammar of ExpressionStatement, “The type of ExpressionWithBlock expressions when used as statements must be the unit type.”

It goes on further around there and speaks of a “trailing semicolon” one “can omit” and clarifies that more precisely, only “When the trailing semicolon is omitted, the result must be type ().” All this means is: A semicolon does change things: it allows the return type to be non-(). If the semicolon is relevant for type-checking an ExpressionStatement containing ExpressionWithBlock, in my view this should imply that said semicolon must be part of the statement.

I‘m not 100% sure what the best solution to improve this is. I’d suggest that the rule becomes:
ExpressionStatement: ExpressionWithoutBlock ; | ExpressionWithBlock ;?
i.e.: allow an optional semicolon behind ExpressionWithBlock, being part of the ExpressionStatement. Maximum munch would mean that if any ; exists it will then be considered part of the statement. But there’s also the issue #762 that seems related. Perhaps there is a different approach that solves both problems together.

@petrochenkov
Copy link
Contributor

A related rust-lang/rust issue - rust-lang/rust#61733.

(The stmt matcher is also actively wrong in the token-based model that I'd like to have implemented.)

@ehuss
Copy link
Contributor

ehuss commented Mar 4, 2020

I think it makes sense to add ;<sup>?</sup> to ExpressionWithBlock since that seems to better match the semantics.

I would also completely remove the note about stmt macro in block-expr.md. It is out of place and confusing. The rules of ; and stmt are already explained in macros-by-example.md.

Would that be sufficient to clarify things for you?

@ehuss ehuss added the A-grammar Area: Syntax and parsing label Mar 4, 2020
Havvy pushed a commit to Havvy/reference that referenced this issue Aug 25, 2020
Semicolon or comma in expression statements or match arms, respectively, are optional, but part of the statement / match arm.
Also remove a note about proc-macros from the page about BlockExpression.
Closes rust-lang#773 and closes rust-lang#774.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-grammar Area: Syntax and parsing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants