Skip to content

Commit

Permalink
address review comments and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Markeffsky committed Feb 7, 2024
1 parent 18e5bbf commit c636c7a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 43 deletions.
25 changes: 15 additions & 10 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1299,25 +1299,30 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {

let mut projections: Vec<_> = predicates
.projection_bounds()
.filter_map(|proj| {
.filter(|&proj| {
// Filter out projections that are implied by the super predicates.
let proj_is_implied = super_projections.iter().any(|&super_proj| {
let super_proj = super_proj.map_bound(|super_proj| {
ty::ExistentialProjection::erase_self_ty(cx.tcx(), super_proj)
});

// This function is sometimes called on types with erased and
// anonymized regions, but the super projections can still
// contain named regions. So we erase and anonymize everything
// here to compare the types modulo regions below.
let proj = cx.tcx().erase_regions(proj);
let proj = cx.tcx().anonymize_bound_vars(proj);
let super_proj = cx.tcx().erase_regions(super_proj);
let super_proj = cx.tcx().anonymize_bound_vars(super_proj);
assert_eq!(proj.bound_vars(), super_proj.bound_vars());

let proj = proj.skip_binder();
let super_proj = ty::ExistentialProjection::erase_self_ty(
cx.tcx(),
super_proj.skip_binder(),
);

proj == super_proj
});

!proj_is_implied
})
.map(|proj| {
// Skip the binder, because we don't want to print the binder in
// front of the associated item.
(!proj_is_implied).then_some(proj.skip_binder())
proj.skip_binder()
})
.collect();

Expand Down
18 changes: 12 additions & 6 deletions tests/ui/traits/object/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,32 @@ trait Super {
trait Any: Super {}
trait Fixed: Super<Assoc = u8> {}
trait FixedSub: Fixed {}
trait FixedStatic: Super<Assoc = &'static u8> {}

trait SuperGeneric<'a> {
type Assoc;
type Assoc2;
}
trait AnyGeneric<'a>: SuperGeneric<'a> {}
trait FixedGeneric1<'a>: SuperGeneric<'a, Assoc = &'a u8> {}
trait FixedGeneric1<'a>: SuperGeneric<'a, Assoc2 = &'a u8> {}
trait FixedGeneric2<'a>: Super<Assoc = &'a u8> {}
trait FixedHrtb: for<'a> SuperGeneric<'a, Assoc = &'a u8> {}
trait FixedHrtb: for<'a> SuperGeneric<'a, Assoc2 = &'a u8> {}
trait AnyDifferentBinders: for<'a> SuperGeneric<'a, Assoc2 = &'a u8> + Super {}
trait FixedDifferentBinders: for<'a> SuperGeneric<'a, Assoc2 = &'a u8> + Super<Assoc = u8> {}

fn dyn_super(x: &dyn Super<Assoc = u8>) { x } //~ERROR mismatched types
fn dyn_any(x: &dyn Any<Assoc = u8>) { x } //~ERROR mismatched types
fn dyn_fixed(x: &dyn Fixed) { x } //~ERROR mismatched types
fn dyn_fixed_multi(x: &dyn Fixed<Assoc = u16>) { x } //~ERROR mismatched types
fn dyn_fixed_sub(x: &dyn FixedSub) { x } //~ERROR mismatched types
fn dyn_fixed_static(x: &dyn FixedStatic) { x } //~ERROR mismatched types

fn dyn_super_generic(x: &dyn for<'a> SuperGeneric<'a, Assoc = &'a u8>) { x } //~ERROR mismatched types
fn dyn_any_generic(x: &dyn for<'a> AnyGeneric<'a, Assoc = &'a u8>) { x } //~ERROR mismatched types
fn dyn_super_generic(x: &dyn for<'a> SuperGeneric<'a, Assoc2 = &'a u8>) { x } //~ERROR mismatched types
fn dyn_any_generic(x: &dyn for<'a> AnyGeneric<'a, Assoc2 = &'a u8>) { x } //~ERROR mismatched types
fn dyn_fixed_generic1(x: &dyn for<'a> FixedGeneric1<'a>) { x } //~ERROR mismatched types
fn dyn_fixed_generic2(x: &dyn for<'a> FixedGeneric2<'a>) { x } //~ERROR mismatched types
fn dyn_fixed_generic_multi(x: &dyn for<'a> FixedGeneric1<'a, Assoc = &u8>) { x } //~ERROR mismatched types
fn dyn_fixed_generic_multi(x: &dyn for<'a> FixedGeneric1<'a, Assoc2 = &u8>) { x } //~ERROR mismatched types
fn dyn_fixed_hrtb(x: &dyn FixedHrtb) { x } //~ERROR mismatched types
fn dyn_any_different_binders(x: &dyn AnyDifferentBinders<Assoc = u8>) { x } //~ERROR mismatched types
fn dyn_fixed_different_binders(x: &dyn FixedDifferentBinders) { x } //~ERROR mismatched types

fn main() {}
87 changes: 60 additions & 27 deletions tests/ui/traits/object/pretty.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0308]: mismatched types
--> $DIR/pretty.rs:18:43
--> $DIR/pretty.rs:21:43
|
LL | fn dyn_super(x: &dyn Super<Assoc = u8>) { x }
| - ^ expected `()`, found `&dyn Super<Assoc = u8>`
Expand All @@ -10,7 +10,7 @@ LL | fn dyn_super(x: &dyn Super<Assoc = u8>) { x }
found reference `&dyn Super<Assoc = u8>`

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

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

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

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

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

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

error[E0308]: mismatched types
--> $DIR/pretty.rs:26:60
--> $DIR/pretty.rs:29:71
|
LL | fn dyn_any_generic(x: &dyn for<'a> AnyGeneric<'a, Assoc2 = &'a u8>) { x }
| - ^ expected `()`, found `&dyn AnyGeneric<'a, Assoc2 = &u8>`
| |
| help: try adding a return type: `-> &dyn for<'a> AnyGeneric<'a, Assoc2 = &'a u8>`
|
= note: expected unit type `()`
found reference `&dyn for<'a> AnyGeneric<'a, Assoc2 = &'a u8>`

error[E0308]: mismatched types
--> $DIR/pretty.rs:30:60
|
LL | fn dyn_fixed_generic1(x: &dyn for<'a> FixedGeneric1<'a>) { x }
| - ^ expected `()`, found `&dyn FixedGeneric1<'a>`
Expand All @@ -87,7 +98,7 @@ LL | fn dyn_fixed_generic1(x: &dyn for<'a> FixedGeneric1<'a>) { x }
found reference `&dyn for<'a> FixedGeneric1<'a>`

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

error[E0308]: mismatched types
--> $DIR/pretty.rs:28:78
--> $DIR/pretty.rs:32:79
|
LL | fn dyn_fixed_generic_multi(x: &dyn for<'a> FixedGeneric1<'a, Assoc = &u8>) { x }
| - ^ expected `()`, found `&dyn FixedGeneric1<'a, Assoc = &u8>`
| |
| help: try adding a return type: `-> &dyn for<'a> FixedGeneric1<'a, Assoc = &u8>`
LL | fn dyn_fixed_generic_multi(x: &dyn for<'a> FixedGeneric1<'a, Assoc2 = &u8>) { x }
| - ^ expected `()`, found `&dyn FixedGeneric1<'a, Assoc2 = ...>`
| |
| help: try adding a return type: `-> &dyn for<'a> FixedGeneric1<'a, Assoc2 = &u8>`
|
= note: expected unit type `()`
found reference `&dyn for<'a> FixedGeneric1<'a, Assoc = &u8>`
found reference `&dyn for<'a> FixedGeneric1<'a, Assoc2 = &u8>`

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

error: aborting due to 11 previous errors
error[E0308]: mismatched types
--> $DIR/pretty.rs:34:73
|
LL | fn dyn_any_different_binders(x: &dyn AnyDifferentBinders<Assoc = u8>) { x }
| - ^ expected `()`, found `&dyn AnyDifferentBinders<Assoc = ...>`
| |
| help: try adding a return type: `-> &dyn AnyDifferentBinders<Assoc = u8>`
|
= note: expected unit type `()`
found reference `&dyn AnyDifferentBinders<Assoc = u8>`

error[E0308]: mismatched types
--> $DIR/pretty.rs:35:65
|
LL | fn dyn_fixed_different_binders(x: &dyn FixedDifferentBinders) { x }
| - ^ expected `()`, found `&dyn FixedDifferentBinders`
| |
| help: try adding a return type: `-> &dyn FixedDifferentBinders`
|
= note: expected unit type `()`
found reference `&dyn FixedDifferentBinders`

error: aborting due to 14 previous errors

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

0 comments on commit c636c7a

Please sign in to comment.