derive_more icon indicating copy to clipboard operation
derive_more copied to clipboard

Generate bespoke compile time errors for deriving conflicting `From` implementations

Open JonathanWoollett-Light opened this issue 3 years ago • 1 comments

An idea for improving explicitness (I may submit a PR in the future)

At the moment:

#[derive(From)]
enum Conflicting {
    A(u8),
    B(u8),
    C(u16)
}
#[test]
fn conflicting_enum() {
    let a = Conflicting::from(4u8);
}

produces:

error[E0119]: conflicting implementations of trait `std::convert::From<u8>` for type `Conflicting`
  --> tests/from.rs:56:10
   |
56 | #[derive(From)]
   |          ^^^^
   |          |
   |          first implementation here
   |          conflicting implementation for `Conflicting`
   |

where it would be better to produce:

error: Conflicting types found when attempting to derive `From` for `Conflicting` (`Conflicting::A(u8)`, `Conflicting::B(u8)`). Please ignore all/all-but-one of the variants.
  --> tests/from.rs:65:5
   |
65 |     std::compile_error!("Conflicting types found when attempting to derive `From` for `Conflicting` (`Conflicting::A(u8)`, Please ignore all/all-but-one of the variants.`Conflicting::B(u8)`).");
   |     

JonathanWoollett-Light avatar Jun 29 '22 15:06 JonathanWoollett-Light

I agree that this error is not very helpful and that we should improve it. It should be fairly simple. The code we use to derive From could simply track if it is going to generate implementations for identical types (using a HashSet for instance) and if so, fail with a useful error.

JelteF avatar Dec 21 '23 23:12 JelteF