]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/const-int-unchecked.rs
Do not suggest `let_else` if no bindings would be introduced
[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 evaluation of constant value failed
17 const SHL_U16: u16 = unsafe { intrinsics::unchecked_shl(5_u16, 16) };
18 //~^ ERROR evaluation of constant value failed
19 const SHL_U32: u32 = unsafe { intrinsics::unchecked_shl(5_u32, 32) };
20 //~^ ERROR evaluation of constant value failed
21 const SHL_U64: u64 = unsafe { intrinsics::unchecked_shl(5_u64, 64) };
22 //~^ ERROR evaluation of constant value failed
23 const SHL_U128: u128 = unsafe { intrinsics::unchecked_shl(5_u128, 128) };
24 //~^ ERROR evaluation of constant value failed
25
26 // signed types:
27
28 const SHL_I8: i8 = unsafe { intrinsics::unchecked_shl(5_i8, 8) };
29 //~^ ERROR evaluation of constant value failed
30 const SHL_I16: i16 = unsafe { intrinsics::unchecked_shl(5_16, 16) };
31 //~^ ERROR evaluation of constant value failed
32 const SHL_I32: i32 = unsafe { intrinsics::unchecked_shl(5_i32, 32) };
33 //~^ ERROR evaluation of constant value failed
34 const SHL_I64: i64 = unsafe { intrinsics::unchecked_shl(5_i64, 64) };
35 //~^ ERROR evaluation of constant value failed
36 const SHL_I128: i128 = unsafe { intrinsics::unchecked_shl(5_i128, 128) };
37 //~^ ERROR evaluation of constant value failed
38
39 // and make sure we capture y < 0:
40
41 const SHL_I8_NEG: i8 = unsafe { intrinsics::unchecked_shl(5_i8, -1) };
42 //~^ ERROR evaluation of constant value failed
43 const SHL_I16_NEG: i16 = unsafe { intrinsics::unchecked_shl(5_16, -1) };
44 //~^ ERROR evaluation of constant value failed
45 const SHL_I32_NEG: i32 = unsafe { intrinsics::unchecked_shl(5_i32, -1) };
46 //~^ ERROR evaluation of constant value failed
47 const SHL_I64_NEG: i64 = unsafe { intrinsics::unchecked_shl(5_i64, -1) };
48 //~^ ERROR evaluation of constant value failed
49 const SHL_I128_NEG: i128 = unsafe { intrinsics::unchecked_shl(5_i128, -1) };
50 //~^ ERROR evaluation of constant value failed
51
52 // and that there's no special relation to the value -1 by picking some
53 // negative values at random:
54
55 const SHL_I8_NEG_RANDOM: i8 = unsafe { intrinsics::unchecked_shl(5_i8, -6) };
56 //~^ ERROR evaluation of constant value failed
57 const SHL_I16_NEG_RANDOM: i16 = unsafe { intrinsics::unchecked_shl(5_16, -13) };
58 //~^ ERROR evaluation of constant value failed
59 const SHL_I32_NEG_RANDOM: i32 = unsafe { intrinsics::unchecked_shl(5_i32, -25) };
60 //~^ ERROR evaluation of constant value failed
61 const SHL_I64_NEG_RANDOM: i64 = unsafe { intrinsics::unchecked_shl(5_i64, -30) };
62 //~^ ERROR evaluation of constant value failed
63 const SHL_I128_NEG_RANDOM: i128 = unsafe { intrinsics::unchecked_shl(5_i128, -93) };
64 //~^ ERROR evaluation of constant value failed
65
66 // Repeat it all over for `unchecked_shr`
67
68 // unsigned types:
69
70 const SHR_U8: u8 = unsafe { intrinsics::unchecked_shr(5_u8, 8) };
71 //~^ ERROR evaluation of constant value failed
72 const SHR_U16: u16 = unsafe { intrinsics::unchecked_shr(5_u16, 16) };
73 //~^ ERROR evaluation of constant value failed
74 const SHR_U32: u32 = unsafe { intrinsics::unchecked_shr(5_u32, 32) };
75 //~^ ERROR evaluation of constant value failed
76 const SHR_U64: u64 = unsafe { intrinsics::unchecked_shr(5_u64, 64) };
77 //~^ ERROR evaluation of constant value failed
78 const SHR_U128: u128 = unsafe { intrinsics::unchecked_shr(5_u128, 128) };
79 //~^ ERROR evaluation of constant value failed
80
81 // signed types:
82
83 const SHR_I8: i8 = unsafe { intrinsics::unchecked_shr(5_i8, 8) };
84 //~^ ERROR evaluation of constant value failed
85 const SHR_I16: i16 = unsafe { intrinsics::unchecked_shr(5_16, 16) };
86 //~^ ERROR evaluation of constant value failed
87 const SHR_I32: i32 = unsafe { intrinsics::unchecked_shr(5_i32, 32) };
88 //~^ ERROR evaluation of constant value failed
89 const SHR_I64: i64 = unsafe { intrinsics::unchecked_shr(5_i64, 64) };
90 //~^ ERROR evaluation of constant value failed
91 const SHR_I128: i128 = unsafe { intrinsics::unchecked_shr(5_i128, 128) };
92 //~^ ERROR evaluation of constant value failed
93
94 // and make sure we capture y < 0:
95
96 const SHR_I8_NEG: i8 = unsafe { intrinsics::unchecked_shr(5_i8, -1) };
97 //~^ ERROR evaluation of constant value failed
98 const SHR_I16_NEG: i16 = unsafe { intrinsics::unchecked_shr(5_16, -1) };
99 //~^ ERROR evaluation of constant value failed
100 const SHR_I32_NEG: i32 = unsafe { intrinsics::unchecked_shr(5_i32, -1) };
101 //~^ ERROR evaluation of constant value failed
102 const SHR_I64_NEG: i64 = unsafe { intrinsics::unchecked_shr(5_i64, -1) };
103 //~^ ERROR evaluation of constant value failed
104 const SHR_I128_NEG: i128 = unsafe { intrinsics::unchecked_shr(5_i128, -1) };
105 //~^ ERROR evaluation of constant value failed
106
107 // and that there's no special relation to the value -1 by picking some
108 // negative values at random:
109
110 const SHR_I8_NEG_RANDOM: i8 = unsafe { intrinsics::unchecked_shr(5_i8, -6) };
111 //~^ ERROR evaluation of constant value failed
112 const SHR_I16_NEG_RANDOM: i16 = unsafe { intrinsics::unchecked_shr(5_16, -13) };
113 //~^ ERROR evaluation of constant value failed
114 const SHR_I32_NEG_RANDOM: i32 = unsafe { intrinsics::unchecked_shr(5_i32, -25) };
115 //~^ ERROR evaluation of constant value failed
116 const SHR_I64_NEG_RANDOM: i64 = unsafe { intrinsics::unchecked_shr(5_i64, -30) };
117 //~^ ERROR evaluation of constant value failed
118 const SHR_I128_NEG_RANDOM: i128 = unsafe { intrinsics::unchecked_shr(5_i128, -93) };
119 //~^ ERROR evaluation of constant value failed
120
121 // Other arithmetic functions:
122
123 const _: u16 = unsafe { std::intrinsics::unchecked_add(40000u16, 30000) };
124 //~^ ERROR evaluation of constant value failed
125
126 const _: u32 = unsafe { std::intrinsics::unchecked_sub(14u32, 22) };
127 //~^ ERROR evaluation of constant value failed
128
129 const _: u16 = unsafe { std::intrinsics::unchecked_mul(300u16, 250u16) };
130 //~^ ERROR evaluation of constant value failed
131
132 const _: i32 = unsafe { std::intrinsics::unchecked_div(1, 0) };
133 //~^ ERROR evaluation of constant value failed
134 const _: i32 = unsafe { std::intrinsics::unchecked_div(i32::MIN, -1) };
135 //~^ ERROR evaluation of constant value failed
136
137 const _: i32 = unsafe { std::intrinsics::unchecked_rem(1, 0) };
138 //~^ ERROR evaluation of constant value failed
139 const _: i32 = unsafe { std::intrinsics::unchecked_rem(i32::MIN, -1) };
140 //~^ ERROR evaluation of constant value failed
141
142 // capture fault with zero value
143
144 const _: u32 = unsafe { std::intrinsics::ctlz_nonzero(0) };
145 //~^ ERROR evaluation of constant value failed
146 const _: u32 = unsafe { std::intrinsics::cttz_nonzero(0) };
147 //~^ ERROR evaluation of constant value failed
148
149 fn main() {}