Skip to content

Commit

Permalink
static mut: allow reference to arbitrary types, not just slices and a…
Browse files Browse the repository at this point in the history
…rrays
  • Loading branch information
RalfJung committed Nov 5, 2023
1 parent 8acf40b commit b85509f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 43 deletions.
34 changes: 10 additions & 24 deletions compiler/rustc_const_eval/src/transform/check_consts/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,35 +471,21 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
}
}

Rvalue::Ref(_, BorrowKind::Mut { .. }, place) => {
let ty = place.ty(self.body, self.tcx).ty;
let is_allowed = match ty.kind() {
// Inside a `static mut`, `&mut [...]` is allowed.
ty::Array(..) | ty::Slice(_)
if self.const_kind() == hir::ConstContext::Static(hir::Mutability::Mut) =>
{
true
}

// FIXME(ecstaticmorse): We could allow `&mut []` inside a const context given
// that this is merely a ZST and it is already eligible for promotion.
// This may require an RFC?
/*
ty::Array(_, len) if len.try_eval_target_usize(cx.tcx, cx.param_env) == Some(0)
=> true,
*/
_ => false,
};
Rvalue::Ref(_, BorrowKind::Mut { .. }, place) | Rvalue::AddressOf(Mutability::Mut, place) => {
// Inside mutable statics, we allow arbitrary mutable references.
// We've allowed `static mut FOO = &mut [elements];` for a long time (the exact
// reasons why are lost to history), and there is no reason to restrict that to
// arrays and slices.
let is_allowed = self.const_kind() == hir::ConstContext::Static(hir::Mutability::Mut);

if !is_allowed {
self.check_mut_borrow(place.local, hir::BorrowKind::Ref)
self.check_mut_borrow(
place.local,
if matches!(rvalue, Rvalue::Ref(..)) { hir::BorrowKind::Ref } else {hir::BorrowKind::Raw}
);
}
}

Rvalue::AddressOf(Mutability::Mut, place) => {
self.check_mut_borrow(place.local, hir::BorrowKind::Raw)
}

Rvalue::Ref(_, BorrowKind::Shared | BorrowKind::Shallow, place)
| Rvalue::AddressOf(Mutability::Not, place) => {
let borrowed_place_has_mut_interior = qualifs::in_place::<HasMutInterior, _>(
Expand Down
15 changes: 0 additions & 15 deletions tests/ui/array-slice-vec/check-static-mut-slices.rs

This file was deleted.

24 changes: 24 additions & 0 deletions tests/ui/consts/static-mut-refs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// run-pass
#![allow(dead_code)]

// Checks that mutable static items can have mutable slices and other references


static mut TEST: &'static mut [isize] = &mut [1];
static mut EMPTY: &'static mut [isize] = &mut [];
static mut INT: &'static mut isize = &mut 1;

// And the same for raw pointers.

static mut TEST_RAW: *mut [isize] = &mut [1isize] as *mut _;
static mut EMPTY_RAW: *mut [isize] = &mut [] as *mut _;
static mut INT_RAW: *mut isize = &mut 1isize as *mut _;

pub fn main() {
unsafe {
TEST[0] += 1;
assert_eq!(TEST[0], 2);
*INT_RAW += 1;
assert_eq!(*INT_RAW, 2);
}
}
2 changes: 1 addition & 1 deletion tests/ui/consts/static_mut_containing_mut_ref2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ static mut STDERR_BUFFER_SPACE: u8 = 0;

pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 42; };
//[mut_refs]~^ ERROR could not evaluate static initializer
//[stock]~^^ ERROR mutable references are not allowed in statics
//[stock]~^^ ERROR mutation through a reference is not allowed in statics

fn main() {}
6 changes: 3 additions & 3 deletions tests/ui/consts/static_mut_containing_mut_ref2.stock.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0658]: mutable references are not allowed in statics
--> $DIR/static_mut_containing_mut_ref2.rs:7:46
error[E0658]: mutation through a reference is not allowed in statics
--> $DIR/static_mut_containing_mut_ref2.rs:7:45
|
LL | pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 42; };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
Expand Down

0 comments on commit b85509f

Please sign in to comment.