Skip to content

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

Open
@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

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

    Issue actions