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