Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add raw-addr-of variant to mir_raw_fat_ptr #68778

Merged
merged 1 commit into from
Feb 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}