E4004

E4004 — Cannot borrow (already mutably borrowed)

What the compiler reports

Error[E4004] at {{.*}}method_returning_mut_keeps_its_receiver_borrowed_mutably.vx:[[@LINE+1]]:11: Cannot borrow 'c' because it is already borrowed as mutable.

The fragments the test suite holds the compiler to. An ellipsis marks text the test does not constrain; the emitted message also carries a source location.

A program that triggers it

//
// Part of the Vx Project, under the Apache License v2.0 with LLVM Exceptions.
// See LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//
// `c.get_mut()` returns a `&mut` into `c`, so `c` stays mutably borrowed while `r` is used, and
// no other borrow of `c` is allowed then.

struct C {
  v : i32,
}

impl C {
  fn get_mut(self : &mut C) -> &mut i32 {
    return &mut self.v;
  }
}

fn main() -> i32 {
  let mut c = C {
    v : 1
  };
  let r = c.get_mut();
  let s = &c;
  *r = 5;
  return s.v;
}

From tests/frontend/fail/method_returning_mut_keeps_its_receiver_borrowed_mutably.vx, which asserts this diagnostic on every commit.

Related