E1061: branches leave a binding in different states
After a case or an if the compiler must know what happened to each binding, so every branch has to leave it in the same ownership and access state (ADR-0016).
After a case or an if the compiler must know what happened to each
binding, so every branch has to leave it in the same ownership and access
state (ADR-0016). A branch that drops or moves an owner while another branch
keeps it leaves the owner live on one path and gone on the other, and the
cleanup after the join could not be decided. The message names the binding and
the state it is in on two paths, in words such as still owned, moved,
dropped or frozen by a view, and says which construct the paths belong to:
a case, an if, a loop or the right operand of && or ||. The error
points at the case or if.
Example
The paths that leave a loop are joined the same way: every break, and for a
for loop over a range or an array also its normal exit, which keeps the
value. A for loop that moves a value and then breaks leaves it moved on one
path and live on the other, so the error points at the loop:
Fix
Treat the binding the same way in every branch, or move the drop or the move after the join.
Run actus explain E1061 in your terminal to read this entry offline. Browse Ownership & types or all error codes.