JelteF / derive_more

Some more derive(Trait) options
MIT License
1.73k stars 123 forks source link

Fix ambiguous associated item for `TryFrom`, `TryInto` and `FromStr` #410

Closed bluurryy closed 1 month ago

bluurryy commented 2 months ago

Resolves #409

Synopsis

Fixes the "abiguous associated item" error when deriving TryFrom, TryInto or FromStr when those types have an associated item called Error or Err respectively.

All these derives produce that error:

use derive_more::*;

#[derive(TryFrom)]
#[try_from(repr)]
#[repr(u8)]
enum LogLevel {
    Error,
}

#[derive(FromStr)]
enum EnumNoFields {
    Err,
}

#[derive(TryInto)]
enum MixedInts {
    Foo(LogLevel),
}

Solution

In the derive macro I replaced Self::Error with the type that's behind it. I could also replace it with <Self as #trait_path>::Error if you'd prefer.

Checklist

bluurryy commented 2 months ago

I added some regression tests. (I checked that those tests fail on the master branch.)

The CI failures are unrelated, they are from compile_fail failures due to some new compiler error help message.