Skip to content

Implementing From for associated type results in confusing error #85576

@oliver-giersch

Description

@oliver-giersch

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-coherenceArea: CoherenceA-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions