On this page

0023. Core IR gains layouts and a tag switch

Context

Records and closed unions need concrete, shared layouts in both backends. This extends ADR-0016 without superseding it. Its anticipated Tag rvalue is replaced by a structured Switch (M4.3 amendment, before merge).

Decision

Add Program.layouts: Vec<Layout>, LayoutId(u32), and Layout { name, kind }. LayoutKind::Record(Vec<FieldDecl>) stores named, typed fields; Union(Vec<VariantDecl>) stores positional payload types. Ty::Record(LayoutId) and Union(LayoutId) remain Copy. Layouts are interned post-order: each referenced layout precedes its owner.

Pure rvalues are Construct { layout, fields } (declared field order), Field { value, index }, Variant { layout, tag, fields } and Payload { value, sources: Vec<(tag, field)> }. Lower source expressions left to right before reordering atoms. Discarding a pure rvalue keeps its operand effects. Aggregate values live in locals, never constants.

Switch { value, arms: Vec<SwitchArm> } is pure and non-trapping, each arm containing tags and a block. Verify every tag exactly once; a payload is valid only inside an arm of the same operand, with one source per covered tag and identical field types. Grouped patterns may read different payload positions. Verify layout IDs/order, construction arity/types, field bounds and types; reject aggregate printing. Monomorphized IR has no parameters or newtypes.

Text stays ir 1; empty layouts print nothing. Records print layout #0 Point = { x: i64, y: i64 }, before functions. User functions keep source-order IDs. C typedefs use ht<id>_<name>, fields f_<name>; equality uses field comparisons, never padding-sensitive memcmp. Emit only equality helpers reached transitively from comparisons, before prototypes.

C switch arms have braces; the last is default to prove fallthrough without unreachable intrinsics. A switch pushes a loop-stack sentinel, Vec<Option<LoopId>>, so source loop exits use labels rather than accidentally breaking the switch. The same sentinel governs use analysis and simple-break emission. A match diverges only when all arms diverge; each arm has its own block. Interpreter aggregates use Rc<[Value]>: Clone handles preserve value semantics and release replaced records, without unsafe or an immortal arena.

Alternatives considered

Consequences

Both backends gain exhaustive record cases in M4.1 and union cases in M4.3. Existing scalar C, IR snapshots, evaluation order and traps stay identical.