Skip to content

Commit 13c796b

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

File tree

5 files changed

+17
-37
lines changed

5 files changed

+17
-37
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -547,15 +547,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
547547

548548
// Lowers a condition (i.e. `cond` in `if cond` or `while cond`), wrapping it in a terminating scope
549549
// 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-
550+
pub(super) fn lower_cond(&mut self, cond: &Expr) -> &'hir hir::Expr<'hir> {
559551
// We have to take special care for `let` exprs in the condition, e.g. in
560552
// `if let pat = val` or `if foo && let pat = val`, as we _do_ want `val` to live beyond the
561553
// condition in this case.
@@ -564,14 +556,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
564556
// we still wrap them in terminating scopes, e.g. `if foo && let pat = val` essentially
565557
// gets transformed into `if { let _t = foo; _t } && let pat = val`
566558
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)))
559+
ExprKind::Binary(BinOp { node: BinOpKind::And | BinOpKind::Or, .. }, _, _) => {
560+
// Terminating scopes for logical `&&` and `||` are handled in
561+
// `rustc_hir_analysis::check::region::resolve_expr`; see the comment there.
562+
self.lower_expr(cond)
575563
}
576564
ExprKind::Let(..) => self.lower_expr(cond),
577565
_ => {
@@ -675,7 +663,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
675663

676664
fn lower_arm(&mut self, arm: &Arm) -> hir::Arm<'hir> {
677665
let pat = self.lower_pat(&arm.pat);
678-
let guard = arm.guard.as_ref().map(|cond| self.lower_expr(cond));
666+
let guard = arm.guard.as_ref().map(|cond| self.lower_cond(cond));
679667
let hir_id = self.next_id();
680668
let span = self.lower_span(arm.span);
681669
self.lower_attrs(hir_id, &arm.attrs, arm.span);

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
134134
);
135135
}
136136
PatKind::Guard(inner, cond) => {
137-
break hir::PatKind::Guard(self.lower_pat(inner), self.lower_expr(cond));
137+
break hir::PatKind::Guard(self.lower_pat(inner), self.lower_cond(cond));
138138
}
139139
PatKind::Slice(pats) => break self.lower_pat_slice(pats),
140140
PatKind::Rest => {

compiler/rustc_hir_analysis/src/check/region.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -177,22 +177,16 @@ fn resolve_block<'tcx>(
177177
}
178178

179179
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-
}
187-
188180
let prev_cx = visitor.cx;
189181

190182
visitor.enter_node_scope_with_dtor(arm.hir_id.local_id, true);
191183
visitor.cx.var_parent = visitor.cx.parent;
192184

193185
resolve_pat(visitor, arm.pat);
194186
if let Some(guard) = arm.guard {
195-
resolve_expr(visitor, guard, !has_let_expr(guard));
187+
// If the guard should be a terminating scope (i.e. it has no `let` expressions), it will
188+
// have been wrapped in a `DropTemps` expression during AST -> HIR lowering.
189+
resolve_expr(visitor, guard, false);
196190
}
197191
resolve_expr(visitor, arm.body, false);
198192

