Skip to content

Commit ed1e943

Browse files
committed
lowering: extract lower_expr_if
1 parent 11251b9 commit ed1e943

File tree

1 file changed

+53
-40
lines changed

1 file changed

+53
-40
lines changed

src/librustc/hir/lowering/expr.rs

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -101,47 +101,8 @@ impl LoweringContext<'_> {
101101
hir::MatchSource::Normal,
102102
)
103103
}
104-
// FIXME(#53667): handle lowering of && and parens.
105104
ExprKind::If(ref cond, ref then, ref else_opt) => {
106-
// `_ => else_block` where `else_block` is `{}` if there's `None`:
107-
let else_pat = self.pat_wild(e.span);
108-
let (else_expr, contains_else_clause) = match else_opt {
109-
None => (self.expr_block_empty(e.span), false),
110-
Some(els) => (self.lower_expr(els), true),
111-
};
112-
let else_arm = self.arm(hir_vec![else_pat], P(else_expr));
113-
114-
// Handle then + scrutinee:
115-
let then_blk = self.lower_block(then, false);
116-
let then_expr = self.expr_block(then_blk, ThinVec::new());
117-
let (then_pats, scrutinee, desugar) = match cond.node {
118-
// `<pat> => <then>`:
119-
ExprKind::Let(ref pats, ref scrutinee) => {
120-
let scrutinee = self.lower_expr(scrutinee);
121-
let pats = pats.iter().map(|pat| self.lower_pat(pat)).collect();
122-
let desugar = hir::MatchSource::IfLetDesugar { contains_else_clause };
123-
(pats, scrutinee, desugar)
124-
}
125-
// `true => <then>`:
126-
_ => {
127-
// Lower condition:
128-
let cond = self.lower_expr(cond);
129-
let span_block = self.mark_span_with_reason(
130-
DesugaringKind::CondTemporary, cond.span, None
131-
);
132-
// Wrap in a construct equivalent to `{ let _t = $cond; _t }`
133-
// to preserve drop semantics since `if cond { ... }` does not
134-
// let temporaries live outside of `cond`.
135-
let cond = self.expr_drop_temps(span_block, P(cond), ThinVec::new());
136-
137-
let desugar = hir::MatchSource::IfDesugar { contains_else_clause };
138-
let pats = hir_vec![self.pat_bool(e.span, true)];
139-
(pats, cond, desugar)
140-
}
141-
};
142-
let then_arm = self.arm(then_pats, P(then_expr));
143-
144-
hir::ExprKind::Match(P(scrutinee), vec![then_arm, else_arm].into(), desugar)
105+
self.lower_expr_if(e.span, cond, then, else_opt.as_deref())
145106
}
146107
ExprKind::While(ref cond, ref body, opt_label) => self.with_loop_scope(e.id, |this| {
147108
this.lower_expr_while_in_loop_scope(e.span, cond, body, opt_label)
@@ -279,6 +240,58 @@ impl LoweringContext<'_> {
279240
}
280241
}
281242

243+
fn lower_expr_if(
244+
&mut self,
245+
span: Span,
246+
cond: &Expr,
247+
then: &Block,
248+
else_opt: Option<&Expr>,
249+
) -> hir::ExprKind {
250+
// FIXME(#53667): handle lowering of && and parens.
251+
252+
// `_ => else_block` where `else_block` is `{}` if there's `None`:
253+
let else_pat = self.pat_wild(span);
254+
let (else_expr, contains_else_clause) = match else_opt {
255+
None => (self.expr_block_empty(span), false),
256+
Some(els) => (self.lower_expr(els), true),
257+
};
258+
let else_arm = self.arm(hir_vec![else_pat], P(else_expr));
259+
260+
// Handle then + scrutinee:
261+
let then_blk = self.lower_block(then, false);
262+
let then_expr = self.expr_block(then_blk, ThinVec::new());
263+
let (then_pats, scrutinee, desugar) = match cond.node {
264+
// `<pat> => <then>`:
265+
ExprKind::Let(ref pats, ref scrutinee) => {
266+
let scrutinee = self.lower_expr(scrutinee);
267+
let pats = pats.iter().map(|pat| self.lower_pat(pat)).collect();
268+
let desugar = hir::MatchSource::IfLetDesugar { contains_else_clause };
269+
(pats, scrutinee, desugar)
270+
}
271+
// `true => <then>`:
272+
_ => {
273+
// Lower condition:
274+
let cond = self.lower_expr(cond);
275+
let span_block = self.mark_span_with_reason(
276+
DesugaringKind::CondTemporary,
277+
cond.span,
278+
None
279+
);
280+
// Wrap in a construct equivalent to `{ let _t = $cond; _t }`
281+
// to preserve drop semantics since `if cond { ... }` does not
282+
// let temporaries live outside of `cond`.
283+
let cond = self.expr_drop_temps(span_block, P(cond), ThinVec::new());
284+
285+
let desugar = hir::MatchSource::IfDesugar { contains_else_clause };
286+
let pats = hir_vec![self.pat_bool(span, true)];
287+
(pats, cond, desugar)
288+
}
289+
};
290+
let then_arm = self.arm(then_pats, P(then_expr));
291+
292+
hir::ExprKind::Match(P(scrutinee), vec![then_arm, else_arm].into(), desugar)
293+
}
294+
282295
fn lower_expr_while_in_loop_scope(
283296
&mut self,
284297
span: Span,

0 commit comments

Comments
 (0)