]> git.proxmox.com Git - rustc.git/blob - src/librustc_error_codes/error_codes/E0520.md
New upstream version 1.47.0+dfsg1
[rustc.git] / src / librustc_error_codes / error_codes / E0520.md
1 A non-default implementation was already made on this type so it cannot be
2 specialized further.
3
4 Erroneous code example:
5
6 ```compile_fail,E0520
7 #![feature(specialization)]
8
9 trait SpaceLlama {
10 fn fly(&self);
11 }
12
13 // applies to all T
14 impl<T> SpaceLlama for T {
15 default fn fly(&self) {}
16 }
17
18 // non-default impl
19 // applies to all `Clone` T and overrides the previous impl
20 impl<T: Clone> SpaceLlama for T {
21 fn fly(&self) {}
22 }
23
24 // since `i32` is clone, this conflicts with the previous implementation
25 impl SpaceLlama for i32 {
26 default fn fly(&self) {}
27 // error: item `fly` is provided by an `impl` that specializes
28 // another, but the item in the parent `impl` is not marked
29 // `default` and so it cannot be specialized.
30 }
31 ```
32
33 Specialization only allows you to override `default` functions in
34 implementations.
35
36 To fix this error, you need to mark all the parent implementations as default.
37 Example:
38
39 ```
40 #![feature(specialization)]
41
42 trait SpaceLlama {
43 fn fly(&self);
44 }
45
46 // applies to all T
47 impl<T> SpaceLlama for T {
48 default fn fly(&self) {} // This is a parent implementation.
49 }
50
51 // applies to all `Clone` T; overrides the previous impl
52 impl<T: Clone> SpaceLlama for T {
53 default fn fly(&self) {} // This is a parent implementation but was
54 // previously not a default one, causing the error
55 }
56
57 // applies to i32, overrides the previous two impls
58 impl SpaceLlama for i32 {
59 fn fly(&self) {} // And now that's ok!
60 }
61 ```