diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2023-09-16 23:20:42 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-16 23:20:42 +0200 |
| commit | abae2b66e83d3a31ff96550aab3c2e28d64837fa (patch) | |
| tree | a0954f949377f4f76ad97e30f74512dfa1a4cac0 /compiler/rustc_middle | |
| parent | edea0d5179a144dcd23b0dcc38e217d30e49d8be (diff) | |
| parent | 5a3410ad1a1858489e7b5d8dc3d14837a09fc37f (diff) | |
| download | rust-abae2b66e83d3a31ff96550aab3c2e28d64837fa.tar.gz rust-abae2b66e83d3a31ff96550aab3c2e28d64837fa.zip | |
Rollup merge of #115884 - RalfJung:const-debug-print, r=oli-obk
make ty::Const debug printing less verbose Similar in spirit to https://github.com/rust-lang/rust/pull/115873
Diffstat (limited to 'compiler/rustc_middle')
| -rw-r--r-- | compiler/rustc_middle/src/ty/structural_impls.rs | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/compiler/rustc_middle/src/ty/structural_impls.rs b/compiler/rustc_middle/src/ty/structural_impls.rs index 1b29a83f23e..7c25d0209c9 100644 --- a/compiler/rustc_middle/src/ty/structural_impls.rs +++ b/compiler/rustc_middle/src/ty/structural_impls.rs @@ -18,6 +18,7 @@ use std::ops::ControlFlow; use std::rc::Rc; use std::sync::Arc; +use super::print::PrettyPrinter; use super::{GenericArg, GenericArgKind, Region}; impl fmt::Debug for ty::TraitDef { @@ -343,14 +344,27 @@ impl<'tcx> DebugWithInfcx<TyCtxt<'tcx>> for ty::Const<'tcx> { this: OptWithInfcx<'_, TyCtxt<'tcx>, InfCtx, &Self>, f: &mut core::fmt::Formatter<'_>, ) -> core::fmt::Result { - // This reflects what `Const` looked liked before `Interned` was - // introduced. We print it like this to avoid having to update expected - // output in a lot of tests. + // If this is a value, we spend some effort to make it look nice. + if let ConstKind::Value(_) = this.data.kind() { + return ty::tls::with(move |tcx| { + // Somehow trying to lift the valtree results in lifetime errors, so we lift the + // entire constant. + let lifted = tcx.lift(*this.data).unwrap(); + let ConstKind::Value(valtree) = lifted.kind() else { + bug!("we checked that this is a valtree") + }; + let cx = FmtPrinter::new(tcx, Namespace::ValueNS); + let cx = + cx.pretty_print_const_valtree(valtree, lifted.ty(), /*print_ty*/ true)?; + f.write_str(&cx.into_buffer()) + }); + } + // Fall back to something verbose. write!( f, - "Const {{ ty: {:?}, kind: {:?} }}", - &this.map(|data| data.ty()), - &this.map(|data| data.kind()) + "{kind:?}: {ty:?}", + ty = &this.map(|data| data.ty()), + kind = &this.map(|data| data.kind()) ) } } |
