Runtime Safety
Zig provides a level of safety, where problems may be found during execution. Safety can be left on, or turned off. Zig has many cases of so-called detectable illegal behaviour, meaning that illegal behaviour will be caught (causing a panic) with safety on, but will result in undefined behaviour with safety off. Users are strongly recommended to develop and test their software with safety on, despite its speed penalties.
For example, runtime safety protects you from out of bounds indices.
The user may disable runtime safety for the current block using the built-in
function
@setRuntimeSafety
.
Safety is off for some build modes (to be discussed later).
Unreachable
unreachable
is an
assertion to the compiler that this statement will not be reached. It can tell
the compiler that a branch is impossible, which the optimiser can then take
advantage of. Reaching an
unreachable
is
detectable illegal behaviour.
As it is of the type
noreturn
, it is
compatible with all other types. Here it coerces to u32.
Here is an unreachable being used in a switch.