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
7 // ignore-pass (test emits codegen-time warnings and verifies that they are not errors)
9 //! This test ensures that when we promote code that fails to evaluate, the build still succeeds.
11 #![warn(const_err, arithmetic_overflow, unconditional_panic)]
13 // The only way to have promoteds that fail is in `const fn` called from `const`/`static`.
14 const fn overflow() -> u32 {
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
19 const fn div_by_zero1() -> i32 {
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
24 const fn div_by_zero2() -> i32 {
27 const fn div_by_zero3() -> i32 {
30 const fn oob() -> i32 {
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`.
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();
49 const fn mk_false() -> bool { false }
51 // An actually used constant referencing failing promoteds in dead code.
52 // This needs to always work.
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();