From 2b5ce93954db1ee3728bacbcd1b42ebb75f7be21 Mon Sep 17 00:00:00 2001 From: xizheyin Date: Wed, 30 Jul 2025 22:54:19 +0800 Subject: [PATCH] Better print ScalarPair when it is slice Signed-off-by: xizheyin --- .../rustc_const_eval/src/interpret/operand.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_const_eval/src/interpret/operand.rs b/compiler/rustc_const_eval/src/interpret/operand.rs index 21afd082a0551..0f5b45e93ac33 100644 --- a/compiler/rustc_const_eval/src/interpret/operand.rs +++ b/compiler/rustc_const_eval/src/interpret/operand.rs @@ -213,7 +213,23 @@ impl std::fmt::Display for ImmTy<'_, Prov> { write!(f, "{:x}: {}", s, self.layout.ty) } Immediate::ScalarPair(a, b) => { - // FIXME(oli-obk): at least print tuples and slices nicely + // Try to print slices nicely first + // It's *only* printed for internal debugging + if let Some(ty) = tcx.lift(self.layout.ty) + && let ty::Slice(element_ty) = ty.kind() + { + // For slices, the first scalar is the pointer, second is the length + let ptr_str = FmtPrinter::print_string(tcx, Namespace::ValueNS, |cx| { + p(cx, a, Ty::new_ptr(tcx, *element_ty, ty::Mutability::Not)) + })?; + let len_str = FmtPrinter::print_string(tcx, Namespace::ValueNS, |cx| { + p(cx, b, tcx.types.usize) + })?; + + write!(f, "&[{}; {}]: {}", ptr_str, len_str, ty)?; + return Ok(()); + } + // Fallback to the original format if we can't pretty print write!(f, "({:x}, {:x}): {}", a, b, self.layout.ty) } Immediate::Uninit => {