Design decisions

100. Validate stores into range-typed places via ExprKind::RangeCheck (2026-11-21)

  • Decision. Every store into a range-typed place (scalar var/let, struct field and constructor arg, array element including compound assignment / max= / min= / fill / comprehension, and List .push/.resize/.fill) lowers through Lowerer::check_range, which wraps unproved values in ExprKind::RangeCheck { value, lo, hi, text }. Both cpl-interp and cpl-codegen (cpl::check_range under #ifdef LOCAL) trap with exit code 3 and {v} is outside \{text}`. Release builds (-DLOCALunset) compilecpl::check_range` to a zero-cost passthrough, including for narrowed 32-bit struct fields (and future narrowed arrays).
  • Why. Narrowed 0..=1_000_000 struct fields previously truncated silently in C++ while cpl run printed the raw 64-bit value, breaking interpreter/C++ parity, and scalar/array ranges were unenforced on assignment. Checking in cpl run and --checked (-DLOCAL) catches every contract violation locally with identical diagnostics, while eliding proven-in-range stores (literals, narrower range-typed sources, x % P into 0..P, Mod<P> branch-refined +/-/neg) keeps hot loops and Mod<P> codegen completely free of checks. Keeping release zero-cost matches fsqrt, shl128, and sort comparator validation (#ifdef LOCAL) so valid programs pay zero runtime overhead on judges.
  • Reverse. Remove ExprKind::RangeCheck and crates/cpl-ir/src/lower/range.rs to revert validation; or remove the #ifdef LOCAL guard in cpl::check_range in crates/cpl-codegen/src/runtime.rs if release builds should also trap before narrowing truncation.