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