On this page

0066. Eliminate checks from range facts

Context

C obscures source relations; hot kernels need check elimination and an inspectable guarantee. Keep structured IR (0016) and arithmetic/sequence semantics: 0012, 0040, 0044.

Decision

Run a conservative intraprocedural forward pass on placed, specialized IR. Track integer intervals and difference inequalities over locals and sequence lengths; refine branches, short-circuit guards, and successful earlier checks. Assignments invalidate affected facts. Loop heads keep only facts preserved by every iteration, using a recursive assignment scan and monotonic changes; joins keep common facts. No fixpoint or interprocedural value assumptions. Cap stored edges/ranges/conditions and query walks; dropping facts only loses precision. Contradictory/unreachable paths conservatively retain checks.

Store proofs in Func.proven, keyed by line/column/kind. Repeated lowered sites share a proof only if all are proven. C alone consumes proofs: emit plain arithmetic/casts or unchecked index/shift/set helpers. Proven-index helpers are assumptions the analysis guarantees (0 <= i < len); on GCC/Clang they expose the range with __builtin_unreachable, otherwise a plain cast. Unproven checks keep their helper and trap position. The interpreter executes every check, so differential tests can expose an unsound C-only elimination.

--report=checks counts remaining/eliminated static sites per function, line, and kind: bounds, overflow, divide (zero and signed-minimum overflow), shift, and slice. Slices always remain; allocation, UTF-8, I/O, platform failures, and checks inside callees are outside this local value-check contract.

Contextual checkfree fn applies to top-level functions only. The shared front end rejects every remaining body check with E0720, ordered by source position; reached generic instances are checked and duplicate source sites reported once. It is a checked assertion, not permission to bypass checks or a no-trap promise. Formatter/source tools retain the modifier; identifiers named checkfree stay legal.

Alternatives considered

Consequences

Traps/output stay identical; C digests change. Teaching/JSON goldens, differential fixtures and helper assertions guard soundness. Measurements show 1.34× on nested loops; dynamic vector-length proofs remain a precision limit.