E1018: borrowed value returned as an owner
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
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:
Storing such an aggregate in a field of an ins parameter is the same escape,
because the caller's object outlives the verb:
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):
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:
Fix
Declare the return as a view. The result then borrows from the caller's value and is never cleaned up by the callee.
For an aggregate, build it from an abs parameter of the verb, which the caller
keeps alive:
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:
Run actus explain E1018 in your terminal to read this entry offline. Browse Ownership & types or all error codes.