@@ -19,18 +19,16 @@ pub trait Print<'tcx, P> {
1919 fn print ( & self , p : & mut P ) -> Result < ( ) , PrintError > ;
2020}
2121
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.
3128pub trait Printer < ' tcx > : Sized {
3229 fn tcx < ' a > ( & ' a self ) -> TyCtxt < ' tcx > ;
3330
31+ /// Appends a representation of an entity with a normal path, e.g. "std::io::Read".
3432 fn print_def_path (
3533 & mut self ,
3634 def_id : DefId ,
@@ -39,6 +37,7 @@ pub trait Printer<'tcx>: Sized {
3937 self . default_print_def_path ( def_id, args)
4038 }
4139
40+ /// Like `print_def_path`, but for `DefPathData::Impl`.
4241 fn print_impl_path (
4342 & mut self ,
4443 impl_def_id : DefId ,
@@ -64,48 +63,67 @@ pub trait Printer<'tcx>: Sized {
6463 self . default_print_impl_path ( impl_def_id, self_ty, impl_trait_ref)
6564 }
6665
66+ /// Appends a representation of a region.
6767 fn print_region ( & mut self , region : ty:: Region < ' tcx > ) -> Result < ( ) , PrintError > ;
6868
69+ /// Appends a representation of a type.
6970 fn print_type ( & mut self , ty : Ty < ' tcx > ) -> Result < ( ) , PrintError > ;
7071
72+ /// Appends a representation of a list of `PolyExistentialPredicate`s.
7173 fn print_dyn_existential (
7274 & mut self ,
7375 predicates : & ' tcx ty:: List < ty:: PolyExistentialPredicate < ' tcx > > ,
7476 ) -> Result < ( ) , PrintError > ;
7577
78+ /// Appends a representation of a const.
7679 fn print_const ( & mut self , ct : ty:: Const < ' tcx > ) -> Result < ( ) , PrintError > ;
7780
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 > ;
7983
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 (
8191 & 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 ,
8494 ) -> Result < ( ) , PrintError > ;
8595
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 (
87101 & mut self ,
88102 print_prefix : impl FnOnce ( & mut Self ) -> Result < ( ) , PrintError > ,
89103 self_ty : Ty < ' tcx > ,
90104 trait_ref : Option < ty:: TraitRef < ' tcx > > ,
91105 ) -> Result < ( ) , PrintError > ;
92106
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 (
94114 & mut self ,
95115 print_prefix : impl FnOnce ( & mut Self ) -> Result < ( ) , PrintError > ,
96- disambiguated_data : & DisambiguatedDefPathData ,
116+ args : & [ GenericArg < ' tcx > ] ,
97117 ) -> Result < ( ) , PrintError > ;
98118
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 (
100122 & 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 > > ,
103125 ) -> Result < ( ) , PrintError > ;
104126
105- fn should_truncate ( & mut self ) -> bool {
106- false
107- }
108-
109127 // Defaults (should not be overridden):
110128
111129 #[ instrument( skip( self ) , level = "debug" ) ]
@@ -120,7 +138,7 @@ pub trait Printer<'tcx>: Sized {
120138 match key. disambiguated_data . data {
121139 DefPathData :: CrateRoot => {
122140 assert ! ( key. parent. is_none( ) ) ;
123- self . path_crate ( def_id. krate )
141+ self . print_crate_name ( def_id. krate )
124142 }
125143
126144 DefPathData :: Impl => self . print_impl_path ( def_id, args) ,
@@ -144,7 +162,7 @@ pub trait Printer<'tcx>: Sized {
144162 ) ) = self . tcx ( ) . coroutine_kind ( def_id)
145163 && args. len ( ) > parent_args. len ( )
146164 {
147- return self . path_generic_args (
165+ return self . print_path_with_generic_args (
148166 |p| p. print_def_path ( def_id, parent_args) ,
149167 & args[ ..parent_args. len ( ) + 1 ] [ ..1 ] ,
150168 ) ;
@@ -166,7 +184,7 @@ pub trait Printer<'tcx>: Sized {
166184 _ => {
167185 if !generics. is_own_empty ( ) && args. len ( ) >= generics. count ( ) {
168186 let args = generics. own_args_no_defaults ( self . tcx ( ) , args) ;
169- return self . path_generic_args (
187+ return self . print_path_with_generic_args (
170188 |p| p. print_def_path ( def_id, parent_args) ,
171189 args,
172190 ) ;
@@ -182,15 +200,15 @@ pub trait Printer<'tcx>: Sized {
182200 && self . tcx ( ) . generics_of ( parent_def_id) . parent_count == 0 ;
183201 }
184202
185- self . path_append (
203+ self . print_path_with_simple (
186204 |p : & mut Self | {
187205 if trait_qualify_parent {
188206 let trait_ref = ty:: TraitRef :: new (
189207 p. tcx ( ) ,
190208 parent_def_id,
191209 parent_args. iter ( ) . copied ( ) ,
192210 ) ;
193- p. path_qualified ( trait_ref. self_ty ( ) , Some ( trait_ref) )
211+ p. print_path_with_qualified ( trait_ref. self_ty ( ) , Some ( trait_ref) )
194212 } else {
195213 p. print_def_path ( parent_def_id, parent_args)
196214 }
@@ -233,11 +251,15 @@ pub trait Printer<'tcx>: Sized {
233251 // If the impl is not co-located with either self-type or
234252 // trait-type, then fallback to a format that identifies
235253 // 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+ )
237259 } else {
238260 // Otherwise, try to give a good form that would be valid language
239261 // 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)
241263 }
242264 }
243265}
0 commit comments