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

WTF Error Message with `impl<T> Deref for T #28981

Closed
Havvy opened this issue Oct 12, 2015 · 6 comments
Closed

WTF Error Message with `impl<T> Deref for T #28981

Havvy opened this issue Oct 12, 2015 · 6 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@Havvy
Copy link
Contributor

Havvy commented Oct 12, 2015

Play link: http://is.gd/WYlDGu

use std::ops::Deref;

struct Foo;

impl<Foo> Deref for Foo { }

fn main() {}

Error message:

<anon>:5:1: 5:28 error: type parameter `Foo` must be used as the type parameter for some local type (e.g. `MyStruct<T>`); only traits defined in the current crate can be implemented for a type parameter [E0210]
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 help: see the detailed explanation for E0210
<anon>:5:1: 5:28 error: conflicting implementations for trait `core::ops::Deref` [E0119]
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 help: see the detailed explanation for E0119
<anon>:5:1: 5:28 note: conflicting implementation in crate `core`
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 error: conflicting implementations for trait `core::ops::Deref` [E0119]
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 help: see the detailed explanation for E0119
<anon>:5:1: 5:28 note: conflicting implementation in crate `core`
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 error: conflicting implementations for trait `core::ops::Deref` [E0119]
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 help: see the detailed explanation for E0119
<anon>:5:1: 5:28 note: conflicting implementation in crate `collections`
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 error: conflicting implementations for trait `core::ops::Deref` [E0119]
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 help: see the detailed explanation for E0119
<anon>:5:1: 5:28 note: conflicting implementation in crate `std`
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 error: conflicting implementations for trait `core::ops::Deref` [E0119]
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 help: see the detailed explanation for E0119
<anon>:5:1: 5:28 note: conflicting implementation in crate `collections`
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 error: conflicting implementations for trait `core::ops::Deref` [E0119]
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 help: see the detailed explanation for E0119
<anon>:5:1: 5:28 note: conflicting implementation in crate `core`
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 error: conflicting implementations for trait `core::ops::Deref` [E0119]
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 help: see the detailed explanation for E0119
<anon>:5:1: 5:28 note: conflicting implementation in crate `std`
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 error: conflicting implementations for trait `core::ops::Deref` [E0119]
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 help: see the detailed explanation for E0119
<anon>:5:1: 5:28 note: conflicting implementation in crate `std`
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 error: conflicting implementations for trait `core::ops::Deref` [E0119]
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 help: see the detailed explanation for E0119
<anon>:5:1: 5:28 note: conflicting implementation in crate `core`
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 error: conflicting implementations for trait `core::ops::Deref` [E0119]
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 help: see the detailed explanation for E0119
<anon>:5:1: 5:28 note: conflicting implementation in crate `std`
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 error: conflicting implementations for trait `core::ops::Deref` [E0119]
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 help: see the detailed explanation for E0119
<anon>:5:1: 5:28 note: conflicting implementation in crate `alloc`
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 error: conflicting implementations for trait `core::ops::Deref` [E0119]
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 help: see the detailed explanation for E0119
<anon>:5:1: 5:28 note: conflicting implementation in crate `std`
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 error: conflicting implementations for trait `core::ops::Deref` [E0119]
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 help: see the detailed explanation for E0119
<anon>:5:1: 5:28 note: conflicting implementation in crate `alloc`
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 error: conflicting implementations for trait `core::ops::Deref` [E0119]
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 help: see the detailed explanation for E0119
<anon>:5:1: 5:28 note: conflicting implementation in crate `collections`
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 error: conflicting implementations for trait `core::ops::Deref` [E0119]
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 help: see the detailed explanation for E0119
<anon>:5:1: 5:28 note: conflicting implementation in crate `core`
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 error: conflicting implementations for trait `core::ops::Deref` [E0119]
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 help: see the detailed explanation for E0119
<anon>:5:1: 5:28 note: conflicting implementation in crate `alloc`
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 error: conflicting implementations for trait `core::ops::Deref` [E0119]
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 help: see the detailed explanation for E0119
<anon>:5:1: 5:28 note: conflicting implementation in crate `collections`
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 error: conflicting implementations for trait `core::ops::Deref` [E0119]
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 help: see the detailed explanation for E0119
<anon>:5:1: 5:28 note: conflicting implementation in crate `std`
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 error: conflicting implementations for trait `core::ops::Deref` [E0119]
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 help: see the detailed explanation for E0119
<anon>:5:1: 5:28 note: conflicting implementation in crate `std`
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 error: conflicting implementations for trait `core::ops::Deref` [E0119]
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 help: see the detailed explanation for E0119
<anon>:5:1: 5:28 note: conflicting implementation in crate `core`
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 error: conflicting implementations for trait `core::ops::Deref` [E0119]
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:1: 5:28 help: see the detailed explanation for E0119
<anon>:5:1: 5:28 note: conflicting implementation in crate `std`
<anon>:5 impl<Foo> Deref for Foo { }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to 22 previous errors
@huonw huonw added the A-diagnostics Area: Messages for errors, warnings, and lints label Oct 12, 2015
@apasel422
Copy link
Contributor

