Skip to content

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

New issue

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

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

Already on GitHub? # to your account

Closed
ChayimFriedman2 opened this issue Feb 24, 2022 · 1 comment · Fixed by #95380
Assignees
Labels
C-bug Category: This is a bug. F-destructuring_assignment `#![feature(destructuring_assignment)]`

Comments

@ChayimFriedman2
Copy link
Contributor

ChayimFriedman2 commented Feb 24, 2022

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

@ChayimFriedman2 ChayimFriedman2 added the C-bug Category: This is a bug. label Feb 24, 2022
@rustbot rustbot added the F-destructuring_assignment `#![feature(destructuring_assignment)]` label Feb 24, 2022
@compiler-errors
Copy link
Member

@rustbot claim

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
C-bug Category: This is a bug. F-destructuring_assignment `#![feature(destructuring_assignment)]`
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants