Skip to content

Commit

Permalink
Auto merge of #68778 - RalfJung:raw-addr-of, r=eddyb
Browse files Browse the repository at this point in the history
add raw-addr-of variant to mir_raw_fat_ptr

As suggested at #48300 (comment)

r? @eddyb
  • Loading branch information
bors committed Feb 3, 2020
2 parents 0d34a87 + ee60158 commit c58e09f
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion src/test/ui/mir/mir_raw_fat_ptr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// run-pass
// check raw fat pointer ops in mir
// FIXME: please improve this when we get monomorphization support
#![feature(raw_ref_op)]

use std::mem;

Expand Down Expand Up @@ -104,7 +105,7 @@ impl<T> Foo for T {

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

fn main() {
fn main_ref() {
let array = [0,1,2,3,4];
let array2 = [5,6,7,8,9];

Expand Down Expand Up @@ -156,3 +157,59 @@ fn main() {
assert!(simple_eq(&0u8 as *const _, &0u8 as *const _));
assert!(!simple_eq(&0u8 as *const _, &1u8 as *const _));
}

// similar to above, but using &raw
fn main_raw() {
let array = [0,1,2,3,4];
let array2 = [5,6,7,8,9];

// fat ptr comparison: addr then extra

// check ordering for arrays
let mut ptrs: Vec<*const [u8]> = vec![
&raw const array[0..0], &raw const array[0..1], &raw const array, &raw const array[1..]
];

let array_addr = &raw const array as *const u8 as usize;
let array2_addr = &raw const array2 as *const u8 as usize;
if array2_addr < array_addr {
ptrs.insert(0, &raw const array2);
} else {
ptrs.push(&raw const array2);
}
assert_inorder(&ptrs, compare_au8);

let u8_ = (0u8, 1u8);
let u32_ = (4u32, 5u32);

// check ordering for ptrs
let buf: &mut [*const dyn Foo] = &mut [
&raw const u8_, &raw const u8_.0,
&raw const u32_, &raw const u32_.0,
];
buf.sort_by(|u,v| {
let u : [*const (); 2] = unsafe { mem::transmute(*u) };
let v : [*const (); 2] = unsafe { mem::transmute(*v) };
u.cmp(&v)
});
assert_inorder(buf, compare_foo);

// check ordering for structs containing arrays
let ss: (S<[u8; 2]>,
S<[u8; 3]>,
S<[u8; 2]>) = (
S(7, [8, 9]),
S(10, [11, 12, 13]),
S(4, [5, 6])
);
assert_inorder(&[
&raw const ss.0 as *const S<[u8]>,
&raw const ss.1 as *const S<[u8]>,
&raw const ss.2 as *const S<[u8]>
], compare_su8);
}

fn main() {
main_ref();
main_raw();
}

0 comments on commit c58e09f

Please sign in to comment.