Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Poor recovery from [_]::method #89388

Closed
dtolnay opened this issue Sep 30, 2021 · 0 comments · Fixed by #89447
Closed

Poor recovery from [_]::method #89388

dtolnay opened this issue Sep 30, 2021 · 0 comments · Fixed by #89447
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@dtolnay
Copy link
Member

dtolnay commented Sep 30, 2021

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=5f20683e51f9be844a0cc50aeb746f5d

fn main() {
    let option: Option<&[u8]> = Some(b"...");
    let _ = option.map([_]::to_vec);
}

The current output is:

  • it thinks to_vec is a crate name
  • it claims that [_] isn't meaningful because it isn't left of an assignment
  • it claims that 2 arguments were passed to map
error: expected one of `)`, `,`, `.`, `?`, or an operator, found `::`
 --> src/main.rs:3:27
  |
3 |     let _ = option.map([_]::to_vec);
  |                           -^
  |                           |
  |                           expected one of `)`, `,`, `.`, `?`, or an operator
  |                           help: missing `,`

error[E0425]: cannot find crate `to_vec` in the list of imported crates
 --> src/main.rs:3:29
  |
3 |     let _ = option.map([_]::to_vec);
  |                             ^^^^^^ not found in the list of imported crates

error: in expressions, `_` can only be used on the left-hand side of an assignment
 --> src/main.rs:3:25
  |
3 |     let _ = option.map([_]::to_vec);
  |                         ^ `_` not allowed here

error[E0061]: this function takes 1 argument but 2 arguments were supplied
   --> src/main.rs:3:20
    |
3   |     let _ = option.map([_]::to_vec);
    |                    ^^^ ----------- supplied 2 arguments
    |                    |
    |                    expected 1 argument
    |
note: associated function defined here
   --> /media/david/coding/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/option.rs:843:12
    |
843 |     pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Option<U> {
    |            ^^^

Some errors have detailed explanations: E0061, E0425.
For more information about an error, try `rustc --explain E0061`.

Ideally the output should look like:

error: missing angle brackets in associated item path
 --> src/main.rs:3:24
  |
3 |     let _ = option.map([_]::to_vec);
  |                        ^^^^^^^^^^^ help: try: `<[_]>::to_vec`

Indeed this is exactly that you already get if you had put [u8]::to_vec instead of [_]::to_vec.

fn main() {
    let option: Option<&[u8]> = Some(b"...");
    let _ = option.map([u8]::to_vec);
}
error: missing angle brackets in associated item path
 --> src/main.rs:3:24
  |
3 |     let _ = option.map([u8]::to_vec);
  |                        ^^^^^^^^^^^^ help: try: `<[u8]>::to_vec`
@dtolnay dtolnay added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 30, 2021
Manishearth added a commit to Manishearth/rust that referenced this issue Oct 4, 2021
Improve error message for missing angle brackets in `[_]::method`

Fixes rust-lang#89388.
Manishearth added a commit to Manishearth/rust that referenced this issue Oct 4, 2021
Improve error message for missing angle brackets in `[_]::method`

Fixes rust-lang#89388.
@bors bors closed this as completed in 08dd414 Oct 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant