Skip to content

Commit 49be3dd

Browse files
Add E0520 error code explanation
1 parent 23a7d30 commit 49be3dd

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

src/librustc_typeck/diagnostics.rs

+54-1
Original file line numberDiff line numberDiff line change
@@ -3644,6 +3644,60 @@ fn main() {
36443644
```
36453645
"##,
36463646

3647+
E0520: r##"
3648+
A non-default implementation was already made on this type
3649+
implementation so it cannot be specialized afterward. Erroneous
3650+
code example:
3651+
3652+
```compile_fail
3653+
#![feature(specialization)]
3654+
3655+
trait SpaceLama {
3656+
fn fly(&self);
3657+
}
3658+
3659+
impl<T> SpaceLama for T {
3660+
default fn fly(&self) {}
3661+
}
3662+
3663+
impl<T: Clone> SpaceLama for T {
3664+
fn fly(&self) {}
3665+
}
3666+
3667+
impl SpaceLama for i32 {
3668+
default fn fly(&self) {}
3669+
// error: item `fly` is provided by an `impl` that specializes
3670+
// another, but the item in the parent `impl` is not marked
3671+
// `default` and so it cannot be specialized.
3672+
}
3673+
```
3674+
3675+
To fix this error, you need to specialize the implementation on the
3676+
parent(s) implementation first. Example:
3677+
3678+
```compile_fail
3679+
#![feature(specialization)]
3680+
3681+
trait SpaceLama {
3682+
fn fly(&self);
3683+
}
3684+
3685+
impl<T> SpaceLama for T {
3686+
default fn fly(&self) {} // This is a parent implementation.
3687+
}
3688+
3689+
impl<T: Clone> SpaceLama for T {
3690+
default fn fly(&self) {} // This is a parent implementation but not
3691+
// a default one so you need to add default
3692+
// keyword.
3693+
}
3694+
3695+
impl SpaceLama for i32 {
3696+
default fn fly(&self) {} // And now that's ok!
3697+
}
3698+
```
3699+
"##,
3700+
36473701
}
36483702

36493703
register_diagnostics! {
@@ -3720,6 +3774,5 @@ register_diagnostics! {
37203774
// type `{}` was overridden
37213775
E0436, // functional record update requires a struct
37223776
E0513, // no type for local variable ..
3723-
E0520, // cannot specialize non-default item
37243777
E0521 // redundant default implementations of trait
37253778
}

0 commit comments

Comments
 (0)