-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Returning a comparison expression with a match fails depending on the operand order #47287
Comments
This is known and intentional IIRC, but I can't remember what it's called or where it's documented. It affects every expression that ends in braces, e.g., |
Every expression statement that ends in braces true && { 1 } > 0; // OK, { 1 } is an intermediate expression somewhere
{ 1 } > 0; // FAIL, expression { 1 } is a full statement, we expect a next statement after it |
So this is not a bug, but an intentional limitation made for better/easier parsing or semantics? I suppose that makes sense, even if it wasn't what I expected. I had assumed that since I wish there was an explanation of why this is the case, though. The reference section on expression statements doesn't say anything about operators not being allowed to appear after a full statement (though maybe that's because the name implies that it is a complete statement, and thus nothing can follow it). I would close this issue, but before I do that, perhaps the error message could be improved? I have a few ideas, though take them with a grain of salt, because I don't know what the compiler/parser actually can and can't do.
|
@NPN if cond {
stmts;
}
*my_ref = value; or this if cond {
stmts;
}
(a + b).method(); You don't want these example to be parsed as multiplication and function call, respectively - It's certainly possible to improve diagnostics and this is probably the first thing that needs to be done. I think it's also possible to "cherry-pick" some operators that can continue an expression, but can't start an expression and continue parsing only when encountering them, but that's somewhat risky and needs to be though out carefully - what if we'll need to extend the grammar and add unary |
Identify when a stmt could have been parsed as an expr There are some expressions that can be parsed as a statement without a trailing semicolon depending on the context, which can lead to confusing errors due to the same looking code being accepted in some places and not others. Identify these cases and suggest enclosing in parenthesis making the parse non-ambiguous without changing the accepted grammar. Fix rust-lang#54186, cc rust-lang#54482, fix rust-lang#59975, fix rust-lang#47287.
Suppose the return expression of a function is a comparison between a match expression and some other operand. If the operand and comparison operator come before the match expression, the code compiles. However, if the operand and comparison operator come after the match expression, the code fails to compile.
The error is thus:
Strangely, the code does work if the comparison is a statement:
Tested using rustc
1.23.0
on Playground.The text was updated successfully, but these errors were encountered: