]> git.lizzy.rs Git - rust.git/blob - src/test/ui/associated-consts/issue-69020-assoc-const-arith-overflow.rs
850f65ae9d183b9c8a70f874b4c51dcd80fa9182
[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 use std::i32;
9
10 pub trait Foo {
11     const NEG: i32;
12     const NEG_REV: i32;
13
14     const ADD: i32;
15     const ADD_REV: i32;
16
17     const DIV: i32;
18     const DIV_REV: i32;
19
20     const OOB: i32;
21     const OOB_REV: i32;
22 }
23
24 // These constants cannot be evaluated already (they depend on `T::N`), so they can just be linted
25 // like normal run-time code. But codegen works a bit different in const context, so this test
26 // makes sure that we still catch overflow. Also make sure we emit the same lints if we reverse the
27 // operands (so that the generic operand comes first).
28 impl<T: Foo> Foo for Vec<T> {
29     const NEG: i32 = -i32::MIN + T::NEG;
30     //~^ ERROR arithmetic operation will overflow
31     const NEG_REV: i32 = T::NEG + (-i32::MIN);
32     //~^ ERROR arithmetic operation will overflow
33
34     const ADD: i32 = (i32::MAX+1) + T::ADD;
35     //~^ ERROR arithmetic operation will overflow
36     const ADD_REV: i32 =  T::ADD + (i32::MAX+1);
37     //~^ ERROR arithmetic operation will overflow
38
39     const DIV: i32 = (1/0) + T::DIV;
40     //~^ ERROR operation will panic
41     const DIV_REV: i32 = T::DIV + (1/0);
42     //~^ ERROR operation will panic
43
44     const OOB: i32 = [1][1] + T::OOB;
45     //~^ ERROR operation will panic
46     const OOB_REV: i32 = T::OOB + [1][1];
47     //~^ ERROR operation will panic
48 }