Skip to content

Commit 720293b

Browse files
committed
do not premote non-ZST mutable references ever
1 parent 6c6003a commit 720293b

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

compiler/rustc_mir/src/transform/promote_consts.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -364,15 +364,7 @@ impl<'tcx> Validator<'_, 'tcx> {
364364
// In theory, any zero-sized value could be borrowed
365365
// mutably without consequences. However, only &mut []
366366
// is allowed right now, and only in functions.
367-
if self.const_kind
368-
== Some(hir::ConstContext::Static(hir::Mutability::Mut))
369-
{
370-
// Inside a `static mut`, &mut [...] is also allowed.
371-
match ty.kind() {
372-
ty::Array(..) | ty::Slice(_) => {}
373-
_ => return Err(Unpromotable),
374-
}
375-
} else if let ty::Array(_, len) = ty.kind() {
367+
if let ty::Array(_, len) = ty.kind() {
376368
// FIXME(eddyb) the `self.is_non_const_fn` condition
377369
// seems unnecessary, given that this is merely a ZST.
378370
match len.try_eval_usize(self.tcx, self.param_env) {
@@ -673,13 +665,7 @@ impl<'tcx> Validator<'_, 'tcx> {
673665
// In theory, any zero-sized value could be borrowed
674666
// mutably without consequences. However, only &mut []
675667
// is allowed right now, and only in functions.
676-
if self.const_kind == Some(hir::ConstContext::Static(hir::Mutability::Mut)) {
677-
// Inside a `static mut`, &mut [...] is also allowed.
678-
match ty.kind() {
679-
ty::Array(..) | ty::Slice(_) => {}
680-
_ => return Err(Unpromotable),
681-
}
682-
} else if let ty::Array(_, len) = ty.kind() {
668+
if let ty::Array(_, len) = ty.kind() {
683669
// FIXME(eddyb): We only return `Unpromotable` for `&mut []` inside a
684670
// const context which seems unnecessary given that this is merely a ZST.
685671
match len.try_eval_usize(self.tcx, self.param_env) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// run-pass
2+
#![feature(const_mut_refs)]
3+
4+
static mut TEST: i32 = {
5+
// We cannot promote this, as CTFE needs to be able to mutate it later.
6+
let x = &mut [1,2,3];
7+
x[0] += 1;
8+
x[0]
9+
};
10+
11+
// This still works -- it's not done via promotion.
12+
#[allow(unused)]
13+
static mut TEST2: &'static mut [i32] = &mut [0,1,2];
14+
15+
fn main() {
16+
assert_eq!(unsafe { TEST }, 2);
17+
}

0 commit comments

Comments
 (0)