Skip to content

Commit 35d77fc

Browse files
committed
unchecked_{shl,shr}: extend const tests.
1 parent d85ec4a commit 35d77fc

File tree

2 files changed

+428
-15
lines changed

2 files changed

+428
-15
lines changed

src/test/ui/consts/const-int-unchecked.rs

+113-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,119 @@
22

33
use std::intrinsics;
44

5-
const SHR: u8 = unsafe { intrinsics::unchecked_shr(5_u8, 8) };
5+
// The documentation of `unchecked_shl` states that it:
6+
//
7+
// Performs an unchecked left shift, resulting in undefined behavior when
8+
// y < 0 or y >= N, where N is the width of T in bits.
9+
//
10+
// So we check this for a few `y`.
11+
12+
// unsigned types:
13+
14+
const SHL_U8: u8 = unsafe { intrinsics::unchecked_shl(5_u8, 8) };
15+
//~^ ERROR any use of this value will cause an error
16+
const SHL_U16: u16 = unsafe { intrinsics::unchecked_shl(5_u16, 16) };
17+
//~^ ERROR any use of this value will cause an error
18+
const SHL_U32: u32 = unsafe { intrinsics::unchecked_shl(5_u32, 32) };
19+
//~^ ERROR any use of this value will cause an error
20+
const SHL_U64: u64 = unsafe { intrinsics::unchecked_shl(5_u64, 64) };
21+
//~^ ERROR any use of this value will cause an error
22+
const SHL_U128: u128 = unsafe { intrinsics::unchecked_shl(5_u128, 128) };
23+
//~^ ERROR any use of this value will cause an error
24+
25+
// signed types:
26+
27+
const SHL_I8: i8 = unsafe { intrinsics::unchecked_shl(5_i8, 8) };
28+
//~^ ERROR any use of this value will cause an error
29+
const SHL_I16: i16 = unsafe { intrinsics::unchecked_shl(5_16, 16) };
30+
//~^ ERROR any use of this value will cause an error
31+
const SHL_I32: i32 = unsafe { intrinsics::unchecked_shl(5_i32, 32) };
32+
//~^ ERROR any use of this value will cause an error
33+
const SHL_I64: i64 = unsafe { intrinsics::unchecked_shl(5_i64, 64) };
34+
//~^ ERROR any use of this value will cause an error
35+
const SHL_I128: i128 = unsafe { intrinsics::unchecked_shl(5_i128, 128) };
36+
//~^ ERROR any use of this value will cause an error
37+
38+
// and make sure we capture y < 0:
39+
40+
const SHL_I8_NEG: i8 = unsafe { intrinsics::unchecked_shl(5_i8, -1) };
41+
//~^ ERROR any use of this value will cause an error
42+
const SHL_I16_NEG: i16 = unsafe { intrinsics::unchecked_shl(5_16, -1) };
43+
//~^ ERROR any use of this value will cause an error
44+
const SHL_I32_NEG: i32 = unsafe { intrinsics::unchecked_shl(5_i32, -1) };
45+
//~^ ERROR any use of this value will cause an error
46+
const SHL_I64_NEG: i64 = unsafe { intrinsics::unchecked_shl(5_i64, -1) };
47+
//~^ ERROR any use of this value will cause an error
48+
const SHL_I128_NEG: i128 = unsafe { intrinsics::unchecked_shl(5_i128, -1) };
49+
//~^ ERROR any use of this value will cause an error
50+
51+
// and that there's no special relation to the value -1 by picking some
52+
// negative values at random:
53+
54+
const SHL_I8_NEG_RANDOM: i8 = unsafe { intrinsics::unchecked_shl(5_i8, -6) };
55+
//~^ ERROR any use of this value will cause an error
56+
const SHL_I16_NEG_RANDOM: i16 = unsafe { intrinsics::unchecked_shl(5_16, -13) };
57+
//~^ ERROR any use of this value will cause an error
58+
const SHL_I32_NEG_RANDOM: i32 = unsafe { intrinsics::unchecked_shl(5_i32, -25) };
59+
//~^ ERROR any use of this value will cause an error
60+
const SHL_I64_NEG_RANDOM: i64 = unsafe { intrinsics::unchecked_shl(5_i64, -30) };
61+
//~^ ERROR any use of this value will cause an error
62+
const SHL_I128_NEG_RANDOM: i128 = unsafe { intrinsics::unchecked_shl(5_i128, -93) };
63+
//~^ ERROR any use of this value will cause an error
64+
65+
// Repeat it all over for `unchecked_shr`
66+
67+
// unsigned types:
68+
69+
const SHR_U8: u8 = unsafe { intrinsics::unchecked_shr(5_u8, 8) };
70+
//~^ ERROR any use of this value will cause an error
71+
const SHR_U16: u16 = unsafe { intrinsics::unchecked_shr(5_u16, 16) };
72+
//~^ ERROR any use of this value will cause an error
73+
const SHR_U32: u32 = unsafe { intrinsics::unchecked_shr(5_u32, 32) };
74+
//~^ ERROR any use of this value will cause an error
75+
const SHR_U64: u64 = unsafe { intrinsics::unchecked_shr(5_u64, 64) };
76+
//~^ ERROR any use of this value will cause an error
77+
const SHR_U128: u128 = unsafe { intrinsics::unchecked_shr(5_u128, 128) };
78+
//~^ ERROR any use of this value will cause an error
79+
80+
// signed types:
81+
82+
const SHR_I8: i8 = unsafe { intrinsics::unchecked_shr(5_i8, 8) };
83+
//~^ ERROR any use of this value will cause an error
84+
const SHR_I16: i16 = unsafe { intrinsics::unchecked_shr(5_16, 16) };
85+
//~^ ERROR any use of this value will cause an error
86+
const SHR_I32: i32 = unsafe { intrinsics::unchecked_shr(5_i32, 32) };
87+
//~^ ERROR any use of this value will cause an error
88+
const SHR_I64: i64 = unsafe { intrinsics::unchecked_shr(5_i64, 64) };
89+
//~^ ERROR any use of this value will cause an error
90+
const SHR_I128: i128 = unsafe { intrinsics::unchecked_shr(5_i128, 128) };
91+
//~^ ERROR any use of this value will cause an error
92+
93+
// and make sure we capture y < 0:
94+
95+
const SHR_I8_NEG: i8 = unsafe { intrinsics::unchecked_shr(5_i8, -1) };
96+
//~^ ERROR any use of this value will cause an error
97+
const SHR_I16_NEG: i16 = unsafe { intrinsics::unchecked_shr(5_16, -1) };
98+
//~^ ERROR any use of this value will cause an error
99+
const SHR_I32_NEG: i32 = unsafe { intrinsics::unchecked_shr(5_i32, -1) };
100+
//~^ ERROR any use of this value will cause an error
101+
const SHR_I64_NEG: i64 = unsafe { intrinsics::unchecked_shr(5_i64, -1) };
102+
//~^ ERROR any use of this value will cause an error
103+
const SHR_I128_NEG: i128 = unsafe { intrinsics::unchecked_shr(5_i128, -1) };
104+
//~^ ERROR any use of this value will cause an error
105+
106+
// and that there's no special relation to the value -1 by picking some
107+
// negative values at random:
108+
109+
const SHR_I8_NEG_RANDOM: i8 = unsafe { intrinsics::unchecked_shr(5_i8, -6) };
110+
//~^ ERROR any use of this value will cause an error
111+
const SHR_I16_NEG_RANDOM: i16 = unsafe { intrinsics::unchecked_shr(5_16, -13) };
112+
//~^ ERROR any use of this value will cause an error
113+
const SHR_I32_NEG_RANDOM: i32 = unsafe { intrinsics::unchecked_shr(5_i32, -25) };
114+
//~^ ERROR any use of this value will cause an error
115+
const SHR_I64_NEG_RANDOM: i64 = unsafe { intrinsics::unchecked_shr(5_i64, -30) };
6116
//~^ ERROR any use of this value will cause an error
7-
const SHL: u8 = unsafe { intrinsics::unchecked_shl(5_u8, 8) };
117+
const SHR_I128_NEG_RANDOM: i128 = unsafe { intrinsics::unchecked_shr(5_i128, -93) };
8118
//~^ ERROR any use of this value will cause an error
9119

10-
fn main() {
11-
}
120+
fn main() {}

0 commit comments

Comments
 (0)