Skip to content

Commit 9d39b59

Browse files
authored
Rollup merge of #129395 - fmease:pp-dyn-w-gat, r=compiler-errors
Pretty-print own args of existential projections (dyn-Trait w/ GAT constraints) Previously we would just drop them. This bug isn't that significant as it can only be triggered by user code that constrains GATs inside trait object types which is currently gated under the interim feature `generic_associated_types_extended` (whose future is questionable) or on stable if the GATs are 'disabled' in dyn-Trait via `where Self: Sized` (in which case the assoc type bindings get ignored anyway (and trigger the warn-by-default lint `unused_associated_type_bounds`)), so yeah. Affects diagnostic output and output of `std::any::type_name{_of_val}`.
2 parents 8b3ca79 + 080c2ca commit 9d39b59

File tree

7 files changed

+50
-20
lines changed

7 files changed

+50
-20
lines changed

compiler/rustc_middle/src/ty/print/pretty.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -3119,7 +3119,10 @@ define_print! {
31193119

31203120
ty::ExistentialProjection<'tcx> {
31213121
let name = cx.tcx().associated_item(self.def_id).name;
3122-
p!(write("{} = ", name), print(self.term))
3122+
// The args don't contain the self ty (as it has been erased) but the corresp.
3123+
// generics do as the trait always has a self ty param. We need to offset.
3124+
let args = &self.args[cx.tcx().generics_of(self.def_id).parent_count - 1..];
3125+
p!(path_generic_args(|cx| write!(cx, "{name}"), args), " = ", print(self.term))
31233126
}
31243127

31253128
ty::ProjectionPredicate<'tcx> {

tests/ui/generic-associated-types/gat-in-trait-path.base.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ LL | type A<'a> where Self: 'a;
5151
= help: the following types implement the trait, consider defining an enum where each variant holds one of these types, implementing `Foo` for this new enum and using it instead:
5252
Fooy
5353
Fooer<T>
54-
= note: required for the cast from `Box<Fooer<{integer}>>` to `Box<(dyn Foo<A = &'a ()> + 'static)>`
54+
= note: required for the cast from `Box<Fooer<{integer}>>` to `Box<(dyn Foo<A<'a> = &'a ()> + 'static)>`
5555

5656
error: aborting due to 3 previous errors
5757

tests/ui/generic-associated-types/issue-76535.base.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ LL | type SubType<'a>: SubTrait where Self: 'a;
4747
= help: consider moving `SubType` to another trait
4848
= help: only type `SuperStruct` is seen to implement the trait in this crate, consider using it directly instead
4949
= note: `SuperTrait` can be implemented in other crates; if you want to support your users passing their own types here, you can't refer to a specific type
50-
= note: required for the cast from `Box<SuperStruct>` to `Box<dyn SuperTrait<SubType = SubStruct<'_>>>`
50+
= note: required for the cast from `Box<SuperStruct>` to `Box<dyn SuperTrait<SubType<'_> = SubStruct<'_>>>`
5151

5252
error: aborting due to 3 previous errors
5353

tests/ui/generic-associated-types/issue-79422.base.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ LL | type VRefCont<'a>: RefCont<'a, V> where Self: 'a;
4949
= help: the following types implement the trait, consider defining an enum where each variant holds one of these types, implementing `MapLike` for this new enum and using it instead:
5050
std::collections::BTreeMap<K, V>
5151
Source
52-
= note: required for the cast from `Box<BTreeMap<u8, u8>>` to `Box<dyn MapLike<u8, u8, VRefCont = (dyn RefCont<'_, u8> + 'static)>>`
52+
= note: required for the cast from `Box<BTreeMap<u8, u8>>` to `Box<dyn MapLike<u8, u8, VRefCont<'_> = (dyn RefCont<'_, u8> + 'static)>>`
5353

5454
error: aborting due to 3 previous errors
5555

tests/ui/generic-associated-types/issue-79422.extended.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ LL | type VRefCont<'a> = &'a V where Self: 'a;
2828
= note: expected trait object `(dyn RefCont<'_, u8> + 'static)`
2929
found reference `&u8`
3030
= help: `&u8` implements `RefCont` so you could box the found value and coerce it to the trait object `Box<dyn RefCont>`, you will have to change the expected type as well
31-
= note: required for the cast from `Box<BTreeMap<u8, u8>>` to `Box<dyn MapLike<u8, u8, VRefCont = (dyn RefCont<'_, u8> + 'static)>>`
31+
= note: required for the cast from `Box<BTreeMap<u8, u8>>` to `Box<dyn MapLike<u8, u8, VRefCont<'_> = (dyn RefCont<'_, u8> + 'static)>>`
3232

3333
error: aborting due to 2 previous errors
3434

tests/ui/traits/object/pretty.rs

+7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ trait FixedHrtb: for<'a> SuperGeneric<'a, Assoc2 = &'a u8> {}
1818
trait AnyDifferentBinders: for<'a> SuperGeneric<'a, Assoc2 = &'a u8> + Super {}
1919
trait FixedDifferentBinders: for<'a> SuperGeneric<'a, Assoc2 = &'a u8> + Super<Assoc = u8> {}
2020

21+
trait HasGat<Outer> {
22+
type Assoc<Inner> where Self: Sized;
23+
}
24+
2125
fn dyn_super(x: &dyn Super<Assoc = u8>) { x } //~ERROR mismatched types
2226
fn dyn_any(x: &dyn Any<Assoc = u8>) { x } //~ERROR mismatched types
2327
fn dyn_fixed(x: &dyn Fixed) { x } //~ERROR mismatched types
@@ -34,4 +38,7 @@ fn dyn_fixed_hrtb(x: &dyn FixedHrtb) { x } //~ERROR mismatched types
3438
fn dyn_any_different_binders(x: &dyn AnyDifferentBinders<Assoc = u8>) { x } //~ERROR mismatched types
3539
fn dyn_fixed_different_binders(x: &dyn FixedDifferentBinders) { x } //~ERROR mismatched types
3640

41+
fn dyn_has_gat(x: &dyn HasGat<u8, Assoc<bool> = ()>) { x } //~ERROR mismatched types
42+
//~^ WARN unnecessary associated type bound
43+
3744
fn main() {}

tests/ui/traits/object/pretty.stderr

+35-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
warning: unnecessary associated type bound for not object safe associated type
2+
--> $DIR/pretty.rs:41:35
3+
|
4+
LL | fn dyn_has_gat(x: &dyn HasGat<u8, Assoc<bool> = ()>) { x }
5+
| ^^^^^^^^^^^^^^^^ help: remove this bound
6+
|
7+
= note: this associated type has a `where Self: Sized` bound, and while the associated type can be specified, it cannot be used because trait objects are never `Sized`
8+
= note: `#[warn(unused_associated_type_bounds)]` on by default
9+
110
error[E0308]: mismatched types
2-
--> $DIR/pretty.rs:21:43
11+
--> $DIR/pretty.rs:25:43
312
|
413
LL | fn dyn_super(x: &dyn Super<Assoc = u8>) { x }
514
| - ^ expected `()`, found `&dyn Super<Assoc = u8>`
@@ -10,7 +19,7 @@ LL | fn dyn_super(x: &dyn Super<Assoc = u8>) { x }
1019
found reference `&dyn Super<Assoc = u8>`
1120

1221
error[E0308]: mismatched types
13-
--> $DIR/pretty.rs:22:39
22+
--> $DIR/pretty.rs:26:39
1423
|
1524
LL | fn dyn_any(x: &dyn Any<Assoc = u8>) { x }
1625
| - ^ expected `()`, found `&dyn Any<Assoc = u8>`
@@ -21,7 +30,7 @@ LL | fn dyn_any(x: &dyn Any<Assoc = u8>) { x }
2130
found reference `&dyn Any<Assoc = u8>`
2231

