Skip to content

Commit d648fc6

Browse files
Add E0393 error explanation
1 parent 772c600 commit d648fc6

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

src/librustc_typeck/diagnostics.rs

+31-2
Original file line numberDiff line numberDiff line change
@@ -3426,6 +3426,37 @@ parameters. You can read more about it in the API documentation:
34263426
https://doc.rust-lang.org/std/marker/struct.PhantomData.html
34273427
"##,
34283428

3429+
E0393: r##"
3430+
A type parameter which references `Self` in its default value was not specified.
3431+
Example of erroneous code:
3432+
3433+
```compile_fail
3434+
trait A<T=Self> {}
3435+
3436+
fn together_we_will_rule_the_galaxy(son: &A) {}
3437+
// error: the type parameter `T` must be explicitly specified in an
3438+
// object type because its default value `Self` references the
3439+
// type `Self`
3440+
```
3441+
3442+
A trait object is defined over a single, fully-defined trait. With a regular
3443+
default parameter, this parameter can just be substituted in. However, if the
3444+
default parameter is `Self`, the trait changes for each concrete type; i.e.
3445+
`i32` will be expected to implement `A<i32>`, `bool` will be expected to
3446+
implement `A<bool>`, etc... These types will not share an implementation of a
3447+
fully-defined trait; instead they share implementations of a trait with
3448+
different parameters substituted in for each implementation. This is
3449+
irreconcilable with what we need to make a trait object work, and is thus
3450+
disallowed. Making the trait concrete by explicitly specifying the value of the
3451+
defaulted parameter will fix this issue. Fixed example:
3452+
3453+
```
3454+
trait A<T=Self> {}
3455+
3456+
fn together_we_will_rule_the_galaxy(son: &A<i32>) {} // Ok!
3457+
```
3458+
"##,
3459+
34293460
E0439: r##"
34303461
The length of the platform-intrinsic function `simd_shuffle`
34313462
wasn't specified. Erroneous code example:
@@ -3714,8 +3745,6 @@ register_diagnostics! {
37143745
// between structures
37153746
E0377, // the trait `CoerceUnsized` may only be implemented for a coercion
37163747
// between structures with the same definition
3717-
E0393, // the type parameter `{}` must be explicitly specified in an object
3718-
// type because its default value `{}` references the type `Self`"
37193748
E0399, // trait items need to be implemented because the associated
37203749
// type `{}` was overridden
37213750
E0436, // functional record update requires a struct

0 commit comments

Comments
 (0)