Skip to content

Commit 6a31628

Browse files
committed
refactor if so that the "then type" is an expression
1 parent 3087a1f commit 6a31628

File tree

9 files changed

+26
-37
lines changed

9 files changed

+26
-37
lines changed

src/librustc/cfg/construct.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
169169
// [..expr..]
170170
//
171171
let cond_exit = self.expr(&cond, pred); // 1
172-
let then_exit = self.block(&then, cond_exit); // 2
172+
let then_exit = self.expr(&then, cond_exit); // 2
173173
self.add_ast_node(expr.id, &[cond_exit, then_exit]) // 3,4
174174
}
175175

@@ -189,7 +189,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
189189
// [..expr..]
190190
//
191191
let cond_exit = self.expr(&cond, pred); // 1
192-
let then_exit = self.block(&then, cond_exit); // 2
192+
let then_exit = self.expr(&then, cond_exit); // 2
193193
let else_exit = self.expr(&otherwise, cond_exit); // 3
194194
self.add_ast_node(expr.id, &[then_exit, else_exit]) // 4, 5
195195
}

src/librustc/hir/intravisit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
960960
}
961961
ExprIf(ref head_expression, ref if_block, ref optional_else) => {
962962
visitor.visit_expr(head_expression);
963-
visitor.visit_block(if_block);
963+
visitor.visit_expr(if_block);
964964
walk_list!(visitor, visit_expr, optional_else);
965965
}
966966
ExprWhile(ref subexpression, ref block, ref opt_sp_name) => {

src/librustc/hir/lowering.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1645,7 +1645,9 @@ impl<'a> LoweringContext<'a> {
16451645
}
16461646
});
16471647

1648-
hir::ExprIf(P(self.lower_expr(cond)), self.lower_block(blk), else_opt)
1648+
let then_blk = self.lower_block(blk);
1649+
let then_expr = self.expr_block(then_blk, ThinVec::new());
1650+
hir::ExprIf(P(self.lower_expr(cond)), P(then_expr), else_opt)
16491651
}
16501652
ExprKind::While(ref cond, ref body, opt_ident) => {
16511653
self.with_loop_scope(e.id, |this|
@@ -1852,7 +1854,7 @@ impl<'a> LoweringContext<'a> {
18521854
attrs: hir_vec![],
18531855
pats: hir_vec![pat_under],
18541856
guard: Some(cond),
1855-
body: P(self.expr_block(then, ThinVec::new())),
1857+
body: then,
18561858
});
18571859
else_opt.map(|else_opt| (else_opt, true))
18581860
}

