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

AsyncBlockExpression is misclassified as an ExpressionWithBlock #1267

Closed
fmease opened this issue Sep 10, 2022 · 4 comments · Fixed by #1268
Closed

AsyncBlockExpression is misclassified as an ExpressionWithBlock #1267

fmease opened this issue Sep 10, 2022 · 4 comments · Fixed by #1268
Labels
A-async Area: async / await A-grammar Area: Syntax and parsing Bug Incorrect statements, terminology, or rendering issues

Comments

@fmease
Copy link
Member

fmease commented Sep 10, 2022

According to section 8.2 Expressions, AsyncBlockExpressions are classified as ExpressionWithBlock while rustc parses them as if they were an ExpressionWithoutBlock.

If AsyncBlockExpression was an ExpressionWithBlock, the following code would be syntactically malformed. In reality however, rustc successfully parses it!

fn main() {
    #[cfg(FALSE)]
    match () { () => async { () }[..] }
}

You can experiment with the code above by replacing async with unsafe / loop (true ExpressionWithBlocks) or an empty string (making it a BlockExpression which is also a true ExpressionWithBlock) and observing that the compiler rightfully reports a syntax error.

Explanation of the test snippet:
For an ExpressionWithBlock, the block ends the expression and the comma at the end of a match arm is optional. Meaning if there isn't one, we immediately expect a pattern (in this case we find [..] – a slice pattern) followed by => (here we find } and emit a syntax error). For an ExpressionWithoutBlock on the other hand, we continue to parse an expression after the end of the block, interpret the [..] as being part of an IndexExpression and succeed.

The reference and the (reference) compiler are in disagreement here. I am not sure who is right. I couldn't find any mention of this in RFC 2394. Should I instead / additionally create an issue at the rust-lang/rust repo?

This issue was first discovered on Zulip when investigating the same issue for the experimental inline consts (ConstBlockExpressions). For the latter, the issue was acknowledged but no straightforward answer could be found and a note was added to its tracking issue.

@rustbot label Bug A-async A-grammar
(hmm, labeling does not work despite the existence of triagebot.toml in the root folder and the rule set relabel.allow-unauthenticated within it)

@ehuss
Copy link
Contributor

ehuss commented Sep 10, 2022

@rustbot label Bug A-async A-grammar

1 similar comment
@Mark-Simulacrum
Copy link
Member

@rustbot label Bug A-async A-grammar

@rustbot rustbot added A-async Area: async / await A-grammar Area: Syntax and parsing Bug Incorrect statements, terminology, or rendering issues labels Sep 10, 2022
@ehuss
Copy link
Contributor

ehuss commented Sep 10, 2022

Good catch! Yea, I think AsyncBlockExpression should be moved. It is treated more like a closure. I don't know if that was done intentionally. There was a lot of discussion about await syntax, but I'm not sure if the async block got the same scrutiny. Perhaps the thinking is that using an async block as a statement expression isn't useful (since the future would get discarded)?

@fmease
Copy link
Member Author

fmease commented Sep 11, 2022

I've opened a Zulip topic to get a confirmation from the Async WG.

Update: Async WG member eholk agreed that this is a bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-async Area: async / await A-grammar Area: Syntax and parsing Bug Incorrect statements, terminology, or rendering issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants