rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.23k stars 12.7k forks source link

Misleading error message with malformed turbofishes #54521

Closed atamis closed 5 years ago

atamis commented 6 years ago

I typoed the angle brackets when attempting turbofish.

fn main() {
    println!("{:?}", vec![1, 2, 3].into_iter().collect::<Vec<usize>>>());
}

(Playground)

I expected to see this happen: To get an error indicating that I had an extra closing angle bracket after my turbofish, or that I had a mismatched delimiter.

Here is what happened instead:

Errors:

   Compiling playground v0.0.1 (/playground)
error: field expressions may not have generic arguments
 --> src/lib.rs:2:55
  |
2 |     println!("{:?}", vec![1, 2, 3].into_iter().collect::<Vec<usize>>>());
  |                                                       ^^^^^^^^^^^^^^

error: aborting due to previous error

error: Could not compile `playground`.

To learn more, run the command again with --verbose.

This error message refers to the case when you attempt to use turbofish on a struct field rather than a method or function, like struct.field::<usize>. However, this error message is confusing to users because, if you look at the end of the collect call, there are parentheses, and this looks like a correctly formed method call, which obscures the real error.

atamis commented 6 years ago

For reference, this is how the compiler responds to the same typo elsewhere.

fn main() {
    println!("{:?}", Vec::<usize>>::new());
}

(Playground)

   Compiling playground v0.0.1 (/playground)
error[E0423]: expected value, found struct `Vec`
 --> src/main.rs:2:22
  |
2 |     println!("{:?}", Vec::<usize>>::new());
  |                      ^^^^^^^^^^^^ did you mean `Vec { /* fields */ }`?

error[E0425]: cannot find function `new` in the crate root
 --> src/main.rs:2:37
  |
2 |     println!("{:?}", Vec::<usize>>::new());
  |                                     ^^^ not found in the crate root
mrcnski commented 5 years ago

Just ran into this. Weird/confusing/unhelpful error message -- the real problem is an extra delimiter as you said.

estebank commented 5 years ago

The opposite problem should also be handled, probably as part of a separate PR: vec![1, 2, 3].into_iter().collect::<Vec<usize>())


Created https://github.com/rust-lang/rust/issues/57819 to track this case.