@@ -18,6 +18,7 @@ use std::ops::ControlFlow;
1818use std:: rc:: Rc ;
1919use std:: sync:: Arc ;
2020
21+ use super :: print:: PrettyPrinter ;
2122use super :: { GenericArg , GenericArgKind , Region } ;
2223
2324impl fmt:: Debug for ty:: TraitDef {
@@ -343,14 +344,27 @@ impl<'tcx> DebugWithInfcx<TyCtxt<'tcx>> for ty::Const<'tcx> {
343344 this : OptWithInfcx < ' _ , TyCtxt < ' tcx > , InfCtx , & Self > ,
344345 f : & mut core:: fmt:: Formatter < ' _ > ,
345346 ) -> core:: fmt:: Result {
346- // This reflects what `Const` looked liked before `Interned` was
347- // introduced. We print it like this to avoid having to update expected
348- // output in a lot of tests.
347+ // If this is a value, we spend some effort to make it look nice.
348+ if let ConstKind :: Value ( _) = this. data . kind ( ) {
349+ return ty:: tls:: with ( move |tcx| {
350+ // Somehow trying to lift the valtree results in lifetime errors, so we lift the
351+ // entire constant.
352+ let lifted = tcx. lift ( * this. data ) . unwrap ( ) ;
353+ let ConstKind :: Value ( valtree) = lifted. kind ( ) else {
354+ bug ! ( "we checked that this is a valtree" )
355+ } ;
356+ let cx = FmtPrinter :: new ( tcx, Namespace :: ValueNS ) ;
357+ let cx =
358+ cx. pretty_print_const_valtree ( valtree, lifted. ty ( ) , /*print_ty*/ true ) ?;
359+ f. write_str ( & cx. into_buffer ( ) )
360+ } ) ;
361+ }
362+ // Fall back to something verbose.
349363 write ! (
350364 f,
351- "Const {{ ty: {:?}, kind : {:?} } }" ,
352- & this. map( |data| data. ty( ) ) ,
353- & this. map( |data| data. kind( ) )
365+ "{kind:?} : {ty:? }" ,
366+ ty = & this. map( |data| data. ty( ) ) ,
367+ kind = & this. map( |data| data. kind( ) )
354368 )
355369 }
356370}
0 commit comments