Skip to content

Commit

Permalink
some more tests for i128 oveflow behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Feb 10, 2020
1 parent 1ddb050 commit d6c5a04
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 13 deletions.
8 changes: 7 additions & 1 deletion src/test/ui/consts/const-err2.rs
Expand Up @@ -17,16 +17,22 @@ fn black_box<T>(_: T) {
fn main() {
let a = -std::i8::MIN;
//~^ ERROR const_err
let a_i128 = -std::i128::MIN;
//~^ ERROR const_err
let b = 200u8 + 200u8 + 200u8;
//~^ ERROR const_err
let b_i128 = std::i128::MIN - std::i128::MAX;
//~^ ERROR const_err
let c = 200u8 * 4;
//~^ ERROR const_err
let d = 42u8 - (42u8 + 1);
//~^ ERROR const_err
let _e = [5u8][1];
//~^ ERROR index out of bounds
//~^ ERROR const_err
black_box(a);
black_box(a_i128);
black_box(b);
black_box(b_i128);
black_box(c);
black_box(d);
}
22 changes: 17 additions & 5 deletions src/test/ui/consts/const-err2.stderr
Expand Up @@ -11,28 +11,40 @@ LL | #![deny(const_err)]
| ^^^^^^^^^

error: this expression will panic at runtime
--> $DIR/const-err2.rs:20:13
--> $DIR/const-err2.rs:20:18
|
LL | let a_i128 = -std::i128::MIN;
| ^^^^^^^^^^^^^^^ attempt to negate with overflow

error: this expression will panic at runtime
--> $DIR/const-err2.rs:22:13
|
LL | let b = 200u8 + 200u8 + 200u8;
| ^^^^^^^^^^^^^ attempt to add with overflow

error: this expression will panic at runtime
--> $DIR/const-err2.rs:22:13
--> $DIR/const-err2.rs:24:18
|
LL | let b_i128 = std::i128::MIN - std::i128::MAX;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow

error: this expression will panic at runtime
--> $DIR/const-err2.rs:26:13
|
LL | let c = 200u8 * 4;
| ^^^^^^^^^ attempt to multiply with overflow

error: this expression will panic at runtime
--> $DIR/const-err2.rs:24:13
--> $DIR/const-err2.rs:28:13
|
LL | let d = 42u8 - (42u8 + 1);
| ^^^^^^^^^^^^^^^^^ attempt to subtract with overflow

error: index out of bounds: the len is 1 but the index is 1
--> $DIR/const-err2.rs:26:14
--> $DIR/const-err2.rs:30:14
|
LL | let _e = [5u8][1];
| ^^^^^^^^

error: aborting due to 5 previous errors
error: aborting due to 7 previous errors

6 changes: 6 additions & 0 deletions src/test/ui/consts/const-err3.rs
Expand Up @@ -17,16 +17,22 @@ fn black_box<T>(_: T) {
fn main() {
let a = -std::i8::MIN;
//~^ ERROR const_err
let a_i128 = -std::i128::MIN;
//~^ ERROR const_err
let b = 200u8 + 200u8 + 200u8;
//~^ ERROR const_err
let b_i128 = std::i128::MIN - std::i128::MAX;
//~^ ERROR const_err
let c = 200u8 * 4;
//~^ ERROR const_err
let d = 42u8 - (42u8 + 1);
//~^ ERROR const_err
let _e = [5u8][1];
//~^ ERROR const_err
black_box(a);
black_box(a_i128);
black_box(b);
black_box(b_i128);
black_box(c);
black_box(d);
}
22 changes: 17 additions & 5 deletions src/test/ui/consts/const-err3.stderr
Expand Up @@ -10,29 +10,41 @@ note: the lint level is defined here
LL | #![deny(const_err)]
| ^^^^^^^^^

error: attempt to negate with overflow
--> $DIR/const-err3.rs:20:18
|
LL | let a_i128 = -std::i128::MIN;
| ^^^^^^^^^^^^^^^

error: attempt to add with overflow
--> $DIR/const-err3.rs:20:13
--> $DIR/const-err3.rs:22:13
|
LL | let b = 200u8 + 200u8 + 200u8;
| ^^^^^^^^^^^^^

error: attempt to subtract with overflow
--> $DIR/const-err3.rs:24:18
|
LL | let b_i128 = std::i128::MIN - std::i128::MAX;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: attempt to multiply with overflow
--> $DIR/const-err3.rs:22:13
--> $DIR/const-err3.rs:26:13
|
LL | let c = 200u8 * 4;
| ^^^^^^^^^

error: attempt to subtract with overflow
--> $DIR/const-err3.rs:24:13
--> $DIR/const-err3.rs:28:13
|
LL | let d = 42u8 - (42u8 + 1);
| ^^^^^^^^^^^^^^^^^

error: index out of bounds: the len is 1 but the index is 1
--> $DIR/const-err3.rs:26:14
--> $DIR/const-err3.rs:30:14
|
LL | let _e = [5u8][1];
| ^^^^^^^^

error: aborting due to 5 previous errors
error: aborting due to 7 previous errors

21 changes: 19 additions & 2 deletions src/test/ui/consts/const-int-arithmetic.rs
Expand Up @@ -7,7 +7,7 @@
#![feature(const_saturating_int_methods)]
#![feature(const_wrapping_int_methods)]

use std::i8;
use std::{i8, i128};

macro_rules! suite {
($(
Expand Down Expand Up @@ -65,6 +65,10 @@ suite!(
C26: 5i8.checked_rem_euclid(0), None;
C27: i8::MIN.checked_rem_euclid(-1), None;
}
checked_i128 -> Option<i128> {
CHK_ADD_I128: i128::MAX.checked_add(1), None;
CHK_MUL_I128: i128::MIN.checked_mul(-1), None;
}

saturating_and_wrapping -> i8 {
// `const_saturating_int_methods`
Expand Down Expand Up @@ -104,6 +108,13 @@ suite!(
C47: 100i8.wrapping_rem_euclid(10), 0;
C48: (-128i8).wrapping_rem_euclid(-1), 0;
}
saturating_and_wrapping_i128 -> i128 {
SAT_ADD_I128: i128::MAX.saturating_add(1), i128::MAX;
SAT_MUL_I128: i128::MAX.saturating_mul(2), i128::MAX;

WRP_ADD_I128: i128::MAX.wrapping_add(1), i128::MIN;
WRP_MUL_I128: i128::MAX.wrapping_mul(3), i128::MAX-2;
}

overflowing -> (i8, bool) {
// `const_overflowing_int_methods`
Expand All @@ -119,12 +130,18 @@ suite!(

C55: 5i8.overflowing_rem_euclid(2), (1, false);
C56: i8::MIN.overflowing_rem_euclid(-1), (0, true);

}
overflowing_i128 -> (i128, bool) {
OFL_ADD_I128: i128::MAX.overflowing_add(1), (i128::MIN, true);
OFL_MUL_I128: i128::MAX.overflowing_mul(3), (i128::MAX-2, true);
}
);

fn main() {
checked();
checked_i128();
saturating_and_wrapping();
saturating_and_wrapping_i128();
overflowing();
overflowing_i128();
}

0 comments on commit d6c5a04

Please sign in to comment.