@@ -19,18 +19,16 @@ pub trait Print<'tcx, P> {
19
19
fn print ( & self , p : & mut P ) -> Result < ( ) , PrintError > ;
20
20
}
21
21
22
- /// Interface for outputting user-facing "type-system entities"
23
- /// (paths, types, lifetimes, constants, etc.) as a side-effect
24
- /// (e.g. formatting, like `PrettyPrinter` implementors do) or by
25
- /// constructing some alternative representation (e.g. an AST),
26
- /// which the associated types allow passing through the methods.
27
- ///
28
- /// For pretty-printing/formatting in particular, see `PrettyPrinter`.
29
- //
30
- // FIXME(eddyb) find a better name; this is more general than "printing".
22
+ /// A trait that "prints" user-facing type system entities: paths, types, lifetimes, constants,
23
+ /// etc. "Printing" here means building up a representation of the entity's path, usually as a
24
+ /// `String` (e.g. "std::io::Read") or a `Vec<Symbol>` (e.g. `[sym::std, sym::io, sym::Read]`). The
25
+ /// representation is built up by appending one or more pieces. The specific details included in
26
+ /// the built-up representation depend on the purpose of the printer. The more advanced printers
27
+ /// also rely on the `PrettyPrinter` sub-trait.
31
28
pub trait Printer < ' tcx > : Sized {
32
29
fn tcx < ' a > ( & ' a self ) -> TyCtxt < ' tcx > ;
33
30
31
+ /// Appends a representation of an entity with a normal path, e.g. "std::io::Read".
34
32
fn print_def_path (
35
33
& mut self ,
36
34
def_id : DefId ,
@@ -39,6 +37,7 @@ pub trait Printer<'tcx>: Sized {
39
37
self . default_print_def_path ( def_id, args)
40
38
}
41
39
40
+ /// Like `print_def_path`, but for `DefPathData::Impl`.
42
41
fn print_impl_path (
43
42
& mut self ,
44
43
impl_def_id : DefId ,
@@ -64,48 +63,67 @@ pub trait Printer<'tcx>: Sized {
64
63
self . default_print_impl_path ( impl_def_id, self_ty, impl_trait_ref)
65
64
}
66
65
66
+ /// Appends a representation of a region.
67
67
fn print_region ( & mut self , region : ty:: Region < ' tcx > ) -> Result < ( ) , PrintError > ;
68
68
69
+ /// Appends a representation of a type.
69
70
fn print_type ( & mut self , ty : Ty < ' tcx > ) -> Result < ( ) , PrintError > ;
70
71
72
+ /// Appends a representation of a list of `PolyExistentialPredicate`s.
71
73
fn print_dyn_existential (
72
74
& mut self ,
73
75
predicates : & ' tcx ty:: List < ty:: PolyExistentialPredicate < ' tcx > > ,
74
76
) -> Result < ( ) , PrintError > ;
75
77
78
+ /// Appends a representation of a const.
76
79
fn print_const ( & mut self , ct : ty:: Const < ' tcx > ) -> Result < ( ) , PrintError > ;
77
80
78
- fn path_crate ( & mut self , cnum : CrateNum ) -> Result < ( ) , PrintError > ;
81
+ /// Appends a representation of a crate name, e.g. `std`, or even ``.
82
+ fn print_crate_name ( & mut self , cnum : CrateNum ) -> Result < ( ) , PrintError > ;
79
83
80
- fn path_qualified (
84
+ /// Appends a representation of a (full or partial) simple path, in two parts. `print_prefix`,
85
+ /// when called, appends the representation of the leading segments. The rest of the method
86
+ /// appends the representation of the final segment, the details of which are in
87
+ /// `disambiguated_data`.
88
+ ///
89
+ /// E.g. `std::io` + `Read` -> `std::io::Read`.
90
+ fn print_path_with_simple (
81
91
& mut self ,
82
- self_ty : Ty < ' tcx > ,
83
- trait_ref : Option < ty :: TraitRef < ' tcx > > ,
92
+ print_prefix : impl FnOnce ( & mut Self ) -> Result < ( ) , PrintError > ,
93
+ disambiguated_data : & DisambiguatedDefPathData ,
84
94
) -> Result < ( ) , PrintError > ;
85
95
86
- fn path_append_impl (
96
+ /// Similar to `print_path_with_simple`, but the final segment is an `impl` segment.
97
+ ///
98
+ /// E.g. `slice` + `<impl [T]>` -> `slice::<impl [T]>`, which may then be further appended to,
99
+ /// giving a longer path representation such as `slice::<impl [T]>::to_vec_in::ConvertVec`.
100
+ fn print_path_with_impl (
87
101
& mut self ,
88
102
print_prefix : impl FnOnce ( & mut Self ) -> Result < ( ) , PrintError > ,
89
103
self_ty : Ty < ' tcx > ,
90
104
trait_ref : Option < ty:: TraitRef < ' tcx > > ,
91
105
) -> Result < ( ) , PrintError > ;
92
106
93
- fn path_append (
107
+ /// Appends a representation of a path ending in generic args, in two parts. `print_prefix`,
108
+ /// when called, appends the leading segments. The rest of the method appends the
109
+ /// representation of the generic args. (Some printers choose to skip appending the generic
110
+ /// args.)
111
+ ///
112
+ /// E.g. `ImplementsTraitForUsize` + `<usize>` -> `ImplementsTraitForUsize<usize>`.
113
+ fn print_path_with_generic_args (
94
114
& mut self ,
95
115
print_prefix : impl FnOnce ( & mut Self ) -> Result < ( ) , PrintError > ,
96
- disambiguated_data : & DisambiguatedDefPathData ,
116
+ args : & [ GenericArg < ' tcx > ] ,
97
117
) -> Result < ( ) , PrintError > ;
98
118
99
- fn path_generic_args (
119
+ /// Appends a representation of a qualified path segment, e.g. `<OsString as From<&T>>`.
120
+ /// If `trait_ref` is `None`, it may fall back to simpler forms, e.g. `<Vec<T>>` or just `Foo`.
121
+ fn print_path_with_qualified (
100
122
& mut self ,
101
- print_prefix : impl FnOnce ( & mut Self ) -> Result < ( ) , PrintError > ,
102
- args : & [ GenericArg < ' tcx > ] ,
123
+ self_ty : Ty < ' tcx > ,
124
+ trait_ref : Option < ty :: TraitRef < ' tcx > > ,
103
125
) -> Result < ( ) , PrintError > ;
104
126
105
- fn should_truncate ( & mut self ) -> bool {
106
- false
107
- }
108
-
109
127
// Defaults (should not be overridden):
110
128
111
129
#[ instrument( skip( self ) , level = "debug" ) ]
@@ -120,7 +138,7 @@ pub trait Printer<'tcx>: Sized {
120
138
match key. disambiguated_data . data {
121
139
DefPathData :: CrateRoot => {
122
140
assert ! ( key. parent. is_none( ) ) ;
123
- self . path_crate ( def_id. krate )
141
+ self . print_crate_name ( def_id. krate )
124
142
}
125
143
126
144
DefPathData :: Impl => self . print_impl_path ( def_id, args) ,
@@ -144,7 +162,7 @@ pub trait Printer<'tcx>: Sized {
144
162
) ) = self . tcx ( ) . coroutine_kind ( def_id)
145
163
&& args. len ( ) > parent_args. len ( )
146
164
{
147
- return self . path_generic_args (
165
+ return self . print_path_with_generic_args (
148
166
|p| p. print_def_path ( def_id, parent_args) ,
149
167
& args[ ..parent_args. len ( ) + 1 ] [ ..1 ] ,
150
168
) ;
@@ -166,7 +184,7 @@ pub trait Printer<'tcx>: Sized {
166
184
_ => {
167
185
if !generics. is_own_empty ( ) && args. len ( ) >= generics. count ( ) {
168
186
let args = generics. own_args_no_defaults ( self . tcx ( ) , args) ;
169
- return self . path_generic_args (
187
+ return self . print_path_with_generic_args (
170
188
|p| p. print_def_path ( def_id, parent_args) ,
171
189
args,
172
190
) ;
@@ -182,15 +200,15 @@ pub trait Printer<'tcx>: Sized {
182
200
&& self . tcx ( ) . generics_of ( parent_def_id) . parent_count == 0 ;
183
201
}
184
202
185
- self . path_append (
203
+ self . print_path_with_simple (
186
204
|p : & mut Self | {
187
205
if trait_qualify_parent {
188
206
let trait_ref = ty:: TraitRef :: new (
189
207
p. tcx ( ) ,
190
208
parent_def_id,
191
209
parent_args. iter ( ) . copied ( ) ,
192
210
) ;
193
- p. path_qualified ( trait_ref. self_ty ( ) , Some ( trait_ref) )
211
+ p. print_path_with_qualified ( trait_ref. self_ty ( ) , Some ( trait_ref) )
194
212
} else {
195
213
p. print_def_path ( parent_def_id, parent_args)
196
214
}
@@ -233,11 +251,15 @@ pub trait Printer<'tcx>: Sized {
233
251
// If the impl is not co-located with either self-type or
234
252
// trait-type, then fallback to a format that identifies
235
253
// the module more clearly.
236
- self . path_append_impl ( |p| p. print_def_path ( parent_def_id, & [ ] ) , self_ty, impl_trait_ref)
254
+ self . print_path_with_impl (
255
+ |p| p. print_def_path ( parent_def_id, & [ ] ) ,
256
+ self_ty,
257
+ impl_trait_ref,
258
+ )
237
259
} else {
238
260
// Otherwise, try to give a good form that would be valid language
239
261
// syntax. Preferably using associated item notation.
240
- self . path_qualified ( self_ty, impl_trait_ref)
262
+ self . print_path_with_qualified ( self_ty, impl_trait_ref)
241
263
}
242
264
}
243
265
}
0 commit comments