Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Sep 8, 2024

Conversation

bluurryy
Copy link
Contributor

@bluurryy bluurryy commented Sep 5, 2024

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

  • Documentation is updated
  • Tests are added/updated
  • CHANGELOG entry is added

Copy link
Owner

@JelteF JelteF left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a reasonable change overall. But this should also add tests for the problematic cases that this fixes.

@bluurryy
Copy link
Contributor Author

bluurryy commented Sep 5, 2024

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.

@bluurryy bluurryy requested a review from JelteF September 5, 2024 22:14
@JelteF JelteF enabled auto-merge (squash) September 8, 2024 07:53
@JelteF JelteF merged commit 71df46c into JelteF:master Sep 8, 2024
17 checks passed
@JelteF JelteF mentioned this pull request Sep 8, 2024
3 tasks
@tyranron tyranron added this to the 2.0.0 milestone Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ambiguous associated item error when deriving TryFrom, TryInto or FromStr
3 participants