]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0119.md
Re-use std::sealed::Sealed in os/linux/process.
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0119.md
1 There are conflicting trait implementations for the same type.
2
3 Erroneous code example:
4
5 ```compile_fail,E0119
6 trait MyTrait {
7     fn get(&self) -> usize;
8 }
9
10 impl<T> MyTrait for T {
11     fn get(&self) -> usize { 0 }
12 }
13
14 struct Foo {
15     value: usize
16 }
17
18 impl MyTrait for Foo { // error: conflicting implementations of trait
19                        //        `MyTrait` for type `Foo`
20     fn get(&self) -> usize { self.value }
21 }
22 ```
23
24 When looking for the implementation for the trait, the compiler finds
25 both the `impl<T> MyTrait for T` where T is all types and the `impl
26 MyTrait for Foo`. Since a trait cannot be implemented multiple times,
27 this is an error. So, when you write:
28
29 ```
30 trait MyTrait {
31     fn get(&self) -> usize;
32 }
33
34 impl<T> MyTrait for T {
35     fn get(&self) -> usize { 0 }
36 }
37 ```
38
39 This makes the trait implemented on all types in the scope. So if you
40 try to implement it on another one after that, the implementations will
41 conflict. Example:
42
43 ```
44 trait MyTrait {
45     fn get(&self) -> usize;
46 }
47
48 impl<T> MyTrait for T {
49     fn get(&self) -> usize { 0 }
50 }
51
52 struct Foo;
53
54 fn main() {
55     let f = Foo;
56
57     f.get(); // the trait is implemented so we can use it
58 }
59 ```