Skip to content

Should a [..] slice pattern constitute a discriminant read #141825

Open
@meithecatte

Description

@meithecatte

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();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-patternsRelating to patterns and pattern matchingA-slice-patternsArea: Slice patterns, https://github.com/rust-lang/rust/issues/23121C-discussionCategory: Discussion or questions that doesn't represent real issues.I-lang-nominatedNominated for discussion during a lang team meeting.P-lang-drag-2Lang team prioritization drag level 2.https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions