E4010
E4010 — A change to a variable declared withoutmut: assigning it, or a field or element of it, borrowing it&mut, or calling a method that takes&mut selfon it. Declare itlet mut x, or for a parametermut x : T.
What the compiler reports
Error[E4010] at [[@LINE+1]]:{{[0-9]+}}: `GOOD` is a `const`, so it cannot be assigned
Error[E3044] at [[@LINE+1]]:11: const 'GOOD' is a table: read it one number at a time, as `GOOD[i]`
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
//
fn one() -> i8 {
return 1i8;
}
const SHORT : Tensor<i8, [3]> = [1, 2];
const CALLED : Tensor<i8, [2]> = [1, one()];
const TOO_BIG : Tensor<i8, [2]> = [1, 300];
const FLAGS : Tensor<bool, [1]> = [true];
const GOOD : Tensor<i8, [2]> = [1, 2];
fn main() -> i32 {
let a = SHORT[0] + CALLED[0] + TOO_BIG[0];
let b = FLAGS[0];
// A table is read-only.
GOOD[0] = 5i8;
// A table is read one number at a time, never bound or passed whole.
let c = GOOD;
return 0;
}
From tests/frontend/fail/const_tables_misused.vx, which asserts this diagnostic on every commit.
Related
Ownership and borrowingThe chapter covering the rule this code enforces.
All diagnosticsEvery code the compiler can emit, grouped by stage.
E4009An 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.
E4011A view of a tensor (a row q[i] or a field h.t) stored where a tensor of its own is held: a variable that already exists (t = q[i]) or a struct field (h.t = q[i], Holder { t : q[i] }). A view shares its owner's memory and is not a copy. Bind it to a new variable with let instead.