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

Support bare unit structs in destructuring assignments #118759

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
| ExprKind::Struct(..)
| ExprKind::Tup(..)
| ExprKind::Underscore => false,
// Check for unit struct constructor.
ExprKind::Path(..) => lower_ctx.extract_unit_struct_path(lhs).is_none(),
// Check for tuple struct constructor.
ExprKind::Call(callee, ..) => lower_ctx.extract_tuple_struct_path(callee).is_none(),
ExprKind::Paren(e) => {
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/destructuring-assignment/bad-expr-lhs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@ fn main() {
(1, 2) = (3, 4);
//~^ ERROR invalid left-hand side of assignment
//~| ERROR invalid left-hand side of assignment

None = Some(3); //~ ERROR invalid left-hand side of assignment
}
10 changes: 1 addition & 9 deletions tests/ui/destructuring-assignment/bad-expr-lhs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,7 @@ LL | (1, 2) = (3, 4);
| |
| cannot assign to this expression

error[E0070]: invalid left-hand side of assignment
--> $DIR/bad-expr-lhs.rs:8:10
|
LL | None = Some(3);
| ---- ^
| |
| cannot assign to this expression

error: aborting due to 5 previous errors
error: aborting due to 4 previous errors

Some errors have detailed explanations: E0067, E0070.
For more information about an error, try `rustc --explain E0067`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
None = Some(3);
//~^ ERROR refutable pattern in local binding
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0005]: refutable pattern in local binding
--> $DIR/non-exhaustive-destructure.rs:2:5
|
LL | None = Some(3);
| ^^^^ pattern `Some(_)` not covered
|
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
= note: the matched value is of type `Option<i32>`
help: you might want to use `if let` to ignore the variant that isn't matched
|
LL | if None = Some(3) { todo!() };
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This diagnostics regression is pre-existing (e.g. Some(x) = None). I will look into fixing it -- the issue is that we don't really know if an assignment comes a destructuring assignment by the time we get to mir-build.

We could use a desugaring kind on the span, perhaps?

| ++ +++++++++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0005`.
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,30 @@ type A = E;
fn main() {
let mut a;

S = S;
(S, a) = (S, ());

E::V = E::V;
(E::V, a) = (E::V, ());

<E>::V = E::V;
(<E>::V, a) = (E::V, ());
A::V = A::V;
(A::V, a) = (E::V, ());
}

impl S {
fn check() {
let a;
Self = S;
(Self, a) = (S, ());
}
}

impl E {
fn check() {
let a;
Self::V = E::V;
(Self::V, a) = (E::V, ());
}
}
6 changes: 1 addition & 5 deletions tests/ui/inference/issue-103587.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,10 @@ error[E0308]: mismatched types
LL | if None = x { }
| ^^^^^^^^ expected `bool`, found `()`
|
help: you might have meant to use pattern matching
help: consider adding `let`
|
LL | if let None = x { }
| +++
help: you might have meant to compare for equality
|
LL | if None == x { }
| +

error: aborting due to 3 previous errors

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/issues/issue-13407.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ mod A {

fn main() {
A::C = 1;
//~^ ERROR: invalid left-hand side of assignment
//~| ERROR: struct `C` is private
//~^ ERROR: mismatched types
//~| ERROR: unit struct `C` is private
}
15 changes: 9 additions & 6 deletions tests/ui/issues/issue-13407.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ note: the unit struct `C` is defined here
LL | struct C;
| ^^^^^^^^^

error[E0070]: invalid left-hand side of assignment
--> $DIR/issue-13407.rs:6:10
error[E0308]: mismatched types
--> $DIR/issue-13407.rs:6:5
|
LL | struct C;
| -------- unit struct defined here
...
LL | A::C = 1;
| ---- ^
| ^^^^ - this expression has type `{integer}`
| |
| cannot assign to this expression
| expected integer, found `C`

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0070, E0603.
For more information about an error, try `rustc --explain E0070`.
Some errors have detailed explanations: E0308, E0603.
For more information about an error, try `rustc --explain E0308`.
Loading