Skip to content

Poor recovery from [_]::method #89388

Closed

Description

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`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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