Repository navigation
Description
Summary
Fix slice pattern ..rest bindings so the bound rest value is actually emitted and usable in MIR. Today sema gives rest a type, but MIR lowering only binds head and tail variables and skips the rest binding entirely.
Spec
§9.7 says [first, ..rest] matches arrays with one or more elements and rest is bound to the remaining count (docs/with-specification.md:3616-3627). The same pattern form is allowed in let destructuring (docs/with-specification.md:3630-3646) and let ... else (docs/with-specification.md:3657-3661).
Current state
Parser records slice pattern metadata with rest_sym in NK_PAT_SLICE.d2 (src/Parser.w:5869-5904). Sema binds a slice rest symbol as i64 when present (src/SemaCheck.w:6839-6857), matching the current spec wording "remaining count." MIR lowering checks slice-pattern length (src/MirLower.w:5245-5267) and binds head/tail variables (src/MirLower.w:5436-5477), but never reads rest_sym or emits a local for it. A program can match [first, ..rest], but using rest later has no MIR local.
Implementation plan
- In
MirBuilder.lower_pattern'sNK_PAT_SLICEbranch, readrest_sym = ast.get_data2(pat_node)andsp_has_rest. - After binding head variables and before tail variables, compute
remaining_count = array_len - head_count - tail_countfor fixed arrays. For dynamic slices, use the slice length rvalue minus head/tail counts once slice lowering exposes length. - Allocate a local for
rest_symwith typei64(or the eventual rest-slice type if the spec changes), bind it withbind_local, emitStorageLive, and assign the computed count. - Ensure match success testing uses the same count condition:
[first, ..rest]with no tail requires length >= head count;[first, ..mid, last]requires length >= head + tail. - Add a loud diagnostic for dynamic slice
..restif count computation is not implemented in the first slice, rather than silently skipping the binding. - Audit let-else and while-let pattern lowering because both reuse
lower_patternafter a successful pattern match.
Test plan
Add test/behavior/behav_slice_pattern_rest_binding.w:
- match
[1, 2, 3]with[first, ..rest]and assertfirst == 1andrest == 2. - match
[1, 2, 3, 4]with[first, ..mid, last]and assertmid == 2under the current count semantics andlast == 4.
Add test/behavior/behav_let_else_slice_rest_binding.w once #399's let-else lowering is complete.
Add negative coverage for too-short fixed arrays when there is no rest and for rest with insufficient head/tail elements.
Acceptance criteria
restin[first, ..rest]is usable after a successful pattern match.- Head, tail, and rest-count bindings all lower to concrete MIR locals.
- Unsupported dynamic-slice rest cases fail loudly rather than producing missing locals.
with build && with build :fixpoint && with build :testpass.
Dependencies
#399 for let-else slice-rest tests. #518 for broader pattern-form coverage.
Traceability
From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 9.7.1.27.
Activity
ehartford commentedon Jun 14, 2026
Implemented in 47008f3. MIR now binds slice-pattern rest symbols as i64 remaining counts for fixed arrays, match success checks account for both head and tail elements, let-else slice rest works for fixed arrays, and dynamic-slice rest bindings fail loudly instead of lowering to missing locals. Added match, let-else, and dynamic-slice negative coverage. Verification passed: build, fixpoint, full test suite, and test-green.