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

conflicting implementations when attempting to `impl<S> Into<S> for ArbitraryStructWithNoOtherImpls<S>` #27403

Closed
jmesmon opened this Issue Jul 30, 2015 · 5 comments

Comments

Projects
None yet
5 participants
@jmesmon
Copy link
Contributor

jmesmon commented Jul 30, 2015

At the very least (if this failure is expected), the error needs to be improved to actually point to the conflicting impl rather than simply printing the crate name and repeating the same function output that conflicts (if it is for some reason not possible to print better info about the previously declared conflicting impl, at the very least the compiler should stop repeating the output for the impl generating the error). On the diagnostics side, this appears related to #27218.

Note that simply changing the struct (in this case, GenX<T>) to a non-generic struct allows the build to proceed without issue (example: http://is.gd/lRAhBF).

It appears that somehow the impl<T, U> Into<U> for T where U: From<T> is considered to be conflicting with all Into<U> impls where U is a generic parameter even in cases where U does not impl From<T>.

error:

<anon>:6:1: 10:2 error: conflicting implementations for trait `core::convert::Into` [E0119]
<anon>: 6 impl<S> Into<S> for GenX<S> {
<anon>: 7     fn into(self) -> S {
<anon>: 8         self.inner
<anon>: 9     }
<anon>:10 }
<anon>:6:1: 10:2 help: see the detailed explanation for E0119
<anon>:6:1: 10:2 note: conflicting implementation in crate `core`
<anon>: 6 impl<S> Into<S> for GenX<S> {
<anon>: 7     fn into(self) -> S {
<anon>: 8         self.inner
<anon>: 9     }
<anon>:10 }
error: aborting due to previous error

source:

pub struct GenX<S> {
    inner: S,
}
impl<S> Into<S> for GenX<S> {
    fn into(self) -> S {
        self.inner
    }
}

playpen: http://is.gd/zrwjve

@eefriedman

This comment has been minimized.

Copy link
Contributor

eefriedman commented Jul 30, 2015

In your example, the compiler can't prove that S doesn't implement From<GenX<S>>. In the general case, S could come from a crate which depends on your crate.

I agree with your assessment of the diagnostic.

@jmesmon

This comment has been minimized.

Copy link
Contributor Author

jmesmon commented Jul 30, 2015

@eefriedman I suppose in that case I'd consider the conflict to come from the provider of the From impl, and give an error when that downstream crate attempted to build (ie: I'm declaring an impl of Into<S> that already exists when that crate builds).

It might be helpful to note that with the current way the compiler thinks about things, Into<S> can't be implemented for any S which I don't control (as the only way to implement it is by impl-ing From on S). This rather reduces the flexibility here, and means I'll probably end up just having into() methods that aren't members of trait Into. Perhaps we should have an IntoWithoutDefaults? That seems like an undesirable workaround.

Edit: note, I haven't thought about the above enough to know if it is possible.

Edit2: and just realized that the behavior I'm looking for in the first paragraph is similar in functionality to negative trait bounds.

@brson

This comment has been minimized.

Copy link
Contributor

brson commented Jan 26, 2017

Op compiles successfully today.

@brson brson added the E-needstest label Jan 26, 2017

@brson

This comment has been minimized.

Copy link
Contributor

brson commented Jan 26, 2017

Just add a test case.

@steveklabnik steveklabnik removed the T-compiler label Mar 9, 2017

@Mark-Simulacrum

This comment has been minimized.

Copy link
Member

Mark-Simulacrum commented Jun 1, 2017

One of the examples does compile, but the issue was reported about a different example which doesn't. Not E-needstest; not sure that I follow what could change with the diagnostic though.

pub struct GenX<S> {
    inner: S,
}

impl<S> Into<S> for GenX<S> {
    fn into(self) -> S {
        self.inner
    }
}

fn main() {
    println!("HI");
}

bors added a commit that referenced this issue Oct 25, 2017

Auto merge of #45455 - kennytm:print-extern-impl-for-e0119, r=nikomat…
…sakis

Improve diagnostic of E0119 with extern crate, try to print the conflicting impl.

Closes #27403.
Closes #23563.

Should improve #23980.

The diagnostic now looks like:

```
error[E0119]: conflicting implementations of trait `std::convert::Into<_>` for type `GenX<_>`:
  --> $DIR/issue-27403.rs:15:1
   |
15 | / impl<S> Into<S> for GenX<S> {
16 | |     fn into(self) -> S {
17 | |         self.inner
18 | |     }
19 | | }
   | |_^
   |
   = note: conflicting implementation in crate `core`:
           - impl<T, U> std::convert::Into<U> for T
             where U: std::convert::From<T>;

error: aborting due to previous error
```

@bors bors closed this in #45455 Oct 25, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.