Skip to content

1. Ownership contracts and deterministic builds

Advanced APIs must state whether an input is borrowed, consumed, returned, or retained. Apply ownership contracts at the narrowest boundary and test every exit: success, validation failure, allocation failure, and partial initialization. A custom lifecycle method does not excuse an ambiguous transfer.

Deterministic builds begin before code generation. Pin the compiler and Longbow version, commit application lockfiles, verify package checksums, sort unordered inputs before serialization, and keep host paths and timestamps out of artifacts unless they are declared inputs. Rebuild from a clean cache and compare the artifact that matters, not just console output.

Use ownership contracts, package ecosystem, and native code generation as audit checklists.

Tested example

Put allocation ownership, construction, destruction, and transfer in one auditable contract.

File: curriculum/main-evidence/single/ownership_contracts.fl

curriculum/main-evidence/single/ownership_contracts.fl
structure Box { own *i32 value; };
unsafe Box Box::init(own *i32 value) override {
return build Box { self.value = value; };
}
unsafe void Box::tear(&self) override { free(self->value); }
unsafe i32 Box::get(&const self) { if self->value == null { return 0; } return *self->value; }
unsafe void consume(own *i32 p) { free(p); }
unsafe (own *i32, error) create() { return new<i32>(42); }
i32 main() {
unsafe {
p, e := create();
if e != none { return 1; }
box := Box::init(p);
return box.get();
}
}

The block above is the complete file executed by the commands below.

Allocation, initialization, transfer, and destruction occur under one ownership contract. Keeping those operations together makes the point where responsibility changes visible to both the compiler and reviewer.

Commands

Commands
flecha check curriculum/main-evidence/single/ownership_contracts.fl
flecha build curriculum/main-evidence/single/ownership_contracts.fl -o build/ownership_contracts
build/ownership_contracts

Expected output

Expected output
(empty stdout)
exit status: 42

Related reference: /examples/ownership_contracts/.