]> git.lizzy.rs Git - rust.git/blob - tests/ui/consts/promotion.rs
Rollup merge of #106951 - tmiasko:rm-simplify-initial, r=oli-obk
[rust.git] / tests / ui / consts / promotion.rs
1 // revisions: noopt opt opt_with_overflow_checks
2 //[noopt]compile-flags: -C opt-level=0
3 //[opt]compile-flags: -O
4 //[opt_with_overflow_checks]compile-flags: -C overflow-checks=on -O
5
6 // build-pass
7
8 const fn assert_static<T>(_: &'static T) {}
9
10 #[allow(unconditional_panic)]
11 const fn fail() -> i32 {
12     1/0
13 }
14 const C: i32 = {
15     // Promoted that fails to evaluate in dead code -- this must work
16     // (for backwards compatibility reasons).
17     if false {
18         assert_static(&fail());
19     }
20     42
21 };
22
23 fn main() {
24     assert_static(&["a", "b", "c"]);
25     assert_static(&["d", "e", "f"]);
26     assert_eq!(C, 42);
27
28     // make sure that these do not cause trouble despite overflowing
29     assert_static(&(0-1));
30     assert_static(&-i32::MIN);
31
32     // div-by-non-0 is okay
33     assert_static(&(1/1));
34     assert_static(&(1%1));
35
36     // in-bounds array access is okay
37     assert_static(&([1,2,3][0] + 1));
38     assert_static(&[[1,2][1]]);
39
40     // Top-level projections are not part of the promoted, so no error here.
41     if false {
42         #[allow(unconditional_panic)]
43         assert_static(&[1,2,3][4]);
44     }
45 }