Skip to content

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

Closed
@schneems

Description

@schneems
Contributor

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 {}.

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Aug 15, 2021
added
D-confusingDiagnostics: Confusing error or lint that should be reworked.
D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
on Aug 15, 2021
scrabsha

scrabsha commented on Aug 15, 2021

@scrabsha
Contributor

Hi there! I'd like to work on this issue, as it's an error I frequently hit too.

jyn514

jyn514 commented on Aug 16, 2021

@jyn514
Member
 | | Multi-line closure requires scope brackets

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

let body = match decl.output {
. You can ask on Zulip for help: https://rust-lang.zulipchat.com/#narrow/stream/182449-t-compiler.2Fhelp

added
A-parserArea: The lexing & parsing of Rust source code to an AST
on Aug 16, 2021
estebank

estebank commented on Aug 16, 2021

@estebank
Contributor

Note that the compiler ignores whitespace

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 like sp1.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.

added a commit that references this issue on Sep 9, 2021

Rollup merge of rust-lang#88546 - scrabsha:scrabsha/closure-missing-b…

09de880
added a commit that references this issue on Sep 10, 2021

Rollup merge of rust-lang#88546 - scrabsha:scrabsha/closure-missing-b…

dc003dd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Participants

      @schneems@estebank@inquisitivecrystal@jyn514@scrabsha

      Issue actions

        Attempting to debug in a iterator's closure without {} gives unhelpful error · Issue #88065 · rust-lang/rust