Skip to content

Commit d6c5a04

Browse files
committed
some more tests for i128 oveflow behavior
1 parent 1ddb050 commit d6c5a04

File tree

5 files changed

+66
-13
lines changed

5 files changed

+66
-13
lines changed

Diff for: src/test/ui/consts/const-err2.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,22 @@ fn black_box<T>(_: T) {
1717
fn main() {
1818
let a = -std::i8::MIN;
1919
//~^ ERROR const_err
20+
let a_i128 = -std::i128::MIN;
21+
//~^ ERROR const_err
2022
let b = 200u8 + 200u8 + 200u8;
2123
//~^ ERROR const_err
24+
let b_i128 = std::i128::MIN - std::i128::MAX;
25+
//~^ ERROR const_err
2226
let c = 200u8 * 4;
2327
//~^ ERROR const_err
2428
let d = 42u8 - (42u8 + 1);
2529
//~^ ERROR const_err
2630
let _e = [5u8][1];
27-
//~^ ERROR index out of bounds
31+
//~^ ERROR const_err
2832
black_box(a);
33+
black_box(a_i128);
2934
black_box(b);
35+
black_box(b_i128);
3036
black_box(c);
3137
black_box(d);
3238
}

Diff for: src/test/ui/consts/const-err2.stderr

+17-5
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,40 @@ LL | #![deny(const_err)]
1111
| ^^^^^^^^^
1212

1313
error: this expression will panic at runtime
14-
--> $DIR/const-err2.rs:20:13
14+
--> $DIR/const-err2.rs:20:18
15+
|
16+
LL | let a_i128 = -std::i128::MIN;
17+
| ^^^^^^^^^^^^^^^ attempt to negate with overflow
18+
19+
error: this expression will panic at runtime
20+
--> $DIR/const-err2.rs:22:13
1521
|
1622
LL | let b = 200u8 + 200u8 + 200u8;
1723
| ^^^^^^^^^^^^^ attempt to add with overflow
1824

1925
error: this expression will panic at runtime
20-
--> $DIR/const-err2.rs:22:13
26+
--> $DIR/const-err2.rs:24:18
27+
|
28+
LL | let b_i128 = std::i128::MIN - std::i128::MAX;
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow
30+
31+
error: this expression will panic at runtime
32+
--> $DIR/const-err2.rs:26:13
2133
|
2234
LL | let c = 200u8 * 4;
2335
| ^^^^^^^^^ attempt to multiply with overflow
2436

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

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

37-
error: aborting due to 5 previous errors
49+
error: aborting due to 7 previous errors
3850

Diff for: src/test/ui/consts/const-err3.rs

+6
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,22 @@ fn black_box<T>(_: T) {
1717
fn main() {
1818
let a = -std::i8::MIN;
1919
//~^ ERROR const_err
20+
let a_i128 = -std::i128::MIN;
21+
//~^ ERROR const_err
2022
let b = 200u8 + 200u8 + 200u8;
2123
//~^ ERROR const_err
24+
let b_i128 = std::i128::MIN - std::i128::MAX;
25+
//~^ ERROR const_err
2226
let c = 200u8 * 4;
2327
//~^ ERROR const_err
2428
let d = 42u8 - (42u8 + 1);
2529
//~^ ERROR const_err
2630
let _e = [5u8][1];
2731
//~^ ERROR const_err
2832
black_box(a);
33+
black_box(a_i128);
2934
black_box(b);
35+
black_box(b_i128);
3036
black_box(c);
3137
black_box(d);
3238
}

Diff for: src/test/ui/consts/const-err3.stderr

+17-5
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,41 @@ note: the lint level is defined here
1010
LL | #![deny(const_err)]
1111
| ^^^^^^^^^
1212

13+
error: attempt to negate with overflow
14+
--> $DIR/const-err3.rs:20:18
15+
|
16+
LL | let a_i128 = -std::i128::MIN;
17+
| ^^^^^^^^^^^^^^^
18+
1319
error: attempt to add with overflow
14-
--> $DIR/const-err3.rs:20:13
20+
--> $DIR/const-err3.rs:22:13
1521
|
1622
LL | let b = 200u8 + 200u8 + 200u8;
1723
| ^^^^^^^^^^^^^
1824

25+
error: attempt to subtract with overflow
26+
--> $DIR/const-err3.rs:24:18
27+
|
28+
LL | let b_i128 = std::i128::MIN - std::i128::MAX;
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30+
1931
error: attempt to multiply with overflow
20-
--> $DIR/const-err3.rs:22:13
32+
--> $DIR/const-err3.rs:26:13
2133
|
2234
LL | let c = 200u8 * 4;
2335
| ^^^^^^^^^
2436

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

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

37-
error: aborting due to 5 previous errors
49+
error: aborting due to 7 previous errors
3850

Diff for: src/test/ui/consts/const-int-arithmetic.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#![feature(const_saturating_int_methods)]
88
#![feature(const_wrapping_int_methods)]
99

10-
use std::i8;
10+
use std::{i8, i128};
1111

1212
macro_rules! suite {
1313
($(
@@ -65,6 +65,10 @@ suite!(
6565
C26: 5i8.checked_rem_euclid(0), None;
6666
C27: i8::MIN.checked_rem_euclid(-1), None;
6767
}
68+
checked_i128 -> Option<i128> {
69+
CHK_ADD_I128: i128::MAX.checked_add(1), None;
70+
CHK_MUL_I128: i128::MIN.checked_mul(-1), None;
71+
}
6872

6973
saturating_and_wrapping -> i8 {
7074
// `const_saturating_int_methods`
@@ -104,6 +108,13 @@ suite!(
104108
C47: 100i8.wrapping_rem_euclid(10), 0;
105109
C48: (-128i8).wrapping_rem_euclid(-1), 0;
106110
}
111+
saturating_and_wrapping_i128 -> i128 {
112+
SAT_ADD_I128: i128::MAX.saturating_add(1), i128::MAX;
113+
SAT_MUL_I128: i128::MAX.saturating_mul(2), i128::MAX;
114+
115+
WRP_ADD_I128: i128::MAX.wrapping_add(1), i128::MIN;
116+
WRP_MUL_I128: i128::MAX.wrapping_mul(3), i128::MAX-2;
117+
}
107118

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

120131
C55: 5i8.overflowing_rem_euclid(2), (1, false);
121132
C56: i8::MIN.overflowing_rem_euclid(-1), (0, true);
122-
133+
}
134+
overflowing_i128 -> (i128, bool) {
135+
OFL_ADD_I128: i128::MAX.overflowing_add(1), (i128::MIN, true);
136+
OFL_MUL_I128: i128::MAX.overflowing_mul(3), (i128::MAX-2, true);
123137
}
124138
);
125139

126140
fn main() {
127141
checked();
142+
checked_i128();
128143
saturating_and_wrapping();
144+
saturating_and_wrapping_i128();
129145
overflowing();
146+
overflowing_i128();
130147
}

0 commit comments

Comments
 (0)