]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/cast-rfc0401.rs
Const-propagate casts
[rust.git] / src / test / run-pass / cast-rfc0401.rs
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.
4 //
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.
10
11 #![allow(dead_code)]
12
13 use std::vec;
14
15 enum Simple {
16     A,
17     B,
18     C
19 }
20
21 enum Valued {
22     H8=163,
23     Z=0,
24     X=256,
25     H7=67,
26 }
27
28 enum ValuedSigned {
29     M1=-1,
30     P1=1
31 }
32
33 fn main()
34 {
35     // coercion-cast
36     let mut it = vec![137].into_iter();
37     let itr: &mut vec::IntoIter<u32> = &mut it;
38     assert_eq!((itr as &mut Iterator<Item=u32>).next(), Some(137));
39     assert_eq!((itr as &mut Iterator<Item=u32>).next(), None);
40
41     assert_eq!(Some(4u32) as Option<u32>, Some(4u32));
42     assert_eq!((1u32,2u32) as (u32,u32), (1,2));
43
44     // this isn't prim-int-cast. Check that it works.
45     assert_eq!(false as bool, false);
46     assert_eq!(true as bool, true);
47
48     // numeric-cast
49     let l: u64 = 0x8090a0b0c0d0e0f0;
50     let lsz: usize = l as usize;
51     assert_eq!(l as u32, 0xc0d0e0f0);
52
53     // numeric-cast
54     assert_eq!(l as u8, 0xf0);
55     assert_eq!(l as i8,-0x10);
56     assert_eq!(l as u32, 0xc0d0e0f0);
57     assert_eq!(l as u32 as usize as u32, l as u32);
58     assert_eq!(l as i32,-0x3f2f1f10);
59     assert_eq!(l as i32 as isize as i32, l as i32);
60     assert_eq!(l as i64,-0x7f6f5f4f3f2f1f10);
61
62     assert_eq!(0 as f64, 0f64);
63     assert_eq!(1 as f64, 1f64);
64
65     assert_eq!(l as f64, 9264081114510712022f64);
66
67     assert_eq!(l as i64 as f64, -9182662959198838444f64);
68 //  float overflow : needs fixing
69 //  assert_eq!(l as f32 as i64 as u64, 9264082620822882088u64);
70 //  assert_eq!(l as i64 as f32 as i64, 9182664080220408446i64);
71
72     assert_eq!(4294967040f32 as u32, 0xffffff00u32);
73     assert_eq!(1.844674407370955e19f64 as u64, 0xfffffffffffff800u64);
74
75     assert_eq!(9.223372036854775e18f64 as i64, 0x7ffffffffffffc00i64);
76     assert_eq!(-9.223372036854776e18f64 as i64, 0x8000000000000000u64 as i64);
77
78     // addr-ptr-cast/ptr-addr-cast (thin ptr)
79     let p: *const [u8; 1] = lsz as *const [u8; 1];
80     assert_eq!(p as usize, lsz);
81
82     // ptr-ptr-cast (thin ptr)
83     let w: *const () = p as *const ();
84     assert_eq!(w as usize, lsz);
85
86     // ptr-ptr-cast (fat->thin)
87     let u: *const [u8] = unsafe{&*p};
88     assert_eq!(u as *const u8, p as *const u8);
89     assert_eq!(u as *const u16, p as *const u16);
90
91     // ptr-ptr-cast (Length vtables)
92     let mut l : [u8; 2] = [0,1];
93     let w: *mut [u16; 2] = &mut l as *mut [u8; 2] as *mut _;
94     let w: *mut [u16] = unsafe {&mut *w};
95     let w_u8 : *const [u8] = w as *const [u8];
96     assert_eq!(unsafe{&*w_u8}, &l);
97
98     let s: *mut str = w as *mut str;
99     let l_via_str = unsafe{&*(s as *const [u8])};
100     assert_eq!(&l, l_via_str);
101
102     // ptr-ptr-cast (Length vtables, check length is preserved)
103     let l: [[u8; 3]; 2] = [[3, 2, 6], [4, 5, 1]];
104     let p: *const [[u8; 3]] = &l;
105     let p: &[[u8; 2]] = unsafe {&*(p as *const [[u8; 2]])};
106     assert_eq!(p, [[3, 2], [6, 4]]);
107
108     // enum-cast
109     assert_eq!(Simple::A as u8, 0);
110     assert_eq!(Simple::B as u8, 1);
111
112     assert_eq!(Valued::H8 as i8, -93);
113     assert_eq!(Valued::H7 as i8, 67);
114     assert_eq!(Valued::Z as i8, 0);
115
116     assert_eq!(Valued::H8 as u8, 163);
117     assert_eq!(Valued::H7 as u8, 67);
118     assert_eq!(Valued::Z as u8, 0);
119
120     assert_eq!(Valued::H8 as u16, 163);
121     assert_eq!(Valued::Z as u16, 0);
122     assert_eq!(Valued::H8 as u16, 163);
123     assert_eq!(Valued::Z as u16, 0);
124
125     assert_eq!(ValuedSigned::M1 as u16, 65535);
126     assert_eq!(ValuedSigned::M1 as i16, -1);
127     assert_eq!(ValuedSigned::P1 as u16, 1);
128     assert_eq!(ValuedSigned::P1 as i16, 1);
129
130     // prim-int-cast
131     assert_eq!(false as u16, 0);
132     assert_eq!(true as u16, 1);
133     assert_eq!(false as i64, 0);
134     assert_eq!(true as i64, 1);
135     assert_eq!('a' as u32, 0x61);
136     assert_eq!('a' as u16, 0x61);
137     assert_eq!('a' as u8, 0x61);
138     assert_eq!('א' as u8, 0xd0);
139     assert_eq!('א' as u16, 0x5d0);
140     assert_eq!('א' as u32, 0x5d0);
141     assert_eq!('🐵' as u8, 0x35);
142     assert_eq!('🐵' as u16, 0xf435);
143     assert_eq!('🐵' as u32, 0x1f435);
144     assert_eq!('英' as i16, -0x7d0f);
145     assert_eq!('英' as u16, 0x82f1);
146
147     // u8-char-cast
148     assert_eq!(0x61 as char, 'a');
149     assert_eq!(0u8 as char, '\0');
150     assert_eq!(0xd7 as char, '×');
151
152     // array-ptr-cast
153     let x = [1,2,3];
154     let first : *const u32 = &x[0];
155
156     assert_eq!(first, &x as *const _);
157     assert_eq!(first, &x as *const u32);
158
159     // fptr-addr-cast
160     fn foo() {
161         println!("foo!");
162     }
163     fn bar() {
164         println!("bar!");
165     }
166
167     assert!(foo as usize != bar as usize);
168
169     // Taking a few bits of a function's address is totally pointless and we detect that
170     // Disabling the lint to ensure that the assertion can still be run
171     #[allow(const_err)]
172     {
173         assert_eq!(foo as i16, foo as usize as i16);
174     }
175
176     // fptr-ptr-cast
177
178     assert_eq!(foo as *const u8 as usize, foo as usize);
179     assert!(foo as *const u32 != first);
180 }
181 fn foo() { }