Skip to content

Bug when implementing From on an associated type defined in an external crate #138816

@roeeshoshani

Description

@roeeshoshani

There seems to be a bug with implementing the From trait when the type argument is an associated type defined in an external crate.

To reproduce this bug you need to have 2 crates.

the first crate, let's call it aaa, is a library crate and contains the following code in its src/lib.rs file:

pub trait DummyTrait {
    type DummyAssociatedType;
}

pub struct DummyStruct;

pub struct DummyAssociatedTypeValue;

impl DummyTrait for DummyStruct {
    type DummyAssociatedType = DummyAssociatedTypeValue;
}

the second crate, let's call it bbb, is also a library crate. it depends on the aaa crate, and it contains the following code in its src/lib.rs file:

pub struct DummyStructInCrateB;

impl From<<aaa::DummyStruct as aaa::DummyTrait>::DummyAssociatedType> for DummyStructInCrateB {
    fn from(value: <aaa::DummyStruct as aaa::DummyTrait>::DummyAssociatedType) -> Self {
        todo!()
    }
}

I expected both crates to compile properly.

Instead, i get the following error when trying to compile crate bbb:

error[E0119]: conflicting implementations of trait `From<DummyStructInCrateB>` for type `DummyStructInCrateB`
 --> src/lib.rs:3:1
  |
3 | impl From<<aaa::DummyStruct as aaa::DummyTrait>::DummyAssociatedType> for DummyStructInCrateB {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: conflicting implementation in crate `core`:
          - impl<T> From<T> for T;

For some reason, the compiler thinks that my implementation conflicts with the blanket implementation, as if it thinks that <aaa::DummyStruct as aaa::DummyTrait>::DummyAssociatedType is somehow the same type as DummyStructInCrateB, which is obviously wrong.

Meta

rustc --version --verbose:

rustc 1.85.1 (4eb161250 2025-03-15)
binary: rustc
commit-hash: 4eb161250e340c8f48f66e2b929ef4a5bed7c181
commit-date: 2025-03-15
host: x86_64-unknown-linux-gnu
release: 1.85.1
LLVM version: 19.1.7

the bug also reproduces on the following nightly version:

rustc 1.87.0-nightly (78948ac25 2025-03-20)
binary: rustc
commit-hash: 78948ac259253ce89effca1e8bb64d16f4684aa4
commit-date: 2025-03-20
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.1

Activity

added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Mar 22, 2025
changed the title [-]Bug when implementing From on an associated type defined in a different crate[/-] [+]Bug when implementing From on an associated type defined in an external crate[/+] on Mar 22, 2025
moxian

moxian commented on Mar 22, 2025

@moxian
Contributor

@rustbot label: +T-types +T-compiler +A-trait-system +A-associated-items +A-coherence -needs-triage

added
A-associated-itemsArea: Associated items (types, constants & functions)
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
T-typesRelevant to the types team, which will review and decide on the PR/issue.
and removed
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Mar 22, 2025
ericmcbride

ericmcbride commented on Mar 25, 2025

@ericmcbride

I also ran into this yesterday.. We had a crate internally that was reexporting a 3rd party trait and it was fine. We decided to deprecate that crate and import in the 3rd party crates directly. This is when this error started appearing.

TheIronBorn

TheIronBorn commented on Jun 14, 2025

@TheIronBorn

I just ran into this with a trait definition in lib.rs and an implementation in main.rs. Not an explicitly external crate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-coherenceArea: CoherenceA-trait-systemArea: Trait systemC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.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

        Participants

        @TheIronBorn@moxian@ericmcbride@rustbot@roeeshoshani

        Issue actions

          Bug when implementing From on an associated type defined in an external crate · Issue #138816 · rust-lang/rust