E4009

E4009 — An assignment to a variable, or a field or element of it, while a & borrow of it is still used afterwards: let r = &x; x = 2; and then a use of r. The borrow would see the value change under it.

What the compiler reports

Error[E4009] at {{.*}}write_through_a_reference_while_it_is_borrowed.vx:[[# @LINE + 1]]:3: `*m` is assigned while it is borrowed
1 error(s) emitted

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
//
// `x` borrows what `m` points at, so `*m = 1` cannot change it while `x` is used.

fn main() -> i32 {
  let mut v = 0;
  let m = &mut v;
  let x = &*m;
  *m = 1;
  print(*x);
  return 0;
}

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

Related