@@ -11,13 +11,15 @@ use crate::ty::{self, AliasTy, InferConst, Lift, Term, TermKind, Ty, TyCtxt};
1111use rustc_hir:: def:: Namespace ;
1212use rustc_index:: { Idx , IndexVec } ;
1313use rustc_target:: abi:: TyAndLayout ;
14- use rustc_type_ir:: ConstKind ;
14+ use rustc_type_ir:: { ConstKind , DebugWithInfcx , InferCtxtLike , OptWithInfcx } ;
1515
16- use std:: fmt;
16+ use std:: fmt:: { self , Debug } ;
1717use std:: ops:: ControlFlow ;
1818use std:: rc:: Rc ;
1919use std:: sync:: Arc ;
2020
21+ use super :: { GenericArg , GenericArgKind , Region } ;
22+
2123impl fmt:: Debug for ty:: TraitDef {
2224 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
2325 ty:: tls:: with ( |tcx| {
@@ -89,7 +91,16 @@ impl fmt::Debug for ty::FreeRegion {
8991
9092impl < ' tcx > fmt:: Debug for ty:: FnSig < ' tcx > {
9193 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;
93104
94105 write ! ( f, "{}" , unsafety. prefix_str( ) ) ?;
95106 match abi {
@@ -98,25 +109,25 @@ impl<'tcx> fmt::Debug for ty::FnSig<'tcx> {
98109 } ;
99110
100111 write ! ( f, "fn(" ) ?;
101- let inputs = self . inputs ( ) ;
112+ let inputs = sig . inputs ( ) ;
102113 match inputs. len ( ) {
103114 0 if * c_variadic => write ! ( f, "...)" ) ?,
104115 0 => write ! ( f, ")" ) ?,
105116 _ => {
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 ) ) ?;
108119 }
109- write ! ( f, "{:?}" , self . inputs( ) . last( ) . unwrap( ) ) ?;
120+ write ! ( f, "{:?}" , & this . wrap ( sig . inputs( ) . last( ) . unwrap( ) ) ) ?;
110121 if * c_variadic {
111122 write ! ( f, "..." ) ?;
112123 }
113124 write ! ( f, ")" ) ?;
114125 }
115126 }
116127
117- match self . output ( ) . kind ( ) {
128+ match sig . output ( ) . kind ( ) {
118129 ty:: Tuple ( list) if list. is_empty ( ) => Ok ( ( ) ) ,
119- _ => write ! ( f, " -> {:?}" , self . output( ) ) ,
130+ _ => write ! ( f, " -> {:?}" , & this . wrap ( sig . output( ) ) ) ,
120131 }
121132 }
122133}
@@ -133,6 +144,14 @@ impl<'tcx> fmt::Debug for ty::TraitRef<'tcx> {
133144 }
134145}
135146
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+ }
136155impl < ' tcx > fmt:: Debug for Ty < ' tcx > {
137156 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
138157 with_no_trimmed_paths ! ( fmt:: Display :: fmt( self , f) )
@@ -217,9 +236,17 @@ impl<'tcx> fmt::Debug for ty::PredicateKind<'tcx> {
217236
218237impl < ' tcx > fmt:: Debug for AliasTy < ' tcx > {
219238 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 {
220247 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 )
223250 . finish ( )
224251 }
225252}
@@ -232,13 +259,93 @@ impl<'tcx> fmt::Debug for ty::InferConst<'tcx> {
232259 }
233260 }
234261}
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+ }
235329
236330impl < ' tcx > fmt:: Debug for ty:: Const < ' tcx > {
237331 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 {
238340 // This reflects what `Const` looked liked before `Interned` was
239341 // introduced. We print it like this to avoid having to update expected
240342 // 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+ )
242349 }
243350}
244351
@@ -261,6 +368,66 @@ impl<T: fmt::Debug> fmt::Debug for ty::Placeholder<T> {
261368 }
262369}
263370
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+
264431///////////////////////////////////////////////////////////////////////////
265432// Atomic structs
266433//
0 commit comments