Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terse parse error on ident @ pat in destructuring assignment #134778

Open
workingjubilee opened this issue Dec 26, 2024 · 1 comment
Open

Terse parse error on ident @ pat in destructuring assignment #134778

workingjubilee opened this issue Dec 26, 2024 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST A-patterns Relating to patterns and pattern matching D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@workingjubilee
Copy link
Member

workingjubilee commented Dec 26, 2024

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

@rustbot rustbot added needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. A-parser Area: The parsing of Rust source code to an AST A-slice-patterns Area: Slice patterns, https://github.com/rust-lang/rust/issues/23121 C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Dec 26, 2024
@workingjubilee workingjubilee added D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. and removed D-confusing Diagnostics: Confusing error or lint that should be reworked. labels Dec 26, 2024
@fmease fmease added A-diagnostics Area: Messages for errors, warnings, and lints A-patterns Relating to patterns and pattern matching and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Dec 26, 2024
@fmease
Copy link
Member

fmease commented Dec 26, 2024

Yes, it's a diagnostic issue. The LHS of an assignment expressions is not a pattern but an expression unlike in let statements: Expression "=" Expression vs. "let" Pattern "=" Expression. CC The Reference.

The LHS expression actually gets "reinterpreted" as a pattern if possible to allow for destructuring assignment (RFC 2909). That means several classes of patterns are inherently unsupported, including "ref"? "mut"? Identifier "@" Pattern. See also RFC 2909 | Unsupported Patterns.

@fmease fmease removed T-lang Relevant to the language team, which will review and decide on the PR/issue. A-slice-patterns Area: Slice patterns, https://github.com/rust-lang/rust/issues/23121 C-bug Category: This is a bug. labels Dec 26, 2024
@fmease fmease changed the title Inconsistency with slice patterns in destructuring assignment Terse parse error on ident @ pat in destructuring assignment Dec 26, 2024
@rustbot rustbot added A-slice-patterns Area: Slice patterns, https://github.com/rust-lang/rust/issues/23121 C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Dec 26, 2024
@fmease fmease removed T-lang Relevant to the language team, which will review and decide on the PR/issue. A-slice-patterns Area: Slice patterns, https://github.com/rust-lang/rust/issues/23121 C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. labels Dec 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST A-patterns Relating to patterns and pattern matching D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants