Skip to content

Bug: slice ..rest binding fails MIR lowering (§9.7) #474

Closed

Description

@ehartford

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

  1. In MirBuilder.lower_pattern's NK_PAT_SLICE branch, read rest_sym = ast.get_data2(pat_node) and sp_has_rest.
  2. After binding head variables and before tail variables, compute remaining_count = array_len - head_count - tail_count for fixed arrays. For dynamic slices, use the slice length rvalue minus head/tail counts once slice lowering exposes length.
  3. Allocate a local for rest_sym with type i64 (or the eventual rest-slice type if the spec changes), bind it with bind_local, emit StorageLive, and assign the computed count.
  4. 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.
  5. Add a loud diagnostic for dynamic slice ..rest if count computation is not implemented in the first slice, rather than silently skipping the binding.
  6. Audit let-else and while-let pattern lowering because both reuse lower_pattern after a successful pattern match.

Test plan

Add test/behavior/behav_slice_pattern_rest_binding.w:

  • match [1, 2, 3] with [first, ..rest] and assert first == 1 and rest == 2.
  • match [1, 2, 3, 4] with [first, ..mid, last] and assert mid == 2 under the current count semantics and last == 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

  • rest in [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 :test pass.

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

  1. ehartford commented on Jun 14, 2026

    @ehartford
    ContributorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions