E4003

E4003 — Cannot borrow as mutable (already immutably borrowed)

What the compiler reports

E4003

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

// Reborrow tracking (#243, case bc9): `probe(m)` reborrows `*m` and the result aliases it;
// mutating through `m` via `insert(m, 42)` while that reborrow is still live (returned) is a
// shared-XOR-mutable violation. Passing the reference *by name* must be tracked, not just `&m`.
struct Map {
  slot : i32, present : i32
}
fn probe(m : &Map) -> &i32 {
  return &m.slot;
}
fn insert(m : &mut Map, v : i32) -> void {
  m.slot = v; m.present = 1;
}
fn bad(m : &mut Map) -> &i32 {
  let found = probe(m);
  insert(m, 42);
  return found;
}
fn main() -> i32 {
  return 0;
}

From tests/middle_end/fail/borrow_reborrow_param_alias.vx, which asserts this diagnostic on every commit.

Related