]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/const-int-unchecked.rs
Auto merge of #83572 - pkubaj:patch-1, r=nagisa
[rust.git] / src / test / ui / consts / const-int-unchecked.rs
1 #![feature(core_intrinsics)]
2 #![feature(const_int_unchecked_arith)]
3
4 use std::intrinsics;
5
6 // The documentation of `unchecked_shl` states that it:
7 //
8 // Performs an unchecked left shift, resulting in undefined behavior when
9 // y < 0 or y >= N, where N is the width of T in bits.
10 //
11 // So we check this for a few `y`.
12
13 // unsigned types:
14
15 const SHL_U8: u8 = unsafe { intrinsics::unchecked_shl(5_u8, 8) };
16 //~^ ERROR any use of this value will cause an error
17 //~| WARN this was previously accepted by the compiler but is being phased out
18 const SHL_U16: u16 = unsafe { intrinsics::unchecked_shl(5_u16, 16) };
19 //~^ ERROR any use of this value will cause an error
20 //~| WARN this was previously accepted by the compiler but is being phased out
21 const SHL_U32: u32 = unsafe { intrinsics::unchecked_shl(5_u32, 32) };
22 //~^ ERROR any use of this value will cause an error
23 //~| WARN this was previously accepted by the compiler but is being phased out
24 const SHL_U64: u64 = unsafe { intrinsics::unchecked_shl(5_u64, 64) };
25 //~^ ERROR any use of this value will cause an error
26 //~| WARN this was previously accepted by the compiler but is being phased out
27 const SHL_U128: u128 = unsafe { intrinsics::unchecked_shl(5_u128, 128) };
28 //~^ ERROR any use of this value will cause an error
29 //~| WARN this was previously accepted by the compiler but is being phased out
30
31 // signed types:
32
33 const SHL_I8: i8 = unsafe { intrinsics::unchecked_shl(5_i8, 8) };
34 //~^ ERROR any use of this value will cause an error
35 //~| WARN this was previously accepted by the compiler but is being phased out
36 const SHL_I16: i16 = unsafe { intrinsics::unchecked_shl(5_16, 16) };
37 //~^ ERROR any use of this value will cause an error
38 //~| WARN this was previously accepted by the compiler but is being phased out
39 const SHL_I32: i32 = unsafe { intrinsics::unchecked_shl(5_i32, 32) };
40 //~^ ERROR any use of this value will cause an error
41 //~| WARN this was previously accepted by the compiler but is being phased out
42 const SHL_I64: i64 = unsafe { intrinsics::unchecked_shl(5_i64, 64) };
43 //~^ ERROR any use of this value will cause an error
44 //~| WARN this was previously accepted by the compiler but is being phased out
45 const SHL_I128: i128 = unsafe { intrinsics::unchecked_shl(5_i128, 128) };
46 //~^ ERROR any use of this value will cause an error
47 //~| WARN this was previously accepted by the compiler but is being phased out
48
49 // and make sure we capture y < 0:
50
51 const SHL_I8_NEG: i8 = unsafe { intrinsics::unchecked_shl(5_i8, -1) };
52 //~^ ERROR any use of this value will cause an error
53 //~| WARN this was previously accepted by the compiler but is being phased out
54 const SHL_I16_NEG: i16 = unsafe { intrinsics::unchecked_shl(5_16, -1) };
55 //~^ ERROR any use of this value will cause an error
56 //~| WARN this was previously accepted by the compiler but is being phased out
57 const SHL_I32_NEG: i32 = unsafe { intrinsics::unchecked_shl(5_i32, -1) };
58 //~^ ERROR any use of this value will cause an error
59 //~| WARN this was previously accepted by the compiler but is being phased out
60 const SHL_I64_NEG: i64 = unsafe { intrinsics::unchecked_shl(5_i64, -1) };
61 //~^ ERROR any use of this value will cause an error
62 //~| WARN this was previously accepted by the compiler but is being phased out
63 const SHL_I128_NEG: i128 = unsafe { intrinsics::unchecked_shl(5_i128, -1) };
64 //~^ ERROR any use of this value will cause an error
65 //~| WARN this was previously accepted by the compiler but is being phased out
66
67 // and that there's no special relation to the value -1 by picking some
68 // negative values at random:
69
70 const SHL_I8_NEG_RANDOM: i8 = unsafe { intrinsics::unchecked_shl(5_i8, -6) };
71 //~^ ERROR any use of this value will cause an error
72 //~| WARN this was previously accepted by the compiler but is being phased out
73 const SHL_I16_NEG_RANDOM: i16 = unsafe { intrinsics::unchecked_shl(5_16, -13) };
74 //~^ ERROR any use of this value will cause an error
75 //~| WARN this was previously accepted by the compiler but is being phased out
76 const SHL_I32_NEG_RANDOM: i32 = unsafe { intrinsics::unchecked_shl(5_i32, -25) };
77 //~^ ERROR any use of this value will cause an error
78 //~| WARN this was previously accepted by the compiler but is being phased out
79 const SHL_I64_NEG_RANDOM: i64 = unsafe { intrinsics::unchecked_shl(5_i64, -30) };
80 //~^ ERROR any use of this value will cause an error
81 //~| WARN this was previously accepted by the compiler but is being phased out
82 const SHL_I128_NEG_RANDOM: i128 = unsafe { intrinsics::unchecked_shl(5_i128, -93) };
83 //~^ ERROR any use of this value will cause an error
84 //~| WARN this was previously accepted by the compiler but is being phased out
85
86 // Repeat it all over for `unchecked_shr`
87
88 // unsigned types:
89
90 const SHR_U8: u8 = unsafe { intrinsics::unchecked_shr(5_u8, 8) };
91 //~^ ERROR any use of this value will cause an error
92 //~| WARN this was previously accepted by the compiler but is being phased out
93 const SHR_U16: u16 = unsafe { intrinsics::unchecked_shr(5_u16, 16) };
94 //~^ ERROR any use of this value will cause an error
95 //~| WARN this was previously accepted by the compiler but is being phased out
96 const SHR_U32: u32 = unsafe { intrinsics::unchecked_shr(5_u32, 32) };
97 //~^ ERROR any use of this value will cause an error
98 //~| WARN this was previously accepted by the compiler but is being phased out
99 const SHR_U64: u64 = unsafe { intrinsics::unchecked_shr(5_u64, 64) };
100 //~^ ERROR any use of this value will cause an error
101 //~| WARN this was previously accepted by the compiler but is being phased out
102 const SHR_U128: u128 = unsafe { intrinsics::unchecked_shr(5_u128, 128) };
103 //~^ ERROR any use of this value will cause an error
104 //~| WARN this was previously accepted by the compiler but is being phased out
105
106 // signed types:
107
108 const SHR_I8: i8 = unsafe { intrinsics::unchecked_shr(5_i8, 8) };
109 //~^ ERROR any use of this value will cause an error
110 //~| WARN this was previously accepted by the compiler but is being phased out
111 const SHR_I16: i16 = unsafe { intrinsics::unchecked_shr(5_16, 16) };
112 //~^ ERROR any use of this value will cause an error
113 //~| WARN this was previously accepted by the compiler but is being phased out
114 const SHR_I32: i32 = unsafe { intrinsics::unchecked_shr(5_i32, 32) };
115 //~^ ERROR any use of this value will cause an error
116 //~| WARN this was previously accepted by the compiler but is being phased out
117 const SHR_I64: i64 = unsafe { intrinsics::unchecked_shr(5_i64, 64) };
118 //~^ ERROR any use of this value will cause an error
119 //~| WARN this was previously accepted by the compiler but is being phased out
120 const SHR_I128: i128 = unsafe { intrinsics::unchecked_shr(5_i128, 128) };
121 //~^ ERROR any use of this value will cause an error
122 //~| WARN this was previously accepted by the compiler but is being phased out
123
124 // and make sure we capture y < 0:
125
126 const SHR_I8_NEG: i8 = unsafe { intrinsics::unchecked_shr(5_i8, -1) };
127 //~^ ERROR any use of this value will cause an error
128 //~| WARN this was previously accepted by the compiler but is being phased out
129 const SHR_I16_NEG: i16 = unsafe { intrinsics::unchecked_shr(5_16, -1) };
130 //~^ ERROR any use of this value will cause an error
131 //~| WARN this was previously accepted by the compiler but is being phased out
132 const SHR_I32_NEG: i32 = unsafe { intrinsics::unchecked_shr(5_i32, -1) };
133 //~^ ERROR any use of this value will cause an error
134 //~| WARN this was previously accepted by the compiler but is being phased out
135 const SHR_I64_NEG: i64 = unsafe { intrinsics::unchecked_shr(5_i64, -1) };
136 //~^ ERROR any use of this value will cause an error
137 //~| WARN this was previously accepted by the compiler but is being phased out
138 const SHR_I128_NEG: i128 = unsafe { intrinsics::unchecked_shr(5_i128, -1) };
139 //~^ ERROR any use of this value will cause an error
140 //~| WARN this was previously accepted by the compiler but is being phased out
141
142 // and that there's no special relation to the value -1 by picking some
143 // negative values at random:
144
145 const SHR_I8_NEG_RANDOM: i8 = unsafe { intrinsics::unchecked_shr(5_i8, -6) };
146 //~^ ERROR any use of this value will cause an error
147 //~| WARN this was previously accepted by the compiler but is being phased out
148 const SHR_I16_NEG_RANDOM: i16 = unsafe { intrinsics::unchecked_shr(5_16, -13) };
149 //~^ ERROR any use of this value will cause an error
150 //~| WARN this was previously accepted by the compiler but is being phased out
151 const SHR_I32_NEG_RANDOM: i32 = unsafe { intrinsics::unchecked_shr(5_i32, -25) };
152 //~^ ERROR any use of this value will cause an error
153 //~| WARN this was previously accepted by the compiler but is being phased out
154 const SHR_I64_NEG_RANDOM: i64 = unsafe { intrinsics::unchecked_shr(5_i64, -30) };
155 //~^ ERROR any use of this value will cause an error
156 //~| WARN this was previously accepted by the compiler but is being phased out
157 const SHR_I128_NEG_RANDOM: i128 = unsafe { intrinsics::unchecked_shr(5_i128, -93) };
158 //~^ ERROR any use of this value will cause an error
159 //~| WARN this was previously accepted by the compiler but is being phased out
160
161 // Other arithmetic functions:
162
163 const _: u16 = unsafe { std::intrinsics::unchecked_add(40000u16, 30000) };
164 //~^ ERROR any use of this value will cause an error
165 //~| WARN this was previously accepted by the compiler but is being phased out
166
167 const _: u32 = unsafe { std::intrinsics::unchecked_sub(14u32, 22) };
168 //~^ ERROR any use of this value will cause an error
169 //~| WARN this was previously accepted by the compiler but is being phased out
170
171 const _: u16 = unsafe { std::intrinsics::unchecked_mul(300u16, 250u16) };
172 //~^ ERROR any use of this value will cause an error
173 //~| WARN this was previously accepted by the compiler but is being phased out
174
175 const _: i32 = unsafe { std::intrinsics::unchecked_div(1, 0) };
176 //~^ ERROR any use of this value will cause an error
177 //~| WARN this was previously accepted by the compiler but is being phased out
178 const _: i32 = unsafe { std::intrinsics::unchecked_div(i32::MIN, -1) };
179 //~^ ERROR any use of this value will cause an error
180 //~| WARN this was previously accepted by the compiler but is being phased out
181
182 const _: i32 = unsafe { std::intrinsics::unchecked_rem(1, 0) };
183 //~^ ERROR any use of this value will cause an error
184 //~| WARN this was previously accepted by the compiler but is being phased out
185 const _: i32 = unsafe { std::intrinsics::unchecked_rem(i32::MIN, -1) };
186 //~^ ERROR any use of this value will cause an error
187 //~| WARN this was previously accepted by the compiler but is being phased out
188
189 // capture fault with zero value
190
191 const _: u32 = unsafe { std::intrinsics::ctlz_nonzero(0) };
192 //~^ ERROR any use of this value will cause an error
193 //~| WARN this was previously accepted by the compiler but is being phased out
194 const _: u32 = unsafe { std::intrinsics::cttz_nonzero(0) };
195 //~^ ERROR any use of this value will cause an error
196 //~| WARN this was previously accepted by the compiler but is being phased out
197
198 fn main() {}