Skip to content
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

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

Closed
jsgf opened this issue Dec 8, 2023 · 2 comments · Fixed by #118759
Closed

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

jsgf opened this issue Dec 8, 2023 · 2 comments · Fixed by #118759
Assignees
Labels
C-bug Category: This is a bug.

Comments

@jsgf
Copy link
Contributor

jsgf commented Dec 8, 2023

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.

@jsgf jsgf added the C-bug Category: This is a bug. label Dec 8, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 8, 2023
@jsgf
Copy link
Contributor Author

jsgf commented Dec 8, 2023

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.

@jsgf
Copy link
Contributor Author

jsgf commented Dec 8, 2023

Unit {} = Unit;

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

@compiler-errors compiler-errors self-assigned this Dec 8, 2023
@jsgf jsgf changed the title Destructuring assignment doesn't work for unit structs Destructuring assignment doesn't work for bare unit structs Dec 8, 2023
@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 10, 2023
@bors bors closed this as completed in dbc6ec6 Dec 13, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Dec 13, 2023
Rollup merge of rust-lang#118759 - compiler-errors:bare-unit-structs, r=petrochenkov

Support bare unit structs in destructuring assignments

We should be allowed to use destructuring assignments on *bare* unit structs, not just unit structs that are located within other pattern constructors.

Fixes rust-lang#118753

r? petrochenkov since you reviewed rust-lang#95380, reassign if you're busy or don't want to review this.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants