rust-lang / rust-lang/rust

Terse parse error on `ident @ pat` in destructuring assignment

Open
#134,778 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-parser A-patterns D-terse T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Consider the following slice pattern:

let arr = [1, 2, 3, 4];
let [a, b, rest @ ..] = arr;
dbg!(a, b, rest); // [src/main.rs:4:5] a = 1
                  // [src/main.rs:4:5] b = 2
                  // [src/main.rs:4:5] rest = [
                  //     3,
                  //     4,
                  // ]

Now, consider the following destructuring assignment with a slice pattern, including a "rest" pattern:

let (c, d);
[c, .., d] = [1, 2, 3, 4];
dbg!(c, d); // [src/main.rs:13:1] c = 1
            // [src/main.rs:13:1] d = 4

Now, consider the following destructuring assignment with a slice pattern, again in this Playground:

let e;
let rest;
let f;
[e, rest @ .., f] = [1, 2, 3, 4];
dbg!(e, rest, f);
/*
error: expected one of `!`, `,`, `.`, `::`, `?`, `]`, `{`, or an operator, found `@`
  --> src/main.rs:19:10
   |
19 | [e, rest @ .., f] = [1, 2, 3, 4];
   |          ^ expected one of 8 possible tokens

error: expected one of `!`, `#`, `(`, `,`, `.`, `::`, `;`, `?`, `[`, `]`, `_`, `async`, `become`, `break`, `continue`, `for`, `if`, `let`, `loop`, `match`, `move`, `return`, `static`, `unsafe`, `while`, `yield`, `{`, `|`, `||`, `}`, an operator, or path, found `@`
  --> src/main.rs:19:10
   |
19 | [e, rest @ .., f] = [1, 2, 3, 4];
   |          ^ expected one of 32 possible tokens
*/

...that wasn't really what I expected! But then, I don't know what I did expect, honestly. It's possible this is "just" a diagnostic issue, but this is one of the somewhat sharper inconsistencies to discover here. I suspect gating this during parsing is not the correct thing to do here, even if we want to error on it.

@rustbot label: +C-bug +T-compiler +T-lang +D-confusing +A-parser +A-slice-patterns

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

Reproduce the slice-pattern destructuring assignment in the linked Rust Playground and compare it with the working rest-pattern examples in the issue. Start by tracing the parser handling for rest @ ..; done should clarify whether the syntax is supported and, if not, provide a consistent diagnostic rather than the terse token errors shown.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.