2332
error[E0308]: mismatched types
24-
--> $DIR/pretty.rs:23:31
33+
--> $DIR/pretty.rs:27:31
2534
|
2635
LL | fn dyn_fixed(x: &dyn Fixed) { x }
2736
| - ^ expected `()`, found `&dyn Fixed`
@@ -32,7 +41,7 @@ LL | fn dyn_fixed(x: &dyn Fixed) { x }
3241
found reference `&dyn Fixed`
3342

3443
error[E0308]: mismatched types
35-
--> $DIR/pretty.rs:24:50
44+
--> $DIR/pretty.rs:28:50
3645
|
3746
LL | fn dyn_fixed_multi(x: &dyn Fixed<Assoc = u16>) { x }
3847
| - ^ expected `()`, found `&dyn Fixed<Assoc = u16>`
@@ -43,7 +52,7 @@ LL | fn dyn_fixed_multi(x: &dyn Fixed<Assoc = u16>) { x }
4352
found reference `&dyn Fixed<Assoc = u16>`
4453

4554
error[E0308]: mismatched types
46-
--> $DIR/pretty.rs:25:38
55+
--> $DIR/pretty.rs:29:38
4756
|
4857
LL | fn dyn_fixed_sub(x: &dyn FixedSub) { x }
4958
| - ^ expected `()`, found `&dyn FixedSub`
@@ -54,7 +63,7 @@ LL | fn dyn_fixed_sub(x: &dyn FixedSub) { x }
5463
found reference `&dyn FixedSub`
5564

5665
error[E0308]: mismatched types
57-
--> $DIR/pretty.rs:26:44
66+
--> $DIR/pretty.rs:30:44
5867
|
5968
LL | fn dyn_fixed_static(x: &dyn FixedStatic) { x }
6069
| - ^ expected `()`, found `&dyn FixedStatic`
@@ -65,7 +74,7 @@ LL | fn dyn_fixed_static(x: &dyn FixedStatic) { x }
6574
found reference `&dyn FixedStatic`
6675

