2020-02-19 04:25:41 -06:00
|
|
|
// revisions: noopt opt opt_with_overflow_checks
|
2020-02-15 03:51:51 -06:00
|
|
|
//[noopt]compile-flags: -C opt-level=0
|
2020-02-15 03:47:27 -06:00
|
|
|
//[opt]compile-flags: -O
|
|
|
|
//[opt_with_overflow_checks]compile-flags: -C overflow-checks=on -O
|
|
|
|
|
2020-01-08 14:31:08 -06:00
|
|
|
// build-pass
|
2020-04-19 08:06:00 -05:00
|
|
|
// ignore-pass (test emits codegen-time warnings and verifies that they are not errors)
|
2019-06-12 15:53:00 -05:00
|
|
|
|
2021-05-09 07:56:19 -05:00
|
|
|
//! This test ensures that when we promote code that fails to evaluate, the build still succeeds.
|
|
|
|
|
2022-09-21 06:05:20 -05:00
|
|
|
#![warn(arithmetic_overflow, unconditional_panic)]
|
2019-06-12 15:53:00 -05:00
|
|
|
|
2021-01-01 07:47:45 -06:00
|
|
|
// The only way to have promoteds that fail is in `const fn` called from `const`/`static`.
|
|
|
|
const fn overflow() -> u32 {
|
2021-01-22 03:56:28 -06:00
|
|
|
0 - 1
|
2022-09-21 06:05:20 -05:00
|
|
|
//~^ WARN this arithmetic operation will overflow
|
2021-01-01 07:47:45 -06:00
|
|
|
}
|
|
|
|
const fn div_by_zero1() -> i32 {
|
2021-01-22 03:56:28 -06:00
|
|
|
1 / 0
|
2022-09-21 06:05:20 -05:00
|
|
|
//~^ WARN this operation will panic at runtime
|
2021-01-01 07:47:45 -06:00
|
|
|
}
|
|
|
|
const fn div_by_zero2() -> i32 {
|
2021-01-30 07:49:22 -06:00
|
|
|
1 / (1 - 1)
|
2022-03-08 11:20:31 -06:00
|
|
|
//~^ WARN this operation will panic at runtime
|
2021-01-01 07:47:45 -06:00
|
|
|
}
|
|
|
|
const fn div_by_zero3() -> i32 {
|
|
|
|
1 / (false as i32)
|
2022-03-08 11:20:31 -06:00
|
|
|
//~^ WARN this operation will panic at runtime
|
2021-01-01 07:47:45 -06:00
|
|
|
}
|
|
|
|
const fn oob() -> i32 {
|
2021-01-30 07:49:22 -06:00
|
|
|
[1, 2, 3][4]
|
2022-03-08 11:20:31 -06:00
|
|
|
//~^ WARN this operation will panic at runtime
|
2021-01-01 07:47:45 -06:00
|
|
|
}
|
|
|
|
|
2021-05-09 07:56:19 -05: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;
|
|
|
|
}
|