some more tests for i128 oveflow behavior

This commit is contained in:
Ralf Jung 2020-02-10 11:37:02 +01:00
parent 1ddb0503ff
commit d6c5a04eff
5 changed files with 66 additions and 13 deletions

View File

@ -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);
}

View File

@ -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

View File

@ -17,8 +17,12 @@ 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);
@ -26,7 +30,9 @@ fn main() {
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);
}

View File

@ -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

View File

@ -7,7 +7,7 @@
#![feature(const_saturating_int_methods)]
#![feature(const_wrapping_int_methods)]
use std::i8;
use std::{i8, i128};
macro_rules! suite {
($(
@ -65,6 +65,10 @@ fn $fn() {
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`
@ -104,6 +108,13 @@ fn $fn() {
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`
@ -119,12 +130,18 @@ fn $fn() {
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();
}