Conceptual
Login

Catching Mistakes at Build Time

Some mistakes can be caught before the code ever runs: an offset larger than the window it indexes, a structure that is the wrong size for the hardware layout it mirrors, a constant that does not fit its field. The kernel crate provides an assertion that is evaluated during compilation and fails the build rather than the machine. Kernel code prefers this everywhere it is possible, because a driver bug found at run time may mean a reboot, and a bug found at build time costs nothing. It is the reason bounds-checked register access adds no cost when the offsets are constants.

Questions this Concept answers

  • Why does kernel code prefer a build-time failure to a run-time check wherever both are possible?