add compile-fail test for &mut promotion

This commit is contained in:
Ralf Jung 2020-08-16 17:51:49 +02:00
parent 720293b640
commit 28ddda76b7
3 changed files with 34 additions and 1 deletions

View file

@ -0,0 +1,10 @@
// ignore-tidy-linelength
// 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
};
fn main() {}

View file

@ -0,0 +1,23 @@
error[E0716]: temporary value dropped while borrowed
--> $DIR/promote-no-mut.rs:3:50
|
LL | static mut TEST1: Option<&mut [i32]> = Some(&mut [1, 2, 3]);
| ----------^^^^^^^^^-
| | | |
| | | temporary value is freed at the end of this statement
| | creates a temporary which is freed while still in use
| using this value as a static requires that borrow lasts for `'static`
error[E0716]: temporary value dropped while borrowed
--> $DIR/promote-no-mut.rs:6:18
|
LL | let x = &mut [1,2,3];
| ^^^^^^^ creates a temporary which is freed while still in use
LL | x
| - using this value as a static requires that borrow lasts for `'static`
LL | };
| - temporary value is freed at the end of this statement
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0716`.

View file

@ -2,7 +2,7 @@
#![feature(const_mut_refs)]
static mut TEST: i32 = {
// We cannot promote this, as CTFE needs to be able to mutate it later.
// We must not promote this, as CTFE needs to be able to mutate it later.
let x = &mut [1,2,3];
x[0] += 1;
x[0]