@@ -18,6 +18,7 @@ use std::ops::ControlFlow;
18
18
use std:: rc:: Rc ;
19
19
use std:: sync:: Arc ;
20
20
21
+ use super :: print:: PrettyPrinter ;
21
22
use super :: { GenericArg , GenericArgKind , Region } ;
22
23
23
24
impl fmt:: Debug for ty:: TraitDef {
@@ -343,14 +344,27 @@ impl<'tcx> DebugWithInfcx<TyCtxt<'tcx>> for ty::Const<'tcx> {
343
344
this : OptWithInfcx < ' _ , TyCtxt < ' tcx > , InfCtx , & Self > ,
344
345
f : & mut core:: fmt:: Formatter < ' _ > ,
345
346
) -> 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.
349
363
write ! (
350
364
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( ) )
354
368
)
355
369
}
356
370
}
0 commit comments