E1009: drop of a binding that is borrowed

View source

While an abs view of an owner is live, the owner is frozen.

While an abs view of an owner is live, the owner is frozen. ADR-0027 says an owner cannot be dropped while it is frozen by a view, because the view would point at a released resource. The view lives until the end of the block that holds it, whether or not it is used again. The error points at the drop.

Example

actus
verb main() {
    erg data: Buffer = Buffer[4];
    abs view = ref data;
    println(int: buffer_length(buffer: abs view));
    drop(data);
}

An abs struct field is a view too. The source stays frozen until the end of the block that builds the struct:

actus
struct Header {
    abs bytes: Buffer,
}

verb main() {
    erg data: Buffer = Buffer[4];
    erg header = Header { bytes: data, };
    drop(data);
}

A call whose result can hold a view freezes the sources it was given through abs parameters, as the struct literal above does, because the callee may return a struct that views any of them:

actus
struct Header {
    abs bytes: Buffer,
}

verb open_header(abs source: Buffer) -> Header {
    return Header { bytes: source, };
}

verb main() {
    erg data: Buffer = Buffer[4];
    erg header = open_header(source: abs data);
    drop(data);
}

Fix

End the view first by putting it in an inner block, then drop the owner.

actus
verb main() {
    erg data: Buffer = Buffer[4];
    {
        abs view = ref data;
        println(int: buffer_length(buffer: abs view));
    }
    drop(data);
}

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