src/librustc/hir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ pub enum Expr_ {
927927
/// An `if` block, with an optional else block
928928
///
929929
/// `if expr { block } else { expr }`
930-
ExprIf(P<Expr>, P<Block>, Option<P<Expr>>),
930+
ExprIf(P<Expr>, P<Expr>, Option<P<Expr>>),
931931
/// A while loop, with an optional label
932932
///
933933
/// `'label: while expr { block }`

src/librustc/hir/print.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,7 @@ impl<'a> State<'a> {
10361036
word(&mut self.s, " else if ")?;
10371037
self.print_expr(&i)?;
10381038
space(&mut self.s)?;
1039-
self.print_block(&then)?;
1039+
self.print_expr(&then)?;
10401040
self.print_else(e.as_ref().map(|e| &**e))
10411041
}
10421042
// "final else"
@@ -1058,13 +1058,13 @@ impl<'a> State<'a> {
10581058

10591059
pub fn print_if(&mut self,
10601060
test: &hir::Expr,
1061-
blk: &hir::Block,
1061+
blk: &hir::Expr,
10621062
elseopt: Option<&hir::Expr>)
10631063
-> io::Result<()> {
10641064
self.head("if")?;
10651065
self.print_expr(test)?;
10661066
space(&mut self.s)?;
1067-
self.print_block(blk)?;
1067+
self.print_expr(blk)?;
10681068
self.print_else(elseopt)
10691069
}
10701070

src/librustc/middle/expr_use_visitor.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,9 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
414414
self.consume_exprs(exprs);
415415
}
416416

417-
hir::ExprIf(ref cond_expr, ref then_blk, ref opt_else_expr) => {
417+
hir::ExprIf(ref cond_expr, ref then_expr, ref opt_else_expr) => {
418418
self.consume_expr(&cond_expr);
419-
self.walk_block(&then_blk);
419+
self.walk_expr(&then_expr);
420420
if let Some(ref else_expr) = *opt_else_expr {
421421
self.consume_expr(&else_expr);
422422
}

src/librustc/middle/liveness.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
943943
// ( succ )
944944
//
945945
let else_ln = self.propagate_through_opt_expr(els.as_ref().map(|e| &**e), succ);
946-
let then_ln = self.propagate_through_block(&then, succ);
946+
let then_ln = self.propagate_through_expr(&then, succ);
947947
let ln = self.live_node(expr.id, expr.span);
948948
self.init_from_succ(ln, else_ln);
949949
self.merge_from_succ(ln, then_ln, false);

src/librustc_mir/hair/cx/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
633633
hir::ExprIf(ref cond, ref then, ref otherwise) => {
634634
ExprKind::If {
635635
condition: cond.to_ref(),
636-
then: block::to_expr_ref(cx, then),
636+
then: then.to_ref(),
637637
otherwise: otherwise.to_ref(),
638638
}
639639
}

src/librustc_typeck/check/mod.rs

+11-24
Original file line numberDiff line numberDiff line change
@@ -2733,7 +2733,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
27332733
// or if-else.
27342734
fn check_then_else(&self,
27352735
cond_expr: &'gcx hir::Expr,
2736-
then_blk: &'gcx hir::Block,
2736+
then_expr: &'gcx hir::Expr,
27372737
opt_else_expr: Option<&'gcx hir::Expr>,
27382738
sp: Span,
27392739
expected: Expectation<'tcx>) -> Ty<'tcx> {
@@ -2742,7 +2742,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
27422742
self.diverges.set(Diverges::Maybe);
27432743

27442744
let expected = expected.adjust_for_branches(self);
2745-
let then_ty = self.check_block_with_expected(then_blk, expected);
2745+
let then_ty = self.check_expr_with_expectation(then_expr, expected);
27462746
let then_diverges = self.diverges.get();
27472747
self.diverges.set(Diverges::Maybe);
27482748

@@ -2757,26 +2757,13 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
27572757
// to assign coercions to, otherwise it's () or diverging.
27582758
expected_ty = then_ty;
27592759
found_ty = else_ty;
2760-
result = if let Some(ref then) = then_blk.expr {
2761-
let res = self.try_find_coercion_lub(&cause, || Some(&**then),
2762-
then_ty, else_expr, else_ty);
2763-
2764-
// In case we did perform an adjustment, we have to update
2765-
// the type of the block, because old trans still uses it.
2766-
if res.is_ok() {
2767-
let adj = self.tables.borrow().adjustments.get(&then.id).cloned();
2768-
if let Some(adj) = adj {
2769-
self.write_ty(then_blk.id, adj.target);
2770-
}
2771-
}
27722760

2773-
res
2774-
} else {
2775-
self.commit_if_ok(|_| {
2776-
let trace = TypeTrace::types(&cause, true, then_ty, else_ty);
2777-
self.lub(true, trace, &then_ty, &else_ty)
2778-
.map(|ok| self.register_infer_ok_obligations(ok))
2779-
})
2761+
let coerce_to = expected.only_has_type(self).unwrap_or(then_ty);
2762+
result = {
2763+
self.try_coerce(then_expr, then_ty, coerce_to)
2764+
.and_then(|t| {
2765+
self.try_find_coercion_lub(&cause, || Some(then_expr), t, else_expr, else_ty)
2766+
})
27802767
};
27812768

27822769
// We won't diverge unless both branches do (or the condition does).
@@ -3566,9 +3553,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
35663553
tcx.mk_nil()
35673554
}
35683555
}
3569-
hir::ExprIf(ref cond, ref then_blk, ref opt_else_expr) => {
3570-
self.check_then_else(&cond, &then_blk, opt_else_expr.as_ref().map(|e| &**e),
3571-
expr.span, expected)
3556+
hir::ExprIf(ref cond, ref then_expr, ref opt_else_expr) => {
3557+
self.check_then_else(&cond, then_expr, opt_else_expr.as_ref().map(|e| &**e),
3558+
expr.span, expected)
35723559
}
35733560
hir::ExprWhile(ref cond, ref body, _) => {
35743561
let unified = self.tcx.mk_nil();

0 commit comments

Comments
 (0)