]> git.lizzy.rs Git - rust.git/blob - src/test/ui/associated-consts/issue-69020-assoc-const-arith-overflow.rs
Rollup merge of #105983 - compiler-errors:issue-105981, r=tmiasko
[rust.git] / src / test / ui / associated-consts / issue-69020-assoc-const-arith-overflow.rs
1 // revisions: noopt opt opt_with_overflow_checks
2 //[noopt]compile-flags: -C opt-level=0
3 //[opt]compile-flags: -O
4 //[opt_with_overflow_checks]compile-flags: -C overflow-checks=on -O
5
6 #![crate_type="lib"]
7
8 pub trait Foo {
9     const NEG: i32;
10     const NEG_REV: i32;
11
12     const ADD: i32;
13     const ADD_REV: i32;
14
15     const DIV: i32;
16     const DIV_REV: i32;
17
18     const OOB: i32;
19     const OOB_REV: i32;
20 }
21
22 // These constants cannot be evaluated already (they depend on `T::N`), so they can just be linted
23 // like normal run-time code. But codegen works a bit different in const context, so this test
24 // makes sure that we still catch overflow. Also make sure we emit the same lints if we reverse the
25 // operands (so that the generic operand comes first).
26 impl<T: Foo> Foo for Vec<T> {
27     const NEG: i32 = -i32::MIN + T::NEG;
28     //~^ ERROR arithmetic operation will overflow
29     const NEG_REV: i32 = T::NEG + (-i32::MIN);
30     //~^ ERROR arithmetic operation will overflow
31
32     const ADD: i32 = (i32::MAX+1) + T::ADD;
33     //~^ ERROR arithmetic operation will overflow
34     const ADD_REV: i32 =  T::ADD + (i32::MAX+1);
35     //~^ ERROR arithmetic operation will overflow
36
37     const DIV: i32 = (1/0) + T::DIV;
38     //~^ ERROR operation will panic
39     const DIV_REV: i32 = T::DIV + (1/0);
40     //~^ ERROR operation will panic
41
42     const OOB: i32 = [1][1] + T::OOB;
43     //~^ ERROR operation will panic
44     const OOB_REV: i32 = T::OOB + [1][1];
45     //~^ ERROR operation will panic
46 }