E4002
E4002 — Cannot access mutably borrowed variable
What the compiler reports
E4002{{.*}}:22:{{.*}}Cannot access 'o' because it is mutably borrowed
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
//
//
// A row that a function returns as `&mut Tensor` is a mutable borrow of the tensor that the
// caller passed. So the caller cannot read or write that tensor while it still uses the row.
//
fn second_row(q : &mut Tensor<f32, [2, 4]>) -> &mut Tensor<f32, [4]> {
return &mut q[1];
}
fn main() -> i32 {
let mut o = Tensor<f32, [2, 4]>::fill(5.0);
let r = second_row(&mut o);
o[0][0] = 1.0;
r[0] = 7.0;
return 0;
}
From tests/frontend/fail/a_returned_view_borrows_its_owner.vx, which asserts this diagnostic on every commit.