]>
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)]
4 #![feature(const_fn_union)]
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
11 static mut TEST2
: &'
static mut [i32] = {
12 let x
= &mut [1,2,3]; //~ ERROR temporary value dropped while borrowed
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] }
21 let _x
: &'
static () = &foo(); //~ ERROR temporary value dropped while borrowed
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
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
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
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
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