]> git.proxmox.com Git - rustc.git/blob - src/test/ui/consts/promote-not.rs
New upstream version 1.53.0+dfsg1
[rustc.git] / src / test / ui / consts / promote-not.rs
1 // ignore-tidy-linelength
2 // Test various things that we do not want to promote.
3 #![allow(unconditional_panic, const_err)]
4 #![feature(const_fn_union)]
5
6 use std::cell::Cell;
7
8 // We do not promote mutable references.
9 static mut TEST1: Option<&mut [i32]> = Some(&mut [1, 2, 3]); //~ ERROR temporary value dropped while borrowed
10
11 static mut TEST2: &'static mut [i32] = {
12 let x = &mut [1,2,3]; //~ ERROR temporary value dropped while borrowed
13 x
14 };
15
16 // We do not promote fn calls in `fn`, including `const fn`.
17 pub const fn promote_cal(b: bool) -> i32 {
18 const fn foo() { [()][42] }
19
20 if b {
21 let _x: &'static () = &foo(); //~ ERROR temporary value dropped while borrowed
22 }
23 13
24 }
25
26 // We do not promote union field accesses in `fn.
27 union U { x: i32, y: i32 }
28 pub const fn promote_union() {
29 let _x: &'static i32 = &unsafe { U { x: 0 }.x }; //~ ERROR temporary value dropped while borrowed
30 }
31
32 // We do not promote union field accesses in `const`, either.
33 const TEST_UNION: () = {
34 let _x: &'static i32 = &unsafe { U { x: 0 }.x }; //~ ERROR temporary value dropped while borrowed
35 };
36
37 // In a `const`, we do not promote things with interior mutability. Not even if we "project it away".
38 const TEST_INTERIOR_MUT: () = {
39 // The "0." case is already ruled out by not permitting any interior mutability in `const`.
40 let _val: &'static _ = &(Cell::new(1), 2).1; //~ ERROR temporary value dropped while borrowed
41 };
42
43 fn main() {
44 // We must not promote things with interior mutability. Not even if we "project it away".
45 let _val: &'static _ = &(Cell::new(1), 2).0; //~ ERROR temporary value dropped while borrowed
46 let _val: &'static _ = &(Cell::new(1), 2).1; //~ ERROR temporary value dropped while borrowed
47
48 // No promotion of fallible operations.
49 let _val: &'static _ = &(1/0); //~ ERROR temporary value dropped while borrowed
50 let _val: &'static _ = &(1/(1-1)); //~ ERROR temporary value dropped while borrowed
51 let _val: &'static _ = &(1%0); //~ ERROR temporary value dropped while borrowed
52 let _val: &'static _ = &(1%(1-1)); //~ ERROR temporary value dropped while borrowed
53 let _val: &'static _ = &([1,2,3][4]+1); //~ ERROR temporary value dropped while borrowed
54 }