Skip to content

Commit abae2b6

Browse files
authored
Rollup merge of #115884 - RalfJung:const-debug-print, r=oli-obk
make ty::Const debug printing less verbose Similar in spirit to #115873
2 parents edea0d5 + 5a3410a commit abae2b6

5 files changed

+26
-12
lines changed

compiler/rustc_middle/src/ty/structural_impls.rs

+20-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::ops::ControlFlow;
1818
use std::rc::Rc;
1919
use std::sync::Arc;
2020

21+
use super::print::PrettyPrinter;
2122
use super::{GenericArg, GenericArgKind, Region};
2223

2324
impl 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
}

tests/mir-opt/issue_99325.main.built.after.32bit.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// MIR for `main` after built
22

33
| User Type Annotations
4-
| 0: user_ty: Canonical { value: TypeOf(DefId(0:3 ~ issue_99325[22bb]::function_with_bytes), UserArgs { args: [Const { ty: &ReStatic [u8; Const { ty: usize, kind: Leaf(0x00000004) }], kind: Branch([Leaf(0x41), Leaf(0x41), Leaf(0x41), Leaf(0x41)]) }], user_self_ty: None }), max_universe: U0, variables: [] }, span: $DIR/issue_99325.rs:12:16: 12:46, inferred_ty: fn() -> &'static [u8] {function_with_bytes::<&*b"AAAA">}
5-
| 1: user_ty: Canonical { value: TypeOf(DefId(0:3 ~ issue_99325[22bb]::function_with_bytes), UserArgs { args: [Const { ty: &ReStatic [u8; Const { ty: usize, kind: Leaf(0x00000004) }], kind: UnevaluatedConst { def: DefId(0:8 ~ issue_99325[22bb]::main::{constant#1}), args: [] } }], user_self_ty: None }), max_universe: U0, variables: [] }, span: $DIR/issue_99325.rs:13:16: 13:68, inferred_ty: fn() -> &'static [u8] {function_with_bytes::<&*b"AAAA">}
4+
| 0: user_ty: Canonical { value: TypeOf(DefId(0:3 ~ issue_99325[22bb]::function_with_bytes), UserArgs { args: [&*b"AAAA"], user_self_ty: None }), max_universe: U0, variables: [] }, span: $DIR/issue_99325.rs:12:16: 12:46, inferred_ty: fn() -> &'static [u8] {function_with_bytes::<&*b"AAAA">}
5+
| 1: user_ty: Canonical { value: TypeOf(DefId(0:3 ~ issue_99325[22bb]::function_with_bytes), UserArgs { args: [UnevaluatedConst { def: DefId(0:8 ~ issue_99325[22bb]::main::{constant#1}), args: [] }: &ReStatic [u8; 4_usize]], user_self_ty: None }), max_universe: U0, variables: [] }, span: $DIR/issue_99325.rs:13:16: 13:68, inferred_ty: fn() -> &'static [u8] {function_with_bytes::<&*b"AAAA">}
66
|
77
fn main() -> () {
88
let mut _0: ();

tests/mir-opt/issue_99325.main.built.after.64bit.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// MIR for `main` after built
22

33
| User Type Annotations
4-
| 0: user_ty: Canonical { value: TypeOf(DefId(0:3 ~ issue_99325[22bb]::function_with_bytes), UserArgs { args: [Const { ty: &ReStatic [u8; Const { ty: usize, kind: Leaf(0x0000000000000004) }], kind: Branch([Leaf(0x41), Leaf(0x41), Leaf(0x41), Leaf(0x41)]) }], user_self_ty: None }), max_universe: U0, variables: [] }, span: $DIR/issue_99325.rs:12:16: 12:46, inferred_ty: fn() -> &'static [u8] {function_with_bytes::<&*b"AAAA">}
5-
| 1: user_ty: Canonical { value: TypeOf(DefId(0:3 ~ issue_99325[22bb]::function_with_bytes), UserArgs { args: [Const { ty: &ReStatic [u8; Const { ty: usize, kind: Leaf(0x0000000000000004) }], kind: UnevaluatedConst { def: DefId(0:8 ~ issue_99325[22bb]::main::{constant#1}), args: [] } }], user_self_ty: None }), max_universe: U0, variables: [] }, span: $DIR/issue_99325.rs:13:16: 13:68, inferred_ty: fn() -> &'static [u8] {function_with_bytes::<&*b"AAAA">}
4+
| 0: user_ty: Canonical { value: TypeOf(DefId(0:3 ~ issue_99325[22bb]::function_with_bytes), UserArgs { args: [&*b"AAAA"], user_self_ty: None }), max_universe: U0, variables: [] }, span: $DIR/issue_99325.rs:12:16: 12:46, inferred_ty: fn() -> &'static [u8] {function_with_bytes::<&*b"AAAA">}
5+
| 1: user_ty: Canonical { value: TypeOf(DefId(0:3 ~ issue_99325[22bb]::function_with_bytes), UserArgs { args: [UnevaluatedConst { def: DefId(0:8 ~ issue_99325[22bb]::main::{constant#1}), args: [] }: &ReStatic [u8; 4_usize]], user_self_ty: None }), max_universe: U0, variables: [] }, span: $DIR/issue_99325.rs:13:16: 13:68, inferred_ty: fn() -> &'static [u8] {function_with_bytes::<&*b"AAAA">}
66
|
77
fn main() -> () {
88
let mut _0: ();

tests/mir-opt/nll/region_subtyping_basic.main.nll.0.32bit.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
|
2323
fn main() -> () {
2424
let mut _0: ();
25-
let mut _1: [usize; Const { ty: usize, kind: Leaf(0x00000003) }];
25+
let mut _1: [usize; ValTree(Leaf(0x00000003): usize)];
2626
let _3: usize;
2727
let mut _4: usize;
2828
let mut _5: bool;

tests/mir-opt/nll/region_subtyping_basic.main.nll.0.64bit.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
|
2323
fn main() -> () {
2424
let mut _0: ();
25-
let mut _1: [usize; Const { ty: usize, kind: Leaf(0x0000000000000003) }];
25+
let mut _1: [usize; ValTree(Leaf(0x0000000000000003): usize)];
2626
let _3: usize;
2727
let mut _4: usize;
2828
let mut _5: bool;

0 commit comments

Comments
 (0)