E1005: use of a moved binding

View source

A dat argument, an assignment to another owner and a returned binding all move ownership.

A dat argument, an assignment to another owner and a returned binding all move ownership. ADR-0016 says the source then becomes Moved and has no cleanup of its own, so the compiler rejects every later use, including a second move. The error points at the later use. A drop of a binding that was moved is such a use too, because the new owner releases the value (a second drop of a binding that was dropped is E1007).

Example

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

verb main() {
    erg data: Buffer = Buffer[4];
    finish(data: dat data);
    println(int: buffer_length(buffer: abs data));
}
actus
verb finish(dat data: Buffer) {
    drop(data);
}

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

A struct field takes ownership of a buffer, an arena, a struct or an enum even when the field has no role, so the source is moved there as well:

actus
struct Holder {
    held: Buffer,
}

verb main() {
    erg data: Buffer = Buffer[4];
    erg holder = Holder { held: data, };
    append(data, 1u8);
}

An enum payload takes ownership in the same way, so Option, Result and user enums move a buffer or an arena into the new value. An erg parameter is a new owner inside the callee, so a buffer or an arena handed to it is moved too, with or without the explicit erg role:

actus
verb main() {
    erg arena: Arena[64] = Arena[64]();
    erg held: Option[Arena[64]] = Option[Arena[64]].Some(arena);
    erg node = place(arena: ins arena, value: 1);
}
actus
verb keep(erg held: Buffer) {
}

verb main() {
    erg data: Buffer = Buffer[4];
    keep(held: erg data);
    append(data, 1u8);
}

A scalar (Int, Bool, Usize, a fixed-width integer or a float) has no ownership state and is never moved (ADR-0126): a binding, an assignment, a field initializer, a payload, an erg or dat argument and a return copy it. An array moves like every aggregate, even when its elements are copy types, and a value of a type parameter moves inside a generic verb.

A struct, an enum or a pack is moved by every one of these operations, whether it holds a resource or only plain data (ADR-0124): a binding, an assignment, a struct field initializer, an enum payload, an erg or dat argument and a return all take the value away from the source. A read does not move it, so header.length, a comparison and an abs argument can be repeated, but a read after one of the moves above is the error:

actus
struct Header {
    length: u16,
}

struct Frame {
    header: Header,
}

verb main() {
    erg header = Header { length: 4u16, };
    erg frame = Frame { header: header, };
    print(int: header.length);
}
actus
enum Mode {
    Idle,
    Busy,
}

verb main() {
    erg mode = Mode.Idle;
    erg other = mode;
    erg again = mode;
}
actus
meta copy
struct Point {
    x: Int,
}

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

A pack is moved in the same places, so a header that was stored must be copied (meta copy and copy(value: abs header)) to be used again:

actus
pack Header {
    erg storage: u16;
    layout little;
    fields {
        erg kind: u8 at 0;
        erg length: u8 at 8;
    }
}

verb main() {
    erg header = Header { storage: 0u16, };
    erg stored = header;
    print(int: header.length);
}

A field of an owner is moved by its field path, so only that path is moved. Moving the same field again, or using the owner as a whole afterwards, is the error, while the other fields stay usable:

actus
struct Config {
    erg first: Buffer,
    erg second: Buffer,
}

verb release(dat held: Buffer) {
    drop(held);
}

verb main() {
    erg config: Config = Config { first: Buffer[4], second: Buffer[4], };
    release(held: dat config.first);
    release(held: dat config.first);
}
actus
struct Config {
    erg first: Buffer,
    erg second: Buffer,
}

verb release(dat held: Buffer) {
    drop(held);
}

verb consume(dat config: Config) {
    drop(config);
}

verb main() {
    erg config: Config = Config { first: Buffer[4], second: Buffer[4], };
    release(held: dat config.first);
    consume(config: dat config);
}

A loop body is analysed once, but it runs again, so a value that is declared before a loop and moved or dropped inside it is moved again by the next iteration (ADR-0016). The error points at the move and says that it was made in a previous iteration. It applies to loop, to for over a range or an array, and to nested loops, and to every kind of move: a dat or erg argument, a struct field or enum payload, an erg binding, a field of an owner and drop:

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

verb main() {
    erg data: Buffer = Buffer[4];
    for erg index in 0..3 {
        finish(data: dat data);
    }
}
actus
verb finish(dat data: Buffer) {
    drop(data);
}

verb main() {
    erg data: Buffer = Buffer[4];
    erg count: Int = 0;
    loop {
        count += 1;
        if count > 3 {
            break;
        }
        if count > 1 {
            finish(data: dat data);
            continue;
        }
    }
}

The state after a loop is the state at its break statements, so a value moved or dropped before a break is moved after the loop. A break belongs to its innermost loop only, and a return does not reach the code after the loop:

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

verb main() {
    erg data: Buffer = Buffer[4];
    erg count: Int = 0;
    loop {
        count += 1;
        if count > 2 {
            finish(data: dat data);
            break;
        }
    }
    append(data, 1u8);
}

Fix

Assign the binding a new value. An assignment reads nothing and releases nothing, because the old value now belongs to its new owner, and it makes the binding live again (ADR-0132). A binding that is moved on only some paths of an if is still rejected at the join (E1061):

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

verb main() {
    erg data: Buffer = Buffer[4];
    finish(data: dat data);
    data = Buffer[8];
    println(int: buffer_length(buffer: abs data));
}

Otherwise use the value before it is moved. If the callee only reads the value, change its parameter to abs so that the caller keeps ownership.

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

verb main() {
    erg data: Buffer = Buffer[4];
    println(int: buffer_length(buffer: abs data));
    finish(data: dat data);
}

In a loop, move the value on the path that leaves the loop, with break or return right after the move, or declare the value inside the body so that every iteration owns a new one. A value that is declared inside the loop is not checked, because it does not exist in the next iteration:

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

verb main() {
    erg data: Buffer = Buffer[4];
    erg count: Int = 0;
    loop {
        count += 1;
        if count > 2 {
            finish(data: dat data);
            break;
        }
    }
    for erg index in 0..3 {
        erg fresh: Buffer = Buffer[4];
        finish(data: dat fresh);
    }
}

A scalar needs no copy to be read again, and an array of copy types is copied with copy(value: abs xs):

actus
verb main() -> Int {
    erg a: Int = 1;
    erg b = a;
    erg xs = Array[Int, 2]();
    erg ys = copy(value: abs xs);
    return a + b + xs[0] + ys[0];
}

A struct, an enum or a pack that holds only plain data can declare meta copy, and the copy is then written with copy(value: abs x); the source stays usable because nothing was moved:

actus
meta copy
struct Header {
    length: u16,
}

struct Frame {
    header: Header,
}

verb main() {
    erg header = Header { length: 4u16, };
    erg frame = Frame { header: copy(value: abs header), };
    println(int: header.length);
}

Different fields of one owner can be moved one after the other. The owner is then partially moved, and the fields that were not moved are released with it:

actus
struct Config {
    erg first: Buffer,
    erg second: Buffer,
}

verb release(dat held: Buffer) {
    drop(held);
}

verb main() {
    erg config: Config = Config { first: Buffer[4], second: Buffer[4], };
    release(held: dat config.first);
    release(held: dat config.second);
}

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