rust-lang / rust

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

Add hints to diagnostic messages about type annotation #88931

Closed elichai closed 1 year ago

elichai commented 3 years ago
fn example(n: &[u8]) -> [u8; 32] {
    match n.try_into() {
        Ok(n) => {
            println!("len: {}", n.len());
            n
        },
        Err(_) => panic!("wops"),
    }
}

https://play.rust-lang.org/?gist=e9720a11b44e59362937210443cb7b86 The current output is:

error[E0282]: type annotations needed
 --> src/lib.rs:6:33
  |
4 |     match n.try_into() {
  |           ------------ this method call resolves to `Result<T, <Self as TryInto<T>>::Error>`
5 |         Ok(n) => {
6 |             println!("len: {}", n.len());
  |                                 ^ cannot infer type
  |
  = note: type must be known at this point
For more information about this error, try `rustc --explain E0282`.
error: could not compile `playground` due to previous error

Ideally it would hint me on how should I annotate, especially here where the answer is not obvious, it could tell you to either do match TryInto::<[u8; 32]>::try_into(n) { or <[u8; 32]>::try_from(n)

estebank commented 1 year ago

Current output:

error[[E0282]](https://doc.rust-lang.org/stable/error_codes/E0282.html): type annotations needed
 --> src/lib.rs:4:13
  |
4 |     match n.try_into() {
  |             ^^^^^^^^
5 |         Ok(n) => {
6 |             println!("len: {}", n.len());
  |                                 - type must be known at this point
  |
help: try using a fully qualified path to specify the expected types
  |
4 |     match <&[u8] as TryInto<T>>::try_into(n) {
  |           ++++++++++++++++++++++++++++++++ ~