Skip to content

Commit 41b3c86

Browse files
committed
Test cases for checking arithmetic overflow during const eval.
1 parent 8d54ea3 commit 41b3c86

File tree

6 files changed

+301
-0
lines changed

6 files changed

+301
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Evaluation of constants in refutable patterns goes through
12+
// different compiler control-flow paths.
13+
14+
#![allow(unused_imports)]
15+
16+
use std::fmt;
17+
use std::{i8, i16, i32, i64, isize};
18+
use std::{u8, u16, u32, u64, usize};
19+
20+
const NEG_128: i8 = -128;
21+
const NEG_NEG_128: i8 = -NEG_128;
22+
//~^ ERROR constant evaluation error: attempted to negate with overflow
23+
//~| ERROR attempted to negate with overflow
24+
25+
fn main() {
26+
match -128i8 {
27+
NEG_NEG_128 => println!("A"),
28+
_ => println!("B"),
29+
}
30+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Evaluation of constants in array-elem count goes through different
12+
// compiler control-flow paths.
13+
//
14+
// This test is checking the count in an array expression.
15+
16+
#![allow(unused_imports)]
17+
18+
use std::fmt;
19+
use std::{i8, i16, i32, i64, isize};
20+
use std::{u8, u16, u32, u64, usize};
21+
22+
const A_I8_I
23+
: [u32; (i8::MAX as usize) + 1]
24+
= [0; (i8::MAX + 1) as usize];
25+
//~^ ERROR error evaluating count: attempted to add with overflow
26+
27+
fn main() {
28+
foo(&A_I8_I[..]);
29+
}
30+
31+
fn foo<T:fmt::Debug>(x: T) {
32+
println!("{:?}", x);
33+
}
34+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Evaluation of constants in array-elem count goes through different
12+
// compiler control-flow paths.
13+
//
14+
// This test is checking the count in an array expression.
15+
//
16+
// This is a variation of another such test, but in this case the
17+
// types for the left- and right-hand sides of the addition do not
18+
// match (as well as overflow).
19+
20+
#![allow(unused_imports)]
21+
22+
use std::fmt;
23+
use std::{i8, i16, i32, i64, isize};
24+
use std::{u8, u16, u32, u64, usize};
25+
26+
const A_I8_I
27+
: [u32; (i8::MAX as usize) + 1]
28+
= [0; (i8::MAX + 1u8) as usize];
29+
//~^ ERROR mismatched types
30+
31+
fn main() {
32+
foo(&A_I8_I[..]);
33+
}
34+
35+
fn foo<T:fmt::Debug>(x: T) {
36+
println!("{:?}", x);
37+
}
38+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// ignore-test this should fail to compile (#23833)
12+
13+
// Evaluation of constants in array-elem count goes through different
14+
// compiler control-flow paths.
15+
//
16+
// This test is checking the count in an array type.
17+
18+
#![allow(unused_imports)]
19+
20+
use std::fmt;
21+
use std::{i8, i16, i32, i64, isize};
22+
use std::{u8, u16, u32, u64, usize};
23+
24+
const A_I8_T
25+
: [u32; (i8::MAX as i8 + 1i8) as usize]
26+
//~^ ERROR error evaluating count: attempted to add with overflow
27+
= [0; (i8::MAX as usize) + 1];
28+
29+
fn main() {
30+
foo(&A_I8_T[..]);
31+
}
32+
33+
fn foo<T:fmt::Debug>(x: T) {
34+
println!("{:?}", x);
35+
}
36+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Evaluation of constants in array-elem count goes through different
12+
// compiler control-flow paths.
13+
//
14+
// This test is checking the count in an array type.
15+
16+
#![allow(unused_imports)]
17+
18+
use std::fmt;
19+
use std::{i8, i16, i32, i64, isize};
20+
use std::{u8, u16, u32, u64, usize};
21+
22+
const A_I8_T
23+
: [u32; (i8::MAX as i8 + 1u8) as usize]
24+
//~^ ERROR mismatched types
25+
= [0; (i8::MAX as usize) + 1];
26+
27+
fn main() {
28+
foo(&A_I8_T[..]);
29+
}
30+
31+
fn foo<T:fmt::Debug>(x: T) {
32+
println!("{:?}", x);
33+
}
34+
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![allow(unused_imports)]
12+
13+
// Note: the relevant lint pass here runs before some of the constant
14+
// evaluation below (e.g. that performed by trans and llvm), so if you
15+
// change this warn to a deny, then the compiler will exit before
16+
// those errors are detected.
17+
#![warn(unsigned_negation)]
18+
19+
use std::fmt;
20+
use std::{i8, i16, i32, i64, isize};
21+
use std::{u8, u16, u32, u64, usize};
22+
23+
const VALS_I8: (i8, i8, i8, i8) =
24+
(-i8::MIN,
25+
//~^ ERROR attempted to negate with overflow
26+
i8::MIN - 1,
27+
//~^ ERROR attempted to sub with overflow
28+
i8::MAX + 1,
29+
//~^ ERROR attempted to add with overflow
30+
i8::MIN * 2,
31+
//~^ ERROR attempted to mul with overflow
32+
);
33+
34+
const VALS_I16: (i16, i16, i16, i16) =
35+
(-i16::MIN,
36+
//~^ ERROR attempted to negate with overflow
37+
i16::MIN - 1,
38+
//~^ ERROR attempted to sub with overflow
39+
i16::MAX + 1,
40+
//~^ ERROR attempted to add with overflow
41+
i16::MIN * 2,
42+
//~^ ERROR attempted to mul with overflow
43+
);
44+
45+
const VALS_I32: (i32, i32, i32, i32) =
46+
(-i32::MIN,
47+
//~^ ERROR attempted to negate with overflow
48+
i32::MIN - 1,
49+
//~^ ERROR attempted to sub with overflow
50+
i32::MAX + 1,
51+
//~^ ERROR attempted to add with overflow
52+
i32::MIN * 2,
53+
//~^ ERROR attempted to mul with overflow
54+
);
55+
56+
const VALS_I64: (i64, i64, i64, i64) =
57+
(-i64::MIN,
58+
//~^ ERROR attempted to negate with overflow
59+
i64::MIN - 1,
60+
//~^ ERROR attempted to sub with overflow
61+
i64::MAX + 1,
62+
//~^ ERROR attempted to add with overflow
63+
i64::MAX * 2,
64+
//~^ ERROR attempted to mul with overflow
65+
);
66+
67+
const VALS_U8: (u8, u8, u8, u8) =
68+
(-u8::MIN,
69+
//~^ WARNING negation of unsigned int variable may be unintentional
70+
// (The above is separately linted; unsigned negation is defined to be !x+1.)
71+
u8::MIN - 1,
72+
//~^ ERROR attempted to sub with overflow
73+
u8::MAX + 1,
74+
//~^ ERROR attempted to add with overflow
75+
u8::MAX * 2,
76+
//~^ ERROR attempted to mul with overflow
77+
);
78+
79+
const VALS_U16: (u16, u16, u16, u16) =
80+
(-u16::MIN,
81+
//~^ WARNING negation of unsigned int variable may be unintentional
82+
// (The above is separately linted; unsigned negation is defined to be !x+1.)
83+
u16::MIN - 1,
84+
//~^ ERROR attempted to sub with overflow
85+
u16::MAX + 1,
86+
//~^ ERROR attempted to add with overflow
87+
u16::MAX * 2,
88+
//~^ ERROR attempted to mul with overflow
89+
);
90+
91+
const VALS_U32: (u32, u32, u32, u32) =
92+
(-u32::MIN,
93+
//~^ WARNING negation of unsigned int variable may be unintentional
94+
// (The above is separately linted; unsigned negation is defined to be !x+1.)
95+
u32::MIN - 1,
96+
//~^ ERROR attempted to sub with overflow
97+
u32::MAX + 1,
98+
//~^ ERROR attempted to add with overflow
99+
u32::MAX * 2,
100+
//~^ ERROR attempted to mul with overflow
101+
);
102+
103+
const VALS_U64: (u64, u64, u64, u64) =
104+
(-u64::MIN,
105+
//~^ WARNING negation of unsigned int variable may be unintentional
106+
// (The above is separately linted; unsigned negation is defined to be !x+1.)
107+
u64::MIN - 1,
108+
//~^ ERROR attempted to sub with overflow
109+
u64::MAX + 1,
110+
//~^ ERROR attempted to add with overflow
111+
u64::MAX * 2,
112+
//~^ ERROR attempted to mul with overflow
113+
);
114+
115+
fn main() {
116+
foo(VALS_I8);
117+
foo(VALS_I16);
118+
foo(VALS_I32);
119+
foo(VALS_I64);
120+
121+
foo(VALS_U8);
122+
foo(VALS_U16);
123+
foo(VALS_U32);
124+
foo(VALS_U64);
125+
}
126+
127+
fn foo<T:fmt::Debug>(x: T) {
128+
println!("{:?}", x);
129+
}

0 commit comments

Comments
 (0)