Skip to content

Commit c636c7a

Browse files
author
Lukas Markeffsky
committed
address review comments and add more tests
1 parent 18e5bbf commit c636c7a

File tree

3 files changed

+87
-43
lines changed

3 files changed

+87
-43
lines changed

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

+15-10
Original file line numberDiff line numberDiff line change
@@ -1299,25 +1299,30 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
12991299

13001300
let mut projections: Vec<_> = predicates
13011301
.projection_bounds()
1302-
.filter_map(|proj| {
1302+
.filter(|&proj| {
13031303
// Filter out projections that are implied by the super predicates.
13041304
let proj_is_implied = super_projections.iter().any(|&super_proj| {
1305+
let super_proj = super_proj.map_bound(|super_proj| {
1306+
ty::ExistentialProjection::erase_self_ty(cx.tcx(), super_proj)
1307+
});
1308+
1309+
// This function is sometimes called on types with erased and
1310+
// anonymized regions, but the super projections can still
1311+
// contain named regions. So we erase and anonymize everything
1312+
// here to compare the types modulo regions below.
1313+
let proj = cx.tcx().erase_regions(proj);
13051314
let proj = cx.tcx().anonymize_bound_vars(proj);
1315+
let super_proj = cx.tcx().erase_regions(super_proj);
13061316
let super_proj = cx.tcx().anonymize_bound_vars(super_proj);
1307-
assert_eq!(proj.bound_vars(), super_proj.bound_vars());
1308-
1309-
let proj = proj.skip_binder();
1310-
let super_proj = ty::ExistentialProjection::erase_self_ty(
1311-
cx.tcx(),
1312-
super_proj.skip_binder(),
1313-
);
13141317

13151318
proj == super_proj
13161319
});
1317-
1320+
!proj_is_implied
1321+
})
1322+
.map(|proj| {
13181323
// Skip the binder, because we don't want to print the binder in
13191324
// front of the associated item.
1320-
(!proj_is_implied).then_some(proj.skip_binder())
1325+
proj.skip_binder()
13211326
})
13221327
.collect();
13231328

tests/ui/traits/object/pretty.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,32 @@ trait Super {
66
trait Any: Super {}
77
trait Fixed: Super<Assoc = u8> {}
88
trait FixedSub: Fixed {}
9+
trait FixedStatic: Super<Assoc = &'static u8> {}
910

1011
trait SuperGeneric<'a> {
11-
type Assoc;
12+
type Assoc2;
1213
}
1314
trait AnyGeneric<'a>: SuperGeneric<'a> {}
14-
trait FixedGeneric1<'a>: SuperGeneric<'a, Assoc = &'a u8> {}
15+
trait FixedGeneric1<'a>: SuperGeneric<'a, Assoc2 = &'a u8> {}
1516
trait FixedGeneric2<'a>: Super<Assoc = &'a u8> {}
16-
trait FixedHrtb: for<'a> SuperGeneric<'a, Assoc = &'a u8> {}
17+
trait FixedHrtb: for<'a> SuperGeneric<'a, Assoc2 = &'a u8> {}
18+
trait AnyDifferentBinders: for<'a> SuperGeneric<'a, Assoc2 = &'a u8> + Super {}
19+
trait FixedDifferentBinders: for<'a> SuperGeneric<'a, Assoc2 = &'a u8> + Super<Assoc = u8> {}
1720

1821
fn dyn_super(x: &dyn Super<Assoc = u8>) { x } //~ERROR mismatched types
1922
fn dyn_any(x: &dyn Any<Assoc = u8>) { x } //~ERROR mismatched types
2023
fn dyn_fixed(x: &dyn Fixed) { x } //~ERROR mismatched types
2124
fn dyn_fixed_multi(x: &dyn Fixed<Assoc = u16>) { x } //~ERROR mismatched types
2225
fn dyn_fixed_sub(x: &dyn FixedSub) { x } //~ERROR mismatched types
26+
fn dyn_fixed_static(x: &dyn FixedStatic) { x } //~ERROR mismatched types
2327

24-
fn dyn_super_generic(x: &dyn for<'a> SuperGeneric<'a, Assoc = &'a u8>) { x } //~ERROR mismatched types
25-
fn dyn_any_generic(x: &dyn for<'a> AnyGeneric<'a, Assoc = &'a u8>) { x } //~ERROR mismatched types
28+
fn dyn_super_generic(x: &dyn for<'a> SuperGeneric<'a, Assoc2 = &'a u8>) { x } //~ERROR mismatched types
29+
fn dyn_any_generic(x: &dyn for<'a> AnyGeneric<'a, Assoc2 = &'a u8>) { x } //~ERROR mismatched types
2630
fn dyn_fixed_generic1(x: &dyn for<'a> FixedGeneric1<'a>) { x } //~ERROR mismatched types
2731
fn dyn_fixed_generic2(x: &dyn for<'a> FixedGeneric2<'a>) { x } //~ERROR mismatched types
28-
fn dyn_fixed_generic_multi(x: &dyn for<'a> FixedGeneric1<'a, Assoc = &u8>) { x } //~ERROR mismatched types
32+
fn dyn_fixed_generic_multi(x: &dyn for<'a> FixedGeneric1<'a, Assoc2 = &u8>) { x } //~ERROR mismatched types
2933
fn dyn_fixed_hrtb(x: &dyn FixedHrtb) { x } //~ERROR mismatched types
34+
fn dyn_any_different_binders(x: &dyn AnyDifferentBinders<Assoc = u8>) { x } //~ERROR mismatched types
35+
fn dyn_fixed_different_binders(x: &dyn FixedDifferentBinders) { x } //~ERROR mismatched types
3036

3137
fn main() {}

tests/ui/traits/object/pretty.stderr

+60-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0308]: mismatched types
2-
--> $DIR/pretty.rs:18:43
2+
--> $DIR/pretty.rs:21:43
33
|
44
LL | fn dyn_super(x: &dyn Super<Assoc = u8>) { x }
55
| - ^ expected `()`, found `&dyn Super<Assoc = u8>`
@@ -10,7 +10,7 @@ LL | fn dyn_super(x: &dyn Super<Assoc = u8>) { x }
1010
found reference `&dyn Super<Assoc = u8>`
1111

1212
error[E0308]: mismatched types
13-
--> $DIR/pretty.rs:19:39
13+
--> $DIR/pretty.rs:22:39
1414
|
1515
LL | fn dyn_any(x: &dyn Any<Assoc = u8>) { x }
1616
| - ^ expected `()`, found `&dyn Any<Assoc = u8>`
@@ -21,7 +21,7 @@ LL | fn dyn_any(x: &dyn Any<Assoc = u8>) { x }
2121
found reference `&dyn Any<Assoc = u8>`
2222

2323
error[E0308]: mismatched types
24-
--> $DIR/pretty.rs:20:31
24+
--> $DIR/pretty.rs:23:31
2525
|
2626
LL | fn dyn_fixed(x: &dyn Fixed) { x }
2727
| - ^ expected `()`, found `&dyn Fixed`
@@ -32,7 +32,7 @@ LL | fn dyn_fixed(x: &dyn Fixed) { x }
3232
found reference `&dyn Fixed`
3333

3434
error[E0308]: mismatched types
35-
--> $DIR/pretty.rs:21:50
35+
--> $DIR/pretty.rs:24:50
3636
|
3737
LL | fn dyn_fixed_multi(x: &dyn Fixed<Assoc = u16>) { x }
3838
| - ^ expected `()`, found `&dyn Fixed<Assoc = u16>`
@@ -43,7 +43,7 @@ LL | fn dyn_fixed_multi(x: &dyn Fixed<Assoc = u16>) { x }
4343
found reference `&dyn Fixed<Assoc = u16>`
4444

4545
error[E0308]: mismatched types
46-
--> $DIR/pretty.rs:22:38
46+
--> $DIR/pretty.rs:25:38
4747
|
4848
LL | fn dyn_fixed_sub(x: &dyn FixedSub) { x }
4949
| - ^ expected `()`, found `&dyn FixedSub`
@@ -54,29 +54,40 @@ LL | fn dyn_fixed_sub(x: &dyn FixedSub) { x }
5454
found reference `&dyn FixedSub`
5555

5656
error[E0308]: mismatched types
57-
--> $DIR/pretty.rs:24:74
57+
--> $DIR/pretty.rs:26:44
5858
|
59-
LL | fn dyn_super_generic(x: &dyn for<'a> SuperGeneric<'a, Assoc = &'a u8>) { x }
60-
| - ^ expected `()`, found `&dyn SuperGeneric<'a, Assoc = &u8>`
61-
| |
62-
| help: try adding a return type: `-> &dyn for<'a> SuperGeneric<'a, Assoc = &'a u8>`
59+
LL | fn dyn_fixed_static(x: &dyn FixedStatic) { x }
60+
| - ^ expected `()`, found `&dyn FixedStatic`
61+
| |
62+
| help: try adding a return type: `-> &dyn FixedStatic`
6363
|
6464
= note: expected unit type `()`
65-
found reference `&dyn for<'a> SuperGeneric<'a, Assoc = &'a u8>`
65+
found reference `&dyn FixedStatic`
6666

6767
error[E0308]: mismatched types
68-
--> $DIR/pretty.rs:25:70
68+
--> $DIR/pretty.rs:28:75
6969
|
70-
LL | fn dyn_any_generic(x: &dyn for<'a> AnyGeneric<'a, Assoc = &'a u8>) { x }
71-
| - ^ expected `()`, found `&dyn AnyGeneric<'a, Assoc = &u8>`
72-
| |
73-
| help: try adding a return type: `-> &dyn for<'a> AnyGeneric<'a, Assoc = &'a u8>`
70+
LL | fn dyn_super_generic(x: &dyn for<'a> SuperGeneric<'a, Assoc2 = &'a u8>) { x }
71+
| - ^ expected `()`, found `&dyn SuperGeneric<'a, Assoc2 = &u8>`
72+
| |
73+
| help: try adding a return type: `-> &dyn for<'a> SuperGeneric<'a, Assoc2 = &'a u8>`
7474
|
7575
= note: expected unit type `()`
76-
found reference `&dyn for<'a> AnyGeneric<'a, Assoc = &'a u8>`
76+
found reference `&dyn for<'a> SuperGeneric<'a, Assoc2 = &'a u8>`
7777

7878
error[E0308]: mismatched types
79-
--> $DIR/pretty.rs:26:60
79+
--> $DIR/pretty.rs:29:71
80+
|
81+
LL | fn dyn_any_generic(x: &dyn for<'a> AnyGeneric<'a, Assoc2 = &'a u8>) { x }
82+
| - ^ expected `()`, found `&dyn AnyGeneric<'a, Assoc2 = &u8>`
83+
| |
84+
| help: try adding a return type: `-> &dyn for<'a> AnyGeneric<'a, Assoc2 = &'a u8>`
85+
|
86+
= note: expected unit type `()`
87+
found reference `&dyn for<'a> AnyGeneric<'a, Assoc2 = &'a u8>`
88+
89+
error[E0308]: mismatched types
90+
--> $DIR/pretty.rs:30:60
8091
|
8192
LL | fn dyn_fixed_generic1(x: &dyn for<'a> FixedGeneric1<'a>) { x }
8293
| - ^ expected `()`, found `&dyn FixedGeneric1<'a>`
@@ -87,7 +98,7 @@ LL | fn dyn_fixed_generic1(x: &dyn for<'a> FixedGeneric1<'a>) { x }
8798
found reference `&dyn for<'a> FixedGeneric1<'a>`
8899

89100
error[E0308]: mismatched types
90-
--> $DIR/pretty.rs:27:60
101+
--> $DIR/pretty.rs:31:60
91102
|
92103
LL | fn dyn_fixed_generic2(x: &dyn for<'a> FixedGeneric2<'a>) { x }
93104
| - ^ expected `()`, found `&dyn FixedGeneric2<'a>`
@@ -98,18 +109,18 @@ LL | fn dyn_fixed_generic2(x: &dyn for<'a> FixedGeneric2<'a>) { x }
98109
found reference `&dyn for<'a> FixedGeneric2<'a>`
99110

100111
error[E0308]: mismatched types
101-
--> $DIR/pretty.rs:28:78
112+
--> $DIR/pretty.rs:32:79
102113
|
103-
LL | fn dyn_fixed_generic_multi(x: &dyn for<'a> FixedGeneric1<'a, Assoc = &u8>) { x }
104-
| - ^ expected `()`, found `&dyn FixedGeneric1<'a, Assoc = &u8>`
105-
| |
106-
| help: try adding a return type: `-> &dyn for<'a> FixedGeneric1<'a, Assoc = &u8>`
114+
LL | fn dyn_fixed_generic_multi(x: &dyn for<'a> FixedGeneric1<'a, Assoc2 = &u8>) { x }
115+
| - ^ expected `()`, found `&dyn FixedGeneric1<'a, Assoc2 = ...>`
116+
| |
117+
| help: try adding a return type: `-> &dyn for<'a> FixedGeneric1<'a, Assoc2 = &u8>`
107118
|
108119
= note: expected unit type `()`
109-
found reference `&dyn for<'a> FixedGeneric1<'a, Assoc = &u8>`
120+
found reference `&dyn for<'a> FixedGeneric1<'a, Assoc2 = &u8>`
110121

111122
error[E0308]: mismatched types
112-
--> $DIR/pretty.rs:29:40
123+
--> $DIR/pretty.rs:33:40
113124
|
114125
LL | fn dyn_fixed_hrtb(x: &dyn FixedHrtb) { x }
115126
| - ^ expected `()`, found `&dyn FixedHrtb`
@@ -119,6 +130,28 @@ LL | fn dyn_fixed_hrtb(x: &dyn FixedHrtb) { x }
119130
= note: expected unit type `()`
120131
found reference `&dyn FixedHrtb`
121132

122-
error: aborting due to 11 previous errors
133+
error[E0308]: mismatched types
134+
--> $DIR/pretty.rs:34:73
135+
|
136+
LL | fn dyn_any_different_binders(x: &dyn AnyDifferentBinders<Assoc = u8>) { x }
137+
| - ^ expected `()`, found `&dyn AnyDifferentBinders<Assoc = ...>`
138+
| |
139+
| help: try adding a return type: `-> &dyn AnyDifferentBinders<Assoc = u8>`
140+
|
141+
= note: expected unit type `()`
142+
found reference `&dyn AnyDifferentBinders<Assoc = u8>`
143+
144+
error[E0308]: mismatched types
145+
--> $DIR/pretty.rs:35:65
146+
|
147+
LL | fn dyn_fixed_different_binders(x: &dyn FixedDifferentBinders) { x }
148+
| - ^ expected `()`, found `&dyn FixedDifferentBinders`
149+
| |
150+
| help: try adding a return type: `-> &dyn FixedDifferentBinders`
151+
|
152+
= note: expected unit type `()`
153+
found reference `&dyn FixedDifferentBinders`
154+
155+
error: aborting due to 14 previous errors
123156

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

0 commit comments

Comments
 (0)