Skip to content

Unit struct and enum variants are not accepted as the pattern for destructuring assignment #94319

Closed
@ChayimFriedman2

Description

@ChayimFriedman2
fn main() {
    let mut a;
    struct S;
    (S, a) = (S, ()); // Error
    (S {}, a) = (S, ()); // Works
    enum E { A }
    (E::A, a) = (E::A, ()); // Error
    (E::A {}, a) = (E::A, ()); // Works
}
error[E0070]: invalid left-hand side of assignment
 --> src/main.rs:4:12
  |
4 |     (S, a) = (S, ()); // Error
  |      -     ^
  |      |
  |      cannot assign to this expression

error[E0070]: invalid left-hand side of assignment
 --> src/main.rs:7:15
  |
7 |     (E::A, a) = (E::A, ()); // Error
  |      ----     ^
  |      |
  |      cannot assign to this expression

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=bf807e599b504f489454388eb27acbbb

The RFC states explicitly we should support them:

https://github.com/rust-lang/rfcs/blob/master/text/2909-destructuring-assignment.md#reference-level-explanation

The class of assignee expressions is defined inductively:

  • Place: place.
  • Underscore: _.
  • Tuples: (assignee, assignee, assignee), (assignee, .., assignee), (.., assignee, assignee), (assignee, assignee, ..).
  • Slices: [assignee, assignee, assignee], [assignee, .., assignee], [.., assignee, assignee], [assignee, assignee, ..].
  • Tuple structs: path(assignee, assignee, assignee), path(assignee, .., assignee), path(.., assignee, assignee), path(assignee, assignee, ..).
  • Structs: path { field: assignee, field: assignee }, path { field: assignee, field: assignee, .. }.
  • Unit structs: path.

@rustbot label: +F-destructuring_assignment

Metadata

Metadata

Labels

C-bugCategory: This is a bug.F-destructuring_assignment`#![feature(destructuring_assignment)]`

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions