Closed
Description
Given the following code:
fn main() {
let num = 5;
(1..num).reduce(|a, b|
println!("{}", a); // <== Here
a * b
).unwrap();
}
The current output is:
Compiling playground v0.0.1 (/playground)
error: expected one of `)`, `,`, `.`, `?`, or an operator, found `;`
--> src/main.rs:4:26
|
4 | println!("{}", a); // <== Here
| ^
| |
| expected one of `)`, `,`, `.`, `?`, or an operator
| help: missing `,`
error[E0425]: cannot find value `a` in this scope
--> src/main.rs:5:9
|
5 | a * b
| ^ not found in this scope
error[E0425]: cannot find value `b` in this scope
--> src/main.rs:5:13
|
5 | a * b
| ^ not found in this scope
error[E0061]: this function takes 1 argument but 2 arguments were supplied
--> src/main.rs:3:14
|
3 | (1..num).reduce(|a, b|
| ______________^^^^^^_-
| | |
| | expected 1 argument
4 | | println!("{}", a); // <== Here
| |_________________________-
5 | a * b
| ----- supplied 2 arguments
|
note: associated function defined here
error: aborting due to 4 previous errors
Some errors have detailed explanations: E0061, E0425.
For more information about an error, try `rustc --explain E0061`.
error: could not compile `playground`
To learn more, run the command again with --verbose.
Ideally the output should look like:
3 | (1..num).reduce(|a, b| {
| ____________________________^
| | Multi-line closure requires scope brackets
I was trying to debug an iterator and added a println!
call. To my surprise, it caused the compiler to fail and emit MANY warnings. None of them pointed at the real problem which is that I was missing curly brackets {}
.
Metadata
Metadata
Assignees
Labels
Area: Messages for errors, warnings, and lintsArea: The lexing & parsing of Rust source code to an ASTCategory: An issue proposing an enhancement or a PR with one.Diagnostics: Confusing error or lint that should be reworked.Diagnostics: Confusing error or lint; hard to understand for new users.Relevant to the compiler team, which will review and decide on the PR/issue.
Activity
scrabsha commentedon Aug 15, 2021
Hi there! I'd like to work on this issue, as it's an error I frequently hit too.
jyn514 commentedon Aug 16, 2021
Note that the compiler ignores whitespace - this should say something like "if a closure body contains a statement, it must be surrounded by braces".
@scrabsha I'm not sure exactly how to fix this issue, but there are general instructions in https://rustc-dev-guide.rust-lang.org/, and the relevant code lives in
rust/compiler/rustc_parse/src/parser/expr.rs
Line 1719 in 2bd17c1
estebank commentedon Aug 16, 2021
But also keep in mind that for error recovery, we are free to use the whitespace as a signal. The parser isn't well set up to make it easy, but there's a method
SourceMap::is_multiline(&self, Span)
that you can use to see if two different things are on different lines (usually by doing something likesp1.between(sp2)
to make a new span covering the space between two spans you already have).This ticket is somewhat related to #27300, where someone might try to use the Ruby syntax. If we fix this one, that one could also get fixed by default, depending on the specifics. I tried unsuccessfully to tackle that one some time back, but I can think of multiple better approaches than what I did back then.
The big problem here is that code is syntactically correct, but there are multiple knock down errors during typechecking and name resolution.
Rollup merge of rust-lang#88546 - scrabsha:scrabsha/closure-missing-b…
Rollup merge of rust-lang#88546 - scrabsha:scrabsha/closure-missing-b…