Skip to content

Commit a329756

Browse files
committed
Show the sign of signed interpreter values
1 parent 0f6f66f commit a329756

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/librustc_mir/interpret/intrinsics.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -417,13 +417,13 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
417417
if self.binary_op(BinOp::Rem, a, b)?.to_bits()? != 0 {
418418
// Then, check if `b` is -1, which is the "min_value / -1" case.
419419
let minus1 = Scalar::from_int(-1, dest.layout.size);
420-
let b = b.to_scalar().unwrap();
421-
if b == minus1 {
420+
let b_scalar = b.to_scalar().unwrap();
421+
if b_scalar == minus1 {
422422
throw_ub_format!("exact_div: result of dividing MIN by -1 cannot be represented")
423423
} else {
424424
throw_ub_format!(
425425
"exact_div: {} cannot be divided by {} without remainder",
426-
a.to_scalar().unwrap(),
426+
a,
427427
b,
428428
)
429429
}

src/librustc_mir/interpret/operand.rs

+37
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use super::{
1919
};
2020
pub use rustc::mir::interpret::ScalarMaybeUndef;
2121
use rustc_macros::HashStable;
22+
use syntax::ast;
2223

2324
/// An `Immediate` represents a single immediate self-contained Rust value.
2425
///
@@ -93,6 +94,42 @@ pub struct ImmTy<'tcx, Tag=()> {
9394
pub layout: TyLayout<'tcx>,
9495
}
9596

97+
// `Tag: Copy` because some methods on `Scalar` consume them by value
98+
impl<Tag: Copy> std::fmt::Display for ImmTy<'tcx, Tag> {
99+
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
100+
match &self.imm {
101+
Immediate::Scalar(ScalarMaybeUndef::Scalar(s)) => match s.to_bits(self.layout.size) {
102+
Ok(s) => {
103+
match self.layout.ty.kind {
104+
ty::Int(_) => return write!(
105+
fmt, "{}",
106+
super::sign_extend(s, self.layout.size) as i128,
107+
),
108+
ty::Uint(_) => return write!(fmt, "{}", s),
109+
ty::Bool if s == 0 => return fmt.write_str("false"),
110+
ty::Bool if s == 1 => return fmt.write_str("true"),
111+
ty::Char => if let Some(c) =
112+
u32::try_from(s).ok().and_then(std::char::from_u32) {
113+
return write!(fmt, "{}", c);
114+
},
115+
ty::Float(ast::FloatTy::F32) => if let Ok(u) = u32::try_from(s) {
116+
return write!(fmt, "{}", f32::from_bits(u));
117+
},
118+
ty::Float(ast::FloatTy::F64) => if let Ok(u) = u64::try_from(s) {
119+
return write!(fmt, "{}", f64::from_bits(u));
120+
},
121+
_ => {},
122+
}
123+
write!(fmt, "{:x}", s)
124+
},
125+
Err(_) => fmt.write_str("{pointer}"),
126+
},
127+
Immediate::Scalar(ScalarMaybeUndef::Undef) => fmt.write_str("{undef}"),
128+
Immediate::ScalarPair(..) => fmt.write_str("{wide pointer or tuple}"),
129+
}
130+
}
131+
}
132+
96133
impl<'tcx, Tag> ::std::ops::Deref for ImmTy<'tcx, Tag> {
97134
type Target = Immediate<Tag>;
98135
#[inline(always)]

0 commit comments

Comments
 (0)