Skip to content

Exhaustiveness: a match on a slice or Vec whose slice patterns cover every length ([] / [_] / [_, ..]) still demands a '_' arm; the §9.7 example is rejected #1533

Closed

Description

@ehartford

§9.7 (Slice patterns) gives this match with no _ arm:

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

The patterns cover every length: 0, then 1, then 1 or more. The exhaustiveness engine (SemaCheck, the #1388 pattern matrix) classifies a dynamic slice as Open. The comment above SemaPatRows says so on purpose: "Every other type (str, floats, dynamic slices, ...) is open: only a catch-all covers it". So such a match is rejected:

fn head(s: []i32) -> i32:
    match s:
        [a, b] => a - b
        [a, ..] => a
        [] => -2
error: non-exhaustive match on '[]i32': `_` is not covered

(stage1 of the #1389 branch. #1389 makes slice patterns over slices and Vecs lower at all, and its fixture adds the _ arm because of this.)

Slice patterns only bind names (Parser.parse_slice_pattern accepts identifiers and one ..), so exhaustiveness over a slice or Vec reduces to length coverage. A rest pattern with k fixed elements covers every length >= k, and an exact pattern covers one length. The match is exhaustive exactly when each length from 0 up to the smallest rest minimum is covered. A SemaExhClass.Slice column with length constructors would give the witness too, for example "a slice of length 2 is not covered".

The spec example also uses a named rest over a dynamic sequence ({rest.len()} more), which is still reported as "slice rest binding for dynamic slices is not implemented yet". §9.7 binds rest to "the remaining count" for fixed-size arrays and uses it as a slice in this example, so which of the two a dynamic rest binds is a ruling. That is part of the #1389 report.

Activity

  1. ehartford commented on Sep 23, 2026

    @ehartford
    ContributorAuthor

    Spec self-conflict noted: §9.7 line 4244 says rest "is bound to the remaining count", while both examples (lines 1849, 4239) write rest.len(), which needs the remaining elements. Proposed to Eric: ..rest binds a view of the remaining elements (&[]T for a dynamic subject; D27: element access observes), and line 4244 is corrected. The rejected []/[_]/[_, ..] exhaustiveness is a compiler bug regardless. Pending Eric's yes.

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