]>
git.proxmox.com Git - rustc.git/blob - 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)]
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
10 static mut TEST2
: &'
static mut [i32] = {
11 let x
= &mut [1,2,3]; //~ ERROR temporary value dropped while borrowed
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] }
20 let _x
: &'
static () = &foo(); //~ ERROR temporary value dropped while borrowed
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
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
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
42 const TEST_DROP
: String
= String
::new();
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
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
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