Skip to content

Commit 3045d22

Browse files
authored
Rollup merge of rust-lang#66148 - oli-obk:it_must_be_a_sign, r=RalfJung
Show the sign for signed ops on `exact_div` r? @RalfJung Cc https://github.com/rust-lang/miri/pull/961/files#r341842128 I'm fairly unhappy with the duplication and the general effort required for this. Maybe it would be better to add a `display` impl for `ImmTy`?
2 parents 4787e97 + a329756 commit 3045d22

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
@@ -424,13 +424,13 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
424424
if self.binary_op(BinOp::Rem, a, b)?.to_bits()? != 0 {
425425
// Then, check if `b` is -1, which is the "min_value / -1" case.
426426
let minus1 = Scalar::from_int(-1, dest.layout.size);
427-
let b = b.to_scalar().unwrap();
428-
if b == minus1 {
427+
let b_scalar = b.to_scalar().unwrap();
428+
if b_scalar == minus1 {
429429
throw_ub_format!("exact_div: result of dividing MIN by -1 cannot be represented")
430430
} else {
431431
throw_ub_format!(
432432
"exact_div: {} cannot be divided by {} without remainder",
433-
a.to_scalar().unwrap(),
433+
a,
434434
b,
435435
)
436436
}

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
///
@@ -100,6 +101,42 @@ pub struct ImmTy<'tcx, Tag=()> {
100101
pub layout: TyLayout<'tcx>,
101102
}
102103

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

0 commit comments

Comments
 (0)