Skip to content

Commit 18e5bbf

Browse files
author
Lukas Markeffsky
committed
improve pretty printing for trait objects
1 parent fd92021 commit 18e5bbf

File tree

5 files changed

+106
-63
lines changed

5 files changed

+106
-63
lines changed

compiler/rustc_middle/src/traits/util.rs

+34-20
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,60 @@
11
use rustc_data_structures::fx::FxHashSet;
22

3-
use crate::ty::{PolyTraitRef, TyCtxt};
3+
use crate::ty::{Clause, PolyTraitRef, ToPolyTraitRef, ToPredicate, TyCtxt};
44

5-
/// Given a PolyTraitRef, get the PolyTraitRefs of the trait's (transitive) supertraits.
5+
/// Given a [`PolyTraitRef`], get the [`Clause`]s implied by the trait's definition.
6+
///
67
/// This only exists in `rustc_middle` because the more powerful elaborator depends on
78
/// `rustc_infer` for elaborating outlives bounds -- this should only be used for pretty
89
/// printing.
10+
pub fn super_predicates_for_pretty_printing<'tcx>(
11+
tcx: TyCtxt<'tcx>,
12+
trait_ref: PolyTraitRef<'tcx>,
13+
) -> impl Iterator<Item = Clause<'tcx>> {
14+
let clause = trait_ref.to_predicate(tcx);
15+
Elaborator { tcx, visited: FxHashSet::from_iter([clause]), stack: vec![clause] }
16+
}
17+
18+
/// Like [`super_predicates_for_pretty_printing`], except it only returns traits and filters out
19+
/// all other [`Clause`]s.
920
pub fn supertraits_for_pretty_printing<'tcx>(
1021
tcx: TyCtxt<'tcx>,
1122
trait_ref: PolyTraitRef<'tcx>,
1223
) -> impl Iterator<Item = PolyTraitRef<'tcx>> {
13-
Elaborator { tcx, visited: FxHashSet::from_iter([trait_ref]), stack: vec![trait_ref] }
24+
super_predicates_for_pretty_printing(tcx, trait_ref).filter_map(|clause| {
25+
clause.as_trait_clause().map(|trait_clause| trait_clause.to_poly_trait_ref())
26+
})
1427
}
1528

1629
struct Elaborator<'tcx> {
1730
tcx: TyCtxt<'tcx>,
18-
visited: FxHashSet<PolyTraitRef<'tcx>>,
19-
stack: Vec<PolyTraitRef<'tcx>>,
31+
visited: FxHashSet<Clause<'tcx>>,
32+
stack: Vec<Clause<'tcx>>,
2033
}
2134

2235
impl<'tcx> Elaborator<'tcx> {
2336
fn elaborate(&mut self, trait_ref: PolyTraitRef<'tcx>) {
24-
let supertrait_refs = self
25-
.tcx
26-
.super_predicates_of(trait_ref.def_id())
27-
.predicates
28-
.into_iter()
29-
.flat_map(|(pred, _)| pred.subst_supertrait(self.tcx, &trait_ref).as_trait_clause())
30-
.map(|t| t.map_bound(|pred| pred.trait_ref))
31-
.filter(|supertrait_ref| self.visited.insert(*supertrait_ref));
32-
33-
self.stack.extend(supertrait_refs);
37+
let super_predicates =
38+
self.tcx.super_predicates_of(trait_ref.def_id()).predicates.iter().filter_map(
39+
|&(pred, _)| {
40+
let clause = pred.subst_supertrait(self.tcx, &trait_ref);
41+
self.visited.insert(clause).then_some(clause)
42+
},
43+
);
44+
45+
self.stack.extend(super_predicates);
3446
}
3547
}
3648

