Skip to content

Erroneous suggestion to add a slice in pattern-matching #96103

Closed
@tjhance

Description

@tjhance

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=44c555e3d1e880be0fcff214d82cfe75

struct Foo {
    v: Vec<u32>,
}

fn f(foo: &Foo) {
    match foo {
        Foo { v: [1, 2] } => { }
        _ => { }
    }
}

fn main() {
    
}

The current output is:

E0529: expected an array or slice, found `Vec<u32>`
 --> src/main.rs:7:18
  |
6 |     match foo {
  |           --- help: consider slicing here: `foo[..]`
7 |         Foo { v: [1, 2] } => { }
  |                  ^^^^^^ pattern cannot match with input type `Vec<u32>`

For more information about this error, try `rustc --explain E0529`.

The suggestion to try writing foo[..] is erroneous and would cause a different type error ("cannot index into a value of type &Foo"). I believe the suggestion should be removed here.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions