rust-lang / rust-lang/rust

strange cannot move out of a mutable reference error

Open
#139,670 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-borrow-checker A-diagnostics D-lack-of-suggestion L-false-positive T-clippy T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=ac7fef256eba962ef5516954ca18da64

I expected to see this happen:

codes can be complied

Instead, this happened:

it compile failed

   Compiling playground v0.0.1 (/playground)
error[E0507]: cannot move out of `buf`, a captured variable in an `FnMut` closure
  --> src/lib.rs:14:54
   |
11 |     async fn recv(&self, buf: &mut [MaybeUninit<u8>]) -> io::Result<usize> {
   |                          --- captured outer variable
12 |         self.0
13 |             .async_io(Interest::READABLE, |fd| {
   |                                           ---- captured by this `FnMut` closure
14 |                 let (data, _) = rustix::io::read(fd, buf)
   |                                                      ^^^ move occurs because `buf` has type `&mut [MaybeUninit<u8>]`, which does not implement the `Copy` trait

For more information about this error, try `rustc --explain E0507`.
error: could not compile `playground` (lib) due to 1 previous error

however, if I try https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=e8ee087d0def6e5885e8687d577ea190

it can compile but clippy says

warning: this call to `as_mut` does nothing
  --> src/lib.rs:14:54
   |
14 |                 let (data, _) = rustix::io::read(fd, buf.as_mut())
   |                                                      ^^^^^^^^^^^^ help: try: `buf`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_asref
   = note: `#[warn(clippy::useless_asref)]` on by default

I think clippy is right, re-borrow a &mut doesn't do anything, so I think maybe rustc has problem? If not, please tell me how to avoid the compile error and clippy warn

Meta

rustc --version --verbose:

rustc 1.88.0-nightly (0fe8f3454 2025-04-10)
binary: rustc
commit-hash: 0fe8f3454dbe9dda52a254991347e96bec579a6f
commit-date: 2025-04-10
host: x86_64-unknown-linux-gnu
release: 1.88.0-nightly
LLVM version: 20.1.2

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

Start by comparing the two Rust Playground reproductions linked in the issue, focusing on src/lib.rs around line 14 and the E0507 diagnostic. Check the behavior against rustc --explain E0507 and determine whether the difference is expected borrow-checker behavior or a compiler/diagnostic bug. Done means a minimized, reproducible diagnosis with an identified compiler change or a clear explanation of the expected behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.