From cdfff9db35d037c51dfd5c2bac2174f651294adb Mon Sep 17 00:00:00 2001 From: Corey Richardson Date: Tue, 6 Jan 2015 00:56:30 -0500 Subject: rustc: implement arithmetic overflow checking Adds overflow checking to integer addition, multiplication, and subtraction when `-Z force-overflow-checks` is true, or if `--cfg ndebug` is not passed to the compiler. On overflow, it panics with `arithmetic operation overflowed`. Also adds `overflowing_add`, `overflowing_sub`, and `overflowing_mul` intrinsics for doing unchecked arithmetic. [breaking-change] --- src/test/run-fail/overflowing-add.rs | 15 +++++++++++++++ src/test/run-fail/overflowing-mul.rs | 15 +++++++++++++++ src/test/run-fail/overflowing-sub.rs | 15 +++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 src/test/run-fail/overflowing-add.rs create mode 100644 src/test/run-fail/overflowing-mul.rs create mode 100644 src/test/run-fail/overflowing-sub.rs (limited to 'src/test/run-fail') diff --git a/src/test/run-fail/overflowing-add.rs b/src/test/run-fail/overflowing-add.rs new file mode 100644 index 00000000000..c3e41110d20 --- /dev/null +++ b/src/test/run-fail/overflowing-add.rs @@ -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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// error-pattern:thread '
' panicked at 'arithmatic operation overflowed' + +fn main() { + let x = 200u8 + 200u8 + 200u8; +} diff --git a/src/test/run-fail/overflowing-mul.rs b/src/test/run-fail/overflowing-mul.rs new file mode 100644 index 00000000000..bf7a9d07586 --- /dev/null +++ b/src/test/run-fail/overflowing-mul.rs @@ -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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// error-pattern:thread '
' panicked at 'arithmatic operation overflowed' + +fn main() { + let x = 200u8 + 4u8; +} diff --git a/src/test/run-fail/overflowing-sub.rs b/src/test/run-fail/overflowing-sub.rs new file mode 100644 index 00000000000..961b36d322c --- /dev/null +++ b/src/test/run-fail/overflowing-sub.rs @@ -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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// error-pattern:thread '
' panicked at 'arithmatic operation overflowed' + +fn main() { + let x = 42u8 - 43u8; +} -- cgit 1.4.1-3-g733a5