You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would expect it to compile successfully, but I got the compile error instead:
error: expected expression, found `!=`
--> src/main.rs:6:16
|
6 | {foo()} != 0
| ^^ expected expression
warning: unnecessary braces around block return value
--> src/main.rs:6:6
|
6 | {foo()} != 0
| ^^^^^^^^^ help: remove these braces
|
= note: `#[warn(unused_braces)]` on by default
error[E0308]: mismatched types
--> src/main.rs:6:8
|
6 | {foo()} != 0
| ^^^^^- help: try adding a semicolon: `;`
| |
| expected `()`, found `u8`
It is a minimal reproducible example, so it triggers the "unnecessary braces around block return value" warning. In the real code an unsafe block that cannot be removed is used.
An expression that consists of only a block expression or control flow expression, if used in a context where a statement is permitted, can omit the trailing semicolon. This can cause an ambiguity between it being parsed as a standalone statement and as a part of another expression; in this case, it is parsed as a statement. The type of ExpressionWithBlock expressions when used as statements must be the unit type.
In this situation, the block is interpreted as a statement. Then it moves on and sees !=, and gives the error of expecting an expression.
I tried this code:
I would expect it to compile successfully, but I got the compile error instead:
It is a minimal reproducible example, so it triggers the "unnecessary braces around block return value" warning. In the real code an unsafe block that cannot be removed is used.
Meta
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: