Open
Description
These examples should compile according to the rust reference:
fn compiles() -> i32 {
({2}) + ({2})
}
fn fails() -> i32 {
{2} + {2}
}
fn compiles2() -> i32 {
2 + {2}
}
fn fails2() -> i32 {
{2} + 2
}
Errors:
Compiling playground v0.0.1 (file:///playground)
error: expected expression, found `+`
--> src/lib.rs:6:9
|
6 | {2} + {2}
| ^ expected expression
error: expected expression, found `+`
--> src/lib.rs:14:9
|
14 | {2} + 2
| ^ expected expression
error: aborting due to 2 previous errors
error: Could not compile `playground`.
To learn more, run the command again with --verbose.
Rust reference snippet (thanks @Moongoodboy-K for the help here):
Expression : BlockExpression | OperatorExpression | ..
OperatorExpression : ArithmeticOrLogicalExpression | ..
ArithmeticOrLogicalExpression : Expression+
Expression | ..
BlockExpression :{
InnerAttribute* Statement* Expression?}
cc: @Moongoodboy-K
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
nagisa commentedon Sep 22, 2018
Note: this is not a regression, these fail to parse all the way back to 1.0. I think this is a duplicate of #7909, but since we are now fairly accepting of look-ahead, this seems fairly valid.
estebank commentedon Sep 22, 2018
I believe that conversation is still ongoing, we'll probably have to go the RFCS route for that...
scottmcm commentedon Sep 23, 2018
I'm not certain this is actually a bug; it might be worse for
+
and-
to have difference parser behaviour here.Havvy commentedon Sep 23, 2018
This isn't a bug. It falls out of block expression statements terminating at the
}
.Havvy commentedon Sep 23, 2018
The relevant section of the reference:
While we could theoretically make
{ {2} + {2} }
work since{ + {2} }
is not a valid expression, the same cannot be true of other binary ops. Specifically-
.{ - {2}; }
is valid.That said, the error for this is terrible. Most likely you want to change your expression statement to be
({2} + {2});
in these cases. Is there a diagnostic issue open for that?Work around rust-lang/rust#54482 when translating statement expressions
as
#59975Rollup merge of rust-lang#60188 - estebank:recover-block, r=varkor
estebank commentedon Jul 23, 2019
The current output for this is:
==
expression with unsafe block on left-hand side fails to parse #74854Spoonbender commentedon Mar 22, 2022
Triage: no change