]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/const-eval/promoted_errors.rs
Merge commit 'd0cf3481a84e3aa68c2f185c460e282af36ebc42' into clippyup
[rust.git] / src / test / ui / consts / const-eval / promoted_errors.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 // build-pass
7 // ignore-pass (test emits codegen-time warnings and verifies that they are not errors)
8
9 //! This test ensures that when we promote code that fails to evaluate, the build still succeeds.
10
11 #![warn(const_err, arithmetic_overflow, unconditional_panic)]
12
13 // The only way to have promoteds that fail is in `const fn` called from `const`/`static`.
14 const fn overflow() -> u32 {
15     0 - 1
16     //[opt_with_overflow_checks,noopt]~^ WARN any use of this value will cause an error
17     //[opt_with_overflow_checks,noopt]~| WARN this was previously accepted by the compiler
18 }
19 const fn div_by_zero1() -> i32 {
20     1 / 0
21     //[opt]~^ WARN any use of this value will cause an error
22     //[opt]~| WARN this was previously accepted by the compiler but is being phased out
23 }
24 const fn div_by_zero2() -> i32 {
25     1 / (1 - 1)
26 }
27 const fn div_by_zero3() -> i32 {
28     1 / (false as i32)
29 }
30 const fn oob() -> i32 {
31     [1, 2, 3][4]
32 }
33
34 // An unused constant containing failing promoteds.
35 // This should work as long as `const_err` can be turned into just a warning;
36 // once it turns into a hard error, just remove `X`.
37 const X: () = {
38     let _x: &'static u32 = &overflow();
39     //[opt_with_overflow_checks,noopt]~^ WARN any use of this value will cause an error
40     //[opt_with_overflow_checks,noopt]~| WARN this was previously accepted by the compiler
41     let _x: &'static i32 = &div_by_zero1();
42     //[opt]~^ WARN any use of this value will cause an error
43     //[opt]~| WARN this was previously accepted by the compiler but is being phased out
44     let _x: &'static i32 = &div_by_zero2();
45     let _x: &'static i32 = &div_by_zero3();
46     let _x: &'static i32 = &oob();
47 };
48
49 const fn mk_false() -> bool { false }
50
51 // An actually used constant referencing failing promoteds in dead code.
52 // This needs to always work.
53 const Y: () = {
54     if mk_false() {
55         let _x: &'static u32 = &overflow();
56         let _x: &'static i32 = &div_by_zero1();
57         let _x: &'static i32 = &div_by_zero2();
58         let _x: &'static i32 = &div_by_zero3();
59         let _x: &'static i32 = &oob();
60     }
61     ()
62 };
63
64 fn main() {
65     let _y = Y;
66 }