E4011

E4011 — A view of a tensor (a row q[i] or a field h.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 with let, pass q[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