Skip to content

Commit d01ef7d

Browse files
committed
improve debug-printing of scalars
Before: Immediate(ScalarMaybeUndef(Scalar(Ptr(Pointer { alloc_id: AllocId(3401), offset: Size { raw: 4 }, tag: Tagged(7723) })))) After: Immediate(Scalar(AllocId(3401).0x4[<7723>])) Before: Immediate(ScalarMaybeUndef(Scalar(Bits { size: 8, bits: 10 }))) After: Immediate(Scalar(0x000000000000000A)) Before: Immediate(ScalarMaybeUndef(Scalar(Bits { size: 1, bits: 1 }))) After: Immediate(Scalar(0x01))
1 parent 4680580 commit d01ef7d

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

src/librustc/mir/interpret/pointer.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::fmt;
2+
13
use crate::mir;
24
use crate::ty::layout::{self, HasDataLayout, Size};
35
use rustc_macros::HashStable;
@@ -70,7 +72,7 @@ impl<T: layout::HasDataLayout> PointerArithmetic for T {}
7072
///
7173
/// Pointer is also generic over the `Tag` associated with each pointer,
7274
/// which is used to do provenance tracking during execution.
73-
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd,
75+
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd,
7476
RustcEncodable, RustcDecodable, Hash, HashStable)]
7577
pub struct Pointer<Tag=(),Id=AllocId> {
7678
pub alloc_id: Id,
@@ -80,6 +82,18 @@ pub struct Pointer<Tag=(),Id=AllocId> {
8082

8183
static_assert_size!(Pointer, 16);
8284

85+
impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Debug for Pointer<Tag, Id> {
86+
default fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87+
write!(f, "{:?}.{:#x}[{:?}]", self.alloc_id, self.offset.bytes(), self.tag)
88+
}
89+
}
90+
// Specialization for no tag
91+
impl<Id: fmt::Debug> fmt::Debug for Pointer<(), Id> {
92+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93+
write!(f, "{:?}.{:#x}", self.alloc_id, self.offset.bytes())
94+
}
95+
}
96+
8397
/// Produces a `Pointer` which points to the beginning of the Allocation
8498
impl From<AllocId> for Pointer {
8599
#[inline(always)]

src/librustc/mir/interpret/value.rs

+32-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl<'tcx> ConstValue<'tcx> {
9393
/// `memory::Allocation`. It is in many ways like a small chunk of a `Allocation`, up to 8 bytes in
9494
/// size. Like a range of bytes in an `Allocation`, a `Scalar` can either represent the raw bytes
9595
/// of a simple value or a pointer into another `Allocation`
96-
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd,
96+
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd,
9797
RustcEncodable, RustcDecodable, Hash, HashStable)]
9898
pub enum Scalar<Tag=(), Id=AllocId> {
9999
/// The raw bytes of a simple value.
@@ -113,6 +113,27 @@ pub enum Scalar<Tag=(), Id=AllocId> {
113113
#[cfg(target_arch = "x86_64")]
114114
static_assert_size!(Scalar, 24);
115115

116+
impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Debug for Scalar<Tag, Id> {
117+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118+
match self {
119+
Scalar::Ptr(ptr) =>
120+
write!(f, "{:?}", ptr),
121+
&Scalar::Bits { bits, size } => {
122+
if size == 0 {
123+
assert_eq!(bits, 0, "ZST value must be 0");
124+
write!(f, "<ZST>")
125+
} else {
126+
assert_eq!(truncate(bits, Size::from_bytes(size as u64)), bits,
127+
"Scalar value {:#x} exceeds size of {} bytes", bits, size);
128+
// Format as hex number wide enough to fit any value of the given `size`.
129+
// So e.g. bits=20, size=1 will be "0x14", but with size=4 it'll be "0x00000014".
130+
write!(f, "0x{:>0width$x}", bits, width=(size*2) as usize)
131+
}
132+
}
133+
}
134+
}
135+
}
136+
116137
impl<Tag> fmt::Display for Scalar<Tag> {
117138
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118139
match self {
@@ -412,7 +433,7 @@ impl<Tag> From<Pointer<Tag>> for Scalar<Tag> {
412433
}
413434
}
414435

415-
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, RustcEncodable, RustcDecodable, Hash)]
436+
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, RustcEncodable, RustcDecodable, Hash)]
416437
pub enum ScalarMaybeUndef<Tag=(), Id=AllocId> {
417438
Scalar(Scalar<Tag, Id>),
418439
Undef,
@@ -425,6 +446,15 @@ impl<Tag> From<Scalar<Tag>> for ScalarMaybeUndef<Tag> {
425446
}
426447
}
427448

449+
impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Debug for ScalarMaybeUndef<Tag, Id> {
450+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
451+
match self {
452+
ScalarMaybeUndef::Undef => write!(f, "Undef"),
453+
ScalarMaybeUndef::Scalar(s) => write!(f, "{:?}", s),
454+
}
455+
}
456+
}
457+
428458
impl<Tag> fmt::Display for ScalarMaybeUndef<Tag> {
429459
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
430460
match self {

0 commit comments

Comments
 (0)