6776
error[E0308]: mismatched types
68-
--> $DIR/pretty.rs:28:75
77+
--> $DIR/pretty.rs:32:75
6978
|
7079
LL | fn dyn_super_generic(x: &dyn for<'a> SuperGeneric<'a, Assoc2 = &'a u8>) { x }
7180
| - ^ expected `()`, found `&dyn SuperGeneric<'a, Assoc2 = &u8>`
@@ -76,7 +85,7 @@ LL | fn dyn_super_generic(x: &dyn for<'a> SuperGeneric<'a, Assoc2 = &'a u8>) { x
7685
found reference `&dyn for<'a> SuperGeneric<'a, Assoc2 = &'a u8>`
7786

7887
error[E0308]: mismatched types
79-
--> $DIR/pretty.rs:29:71
88+
--> $DIR/pretty.rs:33:71
8089
|
8190
LL | fn dyn_any_generic(x: &dyn for<'a> AnyGeneric<'a, Assoc2 = &'a u8>) { x }
8291
| - ^ expected `()`, found `&dyn AnyGeneric<'a, Assoc2 = &u8>`
@@ -87,7 +96,7 @@ LL | fn dyn_any_generic(x: &dyn for<'a> AnyGeneric<'a, Assoc2 = &'a u8>) { x }
8796
found reference `&dyn for<'a> AnyGeneric<'a, Assoc2 = &'a u8>`
8897

8998
error[E0308]: mismatched types
90-
--> $DIR/pretty.rs:30:60
99+
--> $DIR/pretty.rs:34:60
91100
|
92101
LL | fn dyn_fixed_generic1(x: &dyn for<'a> FixedGeneric1<'a>) { x }
93102
| - ^ expected `()`, found `&dyn FixedGeneric1<'a>`
@@ -98,7 +107,7 @@ LL | fn dyn_fixed_generic1(x: &dyn for<'a> FixedGeneric1<'a>) { x }
98107
found reference `&dyn for<'a> FixedGeneric1<'a>`
99108

100109
error[E0308]: mismatched types
101-
--> $DIR/pretty.rs:31:60
110+
--> $DIR/pretty.rs:35:60
102111
|
103112
LL | fn dyn_fixed_generic2(x: &dyn for<'a> FixedGeneric2<'a>) { x }
104113
| - ^ expected `()`, found `&dyn FixedGeneric2<'a>`
@@ -109,7 +118,7 @@ LL | fn dyn_fixed_generic2(x: &dyn for<'a> FixedGeneric2<'a>) { x }
109118
found reference `&dyn for<'a> FixedGeneric2<'a>`
110119

111120
error[E0308]: mismatched types
112-
--> $DIR/pretty.rs:32:79
121+
--> $DIR/pretty.rs:36:79
113122
|
114123
LL | fn dyn_fixed_generic_multi(x: &dyn for<'a> FixedGeneric1<'a, Assoc2 = &u8>) { x }
115124
| - ^ expected `()`, found `&dyn FixedGeneric1<'a, Assoc2 = ...>`
@@ -120,7 +129,7 @@ LL | fn dyn_fixed_generic_multi(x: &dyn for<'a> FixedGeneric1<'a, Assoc2 = &u8>)
120129
found reference `&dyn for<'a> FixedGeneric1<'a, Assoc2 = &u8>`
121130

122131
error[E0308]: mismatched types
123-
--> $DIR/pretty.rs:33:40
132+
--> $DIR/pretty.rs:37:40
124133
|
125134
LL | fn dyn_fixed_hrtb(x: &dyn FixedHrtb) { x }
126135
| - ^ expected `()`, found `&dyn FixedHrtb`
@@ -131,7 +140,7 @@ LL | fn dyn_fixed_hrtb(x: &dyn FixedHrtb) { x }
131140
found reference `&dyn FixedHrtb`
132141

133142
error[E0308]: mismatched types
134-
--> $DIR/pretty.rs:34:73
143+
--> $DIR/pretty.rs:38:73
135144
|
136145
LL | fn dyn_any_different_binders(x: &dyn AnyDifferentBinders<Assoc = u8>) { x }
137146
| - ^ expected `()`, found `&dyn AnyDifferentBinders<Assoc = ...>`
@@ -142,7 +151,7 @@ LL | fn dyn_any_different_binders(x: &dyn AnyDifferentBinders<Assoc = u8>) { x }
142151
found reference `&dyn AnyDifferentBinders<Assoc = u8>`
143152

144153
error[E0308]: mismatched types
145-
--> $DIR/pretty.rs:35:65
154+
--> $DIR/pretty.rs:39:65
146155
|
147156
LL | fn dyn_fixed_different_binders(x: &dyn FixedDifferentBinders) { x }
148157
| - ^ expected `()`, found `&dyn FixedDifferentBinders`
@@ -152,6 +161,17 @@ LL | fn dyn_fixed_different_binders(x: &dyn FixedDifferentBinders) { x }
152161
= note: expected unit type `()`
153162
found reference `&dyn FixedDifferentBinders`
154163

155-
error: aborting due to 14 previous errors
164+
error[E0308]: mismatched types
165+
--> $DIR/pretty.rs:41:56
166+
|
167+
LL | fn dyn_has_gat(x: &dyn HasGat<u8, Assoc<bool> = ()>) { x }
168+
| - ^ expected `()`, found `&dyn HasGat<u8, Assoc<bool> = ()>`
169+
| |
170+
| help: try adding a return type: `-> &dyn HasGat<u8, Assoc<bool> = ()>`
171+
|
172+
= note: expected unit type `()`
173+
found reference `&dyn HasGat<u8, Assoc<bool> = ()>`
174+
175+
error: aborting due to 15 previous errors; 1 warning emitted
156176

157177
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)