diff options
| author | bors <bors@rust-lang.org> | 2020-09-08 05:13:42 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2020-09-08 05:13:42 +0000 |
| commit | e82584a77d019bd5ee4254b5870270b1493763a6 (patch) | |
| tree | b604a20d6fd8c1fae5f042ea0d686e91374410d2 /src/test | |
| parent | fa79db83f6c48c032bb149fc32999f64187c6f93 (diff) | |
| parent | 28ddda76b730960cdfd35f188345af0299e29517 (diff) | |
| download | rust-e82584a77d019bd5ee4254b5870270b1493763a6.tar.gz rust-e82584a77d019bd5ee4254b5870270b1493763a6.zip | |
Auto merge of #75585 - RalfJung:demotion, r=oli-obk
Do not promote &mut of a non-ZST ever
Since ~pre-1.0~ 1.36, we have accepted code like this:
```rust
static mut TEST: &'static mut [i32] = {
let x = &mut [1,2,3];
x
};
```
I tracked it back to https://github.com/rust-lang/rust/pull/21744, but unfortunately could not find any discussion or RFC that would explain why we thought this was a good idea. And it's not, it breaks all sorts of things -- see https://github.com/rust-lang/rust/issues/75556.
To fix https://github.com/rust-lang/rust/issues/75556, we have to stop promoting non-ZST mutable references no matter the context, which is what this PR does. It's a breaking change.
Notice that this still works, since it does not rely on promotion:
```rust
static mut TEST: &'static mut [i32] = &mut [0,1,2];
```
Cc `@rust-lang/wg-const-eval`
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/ui/consts/promote-no-mut.rs | 10 | ||||
| -rw-r--r-- | src/test/ui/consts/promote-no-mut.stderr | 23 | ||||
| -rw-r--r-- | src/test/ui/consts/promotion-mutable-ref.rs | 17 |
3 files changed, 50 insertions, 0 deletions
diff --git a/src/test/ui/consts/promote-no-mut.rs b/src/test/ui/consts/promote-no-mut.rs new file mode 100644 index 00000000000..fb57c8bb934 --- /dev/null +++ b/src/test/ui/consts/promote-no-mut.rs @@ -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() {} diff --git a/src/test/ui/consts/promote-no-mut.stderr b/src/test/ui/consts/promote-no-mut.stderr new file mode 100644 index 00000000000..49d96546ada --- /dev/null +++ b/src/test/ui/consts/promote-no-mut.stderr @@ -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`. diff --git a/src/test/ui/consts/promotion-mutable-ref.rs b/src/test/ui/consts/promotion-mutable-ref.rs new file mode 100644 index 00000000000..d103c5a9d23 --- /dev/null +++ b/src/test/ui/consts/promotion-mutable-ref.rs @@ -0,0 +1,17 @@ +// run-pass +#![feature(const_mut_refs)] + +static mut TEST: i32 = { + // 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] +}; + +// This still works -- it's not done via promotion. +#[allow(unused)] +static mut TEST2: &'static mut [i32] = &mut [0,1,2]; + +fn main() { + assert_eq!(unsafe { TEST }, 2); +} |
