From 720293b6407ad883c609efb6badda990edf59769 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 16 Aug 2020 11:08:29 +0200 Subject: [PATCH 1/2] do not premote non-ZST mutable references ever --- .../rustc_mir/src/transform/promote_consts.rs | 18 ++---------------- src/test/ui/consts/promotion-mutable-ref.rs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 16 deletions(-) create mode 100644 src/test/ui/consts/promotion-mutable-ref.rs diff --git a/compiler/rustc_mir/src/transform/promote_consts.rs b/compiler/rustc_mir/src/transform/promote_consts.rs index 852629a45f7a0..1d2295a37dddf 100644 --- a/compiler/rustc_mir/src/transform/promote_consts.rs +++ b/compiler/rustc_mir/src/transform/promote_consts.rs @@ -364,15 +364,7 @@ impl<'tcx> Validator<'_, 'tcx> { // In theory, any zero-sized value could be borrowed // mutably without consequences. However, only &mut [] // is allowed right now, and only in functions. - if self.const_kind - == Some(hir::ConstContext::Static(hir::Mutability::Mut)) - { - // Inside a `static mut`, &mut [...] is also allowed. - match ty.kind() { - ty::Array(..) | ty::Slice(_) => {} - _ => return Err(Unpromotable), - } - } else if let ty::Array(_, len) = ty.kind() { + if let ty::Array(_, len) = ty.kind() { // FIXME(eddyb) the `self.is_non_const_fn` condition // seems unnecessary, given that this is merely a ZST. match len.try_eval_usize(self.tcx, self.param_env) { @@ -673,13 +665,7 @@ impl<'tcx> Validator<'_, 'tcx> { // In theory, any zero-sized value could be borrowed // mutably without consequences. However, only &mut [] // is allowed right now, and only in functions. - if self.const_kind == Some(hir::ConstContext::Static(hir::Mutability::Mut)) { - // Inside a `static mut`, &mut [...] is also allowed. - match ty.kind() { - ty::Array(..) | ty::Slice(_) => {} - _ => return Err(Unpromotable), - } - } else if let ty::Array(_, len) = ty.kind() { + if let ty::Array(_, len) = ty.kind() { // FIXME(eddyb): We only return `Unpromotable` for `&mut []` inside a // const context which seems unnecessary given that this is merely a ZST. match len.try_eval_usize(self.tcx, self.param_env) { 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 0000000000000..d6457b3b2c123 --- /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 cannot 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); +} From 28ddda76b730960cdfd35f188345af0299e29517 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 16 Aug 2020 17:51:49 +0200 Subject: [PATCH 2/2] add compile-fail test for &mut promotion --- src/test/ui/consts/promote-no-mut.rs | 10 +++++++++ src/test/ui/consts/promote-no-mut.stderr | 23 +++++++++++++++++++++ src/test/ui/consts/promotion-mutable-ref.rs | 2 +- 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/consts/promote-no-mut.rs create mode 100644 src/test/ui/consts/promote-no-mut.stderr 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 0000000000000..fb57c8bb93458 --- /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 0000000000000..49d96546ada3f --- /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 index d6457b3b2c123..d103c5a9d236a 100644 --- a/src/test/ui/consts/promotion-mutable-ref.rs +++ b/src/test/ui/consts/promotion-mutable-ref.rs @@ -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]