Skip to content

Better print ScalarPair when it is tuple or slice #144686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion compiler/rustc_const_eval/src/interpret/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,23 @@ impl<Prov: Provenance> 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)?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have an example of what this looks like in the end?

It seems like it'll print something like &[ptr; 128] which is confusing because it looks like a slice repeating the pointer 128 times, when actually it is a slice of that length starting at ptr.

return Ok(());
}
// Fallback to the original format if we can't pretty print
write!(f, "({:x}, {:x}): {}", a, b, self.layout.ty)
}
Immediate::Uninit => {
Expand Down
Loading