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
{{ message }}
Repository navigation
Incorrect "closure bodies that contain statements must be surrounded by braces" error in unrelatedly invalid code #127223
error: closure bodies that contain statements must be surrounded by braces
--> src/lib.rs:2:17
|
2 |let value = || {};| ^^
3 |
4 | (value;);| ^
|
note: statement found outside of a block
--> src/lib.rs:4:11
|
2 |let value = || {};| -- this expression is a statement because of the trailing semicolon
3 |
4 | (value;);| ^ this `;` turns the preceding closure into a statement
note: the closure body may be incorrectly delimited
--> src/lib.rs:2:17
|
2 |let value = || {};| ^^^^^ this is the parsed closure...
3 |
4 | (value;);| - ...but likely you meant the closure to end here
help: try adding braces
|
2 ~let value = || { {};
3 |
4 ~ (value;});|
Desired output
error: expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `;`
--> src/lib.rs:4:11
|
4 | (value;);| ^ expected one of 8 possible tokens
Rationale and extra context
This error is unrelated to the closure, and indeed, applying the suggestion leaves us with:
fntest(){let value = || {{};(value;});}
Which now has an improperly closed brace. The correct code would (most likely) be (value)
The original code I had made the suggestion even weirder since the closure was an argument to a Higher Order Function (instead of bound to a variable) and the last line was a function call with a ; after its argument (instead of a paren expression), which happened due to a typo 😅. Just in case something following that structure would make for a useful test case as well:
fntest(){let value = std::iter::once_with(|| ());
std::men::drop(value;);}
changed the title [-]Incorrect "closure bodies that contain statements must be surrounded by braces" error unrelatedly invalid code[/-][+]Incorrect "closure bodies that contain statements must be surrounded by braces" error in unrelatedly invalid code[/+]on Jul 1, 2024
Activity
[-]Incorrect "closure bodies that contain statements must be surrounded by braces" error unrelatedly invalid code[/-][+]Incorrect "closure bodies that contain statements must be surrounded by braces" error in unrelatedly invalid code[/+]eggyal commentedon Jul 2, 2024
rust/compiler/rustc_parse/src/parser/mod.rs
Lines 846 to 859 in 7d97c59
I suppose this test should only be applied when at the same depth as the closure definition?
Rollup merge of rust-lang#162721 - chenyukang:yukang-fix-127223-closu…
Unrolled build for #162721 in rollup 162807