tests/coverage/branch/guard.cov-map

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Function name: guard::branch_match_guard
2-
Raw bytes (104): 0x[01, 01, 08, 05, 0d, 09, 05, 05, 0f, 0d, 11, 17, 1b, 01, 05, 1f, 11, 09, 0d, 10, 01, 0c, 01, 00, 26, 01, 01, 05, 00, 0e, 02, 02, 0b, 00, 0c, 06, 01, 14, 02, 0a, 0d, 03, 0e, 00, 0f, 05, 00, 14, 00, 19, 20, 0d, 02, 00, 14, 00, 1e, 0d, 00, 1d, 00, 1e, 0d, 00, 22, 02, 0a, 11, 03, 0e, 00, 0f, 02, 00, 14, 00, 19, 20, 11, 0a, 00, 14, 00, 1e, 11, 00, 1d, 00, 1e, 11, 00, 22, 02, 0a, 12, 03, 0e, 02, 0a, 01, 04, 01, 00, 02]
2+
Raw bytes (94): 0x[01, 01, 08, 05, 0d, 09, 05, 05, 0f, 0d, 11, 17, 1b, 01, 05, 1f, 11, 09, 0d, 0e, 01, 0c, 01, 00, 26, 01, 01, 05, 00, 0e, 02, 02, 0b, 00, 0c, 06, 01, 14, 02, 0a, 0d, 03, 0e, 00, 0f, 05, 00, 14, 00, 19, 20, 0d, 02, 00, 14, 00, 1e, 0d, 00, 22, 02, 0a, 11, 03, 0e, 00, 0f, 02, 00, 14, 00, 19, 20, 11, 0a, 00, 14, 00, 1e, 11, 00, 22, 02, 0a, 12, 03, 0e, 02, 0a, 01, 04, 01, 00, 02]
33
Number of files: 1
44
- file 0 => $DIR/guard.rs
55
Number of expressions: 8
@@ -11,7 +11,7 @@ Number of expressions: 8
1111
- expression 5 operands: lhs = Counter(0), rhs = Counter(1)
1212
- expression 6 operands: lhs = Expression(7, Add), rhs = Counter(4)
1313
- expression 7 operands: lhs = Counter(2), rhs = Counter(3)
14-
Number of file 0 mappings: 16
14+
Number of file 0 mappings: 14
1515
- Code(Counter(0)) at (prev + 12, 1) to (start + 0, 38)
1616
- Code(Counter(0)) at (prev + 1, 5) to (start + 0, 14)
1717
- Code(Expression(0, Sub)) at (prev + 2, 11) to (start + 0, 12)
@@ -23,15 +23,13 @@ Number of file 0 mappings: 16
2323
- Branch { true: Counter(3), false: Expression(0, Sub) } at (prev + 0, 20) to (start + 0, 30)
2424
true = c3
2525
false = (c1 - c3)
26-
- Code(Counter(3)) at (prev + 0, 29) to (start + 0, 30)
2726
- Code(Counter(3)) at (prev + 0, 34) to (start + 2, 10)
2827
- Code(Counter(4)) at (prev + 3, 14) to (start + 0, 15)
2928
- Code(Expression(0, Sub)) at (prev + 0, 20) to (start + 0, 25)
3029
= (c1 - c3)
3130
- Branch { true: Counter(4), false: Expression(2, Sub) } at (prev + 0, 20) to (start + 0, 30)
3231
true = c4
3332
false = (c1 - (c3 + c4))
34-
- Code(Counter(4)) at (prev + 0, 29) to (start + 0, 30)
3533
- Code(Counter(4)) at (prev + 0, 34) to (start + 2, 10)
3634
- Code(Expression(4, Sub)) at (prev + 3, 14) to (start + 2, 10)
3735
= ((c0 + c1) - ((c2 + c3) + c4))

