Skip to content

Commit 829d8de

Browse files
committed
factor out some common code and make the on-construction truncation test debug-only
1 parent aad13a1 commit 829d8de

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/librustc/mir/interpret/value.rs

+19-13
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,10 @@ impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Debug for Scalar<Tag, Id> {
109109
Scalar::Ptr(ptr) =>
110110
write!(f, "{:?}", ptr),
111111
&Scalar::Raw { data, size } => {
112+
Scalar::check_data(data, size);
112113
if size == 0 {
113-
assert_eq!(data, 0, "ZST value must be 0");
114114
write!(f, "<ZST>")
115115
} else {
116-
assert_eq!(truncate(data, Size::from_bytes(size as u64)), data,
117-
"Scalar value {:#x} exceeds size of {} bytes", data, size);
118116
// Format as hex number wide enough to fit any value of the given `size`.
119117
// So data=20, size=1 will be "0x14", but with size=4 it'll be "0x00000014".
120118
write!(f, "0x{:>0width$x}", data, width=(size*2) as usize)
@@ -134,6 +132,12 @@ impl<Tag> fmt::Display for Scalar<Tag> {
134132
}
135133

136134
impl<'tcx> Scalar<()> {
135+
#[inline]
136+
fn check_data(data: u128, size: u8) {
137+
assert_eq!(truncate(data, Size::from_bytes(size as u64)), data,
138+
"Scalar value {:#x} exceeds size of {} bytes", data, size);
139+
}
140+
137141
#[inline]
138142
pub fn with_tag<Tag>(self, new_tag: Tag) -> Scalar<Tag> {
139143
match self {
@@ -269,8 +273,10 @@ impl<'tcx, Tag> Scalar<Tag> {
269273
#[inline]
270274
pub fn from_uint(i: impl Into<u128>, size: Size) -> Self {
271275
let i = i.into();
272-
assert_eq!(truncate(i, size), i,
273-
"Unsigned value {} does not fit in {} bits", i, size.bits());
276+
debug_assert_eq!(
277+
truncate(i, size), i,
278+
"Unsigned value {:#x} does not fit in {} bits", i, size.bits()
279+
);
274280
Scalar::Raw { data: i, size: size.bytes() as u8 }
275281
}
276282

@@ -279,8 +285,10 @@ impl<'tcx, Tag> Scalar<Tag> {
279285
let i = i.into();
280286
// `into` performed sign extension, we have to truncate
281287
let truncated = truncate(i as u128, size);
282-
assert_eq!(sign_extend(truncated, size) as i128, i,
283-
"Signed value {} does not fit in {} bits", i, size.bits());
288+
debug_assert_eq!(
289+
sign_extend(truncated, size) as i128, i,
290+
"Signed value {:#x} does not fit in {} bits", i, size.bits()
291+
);
284292
Scalar::Raw { data: truncated, size: size.bytes() as u8 }
285293
}
286294

@@ -303,9 +311,8 @@ impl<'tcx, Tag> Scalar<Tag> {
303311
match self {
304312
Scalar::Raw { data, size } => {
305313
assert_eq!(target_size.bytes(), size as u64);
306-
assert_ne!(size, 0, "to_bits cannot be used with zsts");
307-
assert_eq!(truncate(data, target_size), data,
308-
"Scalar value {:#x} exceeds size of {} bytes", data, size);
314+
assert_ne!(size, 0, "you should never look at the bits of a ZST");
315+
Scalar::check_data(data, size);
309316
Ok(data)
310317
}
311318
Scalar::Ptr(ptr) => {
@@ -320,9 +327,8 @@ impl<'tcx, Tag> Scalar<Tag> {
320327
match self {
321328
Scalar::Raw { data, size } => {
322329
assert_eq!(target_size.bytes(), size as u64);
323-
assert_ne!(size, 0, "to_bits cannot be used with zsts");
324-
assert_eq!(truncate(data, target_size), data,
325-
"Scalar value {:#x} exceeds size of {} bytes", data, size);
330+
assert_ne!(size, 0, "you should never look at the bits of a ZST");
331+
Scalar::check_data(data, size);
326332
Ok(data)
327333
}
328334
Scalar::Ptr(_) => err!(ReadPointerAsBytes),

0 commit comments

Comments
 (0)