Skip to content

Commit 5ab4735

Browse files
committed
Recognise nested tuples/arrays/structs
1 parent 5fa02ec commit 5ab4735

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

src/librustc_typeck/check/expr.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
723723
);
724724
}
725725

726+
fn is_destructuring_place_expr(&self, expr: &'tcx hir::Expr) -> bool {
727+
match &expr.kind {
728+
ExprKind::Array(comps) | ExprKind::Tup(comps) => {
729+
comps.iter().all(|e| self.is_destructuring_place_expr(e))
730+
}
731+
ExprKind::Struct(_path, fields, rest) => {
732+
rest.as_ref().map(|e| self.is_destructuring_place_expr(e)).unwrap_or(true) &&
733+
fields.iter().all(|f| self.is_destructuring_place_expr(&f.expr))
734+
}
735+
_ => expr.is_syntactic_place_expr(),
736+
}
737+
}
738+
726739
pub(crate) fn check_lhs_assignable(
727740
&self,
728741
lhs: &'tcx hir::Expr,
@@ -736,17 +749,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
736749
DiagnosticId::Error(err_code.into()),
737750
);
738751
err.span_label(lhs.span, "cannot assign to this expression");
739-
let destructuring_assignment = match &lhs.kind {
740-
ExprKind::Array(comps) | ExprKind::Tup(comps) => {
741-
comps.iter().all(|e| e.is_syntactic_place_expr())
742-
}
743-
ExprKind::Struct(_path, fields, rest) => {
744-
rest.as_ref().map(|e| e.is_syntactic_place_expr()).unwrap_or(true) &&
745-
fields.iter().all(|f| f.expr.is_syntactic_place_expr())
746-
}
747-
_ => false,
748-
};
749-
if destructuring_assignment {
752+
if self.is_destructuring_place_expr(lhs) {
750753
err.note("destructuring assignments are not yet supported");
751754
err.note(
752755
"for more information, see https://github.com/rust-lang/rfcs/issues/372",

src/test/ui/bad/destructuring-assignment.rs

+4
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ fn main() {
1818
//~^ ERROR binary assignment operation `+=` cannot be applied
1919

2020
S { x: a, ..s } = S { x: 3, y: 4 }; //~ ERROR invalid left-hand side of assignment
21+
22+
let c = 3;
23+
24+
((a, b), c) = ((3, 4), 5); //~ ERROR invalid left-hand side of assignment
2125
}

src/test/ui/bad/destructuring-assignment.stderr

+12-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,18 @@ LL | S { x: a, ..s } = S { x: 3, y: 4 };
105105
= note: destructuring assignments are not yet supported
106106
= note: for more information, see https://github.com/rust-lang/rfcs/issues/372
107107

108-
error: aborting due to 10 previous errors
108+
error[E0070]: invalid left-hand side of assignment
109+
--> $DIR/destructuring-assignment.rs:24:5
110+
|
111+
LL | ((a, b), c) = ((3, 4), 5);
112+
| -----------^^^^^^^^^^^^^^
113+
| |
114+
| cannot assign to this expression
115+
|
116+
= note: destructuring assignments are not yet supported
117+
= note: for more information, see https://github.com/rust-lang/rfcs/issues/372
118+
119+
error: aborting due to 11 previous errors
109120

110121
Some errors have detailed explanations: E0067, E0070, E0368.
111122
For more information about an error, try `rustc --explain E0067`.

0 commit comments

Comments
 (0)