Skip to content

Commit be191a0

Browse files
committed
de-duplicate condition scoping logic
1 parent 5e749eb commit be191a0

File tree

3 files changed

+26
-55
lines changed

3 files changed

+26
-55
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
532532
then: &Block,
533533
else_opt: Option<&Expr>,
534534
) -> hir::ExprKind<'hir> {
535-
let lowered_cond = self.lower_cond(cond);
535+
let lowered_cond = self.lower_expr(cond);
536536
let then_expr = self.lower_block_expr(then);
537537
if let Some(rslt) = else_opt {
538538
hir::ExprKind::If(
@@ -545,44 +545,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
545545
}
546546
}
547547

548-
// Lowers a condition (i.e. `cond` in `if cond` or `while cond`), wrapping it in a terminating scope
549-
// so that temporaries created in the condition don't live beyond it.
550-
fn lower_cond(&mut self, cond: &Expr) -> &'hir hir::Expr<'hir> {
551-
fn has_let_expr(expr: &Expr) -> bool {
552-
match &expr.kind {
553-
ExprKind::Binary(_, lhs, rhs) => has_let_expr(lhs) || has_let_expr(rhs),
554-
ExprKind::Let(..) => true,
555-
_ => false,
556-
}
557-
}
558-
559-
// We have to take special care for `let` exprs in the condition, e.g. in
560-
// `if let pat = val` or `if foo && let pat = val`, as we _do_ want `val` to live beyond the
561-
// condition in this case.
562-
//
563-
// In order to maintain the drop behavior for the non `let` parts of the condition,
564-
// we still wrap them in terminating scopes, e.g. `if foo && let pat = val` essentially
565-
// gets transformed into `if { let _t = foo; _t } && let pat = val`
566-
match &cond.kind {
567-
ExprKind::Binary(op @ Spanned { node: ast::BinOpKind::And, .. }, lhs, rhs)
568-
if has_let_expr(cond) =>
569-
{
570-
let op = self.lower_binop(*op);
571-
let lhs = self.lower_cond(lhs);
572-
let rhs = self.lower_cond(rhs);
573-
574-
self.arena.alloc(self.expr(cond.span, hir::ExprKind::Binary(op, lhs, rhs)))
575-
}
576-
ExprKind::Let(..) => self.lower_expr(cond),
577-
_ => {
578-
let cond = self.lower_expr(cond);
579-
let reason = DesugaringKind::CondTemporary;
580-
let span_block = self.mark_span_with_reason(reason, cond.span, None);
581-
self.expr_drop_temps(span_block, cond)
582-
}
583-
}
584-
}
585-
586548
// We desugar: `'label: while $cond $body` into:
587549
//
588550
// ```
@@ -606,7 +568,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
606568
body: &Block,
607569
opt_label: Option<Label>,
608570
) -> hir::ExprKind<'hir> {
609-
let lowered_cond = self.with_loop_condition_scope(|t| t.lower_cond(cond));
571+
let lowered_cond = self.with_loop_condition_scope(|t| t.lower_expr(cond));
610572
let then = self.lower_block_expr(body);
611573
let expr_break = self.expr_break(span);
612574
let stmt_break = self.stmt_expr(span, expr_break);

compiler/rustc_hir_analysis/src/check/region.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -176,23 +176,34 @@ fn resolve_block<'tcx>(
176176
visitor.cx = prev_cx;
177177
}
178178

179-
fn resolve_arm<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, arm: &'tcx hir::Arm<'tcx>) {
180-
fn has_let_expr(expr: &Expr<'_>) -> bool {
181-
match &expr.kind {
182-
hir::ExprKind::Binary(_, lhs, rhs) => has_let_expr(lhs) || has_let_expr(rhs),
183-
hir::ExprKind::Let(..) => true,
184-
_ => false,
185-
}
186-
}
179+
/// Resolve a condition from an `if` expression or match guard so that it is a terminating scope
180+
/// if it doesn't contain `let` expressions.
181+
fn resolve_cond<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, cond: &'tcx hir::Expr<'tcx>) {
182+
let terminate = match cond.kind {
183+
// Temporaries for `let` expressions must live into the success branch.
184+
hir::ExprKind::Let(_) => false,
185+
// Logical operator chains are handled in `resolve_expr`. The operators themselves have no
186+
// intermediate value to drop, and operands will be wrapped in terminating scopes.
187+
hir::ExprKind::Binary(
188+
source_map::Spanned { node: hir::BinOpKind::And | hir::BinOpKind::Or, .. },
189+
_,
190+
_,
191+
) => false,
192+
// Otherwise, conditions should always drop their temporaries.
193+
_ => true,
194+
};
195+
resolve_expr(visitor, cond, terminate);
196+
}
187197

198+
fn resolve_arm<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, arm: &'tcx hir::Arm<'tcx>) {
188199
let prev_cx = visitor.cx;
189200

190201
visitor.enter_node_scope_with_dtor(arm.hir_id.local_id, true);
191202
visitor.cx.var_parent = visitor.cx.parent;
192203

193204
resolve_pat(visitor, arm.pat);
194205
if let Some(guard) = arm.guard {
195-
resolve_expr(visitor, guard, !has_let_expr(guard));
206+
resolve_cond(visitor, guard);
196207
}
197208
resolve_expr(visitor, arm.body, false);
198209

@@ -420,7 +431,7 @@ fn resolve_expr<'tcx>(
420431
};
421432
visitor.enter_scope(Scope { local_id: then.hir_id.local_id, data });
422433
visitor.cx.var_parent = visitor.cx.parent;
423-
visitor.visit_expr(cond);
434+
resolve_cond(visitor, cond);
424435
resolve_expr(visitor, then, true);
425436
visitor.cx = expr_cx;
426437
resolve_expr(visitor, otherwise, true);
@@ -435,7 +446,7 @@ fn resolve_expr<'tcx>(
435446
};
436447
visitor.enter_scope(Scope { local_id: then.hir_id.local_id, data });
437448
visitor.cx.var_parent = visitor.cx.parent;
438-
visitor.visit_expr(cond);
449+
resolve_cond(visitor, cond);
439450
resolve_expr(visitor, then, true);
440451
visitor.cx = expr_cx;
441452
}

compiler/rustc_hir_typeck/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,11 +428,9 @@ fn report_unexpected_variant_res(
428428
}
429429
hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Binary(..), hir_id, .. }) => {
430430
suggestion.push((expr.span.shrink_to_lo(), "(".to_string()));
431-
if let hir::Node::Expr(drop_temps) = tcx.parent_hir_node(*hir_id)
432-
&& let hir::ExprKind::DropTemps(_) = drop_temps.kind
433-
&& let hir::Node::Expr(parent) = tcx.parent_hir_node(drop_temps.hir_id)
431+
if let hir::Node::Expr(parent) = tcx.parent_hir_node(*hir_id)
434432
&& let hir::ExprKind::If(condition, block, None) = parent.kind
435-
&& condition.hir_id == drop_temps.hir_id
433+
&& condition.hir_id == *hir_id
436434
&& let hir::ExprKind::Block(block, _) = block.kind
437435
&& block.stmts.is_empty()
438436
&& let Some(expr) = block.expr

0 commit comments

Comments
 (0)