E1061: branches leave a binding in different states

View source

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

actus
enum Mode {
    Fast,
    Slow,
}

verb main() {
    erg data: Buffer = Buffer[4];
    erg mode: Mode = Mode.Fast;
    case abs mode {
        Mode.Fast => {
            drop(data);
        },
        Mode.Slow => {
        },
    };
}
actus
verb main() {
    erg data: Buffer = Buffer[4];
    erg flag: Bool = true;
    if flag {
        drop(data);
    }
}

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:

actus
verb main() {
    erg data: Buffer = Buffer[4];
    for erg index in 0..3 {
        drop(data);
        break;
    }
}

Fix

Treat the binding the same way in every branch, or move the drop or the move after the join.

actus
enum Mode {
    Fast,
    Slow,
}

verb main() {
    erg data: Buffer = Buffer[4];
    erg mode: Mode = Mode.Fast;
    case abs mode {
        Mode.Fast => {
            drop(data);
        },
        Mode.Slow => {
            drop(data);
        },
    };
}
actus
verb main() {
    erg data: Buffer = Buffer[4];
    erg flag: Bool = true;
    if flag {
        println(bool: flag);
    }
    drop(data);
}

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