Skip to content

Commit 257ac58

Browse files
committed
Don't unconditionally mask bitshift rhs
1 parent bb33830 commit 257ac58

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

src/operator.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -178,25 +178,9 @@ pub fn binary_op<'tcx>(
178178

179179
// These ops can have an RHS with a different numeric type.
180180
if bin_op == Shl || bin_op == Shr {
181-
// These are the maximum values a bitshift RHS could possibly have. For example, u16
182-
// can be bitshifted by 0..16, so masking with 0b1111 (16 - 1) will ensure we are in
183-
// that range.
184-
let type_bits: u32 = match left_kind {
185-
I8 | U8 => 8,
186-
I16 | U16 => 16,
187-
I32 | U32 => 32,
188-
I64 | U64 => 64,
189-
I128 | U128 => 128,
190-
_ => bug!("bad MIR: bitshift lhs is not integral"),
191-
};
192-
193-
// Cast to `u32` because `overflowing_sh{l,r}` only take `u32`, then apply the bitmask
194-
// to ensure it's within the valid shift value range.
195-
let masked_shift_width = (r as u32) & (type_bits - 1);
196-
197181
return match bin_op {
198-
Shl => int_shift!(left_kind, overflowing_shl, l, masked_shift_width),
199-
Shr => int_shift!(left_kind, overflowing_shr, l, masked_shift_width),
182+
Shl => int_shift!(left_kind, overflowing_shl, l, r as u32),
183+
Shr => int_shift!(left_kind, overflowing_shr, l, r as u32),
200184
_ => bug!("it has already been checked that this is a shift op"),
201185
};
202186
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![allow(exceeding_bitshifts)]
12+
13+
fn main() {
14+
let _n = 1i64 >> 64; //~ Overflow(Shr)
15+
}

0 commit comments

Comments
 (0)