expand successful-promotion test a bit

This commit is contained in:
Ralf Jung 2021-01-06 20:11:23 +01:00
parent f62cecd807
commit 0c7fd2c685

View File

@ -1,41 +1,43 @@
// run-pass
// 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
// compile-flags: -O
// build-pass
#[allow(arithmetic_overflow)]
fn foo(_: &'static [&'static str]) {}
fn bar(_: &'static [&'static str; 3]) {}
const fn baz_i32(_: &'static i32) {}
const fn baz_u32(_: &'static u32) {}
const fn assert_static<T>(_: &'static T) {}
const fn fail() -> i32 { 1/0 }
const C: i32 = {
// Promoted that fails to evaluate in dead code -- this must work
// (for backwards compatibility reasons).
if false {
baz_i32(&fail());
assert_static(&fail());
}
42
};
fn main() {
foo(&["a", "b", "c"]);
bar(&["d", "e", "f"]);
assert_static(&["a", "b", "c"]);
assert_static(&["d", "e", "f"]);
assert_eq!(C, 42);
// make sure that these do not cause trouble despite overflowing
baz_u32(&(0-1));
baz_i32(&-i32::MIN);
assert_static(&(0-1));
assert_static(&-i32::MIN);
// div-by-non-0 is okay
baz_i32(&(1/1));
baz_i32(&(1%1));
assert_static(&(1/1));
assert_static(&(1%1));
// in-bounds array access is okay
baz_i32(&([1,2,3][0] + 1));
assert_static(&([1,2,3][0] + 1));
assert_static(&[[1,2][1]]);
// Top-level projections do not get promoted, so no error here.
// Top-level projections are not part of the promoted, so no error here.
if false {
#[allow(unconditional_panic)]
baz_i32(&[1,2,3][4]);
assert_static(&[1,2,3][4]);
}
}