Skip to content

Commit

Permalink
expand successful-promotion test a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Jan 22, 2021
1 parent f62cecd commit 0c7fd2c
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/test/ui/consts/promotion.rs
Original file line number Diff line number Diff line change
@@ -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]);
}
}

0 comments on commit 0c7fd2c

Please sign in to comment.