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

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

Open
roeeshoshani opened this issue Mar 22, 2025 · 2 comments
Labels
A-associated-items Area: Associated items (types, constants & functions) A-coherence Area: Coherence A-trait-system Area: Trait system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@roeeshoshani
Copy link

roeeshoshani commented Mar 22, 2025

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
@roeeshoshani roeeshoshani added the C-bug Category: This is a bug. label Mar 22, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 22, 2025
@roeeshoshani roeeshoshani 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 Mar 22, 2025
@moxian
Copy link
Contributor

moxian commented Mar 22, 2025

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

@rustbot rustbot added A-associated-items Area: Associated items (types, constants & functions) A-coherence Area: Coherence A-trait-system Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Mar 22, 2025
@ericmcbride
Copy link

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.

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 (types, constants & functions) A-coherence Area: Coherence A-trait-system Area: Trait system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. 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

4 participants