E1077: arena reference escapes its arena

View source

A value that place puts in an arena is only valid while the arena lives (ADR-0030).

A value that place puts in an arena is only valid while the arena lives (ADR-0030). A reference that is derived from the arena may not be stored in a binding that outlives the arena's scope, and it may not be returned from the verb, because the arena is released when its scope ends. The message names the binding. The error points at the return or at the assignment. A value placed in an arena the verb received as an ins parameter may not be returned or stored into another ins parameter either: both outlive the call, and the callee cannot see how long the caller keeps the arena (bug #81).

An arena that a verb creates is local to that verb (bug #143). Its storage is part of the verb's frame, so the verb cannot return it, alone or inside a value that holds it (a struct that owns it, an enum payload or an array), and cannot store it into a place the caller owns, such as an ins parameter or a field of one. The message names the arena, or the value that holds it, and says that it is local to the verb. An arena received as an erg or dat parameter, or a holder received that way, is the caller's storage and can be returned.

Example

actus
struct Node {
    value: Int,
}

verb make() -> Node {
    erg arena: Arena[64] = Arena[64]();
    erg node: Node = place(arena: ins arena, value: Node { value: 1, });
    return node;
}

verb main() {
}
actus
struct Node {
    value: Int,
}

verb main() {
    erg outer: Node = Node { value: 0, };
    {
        erg arena: Arena[64] = Arena[64]();
        erg inner = place(arena: ins arena, value: Node { value: 1, });
        outer = inner;
    }
}
actus
struct Holder {
    erg arena: Arena[64],
}

verb make() -> Holder {
    return Holder { arena: Arena[64](), };
}

verb main() {
}
actus
verb make() -> Arena[64] {
    return Arena[64]();
}

verb main() {
}
actus
struct Holder {
    erg arena: Arena[64],
}

verb reset(ins holder: Holder) {
    holder.arena = Arena[64]();
}

verb main() {
}

Fix

Return or keep a plain value instead of the reference, for example the field that the caller needs. Move the arena to the scope that needs the data when the data must live longer.

actus
struct Node {
    value: Int,
}

verb make() -> Int {
    erg arena: Arena[64] = Arena[64]();
    erg node: Node = place(arena: ins arena, value: Node { value: 1, });
    return node.value;
}

verb main() {
}
actus
struct Node {
    value: Int,
}

verb main() {
    erg total: Int = 0;
    {
        erg arena: Arena[64] = Arena[64]();
        erg inner = place(arena: ins arena, value: Node { value: 1, });
        total = inner.value;
    }
}

An arena the caller owns can be handed back, because the caller gets its own storage:

actus
verb give(erg arena: Arena[64]) -> Arena[64] {
    return arena;
}

verb main() {
}

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