]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/const-int-unchecked.rs
Rollup merge of #72279 - RalfJung:raw-ref-macros, r=nikomatsakis
[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 const SHL_U16: u16 = unsafe { intrinsics::unchecked_shl(5_u16, 16) };
18 //~^ ERROR any use of this value will cause an error
19 const SHL_U32: u32 = unsafe { intrinsics::unchecked_shl(5_u32, 32) };
20 //~^ ERROR any use of this value will cause an error
21 const SHL_U64: u64 = unsafe { intrinsics::unchecked_shl(5_u64, 64) };
22 //~^ ERROR any use of this value will cause an error
23 const SHL_U128: u128 = unsafe { intrinsics::unchecked_shl(5_u128, 128) };
24 //~^ ERROR any use of this value will cause an error
25
26 // signed types:
27
28 const SHL_I8: i8 = unsafe { intrinsics::unchecked_shl(5_i8, 8) };
29 //~^ ERROR any use of this value will cause an error
30 const SHL_I16: i16 = unsafe { intrinsics::unchecked_shl(5_16, 16) };
31 //~^ ERROR any use of this value will cause an error
32 const SHL_I32: i32 = unsafe { intrinsics::unchecked_shl(5_i32, 32) };
33 //~^ ERROR any use of this value will cause an error
34 const SHL_I64: i64 = unsafe { intrinsics::unchecked_shl(5_i64, 64) };
35 //~^ ERROR any use of this value will cause an error
36 const SHL_I128: i128 = unsafe { intrinsics::unchecked_shl(5_i128, 128) };
37 //~^ ERROR any use of this value will cause an error
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 any use of this value will cause an error
43 const SHL_I16_NEG: i16 = unsafe { intrinsics::unchecked_shl(5_16, -1) };
44 //~^ ERROR any use of this value will cause an error
45 const SHL_I32_NEG: i32 = unsafe { intrinsics::unchecked_shl(5_i32, -1) };
46 //~^ ERROR any use of this value will cause an error
47 const SHL_I64_NEG: i64 = unsafe { intrinsics::unchecked_shl(5_i64, -1) };
48 //~^ ERROR any use of this value will cause an error
49 const SHL_I128_NEG: i128 = unsafe { intrinsics::unchecked_shl(5_i128, -1) };
50 //~^ ERROR any use of this value will cause an error
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 any use of this value will cause an error
57 const SHL_I16_NEG_RANDOM: i16 = unsafe { intrinsics::unchecked_shl(5_16, -13) };
58 //~^ ERROR any use of this value will cause an error
59 const SHL_I32_NEG_RANDOM: i32 = unsafe { intrinsics::unchecked_shl(5_i32, -25) };
60 //~^ ERROR any use of this value will cause an error
61 const SHL_I64_NEG_RANDOM: i64 = unsafe { intrinsics::unchecked_shl(5_i64, -30) };
62 //~^ ERROR any use of this value will cause an error
63 const SHL_I128_NEG_RANDOM: i128 = unsafe { intrinsics::unchecked_shl(5_i128, -93) };
64 //~^ ERROR any use of this value will cause an error
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 any use of this value will cause an error
72 const SHR_U16: u16 = unsafe { intrinsics::unchecked_shr(5_u16, 16) };
73 //~^ ERROR any use of this value will cause an error
74 const SHR_U32: u32 = unsafe { intrinsics::unchecked_shr(5_u32, 32) };
75 //~^ ERROR any use of this value will cause an error
76 const SHR_U64: u64 = unsafe { intrinsics::unchecked_shr(5_u64, 64) };
77 //~^ ERROR any use of this value will cause an error
78 const SHR_U128: u128 = unsafe { intrinsics::unchecked_shr(5_u128, 128) };
79 //~^ ERROR any use of this value will cause an error
80
81 // signed types:
82
83 const SHR_I8: i8 = unsafe { intrinsics::unchecked_shr(5_i8, 8) };
84 //~^ ERROR any use of this value will cause an error
85 const SHR_I16: i16 = unsafe { intrinsics::unchecked_shr(5_16, 16) };
86 //~^ ERROR any use of this value will cause an error
87 const SHR_I32: i32 = unsafe { intrinsics::unchecked_shr(5_i32, 32) };
88 //~^ ERROR any use of this value will cause an error
89 const SHR_I64: i64 = unsafe { intrinsics::unchecked_shr(5_i64, 64) };
90 //~^ ERROR any use of this value will cause an error
91 const SHR_I128: i128 = unsafe { intrinsics::unchecked_shr(5_i128, 128) };
92 //~^ ERROR any use of this value will cause an error
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 any use of this value will cause an error
98 const SHR_I16_NEG: i16 = unsafe { intrinsics::unchecked_shr(5_16, -1) };
99 //~^ ERROR any use of this value will cause an error
100 const SHR_I32_NEG: i32 = unsafe { intrinsics::unchecked_shr(5_i32, -1) };
101 //~^ ERROR any use of this value will cause an error
102 const SHR_I64_NEG: i64 = unsafe { intrinsics::unchecked_shr(5_i64, -1) };
103 //~^ ERROR any use of this value will cause an error
104 const SHR_I128_NEG: i128 = unsafe { intrinsics::unchecked_shr(5_i128, -1) };
105 //~^ ERROR any use of this value will cause an error
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 any use of this value will cause an error
112 const SHR_I16_NEG_RANDOM: i16 = unsafe { intrinsics::unchecked_shr(5_16, -13) };
113 //~^ ERROR any use of this value will cause an error
114 const SHR_I32_NEG_RANDOM: i32 = unsafe { intrinsics::unchecked_shr(5_i32, -25) };
115 //~^ ERROR any use of this value will cause an error
116 const SHR_I64_NEG_RANDOM: i64 = unsafe { intrinsics::unchecked_shr(5_i64, -30) };
117 //~^ ERROR any use of this value will cause an error
118 const SHR_I128_NEG_RANDOM: i128 = unsafe { intrinsics::unchecked_shr(5_i128, -93) };
119 //~^ ERROR any use of this value will cause an error
120
121 // Other arithmetic functions:
122
123 const _: u16 = unsafe { std::intrinsics::unchecked_add(40000u16, 30000) };
124 //~^ ERROR any use of this value will cause an error
125
126 const _: u32 = unsafe { std::intrinsics::unchecked_sub(14u32, 22) };
127 //~^ ERROR any use of this value will cause an error
128
129 const _: u16 = unsafe { std::intrinsics::unchecked_mul(300u16, 250u16) };
130 //~^ ERROR any use of this value will cause an error
131
132 const _: i32 = unsafe { std::intrinsics::unchecked_div(1, 0) };
133 //~^ ERROR any use of this value will cause an error
134 const _: i32 = unsafe { std::intrinsics::unchecked_div(i32::MIN, -1) };
135 //~^ ERROR any use of this value will cause an error
136
137 const _: i32 = unsafe { std::intrinsics::unchecked_rem(1, 0) };
138 //~^ ERROR any use of this value will cause an error
139 const _: i32 = unsafe { std::intrinsics::unchecked_rem(i32::MIN, -1) };
140 //~^ ERROR any use of this value will cause an error
141
142 fn main() {}