E1018: borrowed value returned as an owner

View source

An abs parameter is a view of a value that belongs to the caller.

An abs parameter is a view of a value that belongs to the caller. A verb that returns it as an ordinary value would hand out a second owner, or a view that outlives its source. The compiler rejects this. It also rejects returning a ref expression directly, and then the message says temporary borrow. A verb may return a view only by declaring -> abs, and ADR-0021 then requires that the view comes from exactly one abs parameter. The error points at the returned expression.

A struct, an enum payload, an Option or a Result that holds a view follows the same rule (ADR-0021, ADR-0046 2.3). It can leave the verb, as a return value or stored through an ins parameter, only when every view inside it comes from an abs parameter of the verb. A view of a local, of an erg or dat parameter or of a temporary would dangle once the verb returns, so the message names that source: borrow buffer cannot escape its scope.

Example

actus
verb pick(abs value: Buffer) -> Buffer {
    return value;
}

verb main() {
}
actus
struct Window {
    abs bytes: Buffer,
}

verb open_window() -> Window {
    erg local: Buffer = Buffer[4];
    return Window { bytes: local, };
}

verb main() {
}

The aggregate can be built first and returned later. The views it holds are remembered with the binding, and they are also carried by an aggregate that moves into another one:

actus
struct Window {
    abs bytes: Buffer,
}

verb open_window() -> Window {
    erg local: Buffer = Buffer[4];
    erg window = Window { bytes: local, };
    return window;
}

verb main() {
}

Storing such an aggregate in a field of an ins parameter is the same escape, because the caller's object outlives the verb:

actus
struct Window {
    abs bytes: Buffer,
}

struct Slot {
    erg window: Window,
}

verb fill(ins slot: Slot) {
    erg local: Buffer = Buffer[4];
    slot.window = Window { bytes: local, };
}

verb main() {
}

A view that is stored in a binding belongs to that binding, so the source must live at least as long as the binding. Storing a view of a local that is declared in an inner block into a binding of an outer block, whether by assignment, into a field or into an element, is the same escape (ADR-0046 2.3):

actus
struct Window {
    abs bytes: Buffer,
}

verb open_window() -> Int {
    erg kept: Buffer = Buffer[1];
    erg window = Window { bytes: kept, };
    {
        erg local: Buffer = Buffer[4];
        window = Window { bytes: local, };
    }
    return buffer_length(buffer: abs window.bytes);
}

verb main() {
}

The views of an if or case value are the views of every arm, so returning one arm that views a local is rejected as well:

actus
struct Window {
    abs bytes: Buffer,
}

verb pick(abs flag: Bool) -> Window {
    erg local: Buffer = Buffer[4];
    erg other: Buffer = Buffer[8];
    return if flag { Window { bytes: local, } } else { Window { bytes: other, } };
}

verb main() {
}

Fix

Declare the return as a view. The result then borrows from the caller's value and is never cleaned up by the callee.

actus
verb pick(abs value: Buffer) -> abs Buffer {
    return value;
}

verb main() {
}

For an aggregate, build it from an abs parameter of the verb, which the caller keeps alive:

actus
struct Window {
    abs bytes: Buffer,
}

verb open_window(abs source: Buffer) -> Window {
    return Window { bytes: source, };
}

verb main() {
}

A source that lives in the same block as the binding that holds its view, or in an enclosing one, is accepted. It stays frozen until that block ends:

actus
struct Window {
    abs bytes: Buffer,
}

verb measure(abs flag: Bool) -> Int {
    erg first: Buffer = Buffer[4];
    erg second: Buffer = Buffer[8];
    erg window = if flag { Window { bytes: first, } } else { Window { bytes: second, } };
    return buffer_length(buffer: abs window.bytes);
}

verb main() {
}

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