Skip to content

Destructuring assignment doesn't work for bare unit structs #118753

Closed

Description

@jsgf

I tried this code:

struct Unit;

Unit = Unit;

I expected to see this being treated as a (trivial) destructuring assignment.

Instead, it fails with:

error[E0070]: invalid left-hand side of assignment
 --> src/main.rs:3:10
  |
3 |     Unit = Unit;
  |     ---- ^
  |     |
  |     cannot assign to this expression

For more information about this error, try `rustc --explain E0070`.
error: could not compile `playground` (bin "playground") due to previous error

Likewise:

enum Enum { Unit }

Enum::Unit = Enum::Unit;

fails similarly.

On the other hand the very similar:

struct Tup0();

Tup0() = Tup0();

works as expected.

The RFC for this says:

We support the following classes of expressions:

  • Tuples.
  • Slices.
  • Structs (including unit and tuple structs).
  • Unique variants of enums.

It looks like enums are also not implemented. The PR implementing this for structs seems to overlook unit structs, and I couldn't find any place claiming to implement it for enums.

Meta

rustc --version --verbose:

rustc 1.71.1 (eb26296b5 2023-08-03)
binary: rustc
commit-hash: eb26296b556cef10fb713a38f3d16b9886080f26
commit-date: 2023-08-03
host: x86_64-unknown-linux-gnu
release: 1.71.1
LLVM version: 16.0.5

And is also present in nightly.

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Dec 8, 2023
  2. jsgf commented on Dec 8, 2023

    @jsgf
    ContributorAuthor

    Oh, this was supposed to have been fixed in 1.62.0 with #95380. But that implements:

    struct Unit;
    
    (Unit, _) = (Unit, 123);
    

    which isn't how I read "We support the following classes of expressions ... structs (including unit)", as I'd expect all those classes of expression to be valid at the top-level of the assignment.

  3. jsgf commented on Dec 8, 2023

    @jsgf
    ContributorAuthor
    Unit {} = Unit;
    

    does work, using the "{} can be used on any unit struct" loophole.

  4. changed the title [-]Destructuring assignment doesn't work for unit structs[/-] [+]Destructuring assignment doesn't work for bare unit structs[/+] on Dec 8, 2023
  5. removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Dec 10, 2023
  6. added a commit that references this issue on Dec 13, 2023
    dbc6ec6
  7. added a commit that references this issue on Dec 13, 2023
    fd43070
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

C-bugCategory: This is a bug.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions