2018-08-30 12:18:55 +00:00
|
|
|
//@ run-pass
|
2015-11-09 00:16:19 +00:00
|
|
|
// check raw fat pointer ops in mir
|
|
|
|
// FIXME: please improve this when we get monomorphization support
|
2023-11-09 17:13:52 +00:00
|
|
|
|
|
|
|
#![allow(ambiguous_wide_pointer_comparisons)]
|
2015-11-09 00:16:19 +00:00
|
|
|
|
|
|
|
use std::mem;
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
struct ComparisonResults {
|
|
|
|
lt: bool,
|
|
|
|
le: bool,
|
|
|
|
gt: bool,
|
|
|
|
ge: bool,
|
|
|
|
eq: bool,
|
|
|
|
ne: bool
|
|
|
|
}
|
|
|
|
|
|
|
|
const LT: ComparisonResults = ComparisonResults {
|
|
|
|
lt: true,
|
|
|
|
le: true,
|
|
|
|
gt: false,
|
|
|
|
ge: false,
|
|
|
|
eq: false,
|
|
|
|
ne: true
|
|
|
|
};
|
|
|
|
|
|
|
|
const EQ: ComparisonResults = ComparisonResults {
|
|
|
|
lt: false,
|
|
|
|
le: true,
|
|
|
|
gt: false,
|
|
|
|
ge: true,
|
|
|
|
eq: true,
|
|
|
|
ne: false
|
|
|
|
};
|
|
|
|
|
|
|
|
const GT: ComparisonResults = ComparisonResults {
|
|
|
|
lt: false,
|
|
|
|
le: false,
|
|
|
|
gt: true,
|
|
|
|
ge: true,
|
|
|
|
eq: false,
|
|
|
|
ne: true
|
|
|
|
};
|
|
|
|
|
|
|
|
fn compare_su8(a: *const S<[u8]>, b: *const S<[u8]>) -> ComparisonResults {
|
|
|
|
ComparisonResults {
|
|
|
|
lt: a < b,
|
|
|
|
le: a <= b,
|
|
|
|
gt: a > b,
|
|
|
|
ge: a >= b,
|
|
|
|
eq: a == b,
|
|
|
|
ne: a != b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compare_au8(a: *const [u8], b: *const [u8]) -> ComparisonResults {
|
|
|
|
ComparisonResults {
|
|
|
|
lt: a < b,
|
|
|
|
le: a <= b,
|
|
|
|
gt: a > b,
|
|
|
|
ge: a >= b,
|
|
|
|
eq: a == b,
|
|
|
|
ne: a != b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 18:47:21 +00:00
|
|
|
fn compare_foo<'a>(a: *const (dyn Foo+'a), b: *const (dyn Foo+'a)) -> ComparisonResults {
|
2015-11-09 00:16:19 +00:00
|
|
|
ComparisonResults {
|
|
|
|
lt: a < b,
|
|
|
|
le: a <= b,
|
|
|
|
gt: a > b,
|
|
|
|
ge: a >= b,
|
|
|
|
eq: a == b,
|
|
|
|
ne: a != b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 18:47:21 +00:00
|
|
|
fn simple_eq<'a>(a: *const (dyn Foo+'a), b: *const (dyn Foo+'a)) -> bool {
|
2015-11-09 00:16:19 +00:00
|
|
|
let result = a == b;
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
fn assert_inorder<T: Copy>(a: &[T],
|
|
|
|
compare: fn(T, T) -> ComparisonResults) {
|
|
|
|
for i in 0..a.len() {
|
|
|
|
for j in 0..a.len() {
|
|
|
|
let cres = compare(a[i], a[j]);
|
|
|
|
if i < j {
|
|
|
|
assert_eq!(cres, LT);
|
|
|
|
} else if i == j {
|
|
|
|
assert_eq!(cres, EQ);
|
|
|
|
} else {
|
|
|
|
assert_eq!(cres, GT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-07 02:42:01 +00:00
|
|
|
trait Foo { fn foo(&self) -> usize; } //~ WARN method `foo` is never used
|
2015-11-09 00:16:19 +00:00
|
|
|
impl<T> Foo for T {
|
|
|
|
fn foo(&self) -> usize {
|
|
|
|
mem::size_of::<T>()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-27 22:11:58 +00:00
|
|
|
#[allow(dead_code)]
|
2015-11-09 00:16:19 +00:00
|
|
|
struct S<T:?Sized>(u32, T);
|
|
|
|
|
2020-02-02 19:51:24 +00:00
|
|
|
fn main_ref() {
|
2015-11-09 00:16:19 +00:00
|
|
|
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![
|
|
|
|
&array[0..0], &array[0..1], &array, &array[1..]
|
|
|
|
];
|
|
|
|
|
|
|
|
let array_addr = &array as *const [u8] as *const u8 as usize;
|
|
|
|
let array2_addr = &array2 as *const [u8] as *const u8 as usize;
|
|
|
|
if array2_addr < array_addr {
|
|
|
|
ptrs.insert(0, &array2);
|
|
|
|
} else {
|
|
|
|
ptrs.push(&array2);
|
|
|
|
}
|
|
|
|
assert_inorder(&ptrs, compare_au8);
|
|
|
|
|
|
|
|
let u8_ = (0u8, 1u8);
|
|
|
|
let u32_ = (4u32, 5u32);
|
|
|
|
|
|
|
|
// check ordering for ptrs
|
2019-05-28 18:47:21 +00:00
|
|
|
let buf: &mut [*const dyn Foo] = &mut [
|
2015-11-09 00:16:19 +00:00
|
|
|
&u8_, &u8_.0,
|
|
|
|
&u32_, &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(&[
|
|
|
|
&ss.0 as *const S<[u8]>,
|
|
|
|
&ss.1 as *const S<[u8]>,
|
|
|
|
&ss.2 as *const S<[u8]>
|
|
|
|
], compare_su8);
|
|
|
|
|
|
|
|
assert!(simple_eq(&0u8 as *const _, &0u8 as *const _));
|
|
|
|
assert!(!simple_eq(&0u8 as *const _, &1u8 as *const _));
|
|
|
|
}
|
2020-02-02 19:51:24 +00:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|