Skip to content

Change destructuring assignment lowering to use match. #96588

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

Closed
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
27 changes: 11 additions & 16 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}

/// Destructure the LHS of complex assignments.
/// For instance, lower `(a, b) = t` to `{ let (lhs1, lhs2) = t; a = lhs1; b = lhs2; }`.
/// For instance, lower `(a, b) = t` to `match t { (lhs1, lhs2) => { a = lhs1; b = lhs2; } }`.
fn lower_expr_assign(
&mut self,
lhs: &Expr,
Expand Down Expand Up @@ -979,22 +979,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
let pat = self.destructure_assign(lhs, eq_sign_span, &mut assignments);
let rhs = self.lower_expr(rhs);

// Introduce a `let` for destructuring: `let (lhs1, lhs2) = t`.
let destructure_let = self.stmt_let_pat(
None,
whole_span,
Some(rhs),
pat,
hir::LocalSource::AssignDesugar(self.lower_span(eq_sign_span)),
);
// Block of statements for the assignments: `{ a = lhs1; b = lhs2; }`.
let stmts = self.arena.alloc_from_iter(assignments);
let block = self.block_all(whole_span, stmts, None);
let assignments_block = self.arena.alloc(self.expr_block(block, ThinVec::new()));

// `a = lhs1; b = lhs2;`.
let stmts = self
.arena
.alloc_from_iter(std::iter::once(destructure_let).chain(assignments.into_iter()));

// Wrap everything in a block.
hir::ExprKind::Block(&self.block_all(whole_span, stmts, None), None)
// Produce the `match` for destructuring: `match t { (lhs1, lhs2) => ... }
hir::ExprKind::Match(
rhs,
arena_vec![self; self.arm(pat, assignments_block)],
hir::MatchSource::Normal, // FIXME: is `Normal` fine here?
)
}

/// If the given expression is a path to a tuple struct, returns that path.
Expand Down