Skip to content

Commit 2107e73

Browse files
committed
fix exceeding_bitshift lint and test
1 parent 4b8c784 commit 2107e73

9 files changed

+640
-193
lines changed

src/librustc_mir/transform/const_prop.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -547,16 +547,18 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
547547
left: &Operand<'tcx>,
548548
right: &Operand<'tcx>,
549549
source_info: SourceInfo,
550-
place_layout: TyLayout<'tcx>,
551550
) -> Option<()> {
552551
let r =
553552
self.use_ecx(|this| this.ecx.read_immediate(this.ecx.eval_operand(right, None)?))?;
554553
// Check for exceeding shifts *even if* we cannot evaluate the LHS.
555554
if op == BinOp::Shr || op == BinOp::Shl {
556-
let left_bits = place_layout.size.bits();
555+
// We need the type of the LHS. We cannot use `place_layout` as that is the type
556+
// of the result, which for checked binops is not the same!
557+
let left_ty = left.ty(&self.local_decls, self.tcx);
558+
let left_size_bits = self.ecx.layout_of(left_ty).ok()?.size.bits();
557559
let right_size = r.layout.size;
558560
let r_bits = r.to_scalar().and_then(|r| r.to_bits(right_size));
559-
if r_bits.map_or(false, |b| b >= left_bits as u128) {
561+
if r_bits.map_or(false, |b| b >= left_size_bits as u128) {
560562
self.report_assert_as_lint(
561563
lint::builtin::EXCEEDING_BITSHIFTS,
562564
source_info,
@@ -618,7 +620,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
618620
}
619621
Rvalue::BinaryOp(op, left, right) => {
620622
trace!("checking BinaryOp(op = {:?}, left = {:?}, right = {:?})", op, left, right);
621-
self.check_binary_op(*op, left, right, source_info, place_layout)?;
623+
self.check_binary_op(*op, left, right, source_info)?;
622624
}
623625
Rvalue::CheckedBinaryOp(op, left, right) => {
624626
trace!(
@@ -627,7 +629,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
627629
left,
628630
right
629631
);
630-
self.check_binary_op(*op, left, right, source_info, place_layout)?;
632+
self.check_binary_op(*op, left, right, source_info)?;
631633
}
632634

633635
// Do not try creating references (#67862)
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
error: this arithmetic operation will overflow
2+
--> $DIR/lint-exceeding-bitshifts.rs:22:13
3+
|
4+
LL | let _ = x << 42;
5+
| ^^^^^^^ attempt to shift left with overflow
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/lint-exceeding-bitshifts.rs:9:9
9+
|
10+
LL | #![deny(exceeding_bitshifts, const_err)]
11+
| ^^^^^^^^^^^^^^^^^^^
12+
13+
error: this arithmetic operation will overflow
14+
--> $DIR/lint-exceeding-bitshifts.rs:27:15
15+
|
16+
LL | let n = 1u8 << 8;
17+
| ^^^^^^^^ attempt to shift left with overflow
18+
19+
error: this arithmetic operation will overflow
20+
--> $DIR/lint-exceeding-bitshifts.rs:29:15
21+
|
22+
LL | let n = 1u16 << 16;
23+
| ^^^^^^^^^^ attempt to shift left with overflow
24+
25+
error: this arithmetic operation will overflow
26+
--> $DIR/lint-exceeding-bitshifts.rs:31:15
27+
|
28+
LL | let n = 1u32 << 32;
29+
| ^^^^^^^^^^ attempt to shift left with overflow
30+
31+
error: this arithmetic operation will overflow
32+
--> $DIR/lint-exceeding-bitshifts.rs:33:15
33+
|
34+
LL | let n = 1u64 << 64;
35+
| ^^^^^^^^^^ attempt to shift left with overflow
36+
37+
error: this arithmetic operation will overflow
38+
--> $DIR/lint-exceeding-bitshifts.rs:35:15
39+
|
40+
LL | let n = 1i8 << 8;
41+
| ^^^^^^^^ attempt to shift left with overflow
42+
43+
error: this arithmetic operation will overflow
44+
--> $DIR/lint-exceeding-bitshifts.rs:37:15
45+
|
46+
LL | let n = 1i16 << 16;
47+
| ^^^^^^^^^^ attempt to shift left with overflow
48+
49+
error: this arithmetic operation will overflow
50+
--> $DIR/lint-exceeding-bitshifts.rs:39:15
51+
|
52+
LL | let n = 1i32 << 32;
53+
| ^^^^^^^^^^ attempt to shift left with overflow
54+
55+
error: this arithmetic operation will overflow
56+
--> $DIR/lint-exceeding-bitshifts.rs:41:15
57+
|
58+
LL | let n = 1i64 << 64;
59+
| ^^^^^^^^^^ attempt to shift left with overflow
60+
61+
error: this arithmetic operation will overflow
62+
--> $DIR/lint-exceeding-bitshifts.rs:44:15
63+
|
64+
LL | let n = 1u8 >> 8;
65+
| ^^^^^^^^ attempt to shift right with overflow
66+
67+
error: this arithmetic operation will overflow
68+
--> $DIR/lint-exceeding-bitshifts.rs:46:15
69+
|
70+
LL | let n = 1u16 >> 16;
71+
| ^^^^^^^^^^ attempt to shift right with overflow
72+
73+
error: this arithmetic operation will overflow
74+
--> $DIR/lint-exceeding-bitshifts.rs:48:15
75+
|
76+
LL | let n = 1u32 >> 32;
77+
| ^^^^^^^^^^ attempt to shift right with overflow
78+
79+
error: this arithmetic operation will overflow
80+
--> $DIR/lint-exceeding-bitshifts.rs:50:15
81+
|
82+
LL | let n = 1u64 >> 64;
83+
| ^^^^^^^^^^ attempt to shift right with overflow
84+
85+
error: this arithmetic operation will overflow
86+
--> $DIR/lint-exceeding-bitshifts.rs:52:15
87+
|
88+
LL | let n = 1i8 >> 8;
89+
| ^^^^^^^^ attempt to shift right with overflow
90+
91+
error: this arithmetic operation will overflow
92+
--> $DIR/lint-exceeding-bitshifts.rs:54:15
93+
|
94+
LL | let n = 1i16 >> 16;
95+
| ^^^^^^^^^^ attempt to shift right with overflow
96+
97+
error: this arithmetic operation will overflow
98+
--> $DIR/lint-exceeding-bitshifts.rs:56:15
99+
|
100+
LL | let n = 1i32 >> 32;
101+
| ^^^^^^^^^^ attempt to shift right with overflow
102+
103+
error: this arithmetic operation will overflow
104+
--> $DIR/lint-exceeding-bitshifts.rs:58:15
105+
|
106+
LL | let n = 1i64 >> 64;
107+
| ^^^^^^^^^^ attempt to shift right with overflow
108+
109+
error: this arithmetic operation will overflow
110+
--> $DIR/lint-exceeding-bitshifts.rs:62:15
111+
|
112+
LL | let n = n << 8;
113+
| ^^^^^^ attempt to shift left with overflow
114+
115+
error: this arithmetic operation will overflow
116+
--> $DIR/lint-exceeding-bitshifts.rs:64:15
117+
|
118+
LL | let n = 1u8 << -8;
119+
| ^^^^^^^^^ attempt to shift left with overflow
120+
121+
error: this arithmetic operation will overflow
122+
--> $DIR/lint-exceeding-bitshifts.rs:69:15
123+
|
124+
LL | let n = 1u8 << (4+4);
125+
| ^^^^^^^^^^^^ attempt to shift left with overflow
126+
127+
error: this arithmetic operation will overflow
128+
--> $DIR/lint-exceeding-bitshifts.rs:71:15
129+
|
130+
LL | let n = 1i64 >> [64][0];
131+
| ^^^^^^^^^^^^^^^ attempt to shift right with overflow
132+
133+
error: this arithmetic operation will overflow
134+
--> $DIR/lint-exceeding-bitshifts.rs:77:15
135+
|
136+
LL | let n = 1_isize << BITS;
137+
| ^^^^^^^^^^^^^^^ attempt to shift left with overflow
138+
139+
error: this arithmetic operation will overflow
140+
--> $DIR/lint-exceeding-bitshifts.rs:78:15
141+
|
142+
LL | let n = 1_usize << BITS;
143+
| ^^^^^^^^^^^^^^^ attempt to shift left with overflow
144+
145+
error: aborting due to 23 previous errors
146+
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
error: this arithmetic operation will overflow
2+
--> $DIR/lint-exceeding-bitshifts.rs:22:13
3+
|
4+
LL | let _ = x << 42;
5+
| ^^^^^^^ attempt to shift left with overflow
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/lint-exceeding-bitshifts.rs:9:9
9+
|
10+
LL | #![deny(exceeding_bitshifts, const_err)]
11+
| ^^^^^^^^^^^^^^^^^^^
12+
13+
error: this arithmetic operation will overflow
14+
--> $DIR/lint-exceeding-bitshifts.rs:27:15
15+
|
16+
LL | let n = 1u8 << 8;
17+
| ^^^^^^^^ attempt to shift left with overflow
18+
19+
error: this arithmetic operation will overflow
20+
--> $DIR/lint-exceeding-bitshifts.rs:29:15
21+
|
22+
LL | let n = 1u16 << 16;
23+
| ^^^^^^^^^^ attempt to shift left with overflow
24+
25+
error: this arithmetic operation will overflow
26+
--> $DIR/lint-exceeding-bitshifts.rs:31:15
27+
|
28+
LL | let n = 1u32 << 32;
29+
| ^^^^^^^^^^ attempt to shift left with overflow
30+
31+
error: this arithmetic operation will overflow
32+
--> $DIR/lint-exceeding-bitshifts.rs:33:15
33+
|
34+
LL | let n = 1u64 << 64;
35+
| ^^^^^^^^^^ attempt to shift left with overflow
36+
37+
error: this arithmetic operation will overflow
38+
--> $DIR/lint-exceeding-bitshifts.rs:35:15
39+
|
40+
LL | let n = 1i8 << 8;
41+
| ^^^^^^^^ attempt to shift left with overflow
42+
43+
error: this arithmetic operation will overflow
44+
--> $DIR/lint-exceeding-bitshifts.rs:37:15
45+
|
46+
LL | let n = 1i16 << 16;
47+
| ^^^^^^^^^^ attempt to shift left with overflow
48+
49+
error: this arithmetic operation will overflow
50+
--> $DIR/lint-exceeding-bitshifts.rs:39:15
51+
|
52+
LL | let n = 1i32 << 32;
53+
| ^^^^^^^^^^ attempt to shift left with overflow
54+
55+
error: this arithmetic operation will overflow
56+
--> $DIR/lint-exceeding-bitshifts.rs:41:15
57+
|
58+
LL | let n = 1i64 << 64;
59+
| ^^^^^^^^^^ attempt to shift left with overflow
60+
61+
error: this arithmetic operation will overflow
62+
--> $DIR/lint-exceeding-bitshifts.rs:44:15
63+
|
64+
LL | let n = 1u8 >> 8;
65+
| ^^^^^^^^ attempt to shift right with overflow
66+
67+
error: this arithmetic operation will overflow
68+
--> $DIR/lint-exceeding-bitshifts.rs:46:15
69+
|
70+
LL | let n = 1u16 >> 16;
71+
| ^^^^^^^^^^ attempt to shift right with overflow
72+
73+
error: this arithmetic operation will overflow
74+
--> $DIR/lint-exceeding-bitshifts.rs:48:15
75+
|
76+
LL | let n = 1u32 >> 32;
77+
| ^^^^^^^^^^ attempt to shift right with overflow
78+
79+
error: this arithmetic operation will overflow
80+
--> $DIR/lint-exceeding-bitshifts.rs:50:15
81+
|
82+
LL | let n = 1u64 >> 64;
83+
| ^^^^^^^^^^ attempt to shift right with overflow
84+
85+
error: this arithmetic operation will overflow
86+
--> $DIR/lint-exceeding-bitshifts.rs:52:15
87+
|
88+
LL | let n = 1i8 >> 8;
89+
| ^^^^^^^^ attempt to shift right with overflow
90+
91+
error: this arithmetic operation will overflow
92+
--> $DIR/lint-exceeding-bitshifts.rs:54:15
93+
|
94+
LL | let n = 1i16 >> 16;
95+
| ^^^^^^^^^^ attempt to shift right with overflow
96+
97+
error: this arithmetic operation will overflow
98+
--> $DIR/lint-exceeding-bitshifts.rs:56:15
99+
|
100+
LL | let n = 1i32 >> 32;
101+
| ^^^^^^^^^^ attempt to shift right with overflow
102+
103+
error: this arithmetic operation will overflow
104+
--> $DIR/lint-exceeding-bitshifts.rs:58:15
105+
|
106+
LL | let n = 1i64 >> 64;
107+
| ^^^^^^^^^^ attempt to shift right with overflow
108+
109+
error: this arithmetic operation will overflow
110+
--> $DIR/lint-exceeding-bitshifts.rs:62:15
111+
|
112+
LL | let n = n << 8;
113+
| ^^^^^^ attempt to shift left with overflow
114+
115+
error: this arithmetic operation will overflow
116+
--> $DIR/lint-exceeding-bitshifts.rs:64:15
117+
|
118+
LL | let n = 1u8 << -8;
119+
| ^^^^^^^^^ attempt to shift left with overflow
120+
121+
error: this arithmetic operation will overflow
122+
--> $DIR/lint-exceeding-bitshifts.rs:69:15
123+
|
124+
LL | let n = 1u8 << (4+4);
125+
| ^^^^^^^^^^^^ attempt to shift left with overflow
126+
127+
error: this arithmetic operation will overflow
128+
--> $DIR/lint-exceeding-bitshifts.rs:71:15
129+
|
130+
LL | let n = 1i64 >> [64][0];
131+
| ^^^^^^^^^^^^^^^ attempt to shift right with overflow
132+
133+
error: this arithmetic operation will overflow
134+
--> $DIR/lint-exceeding-bitshifts.rs:77:15
135+
|
136+
LL | let n = 1_isize << BITS;
137+
| ^^^^^^^^^^^^^^^ attempt to shift left with overflow
138+
139+
error: this arithmetic operation will overflow
140+
--> $DIR/lint-exceeding-bitshifts.rs:78:15
141+
|
142+
LL | let n = 1_usize << BITS;
143+
| ^^^^^^^^^^^^^^^ attempt to shift left with overflow
144+
145+
error: aborting due to 23 previous errors
146+

0 commit comments

Comments
 (0)