diff options
| author | Oliver Schneider <github35764891676564198441@oli-obk.de> | 2018-06-27 13:47:19 +0200 |
|---|---|---|
| committer | Oliver Schneider <github35764891676564198441@oli-obk.de> | 2018-06-27 13:47:19 +0200 |
| commit | 0fa166ad7f2684cecb184204141abe159b5b7b4f (patch) | |
| tree | faac57d0b10c3eeaedf327d662188c989f8ef32d | |
| parent | 612c28004cba9e8e7bcd7e2a9dcdf2c2736f0e81 (diff) | |
| download | rust-0fa166ad7f2684cecb184204141abe159b5b7b4f.tar.gz rust-0fa166ad7f2684cecb184204141abe159b5b7b4f.zip | |
Detect overflows of non u32 shifts
| -rw-r--r-- | src/librustc_mir/interpret/operator.rs | 3 | ||||
| -rw-r--r-- | src/test/ui/const-eval/shift_overflow.rs | 19 | ||||
| -rw-r--r-- | src/test/ui/const-eval/shift_overflow.stderr | 9 |
3 files changed, 30 insertions, 1 deletions
diff --git a/src/librustc_mir/interpret/operator.rs b/src/librustc_mir/interpret/operator.rs index 8a2a78daa35..8320add7157 100644 --- a/src/librustc_mir/interpret/operator.rs +++ b/src/librustc_mir/interpret/operator.rs @@ -95,9 +95,10 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> { // These ops can have an RHS with a different numeric type. if right_kind.is_int() && (bin_op == Shl || bin_op == Shr) { let signed = left_layout.abi.is_signed(); + let mut oflo = (r as u32 as u128) != r; let mut r = r as u32; let size = left_layout.size.bits() as u32; - let oflo = r >= size; + oflo |= r >= size; if oflo { r %= size; } diff --git a/src/test/ui/const-eval/shift_overflow.rs b/src/test/ui/const-eval/shift_overflow.rs new file mode 100644 index 00000000000..a2c6ed36d30 --- /dev/null +++ b/src/test/ui/const-eval/shift_overflow.rs @@ -0,0 +1,19 @@ +// Copyright 2018 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. + +enum Foo { + // test that we detect overflows for non-u32 discriminants + X = 1 << ((u32::max_value() as u64) + 1), //~ ERROR E0080 + Y = 42, +} + + +fn main() { +} diff --git a/src/test/ui/const-eval/shift_overflow.stderr b/src/test/ui/const-eval/shift_overflow.stderr new file mode 100644 index 00000000000..00a748249ea --- /dev/null +++ b/src/test/ui/const-eval/shift_overflow.stderr @@ -0,0 +1,9 @@ +error[E0080]: could not evaluate enum discriminant + --> $DIR/shift_overflow.rs:13:9 + | +LL | X = 1 << ((u32::max_value() as u64) + 1), //~ ERROR E0080 + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to shift left with overflow + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0080`. |
