Skip to content

Commit 397e914

Browse files
committed
Handle slices and structs as well
1 parent fc4fd10 commit 397e914

File tree

1 file changed

+59
-10
lines changed
  • compiler/rustc_ast_lowering/src

1 file changed

+59
-10
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -900,24 +900,73 @@ impl<'hir> LoweringContext<'_, 'hir> {
900900
eq_sign_span: Span,
901901
assignments: &mut Vec<hir::Stmt<'hir>>,
902902
) -> &'hir hir::Pat<'hir> {
903+
// TODO: Handle `..` and `_`
903904
match &lhs.kind {
905+
// slices:
906+
ExprKind::Array(elements) => {
907+
let pats = self.arena.alloc_from_iter(
908+
elements.iter().map(|e| self.destructure_assign(e, eq_sign_span, assignments)),
909+
);
910+
let slice_pat = hir::PatKind::Slice(pats, None, &[]);
911+
return self.pat(lhs.span, slice_pat);
912+
}
913+
// tuple structs:
914+
ExprKind::Call(callee, args) => {
915+
if let ExprKind::Path(qself, path) = &callee.kind {
916+
let pats = self.arena.alloc_from_iter(
917+
args.iter().map(|e| self.destructure_assign(e, eq_sign_span, assignments)),
918+
);
919+
let qpath = self.lower_qpath(
920+
callee.id,
921+
qself,
922+
path,
923+
ParamMode::Optional,
924+
ImplTraitContext::disallowed(),
925+
);
926+
let tuple_struct_pat = hir::PatKind::TupleStruct(qpath, pats, None);
927+
return self.pat(lhs.span, tuple_struct_pat);
928+
}
929+
}
930+
// structs:
931+
ExprKind::Struct(path, fields, _rest) => {
932+
let field_pats = self.arena.alloc_from_iter(fields.iter().map(|f| {
933+
let pat = self.destructure_assign(&f.expr, eq_sign_span, assignments);
934+
hir::FieldPat {
935+
hir_id: self.next_id(),
936+
ident: f.ident,
937+
pat,
938+
is_shorthand: f.is_shorthand,
939+
span: f.span,
940+
}
941+
}));
942+
let qpath = self.lower_qpath(
943+
lhs.id,
944+
&None,
945+
path,
946+
ParamMode::Optional,
947+
ImplTraitContext::disallowed(),
948+
);
949+
let struct_pat = hir::PatKind::Struct(qpath, field_pats, false);
950+
return self.pat(lhs.span, struct_pat);
951+
}
952+
// tuples:
904953
ExprKind::Tup(elements) => {
905954
let pats = self.arena.alloc_from_iter(
906955
elements.iter().map(|e| self.destructure_assign(e, eq_sign_span, assignments)),
907956
);
908957
let tuple_pat = hir::PatKind::Tuple(pats, None);
909-
self.pat(lhs.span, tuple_pat)
910-
}
911-
_ => {
912-
let ident = Ident::new(sym::lhs, lhs.span);
913-
let (pat, binding) = self.pat_ident(lhs.span, ident);
914-
let ident = self.expr_ident(lhs.span, ident, binding);
915-
let assign = hir::ExprKind::Assign(self.lower_expr(lhs), ident, eq_sign_span);
916-
let expr = self.expr(lhs.span, assign, ThinVec::new());
917-
assignments.push(self.stmt_expr(lhs.span, expr));
918-
pat
958+
return self.pat(lhs.span, tuple_pat);
919959
}
960+
_ => {}
920961
}
962+
// Treat all other cases as normal lvalue.
963+
let ident = Ident::new(sym::lhs, lhs.span);
964+
let (pat, binding) = self.pat_ident(lhs.span, ident);
965+
let ident = self.expr_ident(lhs.span, ident, binding);
966+
let assign = hir::ExprKind::Assign(self.lower_expr(lhs), ident, eq_sign_span);
967+
let expr = self.expr(lhs.span, assign, ThinVec::new());
968+
assignments.push(self.stmt_expr(lhs.span, expr));
969+
pat
921970
}
922971

923972
/// Desugar `<start>..=<end>` into `std::ops::RangeInclusive::new(<start>, <end>)`.

0 commit comments

Comments
 (0)