2020-02-19 11:25:41 +01:00
|
|
|
// revisions: noopt opt opt_with_overflow_checks
|
2020-02-15 10:51:51 +01:00
|
|
|
//[noopt]compile-flags: -C opt-level=0
|
2020-02-15 10:47:27 +01:00
|
|
|
//[opt]compile-flags: -O
|
|
|
|
|
//[opt_with_overflow_checks]compile-flags: -C overflow-checks=on -O
|
|
|
|
|
|
2020-01-08 21:31:08 +01:00
|
|
|
// build-pass
|
2020-04-19 16:06:00 +03:00
|
|
|
// ignore-pass (test emits codegen-time warnings and verifies that they are not errors)
|
2019-06-12 22:53:00 +02:00
|
|
|
|
2021-05-09 14:56:19 +02:00
|
|
|
//! This test ensures that when we promote code that fails to evaluate, the build still succeeds.
|
|
|
|
|
|
2020-02-18 22:49:47 +01:00
|
|
|
#![warn(const_err, arithmetic_overflow, unconditional_panic)]
|
2019-06-12 22:53:00 +02:00
|
|
|
|
2021-01-01 14:47:45 +01:00
|
|
|
// The only way to have promoteds that fail is in `const fn` called from `const`/`static`.
|
|
|
|
|
const fn overflow() -> u32 {
|
2021-01-22 10:56:28 +01:00
|
|
|
0 - 1
|
2021-01-01 14:47:45 +01:00
|
|
|
//[opt_with_overflow_checks,noopt]~^ WARN any use of this value will cause an error
|
2021-01-30 14:49:22 +01:00
|
|
|
//[opt_with_overflow_checks,noopt]~| WARN this was previously accepted by the compiler
|
2021-01-01 14:47:45 +01:00
|
|
|
}
|
|
|
|
|
const fn div_by_zero1() -> i32 {
|
2021-01-22 10:56:28 +01:00
|
|
|
1 / 0
|
2021-01-01 14:47:45 +01:00
|
|
|
//[opt]~^ WARN any use of this value will cause an error
|
2021-01-30 14:49:22 +01:00
|
|
|
//[opt]~| WARN this was previously accepted by the compiler but is being phased out
|
2021-01-01 14:47:45 +01:00
|
|
|
}
|
|
|
|
|
const fn div_by_zero2() -> i32 {
|
2021-01-30 14:49:22 +01:00
|
|
|
1 / (1 - 1)
|
2021-01-01 14:47:45 +01:00
|
|
|
}
|
|
|
|
|
const fn div_by_zero3() -> i32 {
|
|
|
|
|
1 / (false as i32)
|
|
|
|
|
}
|
|
|
|
|
const fn oob() -> i32 {
|
2021-01-30 14:49:22 +01:00
|
|
|
[1, 2, 3][4]
|
2021-01-01 14:47:45 +01:00
|
|
|
}
|
|
|
|
|
|
2021-05-09 14:56:19 +02:00
|
|
|
// An unused constant containing failing promoteds.
|
|
|
|
|
// This should work as long as `const_err` can be turned into just a warning;
|
|
|
|
|
// once it turns into a hard error, just remove `X`.
|
2021-01-01 14:47:45 +01:00
|
|
|
const X: () = {
|
|
|
|
|
let _x: &'static u32 = &overflow();
|
|
|
|
|
//[opt_with_overflow_checks,noopt]~^ WARN any use of this value will cause an error
|
2021-01-30 14:49:22 +01:00
|
|
|
//[opt_with_overflow_checks,noopt]~| WARN this was previously accepted by the compiler
|
2021-01-01 14:47:45 +01:00
|
|
|
let _x: &'static i32 = &div_by_zero1();
|
|
|
|
|
//[opt]~^ WARN any use of this value will cause an error
|
2021-01-30 14:49:22 +01:00
|
|
|
//[opt]~| WARN this was previously accepted by the compiler but is being phased out
|
2021-01-01 14:47:45 +01:00
|
|
|
let _x: &'static i32 = &div_by_zero2();
|
|
|
|
|
let _x: &'static i32 = &div_by_zero3();
|
|
|
|
|
let _x: &'static i32 = &oob();
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-09 14:56:19 +02:00
|
|
|
const fn mk_false() -> bool { false }
|
|
|
|
|
|
|
|
|
|
// An actually used constant referencing failing promoteds in dead code.
|
|
|
|
|
// This needs to always work.
|
|
|
|
|
const Y: () = {
|
|
|
|
|
if mk_false() {
|
|
|
|
|
let _x: &'static u32 = &overflow();
|
|
|
|
|
let _x: &'static i32 = &div_by_zero1();
|
|
|
|
|
let _x: &'static i32 = &div_by_zero2();
|
|
|
|
|
let _x: &'static i32 = &div_by_zero3();
|
|
|
|
|
let _x: &'static i32 = &oob();
|
|
|
|
|
}
|
|
|
|
|
()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
let _y = Y;
|
|
|
|
|
}
|