about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-04-17 22:45:10 +0000
committerbors <bors@rust-lang.org>2015-04-17 22:45:10 +0000
commitb08d6cf529a83caec2f408cd8b1287e493ec57ca (patch)
tree6ef971f443e9797d767a2b05f837f9026ba68117 /src/libcore
parentf305579e490a9fa046b1b7a14e62daf643e41865 (diff)
parentb8ec7e88fc6c5a81194fd09dad042dc291a1cbb5 (diff)
downloadrust-b08d6cf529a83caec2f408cd8b1287e493ec57ca.tar.gz
rust-b08d6cf529a83caec2f408cd8b1287e493ec57ca.zip
Auto merge of #24500 - pnkfelix:oflo-checked-neg, r=nikomatsakis
Add conditional overflow-checking to signed negate operator.

I argue this can land independently of #24420 , because one can write the implementation of `wrapped_neg()` inline if necessary (as illustrated in two cases on this PR).

This needs to go into beta channel.
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/num/mod.rs6
1 files changed, 5 insertions, 1 deletions
diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs
index c7714afc4fa..a056e585fee 100644
--- a/src/libcore/num/mod.rs
+++ b/src/libcore/num/mod.rs
@@ -1321,7 +1321,11 @@ macro_rules! int_impl {
         #[stable(feature = "rust1", since = "1.0.0")]
         #[inline]
         pub fn abs(self) -> $T {
-            if self.is_negative() { -self } else { self }
+            if self.is_negative() {
+                self.wrapping_neg()
+            } else {
+                self
+            }
         }
 
         /// Returns a number representing sign of `self`.