Compiler suggests #[derive((DeriveMacro).try_into().unwrap())] · Issue #78862 · rust-lang/rust (original) (raw)

Today I encountered a rather unhelpful suggestion from the compiler (1.47.0):

error[E0308]: mismatched types                                                                                                                                                                                                                                               
 --> src/lib.rs:3:10                                                                                                                                                                                                                              
  |                                                                                                                                                                                                                                                                          
3 | #[derive(DeriveMacro)]                                                                                                                                                                                                                                                      
  |          ^^^^^^^^^^^ expected `usize`, found `i32`                                                                                                                                                                                                                          
  |                                                                                                                                                                                                                                                                          
  = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)                                                                                                                                                             
help: you can convert an `i32` to `usize` and panic if the converted value wouldn't fit                                                                                                                                                                                      
  |                                                                                                                                                                                                                                                                          
3 | #[derive((DeriveMacro).try_into().unwrap())]                                                                                                                                                                                                                                
  |   

The code emited from the derive macro contains something along the lines of let value: usize = -1 as i32.

Applying the suggestion, however, doesn't really help (almost as if I had to fix the problem in the derive macro):

error: malformed `derive` attribute input
 --> src/lib.rs:3:1
  |
3 | #[derive((DeriveMacro).try_into().unwrap())]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: missing traits to be derived: `#[derive(Trait1, Trait2, ...)]`

I believe the compiler should only emit that suggestion if the code it applies to is actually an expression of type i32.