]> git.lizzy.rs Git - rust.git/commitdiff
Don't unconditionally mask bitshift rhs
authorOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Tue, 14 Mar 2017 13:24:16 +0000 (14:24 +0100)
committerOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Tue, 14 Mar 2017 13:24:16 +0000 (14:24 +0100)
src/operator.rs
tests/compile-fail/overflowing-rsh-6.rs [new file with mode: 0644]

index 2823e3edbea9ed7341bc99300acf42423f0d2e50..155d5574daa045f73dcc6abe669079a33b58e136 100644 (file)
@@ -178,25 +178,9 @@ fn normalize(val: PrimVal) -> PrimVal {
 
     // These ops can have an RHS with a different numeric type.
     if bin_op == Shl || bin_op == Shr {
-        // These are the maximum values a bitshift RHS could possibly have. For example, u16
-        // can be bitshifted by 0..16, so masking with 0b1111 (16 - 1) will ensure we are in
-        // that range.
-        let type_bits: u32 = match left_kind {
-            I8  | U8  => 8,
-            I16 | U16 => 16,
-            I32 | U32 => 32,
-            I64 | U64 => 64,
-            I128 | U128 => 128,
-            _ => bug!("bad MIR: bitshift lhs is not integral"),
-        };
-
-        // Cast to `u32` because `overflowing_sh{l,r}` only take `u32`, then apply the bitmask
-        // to ensure it's within the valid shift value range.
-        let masked_shift_width = (r as u32) & (type_bits - 1);
-
         return match bin_op {
-            Shl => int_shift!(left_kind, overflowing_shl, l, masked_shift_width),
-            Shr => int_shift!(left_kind, overflowing_shr, l, masked_shift_width),
+            Shl => int_shift!(left_kind, overflowing_shl, l, r as u32),
+            Shr => int_shift!(left_kind, overflowing_shr, l, r as u32),
             _ => bug!("it has already been checked that this is a shift op"),
         };
     }
diff --git a/tests/compile-fail/overflowing-rsh-6.rs b/tests/compile-fail/overflowing-rsh-6.rs
new file mode 100644 (file)
index 0000000..a7ac9d1
--- /dev/null
@@ -0,0 +1,15 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![allow(exceeding_bitshifts)]
+
+fn main() {
+    let _n = 1i64 >> 64; //~ Overflow(Shr)
+}