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

Blanket impl resolution vs. trait bounds #58904

Closed
jcape opened this issue Mar 3, 2019 · 1 comment
Closed

Blanket impl resolution vs. trait bounds #58904

jcape opened this issue Mar 3, 2019 · 1 comment

Comments

@jcape
Copy link

jcape commented Mar 3, 2019

I have run into similar errors to #50133, so that may share the same ultimate cause as mine.

I'm having an issue with the impl<T> From<T> for T blanket implementation causing conflicts with my own blanket implementation of a From trait. For example, if I have a type which implements AsRef<u8>, and From<T: AsRef<[u8]>>, that blanket implementation conflicts, because my implementation of the trait and the trait-bound-from obviously provide a different path for T::from(T). I think this is wrong, though in my specific case I have worked around it by doing From<&'bytes [u8]>, but wrong is wrong :-)

Some sample code:

struct MyType(Vec<u8>);

impl AsRef<[u8]> for MyType {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}

impl<B: AsRef<[u8]> From<B> for MyType {
    fn from(src: B) -> Self {
        Self(Vec::from(src))
    }
}

Resulting error:

error[E0119]: conflicting implementations of trait `std::convert::From<MyType>` for type `MyType`:
  --> src/main.rs:10:1
   |
10 | impl<B: AsRef<[u8]>> From<B> for MyType {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: conflicting implementation in crate `core`:
           - impl<T> std::convert::From<T> for T;
@jonas-schievink
Copy link
Contributor

The second impl could be instantiated with B = MyType because of the first impl. This would collide with the blanket impl in core, shown in the error message. Specialization could possibly allow this, but right now this is intentionally forbidden.

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