Skip to content

Commit c58e09f

Browse files
committed
Auto merge of #68778 - RalfJung:raw-addr-of, r=eddyb
add raw-addr-of variant to mir_raw_fat_ptr As suggested at #48300 (comment) r? @eddyb
2 parents 0d34a87 + ee60158 commit c58e09f

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

src/test/ui/mir/mir_raw_fat_ptr.rs

+58-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// run-pass
22
// check raw fat pointer ops in mir
33
// FIXME: please improve this when we get monomorphization support
4+
#![feature(raw_ref_op)]
45

56
use std::mem;
67

@@ -104,7 +105,7 @@ impl<T> Foo for T {
104105

105106
struct S<T:?Sized>(u32, T);
106107

107-
fn main() {
108+
fn main_ref() {
108109
let array = [0,1,2,3,4];
109110
let array2 = [5,6,7,8,9];
110111

@@ -156,3 +157,59 @@ fn main() {
156157
assert!(simple_eq(&0u8 as *const _, &0u8 as *const _));
157158
assert!(!simple_eq(&0u8 as *const _, &1u8 as *const _));
158159
}
160+
161+
// similar to above, but using &raw
162+
fn main_raw() {
163+
let array = [0,1,2,3,4];
164+
let array2 = [5,6,7,8,9];
165+
166+
// fat ptr comparison: addr then extra
167+
168+
// check ordering for arrays
169+
let mut ptrs: Vec<*const [u8]> = vec![
170+
&raw const array[0..0], &raw const array[0..1], &raw const array, &raw const array[1..]
171+
];
172+
173+
let array_addr = &raw const array as *const u8 as usize;
174+
let array2_addr = &raw const array2 as *const u8 as usize;
175+
if array2_addr < array_addr {
176+
ptrs.insert(0, &raw const array2);
177+
} else {
178+
ptrs.push(&raw const array2);
179+
}
180+
assert_inorder(&ptrs, compare_au8);
181+
182+
let u8_ = (0u8, 1u8);
183+
let u32_ = (4u32, 5u32);
184+
185+
// check ordering for ptrs
186+
let buf: &mut [*const dyn Foo] = &mut [
187+
&raw const u8_, &raw const u8_.0,
188+
&raw const u32_, &raw const u32_.0,
189+
];
190+
buf.sort_by(|u,v| {
191+
let u : [*const (); 2] = unsafe { mem::transmute(*u) };
192+
let v : [*const (); 2] = unsafe { mem::transmute(*v) };
193+
u.cmp(&v)
194+
});
195+
assert_inorder(buf, compare_foo);
196+
197+
// check ordering for structs containing arrays
198+
let ss: (S<[u8; 2]>,
199+
S<[u8; 3]>,
200+
S<[u8; 2]>) = (
201+
S(7, [8, 9]),
202+
S(10, [11, 12, 13]),
203+
S(4, [5, 6])
204+
);
205+
assert_inorder(&[
206+
&raw const ss.0 as *const S<[u8]>,
207+
&raw const ss.1 as *const S<[u8]>,
208+
&raw const ss.2 as *const S<[u8]>
209+
], compare_su8);
210+
}
211+
212+
fn main() {
213+
main_ref();
214+
main_raw();
215+
}

0 commit comments

Comments
 (0)