2020-09-06 14:06:39 +00:00
|
|
|
// ignore-tidy-linelength
|
|
|
|
// Test various things that we do not want to promote.
|
2022-09-21 11:05:20 +00:00
|
|
|
#![allow(unconditional_panic)]
|
2020-09-06 14:06:39 +00:00
|
|
|
|
2020-10-25 16:46:01 +00:00
|
|
|
use std::cell::Cell;
|
|
|
|
|
2020-09-06 14:06:39 +00:00
|
|
|
// We do not promote mutable references.
|
|
|
|
static mut TEST1: Option<&mut [i32]> = Some(&mut [1, 2, 3]); //~ ERROR temporary value dropped while borrowed
|
|
|
|
|
|
|
|
static mut TEST2: &'static mut [i32] = {
|
|
|
|
let x = &mut [1,2,3]; //~ ERROR temporary value dropped while borrowed
|
|
|
|
x
|
|
|
|
};
|
|
|
|
|
|
|
|
// We do not promote fn calls in `fn`, including `const fn`.
|
|
|
|
pub const fn promote_cal(b: bool) -> i32 {
|
|
|
|
const fn foo() { [()][42] }
|
|
|
|
|
|
|
|
if b {
|
|
|
|
let _x: &'static () = &foo(); //~ ERROR temporary value dropped while borrowed
|
|
|
|
}
|
|
|
|
13
|
|
|
|
}
|
|
|
|
|
|
|
|
// We do not promote union field accesses in `fn.
|
|
|
|
union U { x: i32, y: i32 }
|
|
|
|
pub const fn promote_union() {
|
|
|
|
let _x: &'static i32 = &unsafe { U { x: 0 }.x }; //~ ERROR temporary value dropped while borrowed
|
|
|
|
}
|
|
|
|
|
2020-10-04 13:25:26 +00:00
|
|
|
// We do not promote union field accesses in `const`, either.
|
|
|
|
const TEST_UNION: () = {
|
|
|
|
let _x: &'static i32 = &unsafe { U { x: 0 }.x }; //~ ERROR temporary value dropped while borrowed
|
|
|
|
};
|
|
|
|
|
2020-10-25 17:06:41 +00:00
|
|
|
// In a `const`, we do not promote things with interior mutability. Not even if we "project it away".
|
|
|
|
const TEST_INTERIOR_MUT: () = {
|
|
|
|
// The "0." case is already ruled out by not permitting any interior mutability in `const`.
|
|
|
|
let _val: &'static _ = &(Cell::new(1), 2).1; //~ ERROR temporary value dropped while borrowed
|
|
|
|
};
|
|
|
|
|
2021-10-31 00:00:00 +00:00
|
|
|
const TEST_DROP: String = String::new();
|
|
|
|
|
2020-10-25 16:46:01 +00:00
|
|
|
fn main() {
|
2020-10-25 17:06:41 +00:00
|
|
|
// We must not promote things with interior mutability. Not even if we "project it away".
|
2020-10-25 16:46:01 +00:00
|
|
|
let _val: &'static _ = &(Cell::new(1), 2).0; //~ ERROR temporary value dropped while borrowed
|
2020-10-25 17:06:41 +00:00
|
|
|
let _val: &'static _ = &(Cell::new(1), 2).1; //~ ERROR temporary value dropped while borrowed
|
2021-01-01 13:47:45 +00:00
|
|
|
|
|
|
|
// No promotion of fallible operations.
|
|
|
|
let _val: &'static _ = &(1/0); //~ ERROR temporary value dropped while borrowed
|
|
|
|
let _val: &'static _ = &(1/(1-1)); //~ ERROR temporary value dropped while borrowed
|
2024-02-23 17:00:51 +00:00
|
|
|
let _val: &'static _ = &((1+1)/(1-1)); //~ ERROR temporary value dropped while borrowed
|
|
|
|
let _val: &'static _ = &(i32::MIN/-1); //~ ERROR temporary value dropped while borrowed
|
|
|
|
let _val: &'static _ = &(i32::MIN/(0-1)); //~ ERROR temporary value dropped while borrowed
|
|
|
|
let _val: &'static _ = &(-128i8/-1); //~ ERROR temporary value dropped while borrowed
|
2021-01-01 13:47:45 +00:00
|
|
|
let _val: &'static _ = &(1%0); //~ ERROR temporary value dropped while borrowed
|
|
|
|
let _val: &'static _ = &(1%(1-1)); //~ ERROR temporary value dropped while borrowed
|
|
|
|
let _val: &'static _ = &([1,2,3][4]+1); //~ ERROR temporary value dropped while borrowed
|
2021-10-31 00:00:00 +00:00
|
|
|
|
|
|
|
// No promotion of temporaries that need to be dropped.
|
|
|
|
let _val: &'static _ = &TEST_DROP;
|
|
|
|
//~^ ERROR temporary value dropped while borrowed
|
|
|
|
let _val: &'static _ = &&TEST_DROP;
|
|
|
|
//~^ ERROR temporary value dropped while borrowed
|
|
|
|
//~| ERROR temporary value dropped while borrowed
|
|
|
|
let _val: &'static _ = &(&TEST_DROP,);
|
|
|
|
//~^ ERROR temporary value dropped while borrowed
|
|
|
|
//~| ERROR temporary value dropped while borrowed
|
|
|
|
let _val: &'static _ = &[&TEST_DROP; 1];
|
|
|
|
//~^ ERROR temporary value dropped while borrowed
|
|
|
|
//~| ERROR temporary value dropped while borrowed
|
2020-10-25 16:46:01 +00:00
|
|
|
}
|