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] }), passed to a function that takes the tensor by value (f(q[i])), or returned from a function that returns a tensor by value (return q[i]). A view shares its owner's memory and is not a copy. Bind it to a new variable withlet, passq[i].clone(), take the parameter by reference, or return a reference (-> &mut Tensor<..>).
What the compiler reports
E4011{{.*}}:24:{{.*}}returns a view of memory from a raw pointer as a tensor of its own
:28:{{.*}}Function 'tensor_view_1d' expects (ptr, len)
:33:{{.*}}Function 'tensor_view_1d' expects a pointer to a scalar
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
//
//
// `tensor_view_1d(p, len)` takes a pointer to a scalar and a length, and it needs `unsafe`.
// A function that returns such a view by value would give its caller a copy, so it is E4011.
//
fn not_unsafe(p : *mut f32) -> f32 {
let v = tensor_view_1d(p, 4);
return v[0];
}
fn view_of(p : *mut f32) -> Tensor<f32, [4]> {
return unsafe { tensor_view_1d(p, 4) };
}
fn two_lengths(p : *mut f32) -> f32 {
let _v = unsafe { tensor_view_1d(p, 2, 2) };
return 0.0;
}
fn not_a_pointer(x : f32) -> f32 {
let _v = unsafe { tensor_view_1d(x, 4) };
return 0.0;
}
fn main() -> i32 {
return 0;
}
From tests/frontend/fail/wrong_uses_of_a_rank_1_view_of_raw_memory.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.