3749
impl<'tcx> Iterator for Elaborator<'tcx> {
38-
type Item = PolyTraitRef<'tcx>;
50+
type Item = Clause<'tcx>;
3951

40-
fn next(&mut self) -> Option<PolyTraitRef<'tcx>> {
41-
if let Some(trait_ref) = self.stack.pop() {
42-
self.elaborate(trait_ref);
43-
Some(trait_ref)
52+
fn next(&mut self) -> Option<Clause<'tcx>> {
53+
if let Some(clause) = self.stack.pop() {
54+
if let Some(trait_clause) = clause.as_trait_clause() {
55+
self.elaborate(trait_clause.to_poly_trait_ref());
56+
}
57+
Some(clause)
4458
} else {
4559
None
4660
}

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

+41-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::mir::interpret::{AllocRange, GlobalAlloc, Pointer, Provenance, Scalar};
22
use crate::query::IntoQueryParam;
33
use crate::query::Providers;
4-
use crate::traits::util::supertraits_for_pretty_printing;
4+
use crate::traits::util::{super_predicates_for_pretty_printing, supertraits_for_pretty_printing};
55
use crate::ty::GenericArgKind;
66
use crate::ty::{
77
ConstInt, ParamConst, ScalarInt, Term, TermKind, TypeFoldable, TypeSuperFoldable,
@@ -1255,8 +1255,8 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
12551255
// Generate the main trait ref, including associated types.
12561256
let mut first = true;
12571257

1258-
if let Some(principal) = predicates.principal() {
1259-
self.wrap_binder(&principal, |principal, cx| {
1258+
if let Some(bound_principal) = predicates.principal() {
1259+
self.wrap_binder(&bound_principal, |principal, cx| {
12601260
define_scoped_cx!(cx);
12611261
p!(print_def_path(principal.def_id, &[]));
12621262

@@ -1281,19 +1281,48 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
12811281
// HACK(eddyb) this duplicates `FmtPrinter`'s `path_generic_args`,
12821282
// in order to place the projections inside the `<...>`.
12831283
if !resugared {
1284-
// Use a type that can't appear in defaults of type parameters.
1285-
let dummy_cx = Ty::new_fresh(cx.tcx(), 0);
1286-
let principal = principal.with_self_ty(cx.tcx(), dummy_cx);
1284+
let principal_with_self =
1285+
principal.with_self_ty(cx.tcx(), cx.tcx().types.trait_object_dummy_self);
12871286

12881287
let args = cx
12891288
.tcx()
1290-
.generics_of(principal.def_id)
1291-
.own_args_no_defaults(cx.tcx(), principal.args);
1289+
.generics_of(principal_with_self.def_id)
1290+
.own_args_no_defaults(cx.tcx(), principal_with_self.args);
1291+
1292+
let bound_principal_with_self = bound_principal
1293+
.with_self_ty(cx.tcx(), cx.tcx().types.trait_object_dummy_self);
1294+
1295+
let super_projections: Vec<_> =
1296+
super_predicates_for_pretty_printing(cx.tcx(), bound_principal_with_self)
1297+
.filter_map(|clause| clause.as_projection_clause())
1298+
.collect();
1299+
1300+
let mut projections: Vec<_> = predicates
1301+
.projection_bounds()
1302+
.filter_map(|proj| {
1303+
// Filter out projections that are implied by the super predicates.
1304+
let proj_is_implied = super_projections.iter().any(|&super_proj| {
1305+
let proj = cx.tcx().anonymize_bound_vars(proj);
1306+
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+
);
12921314

1293-
let mut projections: Vec<_> = predicates.projection_bounds().collect();
1294-
projections.sort_by_cached_key(|proj| {
1295-
cx.tcx().item_name(proj.item_def_id()).to_string()
1296-
});
1315+
proj == super_proj
1316+
});
1317+
1318+
// Skip the binder, because we don't want to print the binder in
1319+
// front of the associated item.
1320+
(!proj_is_implied).then_some(proj.skip_binder())
1321+
})
1322+
.collect();
1323+
1324+
projections
1325+
.sort_by_cached_key(|proj| cx.tcx().item_name(proj.def_id).to_string());
12971326

12981327
if !args.is_empty() || !projections.is_empty() {
12991328
p!(generic_delimiters(|cx| {

tests/ui/traits/object/pretty.stderr

+25-25
Original file line numberDiff line numberDiff line change
@@ -24,100 +24,100 @@ error[E0308]: mismatched types
2424
--> $DIR/pretty.rs:20:31
2525
|
2626
LL | fn dyn_fixed(x: &dyn Fixed) { x }
27-
| - ^ expected `()`, found `&dyn Fixed<Assoc = u8>`
27+
| - ^ expected `()`, found `&dyn Fixed`
2828
| |
29-
| help: try adding a return type: `-> &dyn Fixed<Assoc = u8>`
29+
| help: try adding a return type: `-> &dyn Fixed`
3030
|
3131
= note: expected unit type `()`
32-
found reference `&dyn Fixed<Assoc = u8>`
32+
found reference `&dyn Fixed`
3333

3434
error[E0308]: mismatched types
3535
--> $DIR/pretty.rs:21:50
3636
|
3737
LL | fn dyn_fixed_multi(x: &dyn Fixed<Assoc = u16>) { x }
38-
| - ^ expected `()`, found `&dyn Fixed<Assoc = u16, Assoc = u8>`
38+
| - ^ expected `()`, found `&dyn Fixed<Assoc = u16>`
3939
| |
40-
| help: try adding a return type: `-> &dyn Fixed<Assoc = u16, Assoc = u8>`
40+
| help: try adding a return type: `-> &dyn Fixed<Assoc = u16>`
4141
|
4242
= note: expected unit type `()`
43-
found reference `&dyn Fixed<Assoc = u16, Assoc = u8>`
43+
found reference `&dyn Fixed<Assoc = u16>`
4444

4545
error[E0308]: mismatched types
4646
--> $DIR/pretty.rs:22:38
4747
|
4848
LL | fn dyn_fixed_sub(x: &dyn FixedSub) { x }
49-
| - ^ expected `()`, found `&dyn FixedSub<Assoc = u8>`
49+
| - ^ expected `()`, found `&dyn FixedSub`
5050
| |
51-
| help: try adding a return type: `-> &dyn FixedSub<Assoc = u8>`
51+
| help: try adding a return type: `-> &dyn FixedSub`
5252
|
5353
= note: expected unit type `()`
54-
found reference `&dyn FixedSub<Assoc = u8>`
54+
found reference `&dyn FixedSub`
5555

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

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

7878
error[E0308]: mismatched types
7979
--> $DIR/pretty.rs:26:60
8080
|
8181
LL | fn dyn_fixed_generic1(x: &dyn for<'a> FixedGeneric1<'a>) { x }
82-
| - ^ expected `()`, found `&dyn FixedGeneric1<'a, Assoc = &u8>`
82+
| - ^ expected `()`, found `&dyn FixedGeneric1<'a>`
8383
| |
84-
| help: try adding a return type: `-> &dyn for<'a> FixedGeneric1<'a, for<'a> Assoc = &'a u8>`
84+
| help: try adding a return type: `-> &dyn for<'a> FixedGeneric1<'a>`
8585
|
8686
= note: expected unit type `()`
87-
found reference `&dyn for<'a> FixedGeneric1<'a, for<'a> Assoc = &'a u8>`
87+
found reference `&dyn for<'a> FixedGeneric1<'a>`
8888

8989
error[E0308]: mismatched types
9090
--> $DIR/pretty.rs:27:60
9191
|
9292
LL | fn dyn_fixed_generic2(x: &dyn for<'a> FixedGeneric2<'a>) { x }
93-
| - ^ expected `()`, found `&dyn FixedGeneric2<'a, Assoc = &u8>`
93+
| - ^ expected `()`, found `&dyn FixedGeneric2<'a>`
9494
| |
95-
| help: try adding a return type: `-> &dyn for<'a> FixedGeneric2<'a, for<'a> Assoc = &'a u8>`
95+
| help: try adding a return type: `-> &dyn for<'a> FixedGeneric2<'a>`
9696
|
9797
= note: expected unit type `()`
98-
found reference `&dyn for<'a> FixedGeneric2<'a, for<'a> Assoc = &'a u8>`
98+
found reference `&dyn for<'a> FixedGeneric2<'a>`
9999

100100
error[E0308]: mismatched types
101101
--> $DIR/pretty.rs:28:78
102102
|
103103
LL | fn dyn_fixed_generic_multi(x: &dyn for<'a> FixedGeneric1<'a, Assoc = &u8>) { x }
104-
| - ^ expected `()`, found `&dyn FixedGeneric1<'a, Assoc = ..., Assoc = ...>`
104+
| - ^ expected `()`, found `&dyn FixedGeneric1<'a, Assoc = &u8>`
105105
| |
106-
| help: try adding a return type: `-> &dyn for<'a> FixedGeneric1<'a, for<'a> Assoc = &u8, for<'a> Assoc = &'a u8>`
106+
| help: try adding a return type: `-> &dyn for<'a> FixedGeneric1<'a, Assoc = &u8>`
107107
|
108108
= note: expected unit type `()`
109-
found reference `&dyn for<'a> FixedGeneric1<'a, for<'a> Assoc = &u8, for<'a> Assoc = &'a u8>`
109+
found reference `&dyn for<'a> FixedGeneric1<'a, Assoc = &u8>`
110110

111111
error[E0308]: mismatched types
112112
--> $DIR/pretty.rs:29:40
113113
|
114114
LL | fn dyn_fixed_hrtb(x: &dyn FixedHrtb) { x }
115-
| - ^ expected `()`, found `&dyn FixedHrtb<Assoc = &u8>`
115+
| - ^ expected `()`, found `&dyn FixedHrtb`
116116
| |
117-
| help: try adding a return type: `-> &dyn FixedHrtb<for<'a> Assoc = &'a u8>`
117+
| help: try adding a return type: `-> &dyn FixedHrtb`
118118
|
119119
= note: expected unit type `()`
120-
found reference `&dyn FixedHrtb<for<'a> Assoc = &'a u8>`
120+
found reference `&dyn FixedHrtb`
121121

122122
error: aborting due to 11 previous errors
123123

tests/ui/wf/hir-wf-canonicalized.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ trait Callback<T: Foo>: Fn(&Bar<'_, T>, &T::V) {}
99
struct Bar<'a, T> {
1010
callback: Box<dyn Callback<dyn Callback<Bar<'a, T>>>>,
1111
//~^ ERROR the trait bound `Bar<'a, T>: Foo` is not satisfied
12-
//~| ERROR the trait bound `(dyn Callback<Bar<'a, T>, for<'b, 'c, 'd> Output = ()> + 'static): Foo` is not satisfied
13-
//~| ERROR the size for values of type `(dyn Callback<Bar<'a, T>, for<'b, 'c, 'd> Output = ()> + 'static)` cannot be known at compilation time
12+
//~| ERROR the trait bound `(dyn Callback<Bar<'a, T>, Output = ()> + 'static): Foo` is not satisfied
13+
//~| ERROR the size for values of type `(dyn Callback<Bar<'a, T>, Output = ()> + 'static)` cannot be known at compilation time
1414
}
1515

1616
impl<T: Foo> Bar<'_, Bar<'_, T>> {}

tests/ui/wf/hir-wf-canonicalized.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@ help: this trait has no implementations, consider adding one
1010
LL | trait Foo {
1111
| ^^^^^^^^^
1212

13-
error[E0277]: the trait bound `(dyn Callback<Bar<'a, T>, for<'b, 'c, 'd> Output = ()> + 'static): Foo` is not satisfied
13+
error[E0277]: the trait bound `(dyn Callback<Bar<'a, T>, Output = ()> + 'static): Foo` is not satisfied
1414
--> $DIR/hir-wf-canonicalized.rs:10:15
1515
|
1616
LL | callback: Box<dyn Callback<dyn Callback<Bar<'a, T>>>>,
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `(dyn Callback<Bar<'a, T>, for<'b, 'c, 'd> Output = ()> + 'static)`
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `(dyn Callback<Bar<'a, T>, Output = ()> + 'static)`
1818
|
1919
help: this trait has no implementations, consider adding one
2020
--> $DIR/hir-wf-canonicalized.rs:3:1
2121
|
2222
LL | trait Foo {
2323
| ^^^^^^^^^
2424

25-
error[E0277]: the size for values of type `(dyn Callback<Bar<'a, T>, for<'b, 'c, 'd> Output = ()> + 'static)` cannot be known at compilation time
25+
error[E0277]: the size for values of type `(dyn Callback<Bar<'a, T>, Output = ()> + 'static)` cannot be known at compilation time
2626
--> $DIR/hir-wf-canonicalized.rs:10:15
2727
|
2828
LL | callback: Box<dyn Callback<dyn Callback<Bar<'a, T>>>>,
2929
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
3030
|
31-
= help: the trait `Sized` is not implemented for `(dyn Callback<Bar<'a, T>, for<'b, 'c, 'd> Output = ()> + 'static)`
31+
= help: the trait `Sized` is not implemented for `(dyn Callback<Bar<'a, T>, Output = ()> + 'static)`
3232
note: required by an implicit `Sized` bound in `Bar`
3333
--> $DIR/hir-wf-canonicalized.rs:9:16
3434
|

0 commit comments

Comments
 (0)