rust-lang / rust-lang/rust

Cannot return value from loop when temporary mutable access occurs before.

Open
#133,539 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-borrow-checker C-bug fixed-by-polonius T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:


struct A {
    buf: [u8; 32],
}

struct Ref<'a> {
    slice: &'a [u8]
}

pub fn mkref(buf: &[u8]) -> Option<Ref> {
    Some(Ref {
        slice: &buf[1..8]
    })
}

impl A {
    // Borrowck error
    pub fn read<'a>(&'a mut self) -> Option<Ref<'a>> {
        loop {
            self.buf.rotate_left(4);

            match mkref(&self.buf) {
                Some(rf) => return Some(rf),
                None => continue,
            }

        }

        None
    }

    // Borrowck error
    pub fn read_break<'a>(&'a mut self) -> Option<Ref<'a>> {
        let r = loop {
            self.buf.rotate_left(4);

            match mkref(&self.buf) {
                Some(rf) => break Some(rf),
                None => continue,
            }
        };

        r
    }

    // OK
    pub fn read_inline<'a>(&'a mut self) -> Option<Ref<'a>> {
        loop {
            self.buf.rotate_left(4);

            return Some(Ref { slice: &self.buf[..] });
        }

        None
    }

    // OK
    pub fn read_noloop<'a>(&'a mut self) -> Option<Ref<'a>> {
        self.buf.rotate_left(4);

        match mkref(&self.buf) {
            Some(rf) => return Some(rf),
            None => return None,
        }
    }
}

I expected to see this happen: No errors

Instead, this happened: Cannot return value from loop when temporary mutable access occurs before.

error[E0502]: cannot borrow `self.buf` as mutable because it is also borrowed as immutable
  --> src/lib.rs:23:13
   |
21 |     pub fn read<'a>(&'a mut self) -> Option<Ref<'a>> {
   |                 -- lifetime `'a` defined here
22 |         loop {
23 |             self.buf.rotate_left(4);
   |             ^^^^^^^^ mutable borrow occurs here
24 |
25 |             match mkref(&self.buf) {
   |                         --------- immutable borrow occurs here
26 |                 Some(rf) => return Some(rf),
   |                                    -------- returning this value requires that `self.buf` is borrowed for `'a`

error[E0502]: cannot borrow `self.buf` as mutable because it is also borrowed as immutable
  --> src/lib.rs:38:13
   |
36 |     pub fn read_break<'a>(&'a mut self) -> Option<Ref<'a>> {
   |                       -- lifetime `'a` defined here
37 |         let r = loop {
38 |             self.buf.rotate_left(4);
   |             ^^^^^^^^ mutable borrow occurs here
39 |
40 |             match mkref(&self.buf) {
   |                         --------- immutable borrow occurs here
...
46 |         r
   |         - returning this value requires that `self.buf` is borrowed for `'a`
Meta

rustc --version --verbose:

rustc 1.85.0-nightly (dff3e7ccd 2024-11-26)
binary: rustc
commit-hash: dff3e7ccd4a18958c938136c4ccdc853fcc86194
commit-date: 2024-11-26
host: x86_64-pc-windows-msvc
release: 1.85.0-nightly
LLVM version: 19.1.4

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

Reproduce the examples in src/lib.rs with the reported rustc nightly version, comparing the loop, break, inline, and noloop variants. Trace the borrow-checker behavior behind E0502; done means the first two variants compile without errors while the existing successful variants remain valid.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.