0065. Byte-class tables and checked conditional updates
- Status: Accepted
- Date: 2026-10-31
- Plan: M8.2
- Review: Compiler and backend
Context
GCC does not reliably recover byte-class tables or branchless checked counts from emitted control flow. Main ee8f811 supports only union patterns, so M8.2 also needs integer-pattern checking before its intended lowering can apply.
Decision
Extend match to integers: literals, nonempty half-open ranges,
| alternatives and one final _ default.
Endpoints denote representable covered values (an exclusive upper
bound may be MAX+1). Evaluate the scrutinee once; overlaps use the
first arm. E0517/E0518 diagnose invalid patterns/missing defaults.
This extends ADR-0022 to integers; its closed-union restrictions
stay in force.
Lower integer matches to flat sibling predicates with a matched flag, not an arm-depth-proportional tree. Keep comparisons in Core IR and the interpreter. Omit i32 endpoint tautologies; expose an unconditional default when all arms diverge, so strict C builds see that a returning match cannot fall through.
Only C emission substitutes a static const 256-entry value table when:
- The sole input is u8 or an immutable, transitively byte-derived local (including widening to i64). Reassigned wider locals carry no byte proof.
- The tree only uses scalar atoms, comparisons, boolean predicates and checked constant negations; all 256 executions produce one integer/bool destination.
- Every intermediate is private and all its reads belong to the replaced tree. Any effect, unsupported operation, missing value or potential trap rejects it.
Index with the proven input directly: never truncate an unproven integer or speculate a table read. Other matches retain comparisons, including all out-of-domain values. No range-check elimination or new C undefined behavior.
For scalar integer/bool atom assignments to the same
destination, emit a C ternary; an omitted else selects the
existing named local, never an uninitialized anonymous result.
This covers min/max and
x = x + if c { 1 } else { 0 }. For exactly
if c { x = x + 1 }, emit the same width-specific
checked addition with (T)!!c. Adding zero cannot
overflow; adding one keeps the original trap and operator
position. Conditions already ran once in IR order. Other
arithmetic/effectful arms retain control flow. The trap branch
remains.
Alternatives considered
- Leave all recognition to gcc — measured classifier/count chains are slower.
- Truncate indices or emit unchecked arithmetic — changes non-byte or overflow semantics; a byte proof and the original checked helper are required.
- A new optimized IR/interpreter operation — unnecessary; retain the independent comparison/control-flow reference for differential testing.
Consequences
No dependencies, runtime or formatter syntax changes. Value tables cost 256 entries; action-class dispatch and broader range analysis can be considered separately. Measurements show 5.04× classification, 3.82× counting, and min/max within noise on seeded 32 MiB input.