Skip to content

Conflict when implementing trait on associated type #89356

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

Closed
ilslv opened this issue Sep 29, 2021 · 1 comment
Closed

Conflict when implementing trait on associated type #89356

ilslv opened this issue Sep 29, 2021 · 1 comment

Comments

@ilslv
Copy link

ilslv commented Sep 29, 2021

Given the following code:

// provider crate

pub struct Wrapper<T>(T);

pub trait Marker {}

pub trait Unpacked {
    type Inner;
}

impl<T: Marker> Unpacked for T {
    type Inner = T;
}

impl<T: Marker> Unpacked for Wrapper<T> {
    type Inner = T;
}
// another crate which depends on provider

use provider::{Marker, Unpacked, Wrapper};

struct Marked;

impl Marker for Marked {}

struct Into;

impl From<<Wrapper<Marked> as Unpacked>::Inner> for Into {
    fn from(_: <Wrapper<Marked> as Unpacked>::Inner) -> Self {
        Self
    }
}

The current output is:

error[E0119]: conflicting implementations of trait `std::convert::From<Into>` for type `Into`
 --> src/lib.rs:9:1
  |
9 | impl From<<Wrapper<Marked> as Unpacked>::Inner> for Into {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: conflicting implementation in crate `core`:
          - impl<T> From<T> for T;
  = note: upstream crates may add a new impl of trait `provider::Marker` for type `provider::Wrapper<Marked>` in future versions

Basically I'm trying to work around not having specialisation by using Wrapper type: one behaviour for Marker implementors and second one for the Wapper<T> where T: Marker. Then I try to describe trait, which will Unpack Wrapper or resolve Marker itself. Everything is fine, until I try to use those traits in another crate.

I vaguely do understand, that compiler may look at impl<T: Marker> Unpacked for T { .. } blanket impl and figure out that Marker implementation theoretically may be added in the future. But clearly these can't be done because of already existing impl impl<T: Marker> Unpacked for Wrapper<T> { .. }. Is this an ok behaviour of the compiler or a bug? And if this is a valid error where I can find more info about restrictions?

@QuineDot
Copy link

Dupe of #51445, at least at the surface level.

@ilslv ilslv closed this as completed Jun 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants