Skip to content

Commit 9ffe558

Browse files
authored
Rollup merge of rust-lang#135971 - compiler-errors:self-projection, r=fmease
Properly report error when object type param default references self I accidentally broke this error for cases where a type parameter references `Self` via a projection (i.e. `trait Foo<Arg = Self::Bar> {}`). This PR fixes that, and also makes the error a bit easier to understand. Fixes rust-lang#135918
2 parents 1fc3491 + ea9a253 commit 9ffe558

11 files changed

+71
-21
lines changed

compiler/rustc_error_codes/src/error_codes/E0393.md

+5-7
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ A type parameter which references `Self` in its default value was not specified.
33
Erroneous code example:
44

55
```compile_fail,E0393
6-
trait A<T=Self> {}
6+
trait A<T = Self> {}
77
8-
fn together_we_will_rule_the_galaxy(son: &A) {}
9-
// error: the type parameter `T` must be explicitly specified in an
10-
// object type because its default value `Self` references the
11-
// type `Self`
8+
fn together_we_will_rule_the_galaxy(son: &dyn A) {}
9+
// error: the type parameter `T` must be explicitly specified
1210
```
1311

1412
A trait object is defined over a single, fully-defined trait. With a regular
@@ -23,7 +21,7 @@ disallowed. Making the trait concrete by explicitly specifying the value of the
2321
defaulted parameter will fix this issue. Fixed example:
2422

2523
```
26-
trait A<T=Self> {}
24+
trait A<T = Self> {}
2725
28-
fn together_we_will_rule_the_galaxy(son: &A<i32>) {} // Ok!
26+
fn together_we_will_rule_the_galaxy(son: &dyn A<i32>) {} // Ok!
2927
```

compiler/rustc_hir_analysis/messages.ftl

+7-1
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,13 @@ hir_analysis_missing_type_params =
353353
[one] reference
354354
*[other] references
355355
} to {$parameters}
356-
.note = because of the default `Self` reference, type parameters must be specified on object types
356+
.note = because the parameter {$parameterCount ->
357+
[one] default references
358+
*[other] defaults reference
359+
} `Self`, the {$parameterCount ->
360+
[one] parameter
361+
*[other] parameters
362+
} must be specified on the object type
357363
358364
hir_analysis_multiple_relaxed_default_bounds =
359365
type parameter has more than one relaxed default bound, only one is supported

compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_compatibility.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -237,16 +237,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
237237
// Skip `Self`
238238
.skip(1)
239239
.map(|(index, arg)| {
240-
if arg == dummy_self.into() {
240+
if arg.walk().any(|arg| arg == dummy_self.into()) {
241241
let param = &generics.own_params[index];
242242
missing_type_params.push(param.name);
243243
Ty::new_misc_error(tcx).into()
244-
} else if arg.walk().any(|arg| arg == dummy_self.into()) {
245-
let guar = self.dcx().span_delayed_bug(
246-
span,
247-
"trait object trait bounds reference `Self`",
248-
);
249-
replace_dummy_self_with_error(tcx, arg, guar)
250244
} else {
251245
arg
252246
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
trait A<C = <Self as D>::E> {}
2+
3+
trait D {
4+
type E;
5+
}
6+
7+
impl A<()> for () {}
8+
impl D for () {
9+
type E = ();
10+
}
11+
12+
fn f() {
13+
let B: &dyn A = &();
14+
//~^ ERROR the type parameter `C` must be explicitly specified
15+
}
16+
17+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0393]: the type parameter `C` must be explicitly specified
2+
--> $DIR/default-param-self-projection.rs:13:17
3+
|
4+
LL | trait A<C = <Self as D>::E> {}
5+
| --------------------------- type parameter `C` must be specified for this
6+
...
7+
LL | let B: &dyn A = &();
8+
| ^
9+
|
10+
= note: because the parameter default references `Self`, the parameter must be specified on the object type
11+
help: set the type parameter to the desired type
12+
|
13+
LL | let B: &dyn A<C> = &();
14+
| +++
15+
16+
error: aborting due to 1 previous error
17+
18+
For more information about this error, try `rustc --explain E0393`.

tests/ui/error-codes/E0393.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL |
77
LL | fn together_we_will_rule_the_galaxy(son: &dyn A) {}
88
| ^
99
|
10-
= note: because of the default `Self` reference, type parameters must be specified on object types
10+
= note: because the parameter default references `Self`, the parameter must be specified on the object type
1111
help: set the type parameter to the desired type
1212
|
1313
LL | fn together_we_will_rule_the_galaxy(son: &dyn A<T>) {}

tests/ui/issues/issue-22370.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL |
77
LL | fn f(a: &dyn A) {}
88
| ^
99
|
10-
= note: because of the default `Self` reference, type parameters must be specified on object types
10+
= note: because the parameter default references `Self`, the parameter must be specified on the object type
1111
help: set the type parameter to the desired type
1212
|
1313
LL | fn f(a: &dyn A<T>) {}

tests/ui/traits/object/canonicalize-fresh-infer-vars-issue-103626.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ fn w<'a, T: 'a, F: Fn(&'a T)>() {
1010
let b: &dyn FromResidual = &();
1111
//~^ ERROR: the trait `FromResidual` is not dyn compatible
1212
//~| ERROR: the trait `FromResidual` is not dyn compatible
13+
//~| ERROR the type parameter `R` must be explicitly specified
1314
}
1415

1516
fn main() {}

tests/ui/traits/object/canonicalize-fresh-infer-vars-issue-103626.stderr

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
error[E0393]: the type parameter `R` must be explicitly specified
2+
--> $DIR/canonicalize-fresh-infer-vars-issue-103626.rs:10:17
3+
|
4+
LL | trait FromResidual<R = <Self as Try>::Residual> {
5+
| ----------------------------------------------- type parameter `R` must be specified for this
6+
...
7+
LL | let b: &dyn FromResidual = &();
8+
| ^^^^^^^^^^^^
9+
|
10+
= note: because the parameter default references `Self`, the parameter must be specified on the object type
11+
help: set the type parameter to the desired type
12+
|
13+
LL | let b: &dyn FromResidual<R> = &();
14+
| +++
15+
116
error[E0038]: the trait `FromResidual` is not dyn compatible
217
--> $DIR/canonicalize-fresh-infer-vars-issue-103626.rs:10:32
318
|
@@ -45,6 +60,7 @@ help: alternatively, consider constraining `from_residual` so it does not apply
4560
LL | fn from_residual(residual: R) -> Self where Self: Sized;
4661
| +++++++++++++++++
4762

48-
error: aborting due to 2 previous errors
63+
error: aborting due to 3 previous errors
4964

50-
For more information about this error, try `rustc --explain E0038`.
65+
Some errors have detailed explanations: E0038, E0393.
66+
For more information about an error, try `rustc --explain E0038`.

tests/ui/traits/unspecified-self-in-trait-ref.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ LL | pub trait Bar<X=usize, A=Self> {
9797
LL | let e = Bar::<usize>::lol();
9898
| ^^^^^^^^^^^^ missing reference to `A`
9999
|
100-
= note: because of the default `Self` reference, type parameters must be specified on object types
100+
= note: because the parameter default references `Self`, the parameter must be specified on the object type
101101

102102
error: aborting due to 5 previous errors; 5 warnings emitted
103103

tests/ui/type/type-parameter-defaults-referencing-Self.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | trait Foo<T=Self> {
77
LL | fn foo(x: &dyn Foo) { }
88
| ^^^
99
|
10-
= note: because of the default `Self` reference, type parameters must be specified on object types
10+
= note: because the parameter default references `Self`, the parameter must be specified on the object type
1111
help: set the type parameter to the desired type
1212
|
1313
LL | fn foo(x: &dyn Foo<T>) { }

0 commit comments

Comments
 (0)