@@ -11,13 +11,15 @@ use crate::ty::{self, AliasTy, InferConst, Lift, Term, TermKind, Ty, TyCtxt};
11
11
use rustc_hir:: def:: Namespace ;
12
12
use rustc_index:: { Idx , IndexVec } ;
13
13
use rustc_target:: abi:: TyAndLayout ;
14
- use rustc_type_ir:: ConstKind ;
14
+ use rustc_type_ir:: { ConstKind , DebugWithInfcx , InferCtxtLike , OptWithInfcx } ;
15
15
16
- use std:: fmt;
16
+ use std:: fmt:: { self , Debug } ;
17
17
use std:: ops:: ControlFlow ;
18
18
use std:: rc:: Rc ;
19
19
use std:: sync:: Arc ;
20
20
21
+ use super :: { GenericArg , GenericArgKind , Region } ;
22
+
21
23
impl fmt:: Debug for ty:: TraitDef {
22
24
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
23
25
ty:: tls:: with ( |tcx| {
@@ -89,7 +91,16 @@ impl fmt::Debug for ty::FreeRegion {
89
91
90
92
impl < ' tcx > fmt:: Debug for ty:: FnSig < ' tcx > {
91
93
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
92
- let ty:: FnSig { inputs_and_output : _, c_variadic, unsafety, abi } = self ;
94
+ OptWithInfcx :: new_no_ctx ( self ) . fmt ( f)
95
+ }
96
+ }
97
+ impl < ' tcx > DebugWithInfcx < TyCtxt < ' tcx > > for ty:: FnSig < ' tcx > {
98
+ fn fmt < InfCtx : InferCtxtLike < TyCtxt < ' tcx > > > (
99
+ this : OptWithInfcx < ' _ , TyCtxt < ' tcx > , InfCtx , & Self > ,
100
+ f : & mut core:: fmt:: Formatter < ' _ > ,
101
+ ) -> core:: fmt:: Result {
102
+ let sig = this. data ;
103
+ let ty:: FnSig { inputs_and_output : _, c_variadic, unsafety, abi } = sig;
93
104
94
105
write ! ( f, "{}" , unsafety. prefix_str( ) ) ?;
95
106
match abi {
@@ -98,25 +109,25 @@ impl<'tcx> fmt::Debug for ty::FnSig<'tcx> {
98
109
} ;
99
110
100
111
write ! ( f, "fn(" ) ?;
101
- let inputs = self . inputs ( ) ;
112
+ let inputs = sig . inputs ( ) ;
102
113
match inputs. len ( ) {
103
114
0 if * c_variadic => write ! ( f, "...)" ) ?,
104
115
0 => write ! ( f, ")" ) ?,
105
116
_ => {
106
- for ty in & self . inputs ( ) [ 0 ..( self . inputs ( ) . len ( ) - 1 ) ] {
107
- write ! ( f, "{ty :?}, " ) ?;
117
+ for ty in & sig . inputs ( ) [ 0 ..( sig . inputs ( ) . len ( ) - 1 ) ] {
118
+ write ! ( f, "{:?}, " , & this . wrap ( ty ) ) ?;
108
119
}
109
- write ! ( f, "{:?}" , self . inputs( ) . last( ) . unwrap( ) ) ?;
120
+ write ! ( f, "{:?}" , & this . wrap ( sig . inputs( ) . last( ) . unwrap( ) ) ) ?;
110
121
if * c_variadic {
111
122
write ! ( f, "..." ) ?;
112
123
}
113
124
write ! ( f, ")" ) ?;
114
125
}
115
126
}
116
127
117
- match self . output ( ) . kind ( ) {
128
+ match sig . output ( ) . kind ( ) {
118
129
ty:: Tuple ( list) if list. is_empty ( ) => Ok ( ( ) ) ,
119
- _ => write ! ( f, " -> {:?}" , self . output( ) ) ,
130
+ _ => write ! ( f, " -> {:?}" , & this . wrap ( sig . output( ) ) ) ,
120
131
}
121
132
}
122
133
}
@@ -133,6 +144,14 @@ impl<'tcx> fmt::Debug for ty::TraitRef<'tcx> {
133
144
}
134
145
}
135
146
147
+ impl < ' tcx > ty:: DebugWithInfcx < TyCtxt < ' tcx > > for Ty < ' tcx > {
148
+ fn fmt < InfCtx : InferCtxtLike < TyCtxt < ' tcx > > > (
149
+ this : OptWithInfcx < ' _ , TyCtxt < ' tcx > , InfCtx , & Self > ,
150
+ f : & mut core:: fmt:: Formatter < ' _ > ,
151
+ ) -> core:: fmt:: Result {
152
+ this. data . fmt ( f)
153
+ }
154
+ }
136
155
impl < ' tcx > fmt:: Debug for Ty < ' tcx > {
137
156
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
138
157
with_no_trimmed_paths ! ( fmt:: Display :: fmt( self , f) )
@@ -217,9 +236,17 @@ impl<'tcx> fmt::Debug for ty::PredicateKind<'tcx> {
217
236
218
237
impl < ' tcx > fmt:: Debug for AliasTy < ' tcx > {
219
238
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
239
+ OptWithInfcx :: new_no_ctx ( self ) . fmt ( f)
240
+ }
241
+ }
242
+ impl < ' tcx > DebugWithInfcx < TyCtxt < ' tcx > > for AliasTy < ' tcx > {
243
+ fn fmt < InfCtx : InferCtxtLike < TyCtxt < ' tcx > > > (
244
+ this : OptWithInfcx < ' _ , TyCtxt < ' tcx > , InfCtx , & Self > ,
245
+ f : & mut core:: fmt:: Formatter < ' _ > ,
246
+ ) -> core:: fmt:: Result {
220
247
f. debug_struct ( "AliasTy" )
221
- . field ( "substs" , & self . substs )
222
- . field ( "def_id" , & self . def_id )
248
+ . field ( "substs" , & this . map ( |data| data . substs ) )
249
+ . field ( "def_id" , & this . data . def_id )
223
250
. finish ( )
224
251
}
225
252
}
@@ -232,13 +259,93 @@ impl<'tcx> fmt::Debug for ty::InferConst<'tcx> {
232
259
}
233
260
}
234
261
}
262
+ impl < ' tcx > DebugWithInfcx < TyCtxt < ' tcx > > for ty:: InferConst < ' tcx > {
263
+ fn fmt < InfCtx : InferCtxtLike < TyCtxt < ' tcx > > > (
264
+ this : OptWithInfcx < ' _ , TyCtxt < ' tcx > , InfCtx , & Self > ,
265
+ f : & mut core:: fmt:: Formatter < ' _ > ,
266
+ ) -> core:: fmt:: Result {
267
+ use ty:: InferConst :: * ;
268
+ match this. infcx . and_then ( |infcx| infcx. universe_of_ct ( * this. data ) ) {
269
+ None => write ! ( f, "{:?}" , this. data) ,
270
+ Some ( universe) => match * this. data {
271
+ Var ( vid) => write ! ( f, "?{}_{}c" , vid. index, universe. index( ) ) ,
272
+ Fresh ( _) => {
273
+ unreachable ! ( )
274
+ }
275
+ } ,
276
+ }
277
+ }
278
+ }
279
+
280
+ impl < ' tcx > fmt:: Debug for ty:: consts:: Expr < ' tcx > {
281
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
282
+ OptWithInfcx :: new_no_ctx ( self ) . fmt ( f)
283
+ }
284
+ }
285
+ impl < ' tcx > DebugWithInfcx < TyCtxt < ' tcx > > for ty:: consts:: Expr < ' tcx > {
286
+ fn fmt < InfCtx : InferCtxtLike < TyCtxt < ' tcx > > > (
287
+ this : OptWithInfcx < ' _ , TyCtxt < ' tcx > , InfCtx , & Self > ,
288
+ f : & mut core:: fmt:: Formatter < ' _ > ,
289
+ ) -> core:: fmt:: Result {
290
+ match this. data {
291
+ ty:: Expr :: Binop ( op, lhs, rhs) => {
292
+ write ! ( f, "({op:?}: {:?}, {:?})" , & this. wrap( lhs) , & this. wrap( rhs) )
293
+ }
294
+ ty:: Expr :: UnOp ( op, rhs) => write ! ( f, "({op:?}: {:?})" , & this. wrap( rhs) ) ,
295
+ ty:: Expr :: FunctionCall ( func, args) => {
296
+ write ! ( f, "{:?}(" , & this. wrap( func) ) ?;
297
+ for arg in args. as_slice ( ) . iter ( ) . rev ( ) . skip ( 1 ) . rev ( ) {
298
+ write ! ( f, "{:?}, " , & this. wrap( arg) ) ?;
299
+ }
300
+ if let Some ( arg) = args. last ( ) {
301
+ write ! ( f, "{:?}" , & this. wrap( arg) ) ?;
302
+ }
303
+
304
+ write ! ( f, ")" )
305
+ }
306
+ ty:: Expr :: Cast ( cast_kind, lhs, rhs) => {
307
+ write ! ( f, "({cast_kind:?}: {:?}, {:?})" , & this. wrap( lhs) , & this. wrap( rhs) )
308
+ }
309
+ }
310
+ }
311
+ }
312
+
313
+ impl < ' tcx > fmt:: Debug for ty:: UnevaluatedConst < ' tcx > {
314
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
315
+ OptWithInfcx :: new_no_ctx ( self ) . fmt ( f)
316
+ }
317
+ }
318
+ impl < ' tcx > DebugWithInfcx < TyCtxt < ' tcx > > for ty:: UnevaluatedConst < ' tcx > {
319
+ fn fmt < InfCtx : InferCtxtLike < TyCtxt < ' tcx > > > (
320
+ this : OptWithInfcx < ' _ , TyCtxt < ' tcx > , InfCtx , & Self > ,
321
+ f : & mut core:: fmt:: Formatter < ' _ > ,
322
+ ) -> core:: fmt:: Result {
323
+ f. debug_struct ( "UnevaluatedConst" )
324
+ . field ( "def" , & this. data . def )
325
+ . field ( "substs" , & this. wrap ( this. data . substs ) )
326
+ . finish ( )
327
+ }
328
+ }
235
329
236
330
impl < ' tcx > fmt:: Debug for ty:: Const < ' tcx > {
237
331
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
332
+ OptWithInfcx :: new_no_ctx ( self ) . fmt ( f)
333
+ }
334
+ }
335
+ impl < ' tcx > DebugWithInfcx < TyCtxt < ' tcx > > for ty:: Const < ' tcx > {
336
+ fn fmt < InfCtx : InferCtxtLike < TyCtxt < ' tcx > > > (
337
+ this : OptWithInfcx < ' _ , TyCtxt < ' tcx > , InfCtx , & Self > ,
338
+ f : & mut core:: fmt:: Formatter < ' _ > ,
339
+ ) -> core:: fmt:: Result {
238
340
// This reflects what `Const` looked liked before `Interned` was
239
341
// introduced. We print it like this to avoid having to update expected
240
342
// output in a lot of tests.
241
- write ! ( f, "Const {{ ty: {:?}, kind: {:?} }}" , self . ty( ) , self . kind( ) )
343
+ write ! (
344
+ f,
345
+ "Const {{ ty: {:?}, kind: {:?} }}" ,
346
+ & this. map( |data| data. ty( ) ) ,
347
+ & this. map( |data| data. kind( ) )
348
+ )
242
349
}
243
350
}
244
351
@@ -261,6 +368,66 @@ impl<T: fmt::Debug> fmt::Debug for ty::Placeholder<T> {
261
368
}
262
369
}
263
370
371
+ impl < ' tcx > fmt:: Debug for GenericArg < ' tcx > {
372
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
373
+ match self . unpack ( ) {
374
+ GenericArgKind :: Lifetime ( lt) => lt. fmt ( f) ,
375
+ GenericArgKind :: Type ( ty) => ty. fmt ( f) ,
376
+ GenericArgKind :: Const ( ct) => ct. fmt ( f) ,
377
+ }
378
+ }
379
+ }
380
+ impl < ' tcx > DebugWithInfcx < TyCtxt < ' tcx > > for GenericArg < ' tcx > {
381
+ fn fmt < InfCtx : InferCtxtLike < TyCtxt < ' tcx > > > (
382
+ this : OptWithInfcx < ' _ , TyCtxt < ' tcx > , InfCtx , & Self > ,
383
+ f : & mut core:: fmt:: Formatter < ' _ > ,
384
+ ) -> core:: fmt:: Result {
385
+ match this. data . unpack ( ) {
386
+ GenericArgKind :: Lifetime ( lt) => write ! ( f, "{:?}" , & this. wrap( lt) ) ,
387
+ GenericArgKind :: Const ( ct) => write ! ( f, "{:?}" , & this. wrap( ct) ) ,
388
+ GenericArgKind :: Type ( ty) => write ! ( f, "{:?}" , & this. wrap( ty) ) ,
389
+ }
390
+ }
391
+ }
392
+
393
+ impl < ' tcx > fmt:: Debug for Region < ' tcx > {
394
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
395
+ write ! ( f, "{:?}" , self . kind( ) )
396
+ }
397
+ }
398
+ impl < ' tcx > DebugWithInfcx < TyCtxt < ' tcx > > for Region < ' tcx > {
399
+ fn fmt < InfCtx : InferCtxtLike < TyCtxt < ' tcx > > > (
400
+ this : OptWithInfcx < ' _ , TyCtxt < ' tcx > , InfCtx , & Self > ,
401
+ f : & mut core:: fmt:: Formatter < ' _ > ,
402
+ ) -> core:: fmt:: Result {
403
+ write ! ( f, "{:?}" , & this. map( |data| data. kind( ) ) )
404
+ }
405
+ }
406
+
407
+ impl < ' tcx > DebugWithInfcx < TyCtxt < ' tcx > > for ty:: RegionVid {
408
+ fn fmt < InfCtx : InferCtxtLike < TyCtxt < ' tcx > > > (
409
+ this : OptWithInfcx < ' _ , TyCtxt < ' tcx > , InfCtx , & Self > ,
410
+ f : & mut core:: fmt:: Formatter < ' _ > ,
411
+ ) -> core:: fmt:: Result {
412
+ match this. infcx . and_then ( |infcx| infcx. universe_of_lt ( * this. data ) ) {
413
+ Some ( universe) => write ! ( f, "'?{}_{}" , this. data. index( ) , universe. index( ) ) ,
414
+ None => write ! ( f, "{:?}" , this. data) ,
415
+ }
416
+ }
417
+ }
418
+
419
+ impl < ' tcx , T : DebugWithInfcx < TyCtxt < ' tcx > > > DebugWithInfcx < TyCtxt < ' tcx > > for ty:: Binder < ' tcx , T > {
420
+ fn fmt < InfCtx : InferCtxtLike < TyCtxt < ' tcx > > > (
421
+ this : OptWithInfcx < ' _ , TyCtxt < ' tcx > , InfCtx , & Self > ,
422
+ f : & mut core:: fmt:: Formatter < ' _ > ,
423
+ ) -> core:: fmt:: Result {
424
+ f. debug_tuple ( "Binder" )
425
+ . field ( & this. map ( |data| data. as_ref ( ) . skip_binder ( ) ) )
426
+ . field ( & this. data . bound_vars ( ) )
427
+ . finish ( )
428
+ }
429
+ }
430
+
264
431
///////////////////////////////////////////////////////////////////////////
265
432
// Atomic structs
266
433
//
0 commit comments