Skip to content

Sub-optional error message with slice rest syntax in patterns #72373

Closed
@leonardo-m

Description

@leonardo-m
fn foo(c: &[u32], n: u32) -> u32 {
    match *c {
        [h, ..] if h > n => 0,
        [h, ..] if h == n => 1,
        //[h, ref ts @..] => foo(c, n - h) + foo(ts, n), // OK
        [h, ref ts..] => foo(c, n - h) + foo(ts, n), // Wrong
        [] => 0,
    }
}
fn main() {}

Gives:

error: expected one of `,`, `@`, `]`, or `|`, found `..`
 --> ...\test.rs:6:19
  |
6 |         [h, ref ts..] => foo(c, n - h) + foo(ts, n),
  |                   -^
  |                   |
  |                   expected one of `,`, `@`, `]`, or `|`
  |                   help: missing `,`

error[E0308]: mismatched types
 --> ...\test.rs:6:46
  |
6 |         [h, ref ts..] => foo(c, n - h) + foo(ts, n),
  |                                              ^^ expected slice `[u32]`, found `u32`
  |
  = note: expected reference `&[u32]`
             found reference `&u32`

error: aborting due to 2 previous errors

I think a better error message should suggest the usage of ref ts @...

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTA-patternsRelating to patterns and pattern matchingA-slice-patternsArea: Slice patterns, https://github.com/rust-lang/rust/issues/23121C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-papercutDiagnostics: An error or lint that needs small tweaks.T-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