Skip to content

Attempting to debug in a iterator's closure without {} gives unhelpful error #88065

Closed
@schneems

Description

@schneems

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=9b9dfd706291c45a1f6a567324b6c860

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

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions