]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/promotion.rs
Do not suggest `let_else` if no bindings would be introduced
[rust.git] / src / test / 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 #[allow(arithmetic_overflow)]
8
9 const fn assert_static<T>(_: &'static T) {}
10
11 const fn fail() -> i32 { 1/0 }
12 const C: i32 = {
13     // Promoted that fails to evaluate in dead code -- this must work
14     // (for backwards compatibility reasons).
15     if false {
16         assert_static(&fail());
17     }
18     42
19 };
20
21 fn main() {
22     assert_static(&["a", "b", "c"]);
23     assert_static(&["d", "e", "f"]);
24     assert_eq!(C, 42);
25
26     // make sure that these do not cause trouble despite overflowing
27     assert_static(&(0-1));
28     assert_static(&-i32::MIN);
29
30     // div-by-non-0 is okay
31     assert_static(&(1/1));
32     assert_static(&(1%1));
33
34     // in-bounds array access is okay
35     assert_static(&([1,2,3][0] + 1));
36     assert_static(&[[1,2][1]]);
37
38     // Top-level projections are not part of the promoted, so no error here.
39     if false {
40         #[allow(unconditional_panic)]
41         assert_static(&[1,2,3][4]);
42     }
43 }