Skip to content

Commit eb7b9cf

Browse files
committed
Better print ScalarPair when it is tuple or slice
Signed-off-by: xizheyin <[email protected]>
1 parent cb6785f commit eb7b9cf

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

compiler/rustc_const_eval/src/interpret/operand.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,47 @@ impl<Prov: Provenance> std::fmt::Display for ImmTy<'_, Prov> {
213213
write!(f, "{:x}: {}", s, self.layout.ty)
214214
}
215215
Immediate::ScalarPair(a, b) => {
216-
// FIXME(oli-obk): at least print tuples and slices nicely
216+
// Try to print tuples and slices nicely first
217+
// It's *only* printed for internal debugging
218+
if let Some(ty) = tcx.lift(self.layout.ty) {
219+
match ty.kind() {
220+
ty::Tuple(tys) => {
221+
// For tuples, print each field with its proper type
222+
if tys.len() == 2 {
223+
let a_str =
224+
FmtPrinter::print_string(tcx, Namespace::ValueNS, |cx| {
225+
p(cx, a, tys[0])
226+
})?;
227+
let b_str =
228+
FmtPrinter::print_string(tcx, Namespace::ValueNS, |cx| {
229+
p(cx, b, tys[1])
230+
})?;
231+
232+
write!(f, "({}, {}): {}", a_str, b_str, ty)?;
233+
return Ok(());
234+
}
235+
}
236+
ty::Slice(element_ty) => {
237+
// For slices, the first scalar is the pointer, second is the length
238+
let ptr_str =
239+
FmtPrinter::print_string(tcx, Namespace::ValueNS, |cx| {
240+
p(cx, a, Ty::new_ptr(tcx, *element_ty, ty::Mutability::Not))
241+
})?;
242+
let len_str =
243+
FmtPrinter::print_string(tcx, Namespace::ValueNS, |cx| {
244+
p(cx, b, tcx.types.usize)
245+
})?;
246+
247+
write!(f, "&[{}; {}]: {}", ptr_str, len_str, ty)?;
248+
return Ok(());
249+
}
250+
_ => {
251+
// For other types, try to print each component with the field types
252+
// Since we can't easily get field types, fall back to the original format for now
253+
}
254+
}
255+
}
256+
// Fallback to the original format if we can't pretty print
217257
write!(f, "({:x}, {:x}): {}", a, b, self.layout.ty)
218258
}
219259
Immediate::Uninit => {

0 commit comments

Comments
 (0)