Skip to content

Slice pattern rest binding on a Vec or slice: refused as not implemented; spec says count for arrays but uses rest.len() #2283

Closed

Description

@ehartford

Spec §9.7 (docs/spec/functions.md, Slice patterns) shows

match items:
    [first, ..rest] => "head: {first}, {rest.len()} more"

and let [first, ..rest] = items else return Err(.Empty), but the rule text says that for fixed-size arrays rest is bound to the remaining count (the compiler binds an i64).

On a Vec or a slice the compiler refuses the binding: error: slice rest binding for dynamic slices is not implemented yet (src/SemaCheck.w, check_pattern NK_PAT_SLICE).

Repro:

fn main:
    let v = [1, 2, 3]          // a Vec (D113)
    match v:
        [first, ..rest] => print(first)
        _ => print(0)

Expected: compiles; rest is what the spec's own example uses (rest.len()), presumably a []T view of the remaining elements.
Actual: refused.

D113 makes this common: before it, let arr = [1, 2, 3] was a fixed array and the count form applied. The behavior tests that exercised it (behav_slice_pattern_rest_binding, slice_pat_both, slice_pat_head_rest, slice_pat_exhaustive, behav_1367) now annotate [i32; N].

Two things are owed: the implementation, and a ruling on the array form. A rest that is a count for an array and a view for a Vec means the same source changes meaning with the demanded type; the uniform reading is a view in both.

Activity

  1. ehartford commented on Oct 9, 2026

    @ehartford
    ContributorAuthor

    Fixed by D115 phase 1 (#2297, merged 535711d). Spec §9.7 rules rest as the remaining elements (D115, #2288). Verified on the installed compiler at 58b1448: match v: [a, ..rest] over a List place prints 10 2 20, and let [first, ..rest] = s else ... over a slice returns 3. The O(1) owned-List remainder (D115 Amendment 1) is tracked with the List header change; the guard refusal is #2289.

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