Lyricdev
GitHub

LYR-SEM0115

a view of an inline array that lies in a frame

error · spec 03-types

A view ('Slice') of an inline array 'T[n]' is taken — by slicing it, passing it as a view, or calling a member written for views — where the array is a local, a parameter or a field of a struct local, not in the heap.

Explanation

An inline array T[n] is stored where it stands: in the frame for a local or a parameter. A view points into its storage, and may outlive the frame — returned, stored in an object, captured — after which it would point into freed memory. So a view of an inline array is taken only where the array lies in the heap: a field of an object, an element of an array.

Copy the array into a heap array (a.toArray()), declare the local as T[], or pass the inline array itself.

Wrong

fn total(xs: Slice<int>): int { return xs.length(); }

fn main(): void {
    let a: int[3] = [1, 2, 3];
    let _ = total(a[1..]);
}
fn total(xs: Slice<int>): int { return xs.length(); }

fn main(): void {
    let a: int[] = [1, 2, 3];
    let _ = total(a[1..]);
}