Skip to content

Commit fd43070

Browse files
authored
Unrolled build for rust-lang#118759
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.
2 parents 7176b8b + d473bdf commit fd43070

9 files changed

+42
-24
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
12221222
| ExprKind::Struct(..)
12231223
| ExprKind::Tup(..)
12241224
| ExprKind::Underscore => false,
1225+
// Check for unit struct constructor.
1226+
ExprKind::Path(..) => lower_ctx.extract_unit_struct_path(lhs).is_none(),
12251227
// Check for tuple struct constructor.
12261228
ExprKind::Call(callee, ..) => lower_ctx.extract_tuple_struct_path(callee).is_none(),
12271229
ExprKind::Paren(e) => {

tests/ui/destructuring-assignment/bad-expr-lhs.rs

-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@ fn main() {
44
(1, 2) = (3, 4);
55
//~^ ERROR invalid left-hand side of assignment
66
//~| ERROR invalid left-hand side of assignment
7-
8-
None = Some(3); //~ ERROR invalid left-hand side of assignment
97
}

tests/ui/destructuring-assignment/bad-expr-lhs.stderr

+1-9
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,7 @@ LL | (1, 2) = (3, 4);
3030
| |
3131
| cannot assign to this expression
3232

33-
error[E0070]: invalid left-hand side of assignment
34-
--> $DIR/bad-expr-lhs.rs:8:10
35-
|
36-
LL | None = Some(3);
37-
| ---- ^
38-
| |
39-
| cannot assign to this expression
40-
41-
error: aborting due to 5 previous errors
33+
error: aborting due to 4 previous errors
4234

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

tests/ui/destructuring-assignment/struct-or-enum-variant-path.rs

+6
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,30 @@ type A = E;
1111
fn main() {
1212
let mut a;
1313

14+
S = S;
1415
(S, a) = (S, ());
1516

17+
E::V = E::V;
1618
(E::V, a) = (E::V, ());
1719

20+
<E>::V = E::V;
1821
(<E>::V, a) = (E::V, ());
22+
A::V = A::V;
1923
(A::V, a) = (E::V, ());
2024
}
2125

2226
impl S {
2327
fn check() {
2428
let a;
29+
Self = S;
2530
(Self, a) = (S, ());
2631
}
2732
}
2833

2934
impl E {
3035
fn check() {
3136
let a;
37+
Self::V = E::V;
3238
(Self::V, a) = (E::V, ());
3339
}
3440
}

tests/ui/inference/issue-103587.stderr

+1-5
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,10 @@ error[E0308]: mismatched types
2626
LL | if None = x { }
2727
| ^^^^^^^^ expected `bool`, found `()`
2828
|
29-
help: you might have meant to use pattern matching
29+
help: consider adding `let`
3030
|
3131
LL | if let None = x { }
3232
| +++
33-
help: you might have meant to compare for equality
34-
|
35-
LL | if None == x { }
36-
| +
3733

3834
error: aborting due to 3 previous errors
3935

tests/ui/issues/issue-13407.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ mod A {
44

55
fn main() {
66
A::C = 1;
7-
//~^ ERROR: invalid left-hand side of assignment
8-
//~| ERROR: struct `C` is private
7+
//~^ ERROR: mismatched types
8+
//~| ERROR: unit struct `C` is private
99
}

tests/ui/issues/issue-13407.stderr

+9-6
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@ note: the unit struct `C` is defined here
1010
LL | struct C;
1111
| ^^^^^^^^^
1212

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

2124
error: aborting due to 2 previous errors
2225

23-
Some errors have detailed explanations: E0070, E0603.
24-
For more information about an error, try `rustc --explain E0070`.
26+
Some errors have detailed explanations: E0308, E0603.
27+
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)