Skip to content

Commit 0c48ba8

Browse files
committed
fix comparing wide raw pointers
1 parent 1118d94 commit 0c48ba8

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

src/operator.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,24 @@ impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
4444
}
4545

4646
Lt | Le | Gt | Ge => {
47-
// Just compare the integers.
48-
let left = left.to_scalar()?.to_bits(left.layout.size)?;
49-
let right = right.to_scalar()?.to_bits(right.layout.size)?;
47+
// Just compare the bits. ScalarPairs are compared lexicographically.
48+
// We thus always compare pairs and simply fill scalars up with 0.
49+
let left = match **left {
50+
Immediate::Scalar(l) => (l.check_init()?.to_bits(left.layout.size)?, 0),
51+
Immediate::ScalarPair(l1, l2) =>
52+
(
53+
l1.check_init()?.to_bits(self.pointer_size())?,
54+
l2.check_init()?.to_bits(self.pointer_size())?,
55+
),
56+
};
57+
let right = match **right {
58+
Immediate::Scalar(r) => (r.check_init()?.to_bits(right.layout.size)?, 0),
59+
Immediate::ScalarPair(r1, r2) =>
60+
(
61+
r1.check_init()?.to_bits(self.pointer_size())?,
62+
r2.check_init()?.to_bits(self.pointer_size())?,
63+
),
64+
};
5065
let res = match bin_op {
5166
Lt => left < right,
5267
Le => left <= right,

tests/pass/issue-96169-1.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// run-rustfix
2+
#![allow(unused)]
3+
fn a() -> usize { 0 }
4+
//~^ ERROR return types are denoted using `->`
5+
6+
fn bar(_: u32) {}
7+
8+
fn baz() -> *const dyn Fn(u32) { unimplemented!() }
9+
10+
fn foo() {
11+
match () {
12+
_ if baz() == &bar as &dyn Fn(u32) => (),
13+
() => (),
14+
}
15+
}
16+
17+
fn main() {
18+
}

tests/pass/pointers.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::mem::transmute;
2+
13
fn one_line_ref() -> i16 {
24
*&1
35
}
@@ -48,6 +50,27 @@ fn dangling_pointer() -> *const i32 {
4850
&b.0 as *const i32
4951
}
5052

53+
fn wide_ptr_ops() {
54+
let a: *const dyn Send = &1 as &dyn Send;
55+
let b: *const dyn Send = &1 as &dyn Send;
56+
let _val = a == b;
57+
let _val = a != b;
58+
let _val = a < b;
59+
let _val = a <= b;
60+
let _val = a > b;
61+
let _val = a >= b;
62+
63+
let a: *const [u8] = unsafe { transmute((1usize, 1usize)) };
64+
let b: *const [u8] = unsafe { transmute((1usize, 2usize)) };
65+
// confirmed with rustc.
66+
assert!(!(a == b));
67+
assert!(a != b);
68+
assert!(a <= b);
69+
assert!(a < b);
70+
assert!(!(a >= b));
71+
assert!(!(a > b));
72+
}
73+
5174
fn main() {
5275
assert_eq!(one_line_ref(), 1);
5376
assert_eq!(basic_ref(), 1);
@@ -91,4 +114,6 @@ fn main() {
91114
assert!(dangling > 2);
92115
assert!(dangling > 3);
93116
assert!(dangling >= 4);
117+
118+
wide_ptr_ops();
94119
}

0 commit comments

Comments
 (0)