@@ -18,39 +18,51 @@ fn main() {
18
18
test_right_shift ( ) ;
19
19
}
20
20
21
+ pub static mut HACK : i32 = 0 ;
22
+
23
+ // Work around constant-evaluation
24
+ // The point of this test is to exercise the code generated for execution at runtime,
25
+ // `id` can never be flagged as a const fn by future aggressive analyses...
26
+ // due to the modification of the static
27
+ #[ inline( never) ]
28
+ fn id < T > ( x : T ) -> T {
29
+ unsafe { HACK += 1 ; }
30
+ x
31
+ }
32
+
21
33
fn test_left_shift ( ) {
22
34
// negative rhs can panic, but values in [0,N-1] are okay for iN
23
35
24
36
macro_rules! tests {
25
37
( $iN: ty, $uN: ty, $max_rhs: expr, $expect_i: expr, $expect_u: expr) => { {
26
- let x = ( 1 as $iN) << 0 ;
38
+ let x = ( 1 as $iN) << id ( 0 ) ;
27
39
assert_eq!( x, 1 ) ;
28
- let x = ( 1 as $uN) << 0 ;
40
+ let x = ( 1 as $uN) << id ( 0 ) ;
29
41
assert_eq!( x, 1 ) ;
30
- let x = ( 1 as $iN) << $max_rhs;
42
+ let x = ( 1 as $iN) << id ( $max_rhs) ;
31
43
assert_eq!( x, $expect_i) ;
32
- let x = ( 1 as $uN) << $max_rhs;
44
+ let x = ( 1 as $uN) << id ( $max_rhs) ;
33
45
assert_eq!( x, $expect_u) ;
34
46
// high-order bits on LHS are silently discarded without panic.
35
- let x = ( 3 as $iN) << $max_rhs;
47
+ let x = ( 3 as $iN) << id ( $max_rhs) ;
36
48
assert_eq!( x, $expect_i) ;
37
- let x = ( 3 as $uN) << $max_rhs;
49
+ let x = ( 3 as $uN) << id ( $max_rhs) ;
38
50
assert_eq!( x, $expect_u) ;
39
51
} }
40
52
}
41
53
42
- let x = 1_i8 << 0 ;
54
+ let x = 1_i8 << id ( 0 ) ;
43
55
assert_eq ! ( x, 1 ) ;
44
- let x = 1_u8 << 0 ;
56
+ let x = 1_u8 << id ( 0 ) ;
45
57
assert_eq ! ( x, 1 ) ;
46
- let x = 1_i8 << 7 ;
58
+ let x = 1_i8 << id ( 7 ) ;
47
59
assert_eq ! ( x, std:: i8 :: MIN ) ;
48
- let x = 1_u8 << 7 ;
60
+ let x = 1_u8 << id ( 7 ) ;
49
61
assert_eq ! ( x, 0x80 ) ;
50
62
// high-order bits on LHS are silently discarded without panic.
51
- let x = 3_i8 << 7 ;
63
+ let x = 3_i8 << id ( 7 ) ;
52
64
assert_eq ! ( x, std:: i8 :: MIN ) ;
53
- let x = 3_u8 << 7 ;
65
+ let x = 3_u8 << id ( 7 ) ;
54
66
assert_eq ! ( x, 0x80 ) ;
55
67
56
68
// above is (approximately) expanded from:
@@ -68,23 +80,23 @@ fn test_right_shift() {
68
80
( $iN: ty, $uN: ty, $max_rhs: expr,
69
81
$signbit_i: expr, $highbit_i: expr, $highbit_u: expr) =>
70
82
{ {
71
- let x = ( 1 as $iN) >> 0 ;
83
+ let x = ( 1 as $iN) >> id ( 0 ) ;
72
84
assert_eq!( x, 1 ) ;
73
- let x = ( 1 as $uN) >> 0 ;
85
+ let x = ( 1 as $uN) >> id ( 0 ) ;
74
86
assert_eq!( x, 1 ) ;
75
- let x = ( $highbit_i) >> $max_rhs-1 ;
87
+ let x = ( $highbit_i) >> id ( $max_rhs-1 ) ;
76
88
assert_eq!( x, 1 ) ;
77
- let x = ( $highbit_u) >> $max_rhs;
89
+ let x = ( $highbit_u) >> id ( $max_rhs) ;
78
90
assert_eq!( x, 1 ) ;
79
91
// sign-bit is carried by arithmetic right shift
80
- let x = ( $signbit_i) >> $max_rhs;
92
+ let x = ( $signbit_i) >> id ( $max_rhs) ;
81
93
assert_eq!( x, -1 ) ;
82
94
// low-order bits on LHS are silently discarded without panic.
83
- let x = ( $highbit_i + 1 ) >> $max_rhs-1 ;
95
+ let x = ( $highbit_i + 1 ) >> id ( $max_rhs-1 ) ;
84
96
assert_eq!( x, 1 ) ;
85
- let x = ( $highbit_u + 1 ) >> $max_rhs;
97
+ let x = ( $highbit_u + 1 ) >> id ( $max_rhs) ;
86
98
assert_eq!( x, 1 ) ;
87
- let x = ( $signbit_i + 1 ) >> $max_rhs;
99
+ let x = ( $signbit_i + 1 ) >> id ( $max_rhs) ;
88
100
assert_eq!( x, -1 ) ;
89
101
} }
90
102
}
0 commit comments