@@ -59,11 +59,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
59
59
/// root, unless with_forced_absolute_paths was used.
60
60
pub fn item_path_str ( self , def_id : DefId ) -> String {
61
61
debug ! ( "item_path_str: def_id={:?}" , def_id) ;
62
- let mut cx = PrintCx :: new ( self ) ;
63
62
if FORCE_ABSOLUTE . with ( |force| force. get ( ) ) {
64
- AbsolutePathPrinter :: print_item_path ( & mut cx , def_id)
63
+ PrintCx :: new ( self , AbsolutePathPrinter ) . print_item_path ( def_id)
65
64
} else {
66
- LocalPathPrinter :: print_item_path ( & mut cx , def_id)
65
+ PrintCx :: new ( self , LocalPathPrinter ) . print_item_path ( def_id)
67
66
}
68
67
}
69
68
@@ -76,26 +75,23 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
76
75
/// suitable for user output. It always begins with a crate identifier.
77
76
pub fn absolute_item_path_str ( self , def_id : DefId ) -> String {
78
77
debug ! ( "absolute_item_path_str: def_id={:?}" , def_id) ;
79
- let mut cx = PrintCx :: new ( self ) ;
80
- AbsolutePathPrinter :: print_item_path ( & mut cx, def_id)
78
+ PrintCx :: new ( self , AbsolutePathPrinter ) . print_item_path ( def_id)
81
79
}
82
80
}
83
81
84
- impl PrintCx < ' a , ' gcx , ' tcx > {
85
- pub fn default_print_item_path < P > ( & mut self , def_id : DefId ) -> P :: Path
86
- where P : ItemPathPrinter
87
- {
82
+ impl < P : ItemPathPrinter > PrintCx < ' a , ' gcx , ' tcx , P > {
83
+ pub fn default_print_item_path ( & mut self , def_id : DefId ) -> P :: Path {
88
84
debug ! ( "default_print_item_path: def_id={:?}" , def_id) ;
89
85
let key = self . tcx . def_key ( def_id) ;
90
86
debug ! ( "default_print_item_path: key={:?}" , key) ;
91
87
match key. disambiguated_data . data {
92
88
DefPathData :: CrateRoot => {
93
89
assert ! ( key. parent. is_none( ) ) ;
94
- P :: path_crate ( self , def_id. krate )
90
+ self . path_crate ( def_id. krate )
95
91
}
96
92
97
93
DefPathData :: Impl => {
98
- self . default_print_impl_path :: < P > ( def_id)
94
+ self . default_print_impl_path ( def_id)
99
95
}
100
96
101
97
// Unclear if there is any value in distinguishing these.
@@ -120,20 +116,18 @@ impl PrintCx<'a, 'gcx, 'tcx> {
120
116
data @ DefPathData :: ImplTrait |
121
117
data @ DefPathData :: GlobalMetaData ( ..) => {
122
118
let parent_did = self . tcx . parent_def_id ( def_id) . unwrap ( ) ;
123
- let path = P :: print_item_path ( self , parent_did) ;
124
- P :: path_append ( path, & data. as_interned_str ( ) . as_symbol ( ) . as_str ( ) )
119
+ let path = self . print_item_path ( parent_did) ;
120
+ self . path_append ( path, & data. as_interned_str ( ) . as_symbol ( ) . as_str ( ) )
125
121
} ,
126
122
127
123
DefPathData :: StructCtor => { // present `X` instead of `X::{{constructor}}`
128
124
let parent_def_id = self . tcx . parent_def_id ( def_id) . unwrap ( ) ;
129
- P :: print_item_path ( self , parent_def_id)
125
+ self . print_item_path ( parent_def_id)
130
126
}
131
127
}
132
128
}
133
129
134
- fn default_print_impl_path < P > ( & mut self , impl_def_id : DefId ) -> P :: Path
135
- where P : ItemPathPrinter
136
- {
130
+ fn default_print_impl_path ( & mut self , impl_def_id : DefId ) -> P :: Path {
137
131
debug ! ( "default_print_impl_path: impl_def_id={:?}" , impl_def_id) ;
138
132
let parent_def_id = self . tcx . parent_def_id ( impl_def_id) . unwrap ( ) ;
139
133
@@ -146,7 +140,7 @@ impl PrintCx<'a, 'gcx, 'tcx> {
146
140
} ;
147
141
148
142
if !use_types {
149
- return self . default_print_impl_path_fallback :: < P > ( impl_def_id) ;
143
+ return self . default_print_impl_path_fallback ( impl_def_id) ;
150
144
}
151
145
152
146
// Decide whether to print the parent path for the impl.
@@ -170,11 +164,11 @@ impl PrintCx<'a, 'gcx, 'tcx> {
170
164
// If the impl is not co-located with either self-type or
171
165
// trait-type, then fallback to a format that identifies
172
166
// the module more clearly.
173
- let path = P :: print_item_path ( self , parent_def_id) ;
167
+ let path = self . print_item_path ( parent_def_id) ;
174
168
if let Some ( trait_ref) = impl_trait_ref {
175
- return P :: path_append ( path, & format ! ( "<impl {} for {}>" , trait_ref, self_ty) ) ;
169
+ return self . path_append ( path, & format ! ( "<impl {} for {}>" , trait_ref, self_ty) ) ;
176
170
} else {
177
- return P :: path_append ( path, & format ! ( "<impl {}>" , self_ty) ) ;
171
+ return self . path_append ( path, & format ! ( "<impl {}>" , self_ty) ) ;
178
172
}
179
173
}
180
174
@@ -183,7 +177,7 @@ impl PrintCx<'a, 'gcx, 'tcx> {
183
177
184
178
if let Some ( trait_ref) = impl_trait_ref {
185
179
// Trait impls.
186
- return P :: path_impl ( & format ! ( "<{} as {}>" , self_ty, trait_ref) ) ;
180
+ return self . path_impl ( & format ! ( "<{} as {}>" , self_ty, trait_ref) ) ;
187
181
}
188
182
189
183
// Inherent impls. Try to print `Foo::bar` for an inherent
@@ -193,42 +187,40 @@ impl PrintCx<'a, 'gcx, 'tcx> {
193
187
ty:: Adt ( adt_def, substs) => {
194
188
// FIXME(eddyb) always print without <> here.
195
189
if substs. types ( ) . next ( ) . is_none ( ) { // ignore regions
196
- P :: print_item_path ( self , adt_def. did )
190
+ self . print_item_path ( adt_def. did )
197
191
} else {
198
- P :: path_impl ( & format ! ( "<{}>" , self_ty) )
192
+ self . path_impl ( & format ! ( "<{}>" , self_ty) )
199
193
}
200
194
}
201
195
202
- ty:: Foreign ( did) => P :: print_item_path ( self , did) ,
196
+ ty:: Foreign ( did) => self . print_item_path ( did) ,
203
197
204
198
ty:: Bool |
205
199
ty:: Char |
206
200
ty:: Int ( _) |
207
201
ty:: Uint ( _) |
208
202
ty:: Float ( _) |
209
203
ty:: Str => {
210
- P :: path_impl ( & self_ty. to_string ( ) )
204
+ self . path_impl ( & self_ty. to_string ( ) )
211
205
}
212
206
213
207
_ => {
214
- P :: path_impl ( & format ! ( "<{}>" , self_ty) )
208
+ self . path_impl ( & format ! ( "<{}>" , self_ty) )
215
209
}
216
210
}
217
211
}
218
212
219
- fn default_print_impl_path_fallback < P > ( & mut self , impl_def_id : DefId ) -> P :: Path
220
- where P : ItemPathPrinter
221
- {
213
+ fn default_print_impl_path_fallback ( & mut self , impl_def_id : DefId ) -> P :: Path {
222
214
// If no type info is available, fall back to
223
215
// pretty printing some span information. This should
224
216
// only occur very early in the compiler pipeline.
225
217
// FIXME(eddyb) this should just be using `tcx.def_span(impl_def_id)`
226
218
let parent_def_id = self . tcx . parent_def_id ( impl_def_id) . unwrap ( ) ;
227
- let path = P :: print_item_path ( self , parent_def_id) ;
219
+ let path = self . print_item_path ( parent_def_id) ;
228
220
let node_id = self . tcx . hir ( ) . as_local_node_id ( impl_def_id) . unwrap ( ) ;
229
221
let item = self . tcx . hir ( ) . expect_item ( node_id) ;
230
222
let span_str = self . tcx . sess . source_map ( ) . span_to_string ( item. span ) ;
231
- P :: path_append ( path, & format ! ( "<impl at {}>" , span_str) )
223
+ self . path_append ( path, & format ! ( "<impl at {}>" , span_str) )
232
224
}
233
225
}
234
226
@@ -295,27 +287,35 @@ pub fn characteristic_def_id_of_type(ty: Ty<'_>) -> Option<DefId> {
295
287
pub trait ItemPathPrinter : Sized {
296
288
type Path ;
297
289
298
- fn print_item_path ( cx : & mut PrintCx < ' _ , ' _ , ' _ > , def_id : DefId ) -> Self :: Path {
299
- cx . default_print_item_path :: < Self > ( def_id)
290
+ fn print_item_path ( self : & mut PrintCx < ' _ , ' _ , ' _ , Self > , def_id : DefId ) -> Self :: Path {
291
+ self . default_print_item_path ( def_id)
300
292
}
301
293
302
- fn path_crate ( cx : & mut PrintCx < ' _ , ' _ , ' _ > , cnum : CrateNum ) -> Self :: Path ;
303
- fn path_impl ( text : & str ) -> Self :: Path ;
304
- fn path_append ( path : Self :: Path , text : & str ) -> Self :: Path ;
294
+ fn path_crate ( self : & mut PrintCx < ' _ , ' _ , ' _ , Self > , cnum : CrateNum ) -> Self :: Path ;
295
+ fn path_impl ( self : & mut PrintCx < ' _ , ' _ , ' _ , Self > , text : & str ) -> Self :: Path ;
296
+ fn path_append (
297
+ self : & mut PrintCx < ' _ , ' _ , ' _ , Self > ,
298
+ path : Self :: Path ,
299
+ text : & str ,
300
+ ) -> Self :: Path ;
305
301
}
306
302
307
303
struct AbsolutePathPrinter ;
308
304
309
305
impl ItemPathPrinter for AbsolutePathPrinter {
310
306
type Path = String ;
311
307
312
- fn path_crate ( cx : & mut PrintCx < ' _ , ' _ , ' _ > , cnum : CrateNum ) -> Self :: Path {
313
- cx . tcx . original_crate_name ( cnum) . to_string ( )
308
+ fn path_crate ( self : & mut PrintCx < ' _ , ' _ , ' _ , Self > , cnum : CrateNum ) -> Self :: Path {
309
+ self . tcx . original_crate_name ( cnum) . to_string ( )
314
310
}
315
- fn path_impl ( text : & str ) -> Self :: Path {
311
+ fn path_impl ( self : & mut PrintCx < ' _ , ' _ , ' _ , Self > , text : & str ) -> Self :: Path {
316
312
text. to_string ( )
317
313
}
318
- fn path_append ( mut path : Self :: Path , text : & str ) -> Self :: Path {
314
+ fn path_append (
315
+ self : & mut PrintCx < ' _ , ' _ , ' _ , Self > ,
316
+ mut path : Self :: Path ,
317
+ text : & str ,
318
+ ) -> Self :: Path {
319
319
if !path. is_empty ( ) {
320
320
path. push_str ( "::" ) ;
321
321
}
@@ -331,7 +331,7 @@ impl LocalPathPrinter {
331
331
/// from at least one local module and returns true. If the crate defining `def_id` is
332
332
/// declared with an `extern crate`, the path is guaranteed to use the `extern crate`.
333
333
fn try_print_visible_item_path (
334
- cx : & mut PrintCx < ' _ , ' _ , ' _ > ,
334
+ self : & mut PrintCx < ' _ , ' _ , ' _ , Self > ,
335
335
def_id : DefId ,
336
336
) -> Option < <Self as ItemPathPrinter >:: Path > {
337
337
debug ! ( "try_print_visible_item_path: def_id={:?}" , def_id) ;
@@ -342,7 +342,7 @@ impl LocalPathPrinter {
342
342
let cnum = def_id. krate ;
343
343
344
344
if cnum == LOCAL_CRATE {
345
- return Some ( Self :: path_crate ( cx , cnum) ) ;
345
+ return Some ( self . path_crate ( cnum) ) ;
346
346
}
347
347
348
348
// In local mode, when we encounter a crate other than
@@ -355,7 +355,7 @@ impl LocalPathPrinter {
355
355
// 2. for an extern inferred from a path or an indirect crate,
356
356
// where there is no explicit `extern crate`, we just prepend
357
357
// the crate name.
358
- match * cx . tcx . extern_crate ( def_id) {
358
+ match * self . tcx . extern_crate ( def_id) {
359
359
Some ( ExternCrate {
360
360
src : ExternCrateSource :: Extern ( def_id) ,
361
361
direct : true ,
@@ -364,14 +364,14 @@ impl LocalPathPrinter {
364
364
} ) => {
365
365
debug ! ( "try_print_visible_item_path: def_id={:?}" , def_id) ;
366
366
let path = if !span. is_dummy ( ) {
367
- Self :: print_item_path ( cx , def_id)
367
+ self . print_item_path ( def_id)
368
368
} else {
369
- Self :: path_crate ( cx , cnum)
369
+ self . path_crate ( cnum)
370
370
} ;
371
371
return Some ( path) ;
372
372
}
373
373
None => {
374
- return Some ( Self :: path_crate ( cx , cnum) ) ;
374
+ return Some ( self . path_crate ( cnum) ) ;
375
375
}
376
376
_ => { } ,
377
377
}
@@ -381,9 +381,9 @@ impl LocalPathPrinter {
381
381
return None ;
382
382
}
383
383
384
- let visible_parent_map = cx . tcx . visible_parent_map ( LOCAL_CRATE ) ;
384
+ let visible_parent_map = self . tcx . visible_parent_map ( LOCAL_CRATE ) ;
385
385
386
- let mut cur_def_key = cx . tcx . def_key ( def_id) ;
386
+ let mut cur_def_key = self . tcx . def_key ( def_id) ;
387
387
debug ! ( "try_print_visible_item_path: cur_def_key={:?}" , cur_def_key) ;
388
388
389
389
// For a UnitStruct or TupleStruct we want the name of its parent rather than <unnamed>.
@@ -393,12 +393,12 @@ impl LocalPathPrinter {
393
393
index : cur_def_key. parent . expect ( "DefPathData::StructCtor missing a parent" ) ,
394
394
} ;
395
395
396
- cur_def_key = cx . tcx . def_key ( parent) ;
396
+ cur_def_key = self . tcx . def_key ( parent) ;
397
397
}
398
398
399
399
let visible_parent = visible_parent_map. get ( & def_id) . cloned ( ) ?;
400
- let path = Self :: try_print_visible_item_path ( cx , visible_parent) ?;
401
- let actual_parent = cx . tcx . parent ( def_id) ;
400
+ let path = self . try_print_visible_item_path ( visible_parent) ?;
401
+ let actual_parent = self . tcx . parent ( def_id) ;
402
402
403
403
let data = cur_def_key. disambiguated_data . data ;
404
404
debug ! (
@@ -440,7 +440,7 @@ impl LocalPathPrinter {
440
440
// have access to the re-exported name.
441
441
DefPathData :: Module ( actual_name) |
442
442
DefPathData :: TypeNs ( actual_name) if Some ( visible_parent) != actual_parent => {
443
- self . item_children ( visible_parent)
443
+ self . tcx . item_children ( visible_parent)
444
444
. iter ( )
445
445
. find ( |child| child. def . def_id ( ) == def_id)
446
446
. map ( |child| child. ident . as_str ( ) )
@@ -450,43 +450,47 @@ impl LocalPathPrinter {
450
450
data. get_opt_name ( ) . map ( |n| n. as_str ( ) ) . unwrap_or_else ( || {
451
451
// Re-exported `extern crate` (#43189).
452
452
if let DefPathData :: CrateRoot = data {
453
- cx . tcx . original_crate_name ( def_id. krate ) . as_str ( )
453
+ self . tcx . original_crate_name ( def_id. krate ) . as_str ( )
454
454
} else {
455
455
Symbol :: intern ( "<unnamed>" ) . as_str ( )
456
456
}
457
457
} )
458
458
} ,
459
459
} ;
460
460
debug ! ( "try_print_visible_item_path: symbol={:?}" , symbol) ;
461
- Some ( Self :: path_append ( path, & symbol) )
461
+ Some ( self . path_append ( path, & symbol) )
462
462
}
463
463
}
464
464
465
465
impl ItemPathPrinter for LocalPathPrinter {
466
466
type Path = String ;
467
467
468
- fn print_item_path ( cx : & mut PrintCx < ' _ , ' _ , ' _ > , def_id : DefId ) -> Self :: Path {
469
- Self :: try_print_visible_item_path ( cx , def_id)
470
- . unwrap_or_else ( || cx . default_print_item_path :: < Self > ( def_id) )
468
+ fn print_item_path ( self : & mut PrintCx < ' _ , ' _ , ' _ , Self > , def_id : DefId ) -> Self :: Path {
469
+ self . try_print_visible_item_path ( def_id)
470
+ . unwrap_or_else ( || self . default_print_item_path ( def_id) )
471
471
}
472
472
473
- fn path_crate ( cx : & mut PrintCx < ' _ , ' _ , ' _ > , cnum : CrateNum ) -> Self :: Path {
473
+ fn path_crate ( self : & mut PrintCx < ' _ , ' _ , ' _ , Self > , cnum : CrateNum ) -> Self :: Path {
474
474
if cnum == LOCAL_CRATE {
475
- if cx . tcx . sess . rust_2018 ( ) {
475
+ if self . tcx . sess . rust_2018 ( ) {
476
476
// We add the `crate::` keyword on Rust 2018, only when desired.
477
477
if SHOULD_PREFIX_WITH_CRATE . with ( |flag| flag. get ( ) ) {
478
478
return keywords:: Crate . name ( ) . to_string ( ) ;
479
479
}
480
480
}
481
481
String :: new ( )
482
482
} else {
483
- cx . tcx . crate_name ( cnum) . to_string ( )
483
+ self . tcx . crate_name ( cnum) . to_string ( )
484
484
}
485
485
}
486
- fn path_impl ( text : & str ) -> Self :: Path {
486
+ fn path_impl ( self : & mut PrintCx < ' _ , ' _ , ' _ , Self > , text : & str ) -> Self :: Path {
487
487
text. to_string ( )
488
488
}
489
- fn path_append ( mut path : Self :: Path , text : & str ) -> Self :: Path {
489
+ fn path_append (
490
+ self : & mut PrintCx < ' _ , ' _ , ' _ , Self > ,
491
+ mut path : Self :: Path ,
492
+ text : & str ,
493
+ ) -> Self :: Path {
490
494
if !path. is_empty ( ) {
491
495
path. push_str ( "::" ) ;
492
496
}
0 commit comments