E1010: value cannot be moved out of a view or transferred with dat

View source

A dat argument moves ownership into the callee (ADR-0016).

A dat argument moves ownership into the callee (ADR-0016). Only an owner can be moved, so the argument must be an erg binding, a dat binding or an erg field of such a binding, or an expression that produces a fresh owned value (ADR-0129): a struct or pack literal, an enum constructor, an array constructor such as Array[Int, 2](), a call whose declared result is owned (not abs) or copy(value: abs x); an if or case of aggregate type is not accepted as an argument, so bind it first. Nobody else holds such a value, so it needs no binding. An abs binding is a view of someone else's value. A constructor such as Buffer[4] has no binding to hand over. An ins binding is a loan and is rejected with E1082. The message names the binding or field; for an argument that is not a name it names the construct it rejects, such as a ref view or the abs result of a call. The error points at the argument.

The same rule covers a field or an element read by value through an abs view (ADR-0124). A struct, enum, pack, array or buffer taken from view.field, view.a.b or view.items[0] and returned, bound to an erg, assigned, passed to a dat parameter, or stored in a struct literal field or an enum payload cannot be moved, because a view owns nothing, and it is not copied, because nothing writes a copy, so it would be shared silently. The message names the path. When the type is a copy type it says to write copy(value: abs view.field); otherwise it says the value cannot leave the view: borrow it with abs, or take ownership of the owner. Through an ins loan the same read is E1082. A scalar read is not affected, nor is taking a view of the field (abs h = ref view.field, an abs argument, -> abs).

An array element is always owned by its array: the language does not track moves of single elements, so a struct, enum, pack or array (or a buffer) cannot be read by value out of an element of an owner, at a constant or a run-time index, or through a field of an element (xs[i], items[0].name), whether it holds a resource or only plain data (ADR-0124). The array would still release the element, so a value that holds a resource would be released twice (ADR-0125), and nothing writes a copy, so plain data would be shared silently. The rule covers a binding, an assignment, an element assignment, a struct field initializer, an enum payload, an argument, a return and a branch value. The message names the path. When the element type is a copy type it says to write copy(value: abs xs[i]); otherwise borrow the element with abs, or assign a new value to the element, which releases the value it held. A scalar element is not affected.

Example

actus
verb eat(dat data: Buffer) {
    drop(data);
}

verb pass(abs data: Buffer) {
    eat(data: dat data);
}

verb main() {
}
actus
verb eat(dat data: Buffer) {
    drop(data);
}

verb main() {
    eat(data: dat Buffer[4]);
}
actus
struct Point {
    x: Int,
}

verb read(abs point: Point) -> abs Point {
    return point;
}

verb give(dat point: Point) -> Int {
    return point.x;
}

verb main() -> Int {
    erg point: Point = Point { x: 1, };
    return give(point: dat read(point: abs point));
}
actus
meta copy
struct Header {
    kind: u8,
    payload_len: u16,
}

struct Parser {
    erg header: Header,
}

verb get(abs parser: Parser) -> Header {
    return parser.header;
}

verb main() {
}
actus
struct Event {
    erg name: Buffer,
}

verb take(dat name: Buffer) {
    drop(name);
}

verb relay(abs event: Event) {
    take(name: dat event.name);
}

verb main() {
}
actus
verb take(dat name: Buffer) {
    drop(name);
}

verb main() {
    erg names: Array[Buffer, 2] = Array[Buffer, 2]();
    erg first: Buffer = names[0u32];
    take(name: dat first);
}
actus
meta copy
struct Point {
    x: Int,
    y: Int,
}

verb main() -> Int {
    erg points: Array[Point, 2] = Array[Point, 2]();
    erg first: Point = points[0u32];
    return first.x;
}

Fix

For a view field or an array element that is a copy type, write the copy. Otherwise borrow the field, or let the caller own the value. Declare the forwarding parameter dat, or bind the new value to an erg first and move that binding.

actus
verb eat(dat data: Buffer) {
    drop(data);
}

verb pass(dat data: Buffer) {
    eat(data: dat data);
}

verb main() {
}
actus
verb eat(dat data: Buffer) {
    drop(data);
}

verb main() {
    erg data: Buffer = Buffer[4];
    eat(data: dat data);
}

A fresh owned value needs no binding (ADR-0129):

actus
struct Packet {
    erg body: Buffer,
}

verb send(dat packet: Packet) {
    drop(packet);
}

verb main() {
    send(packet: dat Packet { body: Buffer[4], });
}
actus
meta copy
struct Header {
    kind: u8,
    payload_len: u16,
}

struct Parser {
    erg header: Header,
}

verb get(abs parser: Parser) -> Header {
    return copy(value: abs parser.header);
}

verb main() {
}
actus
meta copy
struct Header {
    kind: u8,
    payload_len: u16,
}

struct Parser {
    erg header: Header,
}

verb show(abs header: Header) -> u16 {
    return header.payload_len;
}

verb read(abs parser: Parser) -> u16 {
    return show(header: abs parser.header);
}

verb main() {
}
actus
verb length(abs name: Buffer) -> Int {
    return 1;
}

verb main() {
    erg names: Array[Buffer, 2] = Array[Buffer, 2]();
    names[0u32] = Buffer[4];
    erg size: Int = length(name: abs names[0u32]);
}
actus
meta copy
struct Point {
    x: Int,
    y: Int,
}

verb main() -> Int {
    erg points: Array[Point, 2] = Array[Point, 2]();
    points[0u32] = Point { x: 40, y: 2, };
    erg first: Point = copy(value: abs points[0u32]);
    return first.x + points[0u32].y;
}

Run actus explain E1010 in your terminal to read this entry offline. Browse Ownership & types or all error codes.