rust/src/test/ui/consts/const-eval/promoted_errors.rs

42 lines
1.2 KiB
Rust
Raw Normal View History

2020-02-19 10:25:41 +00:00
// revisions: noopt opt opt_with_overflow_checks
//[noopt]compile-flags: -C opt-level=0
//[opt]compile-flags: -O
//[opt_with_overflow_checks]compile-flags: -C overflow-checks=on -O
// build-pass
// ignore-pass (test emits codegen-time warnings and verifies that they are not errors)
2019-06-12 20:53:00 +00:00
2020-02-18 21:49:47 +00:00
#![warn(const_err, arithmetic_overflow, unconditional_panic)]
2019-06-12 20:53:00 +00:00
// The only way to have promoteds that fail is in `const fn` called from `const`/`static`.
const fn overflow() -> u32 {
2021-01-22 09:56:28 +00:00
0 - 1
//[opt_with_overflow_checks,noopt]~^ WARN any use of this value will cause an error
}
const fn div_by_zero1() -> i32 {
2021-01-22 09:56:28 +00:00
1 / 0
//[opt]~^ WARN any use of this value will cause an error
}
const fn div_by_zero2() -> i32 {
1 / (1-1)
}
const fn div_by_zero3() -> i32 {
1 / (false as i32)
}
const fn oob() -> i32 {
[1,2,3][4]
}
const X: () = {
let _x: &'static u32 = &overflow();
//[opt_with_overflow_checks,noopt]~^ WARN any use of this value will cause an error
let _x: &'static i32 = &div_by_zero1();
//[opt]~^ WARN any use of this value will cause an error
let _x: &'static i32 = &div_by_zero2();
let _x: &'static i32 = &div_by_zero3();
let _x: &'static i32 = &oob();
};
fn main() {
}