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

Implementing From for associated type results in confusing error #85576

Open
oliver-giersch opened this issue May 22, 2021 · 3 comments
Open
Labels
A-associated-items Area: Associated items such as associated types and consts. A-coherence Area: Coherence A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@oliver-giersch
Copy link
Contributor

Disclaimer: I am not sure if this an actual bug or rather a diagnostic issue.

The following simplified code

use std::str::FromStr;

type BoolErr = <bool as FromStr>::Err;

struct Error(BoolErr);

impl From<BoolErr> for Error {
    fn from(err: BoolErr) -> Self {
        Self(err)
    }
}

Playground

Produces the following incorrect/confusing error message on both stable and nigthly:

error[E0119]: conflicting implementations of trait `std::convert::From<Error>` for type `Error`:
 --> src/lib.rs:7:1
  |
7 | impl From<BoolErr> for Error {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: conflicting implementation in crate `core`:
          - impl<T> From<T> for T;

I encountered this issue when I wanted to wrap the result returned by str::parse for a type from crate A which uses an error type from another crate B, which I did not want to depend on and import directly. I am aware that using an associated type here has implications for backwards compatibility, but in this case this is actually desirable I think, since I only want to forward the result into my custom error type, even if the concrete error type in crate A/B changes.

@oliver-giersch oliver-giersch added the C-bug Category: This is a bug. label May 22, 2021
@ChrisDenton ChrisDenton added the needs-triage-legacy Old issue that were never triaged. Remove this label once the issue has been sufficiently triaged. label Jul 16, 2023
@smmoosavi
Copy link

smmoosavi commented Nov 7, 2023

This is a bug.

When it happens

When type is an associated type of a trait, and the trait is implemented in another crate. if the trait is implemented in the same crate, it works fine. Also if types are used directly, it works fine.

playground 1 use ParseBoolError directly and has no error
playground 2 use type from trait and have the error

ParseBoolError and BoolErr are equal.

type BoolErr = <bool as FromStr>::Err;
assert_eq!(TypeId::of::<BoolErr>(), TypeId::of::<ParseBoolError>());

I have created a new repository that showcases another example of the same error.

type OtherDuration = <other::Systick as other::Monotonic>::Duration;
type OtherInstant = <other::Systick as other::Monotonic>::Instant;

impl FromTime for OtherDuration {}
impl FromTime for OtherInstant {}

@fmease fmease added A-diagnostics Area: Messages for errors, warnings, and lints A-associated-items Area: Associated items such as associated types and consts. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. D-confusing Diagnostics: Confusing error or lint that should be reworked. A-coherence Area: Coherence and removed needs-triage-legacy Old issue that were never triaged. Remove this label once the issue has been sufficiently triaged. C-bug Category: This is a bug. labels Jan 23, 2024
@lcnr lcnr added T-types Relevant to the types team, which will review and decide on the PR/issue. and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 12, 2024
@lcnr
Copy link
Contributor

lcnr commented May 12, 2024

FOr an explanation of the affected issue, see #99940 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items such as associated types and consts. A-coherence Area: Coherence A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants