diff options
| author | varkor <github@varkor.com> | 2019-12-22 20:27:42 +0000 |
|---|---|---|
| committer | varkor <github@varkor.com> | 2019-12-23 11:17:55 +0000 |
| commit | 5ab4735559aeeece0b5811dad95fdf515b1bcfbd (patch) | |
| tree | 37c62eb8126da58cc81d23f943751be20ed01609 | |
| parent | 5fa02ecc291f0a6b356fbb3b1e14649082b93a2f (diff) | |
| download | rust-5ab4735559aeeece0b5811dad95fdf515b1bcfbd.tar.gz rust-5ab4735559aeeece0b5811dad95fdf515b1bcfbd.zip | |
Recognise nested tuples/arrays/structs
| -rw-r--r-- | src/librustc_typeck/check/expr.rs | 25 | ||||
| -rw-r--r-- | src/test/ui/bad/destructuring-assignment.rs | 4 | ||||
| -rw-r--r-- | src/test/ui/bad/destructuring-assignment.stderr | 13 |
3 files changed, 30 insertions, 12 deletions
diff --git a/src/librustc_typeck/check/expr.rs b/src/librustc_typeck/check/expr.rs index 9085528c84e..13046d8002a 100644 --- a/src/librustc_typeck/check/expr.rs +++ b/src/librustc_typeck/check/expr.rs @@ -723,6 +723,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ); } + fn is_destructuring_place_expr(&self, expr: &'tcx hir::Expr) -> bool { + match &expr.kind { + ExprKind::Array(comps) | ExprKind::Tup(comps) => { + comps.iter().all(|e| self.is_destructuring_place_expr(e)) + } + ExprKind::Struct(_path, fields, rest) => { + rest.as_ref().map(|e| self.is_destructuring_place_expr(e)).unwrap_or(true) && + fields.iter().all(|f| self.is_destructuring_place_expr(&f.expr)) + } + _ => expr.is_syntactic_place_expr(), + } + } + pub(crate) fn check_lhs_assignable( &self, lhs: &'tcx hir::Expr, @@ -736,17 +749,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { DiagnosticId::Error(err_code.into()), ); err.span_label(lhs.span, "cannot assign to this expression"); - let destructuring_assignment = match &lhs.kind { - ExprKind::Array(comps) | ExprKind::Tup(comps) => { - comps.iter().all(|e| e.is_syntactic_place_expr()) - } - ExprKind::Struct(_path, fields, rest) => { - rest.as_ref().map(|e| e.is_syntactic_place_expr()).unwrap_or(true) && - fields.iter().all(|f| f.expr.is_syntactic_place_expr()) - } - _ => false, - }; - if destructuring_assignment { + if self.is_destructuring_place_expr(lhs) { err.note("destructuring assignments are not yet supported"); err.note( "for more information, see https://github.com/rust-lang/rfcs/issues/372", diff --git a/src/test/ui/bad/destructuring-assignment.rs b/src/test/ui/bad/destructuring-assignment.rs index 7112cedfd00..876c9efea26 100644 --- a/src/test/ui/bad/destructuring-assignment.rs +++ b/src/test/ui/bad/destructuring-assignment.rs @@ -18,4 +18,8 @@ fn main() { //~^ ERROR binary assignment operation `+=` cannot be applied S { x: a, ..s } = S { x: 3, y: 4 }; //~ ERROR invalid left-hand side of assignment + + let c = 3; + + ((a, b), c) = ((3, 4), 5); //~ ERROR invalid left-hand side of assignment } diff --git a/src/test/ui/bad/destructuring-assignment.stderr b/src/test/ui/bad/destructuring-assignment.stderr index 676576b7bc5..845008b0693 100644 --- a/src/test/ui/bad/destructuring-assignment.stderr +++ b/src/test/ui/bad/destructuring-assignment.stderr @@ -105,7 +105,18 @@ LL | S { x: a, ..s } = S { x: 3, y: 4 }; = note: destructuring assignments are not yet supported = note: for more information, see https://github.com/rust-lang/rfcs/issues/372 -error: aborting due to 10 previous errors +error[E0070]: invalid left-hand side of assignment + --> $DIR/destructuring-assignment.rs:24:5 + | +LL | ((a, b), c) = ((3, 4), 5); + | -----------^^^^^^^^^^^^^^ + | | + | cannot assign to this expression + | + = note: destructuring assignments are not yet supported + = note: for more information, see https://github.com/rust-lang/rfcs/issues/372 + +error: aborting due to 11 previous errors Some errors have detailed explanations: E0067, E0070, E0368. For more information about an error, try `rustc --explain E0067`. |
