Effect attributes
Use #[no_alloc] to rule out allocation and #[no_panic] to rule out
panics and traps. The compiler checks both attributes on each compiled
function instance after move, drop and borrow checking. It follows direct
calls and destructors on the elaborated MIR. An error points to the marked
function and includes the calls that lead to the operation.
Avoiding panic checks
Ordinary indexing and slicing can emit bounds checks. Use
certified slice extents when a shape guard, an order comparison
or a range loop establishes the needed bounds. extent_at and extent_slice
are checked during lowering and emit no bounds assertion.
import std.extent
#[no_panic]
#[no_alloc]
fn sum(xs: [i64]) -> i64 {
var s = 0
let n = xs.len as u64
for i in 0..n {
s +%= extent_at(xs, i)
}
s
}
#[no_panic]
fn first(xs: [i64]) -> i64 {
if 0 < xs.len as u64 { extent_at(xs, 0) } else { 0 }
}
Checked +, - and * can fail on overflow. Use wrapping operators
when wrapping is the intended behavior. With --release, those ordinary
operators also wrap. Division or remainder by a divisor that is not a
nonzero MIR constant may still trap. An assertion in the elaborated MIR
counts as a possible panic.
Calls and drops
- Calls to the standard library’s
sys.alloc_bytesandsys.realloc_bytescount as allocation in every profile. The check also recognizes C’smalloc,calloc,realloc,aligned_allocandposix_memalign. - Other external functions are assumed to have neither effect. An external
function declared to return
nevercounts against#[no_panic]. - A call through an unknown function pointer or closure value, or a drop
with an unknown destructor, can have either effect. Statically resolved
calls and inlined
comptimeclosures expose their implementations to the check. - Drops include the
Dropimplementations for the type and its parts.
Both attributes are checked by jihoo check, the language server,
jihoo emit-mir in its default mode, and commands that generate code.