E1079: arena moved or dropped while a reference is live

View source

A reference into an arena is only valid while the arena exists, so the arena may not be moved, dropped, or overwritten while a live abs or ins binding refers to one of its value...

A reference into an arena is only valid while the arena exists, so the arena may not be moved, dropped, or overwritten while a live abs or ins binding refers to one of its values (ADR-0030). Every way of giving the arena up counts: drop(arena), a dat argument, erg other = arena, an assignment from or to the arena, and a struct field that takes it, whether the field is erg or has no role. The message names the arena and the reference. The error points at the move or the drop.

A view lives until the end of the block that holds it, as for E1011, so a view in a block that has already ended does not block the release. A value that was placed in the arena is not a view, so it does not block the release either, but it may not be read once the arena is gone. That read is the same error, and it points at the read.

Example

actus
struct Node {
    value: Int,
}

verb main() {
    erg arena: Arena[64] = Arena[64]();
    erg node = place(arena: ins arena, value: Node { value: 1, });
    abs view = ref node;
    drop(arena);
    println(int: view.value);
}
actus
struct Node {
    value: Int,
}

verb take(dat arena: Arena[64]) {
}

verb main() {
    erg arena: Arena[64] = Arena[64]();
    erg node = place(arena: ins arena, value: Node { value: 1, });
    abs view = ref node;
    take(arena: arena);
    println(int: view.value);
}

A struct field moves the arena the same way. A field of an arena type is declared erg (E1148):

actus
struct Node {
    value: Int,
}

struct Holder {
    erg held: Arena[64],
}

verb main() {
    erg arena: Arena[64] = Arena[64]();
    erg node = place(arena: ins arena, value: Node { value: 1, });
    abs view = ref node;
    erg holder = Holder { held: arena, };
    println(int: view.value);
}

A struct that holds an arena is the arena's owner (ADR-0130), so the rule applies to the holder: a value placed through it, place(arena: ins holder.held, ...), has the holder as its owner. While a view of such a value is live, the holder cannot be dropped, moved, assigned to, given to a dat parameter, or have its arena field moved out, and the message names the holder:

actus
struct Node {
    value: Int,
}

struct Holder {
    erg held: Arena[64],
}

verb main() {
    erg holder = Holder { held: Arena[64](), };
    erg node = place(arena: ins holder.held, value: Node { value: 1, });
    abs view = ref node;
    drop(holder);
    println(int: view.value);
}

A placed value that is not a view may not be read once the holder is gone:

actus
struct Node {
    value: Int,
}

struct Holder {
    erg held: Arena[64],
}

verb main() {
    erg holder = Holder { held: Arena[64](), };
    erg node = place(arena: ins holder.held, value: Node { value: 1, });
    drop(holder);
    println(int: node.value);
}

A placed value is only readable while an owner of its arena is still in scope. Here the new owner ends with its block, so the read of node fails:

actus
struct Node {
    value: Int,
}

verb main() {
    erg arena: Arena[64] = Arena[64]();
    erg node = place(arena: ins arena, value: Node { value: 1, });
    {
        erg other = arena;
    }
    println(int: node.value);
}

Fix

Finish with the values first, and create no view of them that is still live when the arena is moved or dropped. Keep a view in its own block so that it ends before the arena is released. An arena that is not dropped explicitly is released when its scope ends.

actus
struct Node {
    value: Int,
}

verb take(dat arena: Arena[64]) {
}

verb main() {
    erg arena: Arena[64] = Arena[64]();
    erg node = place(arena: ins arena, value: Node { value: 1, });
    {
        abs view = ref node;
        println(int: view.value);
    }
    take(arena: dat arena);
}
actus
struct Node {
    value: Int,
}

verb main() {
    erg arena: Arena[64] = Arena[64]();
    erg node = place(arena: ins arena, value: Node { value: 1, });
    println(int: node.value);
    drop(arena);
}

A holder is released the same way once its views have ended, and an arena that moved into a holder leaves its placed values readable through the holder:

actus
struct Node {
    value: Int,
}

struct Holder {
    erg held: Arena[64],
}

verb main() {
    erg arena: Arena[64] = Arena[64]();
    erg node = place(arena: ins arena, value: Node { value: 1, });
    erg holder = Holder { held: arena, };
    println(int: node.value);
    drop(holder);
}

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