E4011
E4011 — A view of a tensor (a rowq[i]or a fieldh.t) used where a tensor of its own is held: stored in a variable that already exists (t = q[i]) or a struct field (h.t = q[i],Holder { t : q[i] }), or passed to a function that takes the tensor by value (f(q[i])). A view shares its owner's memory and is not a copy. Bind it to a new variable withlet, passq[i].clone(), or take the parameter by reference.
What the compiler reports
Error[E4011] at {{.*}}tensor_views_borrow_their_owner.vx:83:11: this stores a view of `q` in `keep`, which holds a tensor of its own
Error[E4011] at {{.*}}tensor_views_borrow_their_owner.vx:91:10: this stores a view of `q` in the field `t`
Error[E4011] at {{.*}}tensor_views_borrow_their_owner.vx:103:17: this passes a view of `q` to `first`, which takes a tensor of its own
Error[E4007] at {{.*}}tensor_views_borrow_their_owner.vx:114:16: `q` is moved while it is borrowed
Error[E4005] at {{.*}}tensor_views_borrow_their_owner.vx:121:3: `r` is given a value that points at a variable of an inner block
Error[E4007] at {{.*}}tensor_views_borrow_their_owner.vx:134:16: `q` is moved while it is borrowed
Error[E4007] at {{.*}}tensor_views_borrow_their_owner.vx:143:16: `q` is moved while it is borrowed
Error[E4007] at {{.*}}tensor_views_borrow_their_owner.vx:152:16: `q` is moved while it is borrowed
Error[E4007] at {{.*}}tensor_views_borrow_their_owner.vx:161:16: `q` is moved while it is borrowed
Error[E4007] at {{.*}}tensor_views_borrow_their_owner.vx:170:16: `q` is moved while it is borrowed
Error[E4007] at {{.*}}tensor_views_borrow_their_owner.vx:180:16: `q` is moved while it is borrowed
Error[E4007] at {{.*}}tensor_views_borrow_their_owner.vx:191:16: `q` is moved while it is 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 `q[1]` or a tensor field `h.t` is a view of its owner's memory, not a copy, so it
// borrows the owner as `&q` would. Each function below breaks one rule of that borrow.
struct Holder {
t : Tensor<f32, [4]>,
}
fn take(t : Tensor<f32, [2, 4]>) -> f32 {
return t[0][0];
}
fn take_holder(h : Holder) -> f32 {
return h.t[0];
}
// The tensor is moved while its row is still used.
fn owner_moved() -> f32 {
let q = Tensor<f32, [2, 4]>::new();
let r = q[1];
let s = take(q);
return r[0] + s;
}
// The struct is moved while a view of its field is still used.
fn struct_moved() -> f32 {
let h = Holder {
t : Tensor<f32, [4]>::new()
};
let t = h.t;
let s = take_holder(h);
return t[0] + s;
}
// The tensor is written while its row is still used.
fn owner_written() -> f32 {
let mut q = Tensor<f32, [2, 4]>::new();
let r = q[1];
q[1][0] = 5.0;
return r[0];
}
// The tensor is read while a row that can write it is still used.
fn owner_read() -> f32 {
let mut q = Tensor<f32, [2, 4]>::new();
let mut r = q[1];
let x = q[1][0];
r[0] = 5.0;
return x + r[0];
}
// A row that can write is taken from a tensor that is not `mut`.
fn row_of_read_only() -> f32 {
let q = Tensor<f32, [2, 4]>::new();
let mut r = q[1];
r[0] = 5.0;
return r[0];
}
// A row of a tensor the function owns is returned.
fn row_of_a_local() -> Tensor<f32, [4]> {
let q = Tensor<f32, [2, 4]>::new();
return q[1];
}
// A parameter taken by value is owned by the function too.
fn row_of_a_parameter(q : Tensor<f32, [2, 4]>) -> Tensor<f32, [4]> {
return q[1];
}
// A row is stored in a variable that already holds a tensor.
fn stored_in_a_variable() -> f32 {
let q = Tensor<f32, [2, 4]>::new();
let mut keep = Tensor<f32, [4]>::new();
keep = q[1];
return keep[0];
}
// A row is stored in a struct field.
fn stored_in_a_field() -> f32 {
let q = Tensor<f32, [2, 4]>::new();
let h = Holder {
t : q[1]
};
return h.t[0];
}
fn first(t : Tensor<f32, [4]>) -> f32 {
return t[0];
}
// A row is passed to a function that takes a tensor by value, and would own it.
fn passed_by_value() -> f32 {
let q = Tensor<f32, [2, 4]>::new();
return first(q[1]);
}
// A row chosen by an `if` borrows each tensor it may come from.
fn chosen_by_if(c : bool) -> f32 {
let q = Tensor<f32, [2, 4]>::new();
let r = if c {
q[0]
} else {
q[1]
};
let s = take(q);
return r[0] + s;
}
// The tensor a row is chosen from was declared in the `if` arm, and is gone after it.
fn chosen_from_an_arm(c : bool) -> f32 {
let q = Tensor<f32, [2, 4]>::new();
let r = if c {
let t = Tensor<f32, [2, 4]>::new();
t[0]
} else {
q[1]
};
return r[0];
}
// `reshape` is a view of the same memory.
fn reshaped() -> f32 {
let q = Tensor<f32, [2, 4]>::new();
let r = q.reshape([8]);
let s = take(q);
return r[0] + s;
}
// A row taken through a reference points into the tensor, not into the reference.
fn through_a_reference() -> f32 {
let q = Tensor<f32, [2, 4]>::new();
let rq = &q;
let r = rq[1];
let s = take(q);
return r[0] + s;
}
// A closure that uses a row keeps the row's tensor borrowed while the closure is used.
fn used_by_a_closure() -> f32 {
let q = Tensor<f32, [2, 4]>::new();
let r = q[1];
let f = || r[0];
let s = take(q);
return f() + s;
}
// A reborrow `&*rq` points at what `rq` points at.
fn reborrowed() -> f32 {
let q = Tensor<f32, [2, 4]>::new();
let rq = &q;
let n = &*rq;
let s = take(q);
return n[0][0] + s;
}
// A reference copied to another variable keeps the tensor borrowed.
fn reference_copied() -> f32 {
let q = Tensor<f32, [2, 4]>::new();
let r = &q;
let r2 = r;
let s = take(q);
return r2[0][0] + s;
}
// So does a closure moved to another variable.
fn closure_moved() -> f32 {
let q = Tensor<f32, [2, 4]>::new();
let r = q[1];
let f = || r[0];
let g = f;
let s = take(q);
return g() + s;
}
// The same when the reference is assigned to a variable that already exists.
fn reference_assigned() -> f32 {
let q = Tensor<f32, [2, 4]>::new();
let p = Tensor<f32, [2, 4]>::new();
let r = &q;
let mut r2 = &p;
r2 = r;
let s = take(q);
return r2[0][0] + s;
}
fn main() -> i32 {
return 0;
}
From tests/frontend/fail/tensor_views_borrow_their_owner.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.
E4010A change to a variable declared without
mut: assigning it, or a field or element of it, borrowing it &mut, or calling a method that takes &mut self on it. Declare it let mut x, or for a parameter mut x : T.
E4012A call to the drop method of a Drop impl by hand: x.drop(). drop runs when the value is dropped, so calling it as well would run it twice. Write drop(x), from core::mem, to drop a value early.