The error messages are correct. The existence of struct Foo here is irrelevant, as the impl is saying to implement Deref for all types Foo that are Sized. To make the issue more obvious, the impl could be rewritten as impl<T> Deref for T {} -- this clearly conflicts with all other types U: Sized that implement Deref, including those in the std, core, alloc, and collections crates.

It might be possible to improve the error message to suggest removing the type parameter from the impl when a type parameter shadows a type that is in scope.

Similar to #27218.

@jonas-schievink
Copy link
Contributor

It would be useful if the note actually contained the conflicting impl and not the same code that's already printed by the error. I wonder how hard it would be to reconstruct the impl header from the crate metadata.

It should also probably abort after like 5 conflicts for the same impl and just print note: N more conflicts not shown or something similar.

@Havvy
Copy link
Contributor Author

Havvy commented Oct 12, 2015

Yeah, I wasn't claim the error message was incorrect. Sorry about that. I was more claiming that the error message is unwieldy.

bors added a commit that referenced this issue Oct 28, 2015
This makes the error message in #28981 a bit shorter (152 to 115 lines).

Previous output (the local impl was always printed twice when it conflicted with an external impl):
```
test.rs:3:1: 3:23 error: conflicting implementations for trait `core::ops::Deref` [E0119]
test.rs:3 impl<T> Deref for T {}
          ^~~~~~~~~~~~~~~~~~~~~~
test.rs:3:1: 3:23 help: run `rustc --explain E0119` to see a detailed explanation
test.rs:3:1: 3:23 note: conflicting implementation in crate `std`
test.rs:3 impl<T> Deref for T {}
          ^~~~~~~~~~~~~~~~~~~~~~
```

Output after this patch:
```
test.rs:3:1: 3:23 error: conflicting implementations for trait `core::ops::Deref` [E0119]
test.rs:3 impl<T> Deref for T {}
          ^~~~~~~~~~~~~~~~~~~~~~
test.rs:3:1: 3:23 help: run `rustc --explain E0119` to see a detailed explanation
note: conflicting implementation in crate `std`
```
@birkenfeld
Copy link
Contributor

The "type parameter aliases type" part of this issue is #24354.

@steveklabnik steveklabnik removed the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum
Copy link
Member

Today's error message is much shorter, and a little more helpful. We explicitly call out Foo as a type parameter; and also state that it must be used for some local type -- this sort of helps lead to a "oh, the Foo in the RHS isn't a local type" realization, hopefully. I'm not really sure what the best way of communicating this to the user would be, though. Any thoughts?

error[E0119]: conflicting implementations of trait `std::ops::Deref` for type `&_`:
 --> test.rs:5:1
  |
5 | impl<Foo> Deref for Foo { }
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: conflicting implementation in crate `core`

error[E0210]: type parameter `Foo` must be used as the type parameter for some local type (e.g. `MyStruct<T>`); only traits defined in the current crate can be implemented for a type parameter
 --> test.rs:5:1
  |
5 | impl<Foo> Deref for Foo { }
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error(s)

@Havvy
Copy link
Contributor Author

Havvy commented Jun 1, 2017

The lack of the repeating error counts as a fix for this issue. Marking as closed.

@Mark-Simulacrum, open another issue for that if you feel it deserves it.

@Havvy Havvy closed this as completed Jun 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints
Projects
None yet
Development

No branches or pull requests

7 participants