rust-lang / rust-lang/rust

Should a `[..]` slice pattern constitute a discriminant read

Open
#141,825 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-patterns A-slice-patterns C-discussion I-lang-radar T-lang
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

As an unresolved question in the discussion around #138961, it's unclear whether matching [..] against a slice should constitute a read of the length (which would behave like a discriminant read), or if it should behave like a wildcard pattern _.

This affects borrow checking and closure captures.

The behavior implemented for closure captures in #138961 is that [..] does not read the length. @Nadrieril suggests that it would be more consistent to perform the read regardless.

The purpose of this issue is to track the resolution of this spec question and subsequent implementation.

An example program affected by this would be:

fn main() {
    let mut a: &mut [i32] = &mut [1, 2, 3];
    let mut f = || {
        // currently, this does not capture anything.
        // a read of the length would cause a capture of `a`
        let [..] = a;
    };
    a[0] += 1; // mutate `a`. this would not compile if a discriminant read were performed
    f();
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the discussion in #138961 and the example program in this issue; compare the existing closure-capture behavior with the borrow-checking implications of treating [..] as a length read or as a wildcard. Done means the specification question is resolved and the corresponding behavior is implemented consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.