rust-lang / rust-lang/rust

Allow moving out of `&'static mut T`

Open
#125,868 17 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-discussion T-lang T-opsem
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Hello, it is perfectly sound to move out of &'static mut T and have that reference consumed, as no safe code can access T under the reference after that point.

struct NotDefaultNotCopy([u8; 20]);

fn main() {
    let foo: &'static mut NotDefaultNotCopy = Box::leak(Box::new(NotDefaultNotCopy([0; 20])));
    let foo_taken = *foo;

    println!("{:?}", foo_taken.0);
}

For example, miri is happy with that code if take in unsafely written (but detects leak) playground

Here is code with more unsafe but with no miri leak warnings: playground

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue provides Rust reproductions and Rust Playground links demonstrating the proposed move from &'static mut T. Start by investigating the compiler's handling of moves out of mutable references and determine what focused compiler tests are needed; done means the demonstrated non-Copy move is accepted with sound behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.