E1005: use of a moved binding
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
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:
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:
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:
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:
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:
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:
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:
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):
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.
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:
A scalar needs no copy to be read again, and an array of copy types is copied
with copy(value: abs xs):
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:
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:
Run actus explain E1005 in your terminal to read this entry offline. Browse Ownership & types or all error codes.