Skip to content

Compilation error when implementing From using a concrete associated type from another crate #109885

Open
@stormshield-kg

Description

@stormshield-kg

I have the following files from 2 crates:

// dep/src/lib.rs

pub trait Wrapped {
    type WrappedType;
}

pub struct W3;
pub struct S3;

impl Wrapped for S3 {
    type WrappedType = W3;
}
// main/src/lib.rs

use dep::Wrapped;

struct W;
struct S;

impl Wrapped for S {
    type WrappedType = W;
}

struct W2;
struct S2;

impl Wrapped for S2 {
    type WrappedType = W2;
}

// This works
impl From<<S2 as Wrapped>::WrappedType> for W {
    fn from(value: W2) -> Self {
        todo!()
    }
}

// This doesn't work
impl From<<dep::S3 as Wrapped>::WrappedType> for W {
    fn from(value: dep::W3) -> Self {
        todo!()
    }
}

Currently I have the following error:

error[E0119]: conflicting implementations of trait `std::convert::From<W>` for type `W`
  --> main/src/lib.rs:23:1
   |
23 | impl From<<dep::S3 as Wrapped>::WrappedType> for W {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: conflicting implementation in crate `core`:
           - impl<T> std::convert::From<T> for T;

I expected that <dep::S3 as Wrapped>::WrappedType would be resolved to a single type like <S2 as Wrapped>::WrappedType>, and that the code would compile.

Context

I'm using dep::S3 and W as the inputs of a proc-macro, and I would like to generate impl From<dep::W3> for W inside the proc-macro, but without mentioning dep::W3 explicitely.

Meta

rustc --version --verbose:

rustc 1.70.0-nightly (0599b6b93 2023-04-01)
binary: rustc
commit-hash: 0599b6b931816ab46ab79072189075f543931cbd
commit-date: 2023-04-01
host: x86_64-unknown-linux-gnu
release: 1.70.0-nightly
LLVM version: 16.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-coherenceArea: CoherenceC-bugCategory: This is a bug.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