On this page

0065. Byte-class tables and checked conditional updates

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:

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

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.