1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #![feature(rustc_attrs)]
13 // ignore-pretty : (#23623) problems when ending with // comments
15 // check raw fat pointer ops in mir
16 // FIXME: please improve this when we get monomorphization support
20 #[derive(Debug, PartialEq, Eq)]
21 struct ComparisonResults {
30 const LT: ComparisonResults = ComparisonResults {
39 const EQ: ComparisonResults = ComparisonResults {
48 const GT: ComparisonResults = ComparisonResults {
58 fn compare_su8(a: *const S<[u8]>, b: *const S<[u8]>) -> ComparisonResults {
70 fn compare_au8(a: *const [u8], b: *const [u8]) -> ComparisonResults {
82 fn compare_foo<'a>(a: *const (Foo+'a), b: *const (Foo+'a)) -> ComparisonResults {
94 fn simple_eq<'a>(a: *const (Foo+'a), b: *const (Foo+'a)) -> bool {
99 fn assert_inorder<T: Copy>(a: &[T],
100 compare: fn(T, T) -> ComparisonResults) {
101 for i in 0..a.len() {
102 for j in 0..a.len() {
103 let cres = compare(a[i], a[j]);
105 assert_eq!(cres, LT);
107 assert_eq!(cres, EQ);
109 assert_eq!(cres, GT);
115 trait Foo { fn foo(&self) -> usize; }
117 fn foo(&self) -> usize {
122 struct S<T:?Sized>(u32, T);
124 #[rustc_no_mir] // FIXME #27840 MIR can't do rvalue promotion yet.
126 let array = [0,1,2,3,4];
127 let array2 = [5,6,7,8,9];
129 // fat ptr comparison: addr then extra
131 // check ordering for arrays
132 let mut ptrs: Vec<*const [u8]> = vec![
133 &array[0..0], &array[0..1], &array, &array[1..]
136 let array_addr = &array as *const [u8] as *const u8 as usize;
137 let array2_addr = &array2 as *const [u8] as *const u8 as usize;
138 if array2_addr < array_addr {
139 ptrs.insert(0, &array2);
143 assert_inorder(&ptrs, compare_au8);
145 let u8_ = (0u8, 1u8);
146 let u32_ = (4u32, 5u32);
148 // check ordering for ptrs
149 let buf: &mut [*const Foo] = &mut [
154 let u : [*const (); 2] = unsafe { mem::transmute(*u) };
155 let v : [*const (); 2] = unsafe { mem::transmute(*v) };
158 assert_inorder(buf, compare_foo);
160 // check ordering for structs containing arrays
169 &ss.0 as *const S<[u8]>,
170 &ss.1 as *const S<[u8]>,
171 &ss.2 as *const S<[u8]>
174 assert!(simple_eq(&0u8 as *const _, &0u8 as *const _));
175 assert!(!simple_eq(&0u8 as *const _, &1u8 as *const _));