tests/coverage/branch/match-arms.cov-map

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Function name: match_arms::guards
2-
Raw bytes (158): 0x[01, 01, 08, 15, 05, 19, 09, 1d, 0d, 21, 11, 01, 17, 1b, 11, 1f, 0d, 05, 09, 1a, 01, 30, 01, 00, 23, 01, 01, 05, 00, 0e, 21, 02, 0b, 00, 10, 05, 01, 11, 00, 12, 20, 05, 02, 00, 17, 00, 1b, 05, 00, 1a, 00, 1b, 05, 00, 1f, 00, 26, 05, 00, 27, 00, 28, 09, 01, 11, 00, 12, 20, 09, 06, 00, 17, 00, 1b, 09, 00, 1a, 00, 1b, 09, 00, 1f, 00, 26, 09, 00, 27, 00, 28, 0d, 01, 11, 00, 12, 20, 0d, 0a, 00, 17, 00, 1b, 0d, 00, 1a, 00, 1b, 0d, 00, 1f, 00, 26, 0d, 00, 27, 00, 28, 11, 01, 11, 00, 12, 20, 11, 0e, 00, 17, 00, 1b, 11, 00, 1a, 00, 1b, 11, 00, 1f, 00, 26, 11, 00, 27, 00, 28, 12, 01, 0e, 00, 15, 01, 03, 05, 00, 0c, 01, 01, 01, 00, 02]
2+
Raw bytes (158): 0x[01, 01, 08, 15, 05, 19, 09, 1d, 0d, 21, 11, 01, 17, 1b, 11, 1f, 0d, 05, 09, 1a, 01, 30, 01, 00, 23, 01, 01, 05, 00, 0e, 21, 02, 0b, 00, 10, 05, 01, 11, 00, 12, 05, 00, 17, 00, 1b, 20, 05, 02, 00, 17, 00, 1b, 05, 00, 1f, 00, 26, 05, 00, 27, 00, 28, 09, 01, 11, 00, 12, 09, 00, 17, 00, 1b, 20, 09, 06, 00, 17, 00, 1b, 09, 00, 1f, 00, 26, 09, 00, 27, 00, 28, 0d, 01, 11, 00, 12, 0d, 00, 17, 00, 1b, 20, 0d, 0a, 00, 17, 00, 1b, 0d, 00, 1f, 00, 26, 0d, 00, 27, 00, 28, 11, 01, 11, 00, 12, 11, 00, 17, 00, 1b, 20, 11, 0e, 00, 17, 00, 1b, 11, 00, 1f, 00, 26, 11, 00, 27, 00, 28, 12, 01, 0e, 00, 15, 01, 03, 05, 00, 0c, 01, 01, 01, 00, 02]
33
Number of files: 1
44
- file 0 => $DIR/match-arms.rs
55
Number of expressions: 8
@@ -16,31 +16,31 @@ Number of file 0 mappings: 26
1616
- Code(Counter(0)) at (prev + 1, 5) to (start + 0, 14)
1717
- Code(Counter(8)) at (prev + 2, 11) to (start + 0, 16)
1818
- Code(Counter(1)) at (prev + 1, 17) to (start + 0, 18)
19+
- Code(Counter(1)) at (prev + 0, 23) to (start + 0, 27)
1920
- Branch { true: Counter(1), false: Expression(0, Sub) } at (prev + 0, 23) to (start + 0, 27)
2021
true = c1
2122
false = (c5 - c1)
22-
- Code(Counter(1)) at (prev + 0, 26) to (start + 0, 27)
2323
- Code(Counter(1)) at (prev + 0, 31) to (start + 0, 38)
2424
- Code(Counter(1)) at (prev + 0, 39) to (start + 0, 40)
2525
- Code(Counter(2)) at (prev + 1, 17) to (start + 0, 18)
26+
- Code(Counter(2)) at (prev + 0, 23) to (start + 0, 27)
2627
- Branch { true: Counter(2), false: Expression(1, Sub) } at (prev + 0, 23) to (start + 0, 27)
2728
true = c2
2829
false = (c6 - c2)
29-
- Code(Counter(2)) at (prev + 0, 26) to (start + 0, 27)
3030
- Code(Counter(2)) at (prev + 0, 31) to (start + 0, 38)
3131
- Code(Counter(2)) at (prev + 0, 39) to (start + 0, 40)
3232
- Code(Counter(3)) at (prev + 1, 17) to (start + 0, 18)
33+
- Code(Counter(3)) at (prev + 0, 23) to (start + 0, 27)
3334
- Branch { true: Counter(3), false: Expression(2, Sub) } at (prev + 0, 23) to (start + 0, 27)
3435
true = c3
3536
false = (c7 - c3)
36-
- Code(Counter(3)) at (prev + 0, 26) to (start + 0, 27)
3737
- Code(Counter(3)) at (prev + 0, 31) to (start + 0, 38)
3838
- Code(Counter(3)) at (prev + 0, 39) to (start + 0, 40)
3939
- Code(Counter(4)) at (prev + 1, 17) to (start + 0, 18)
40+
- Code(Counter(4)) at (prev + 0, 23) to (start + 0, 27)
4041
- Branch { true: Counter(4), false: Expression(3, Sub) } at (prev + 0, 23) to (start + 0, 27)
4142
true = c4
4243
false = (c8 - c4)
43-
- Code(Counter(4)) at (prev + 0, 26) to (start + 0, 27)
4444
- Code(Counter(4)) at (prev + 0, 31) to (start + 0, 38)
4545
- Code(Counter(4)) at (prev + 0, 39) to (start + 0, 40)
4646
- Code(Expression(4, Sub)) at (prev + 1, 14) to (start + 0, 21)

0 commit comments

Comments
 (0)