From 867ff1b00a801807c12b7a904e650eaddeadf64c Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sat, 15 Jun 2019 02:19:23 +0200 Subject: [PATCH] typeck/expr.rs: extract out check_expr_while. --- src/librustc_typeck/check/expr.rs | 53 ++++++++++++++++++------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/src/librustc_typeck/check/expr.rs b/src/librustc_typeck/check/expr.rs index 7e8220573ae..6bf13f6a2f6 100644 --- a/src/librustc_typeck/check/expr.rs +++ b/src/librustc_typeck/check/expr.rs @@ -93,28 +93,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.check_expr_assign(expr, expected, lhs, rhs) } ExprKind::While(ref cond, ref body, _) => { - let ctxt = BreakableCtxt { - // cannot use break with a value from a while loop - coerce: None, - may_break: false, // Will get updated if/when we find a `break`. - }; - - let (ctxt, ()) = self.with_breakable_ctxt(expr.hir_id, ctxt, || { - self.check_expr_has_type_or_error(&cond, tcx.types.bool); - let cond_diverging = self.diverges.get(); - self.check_block_no_value(&body); - - // We may never reach the body so it diverging means nothing. - self.diverges.set(cond_diverging); - }); - - if ctxt.may_break { - // No way to know whether it's diverging because - // of a `break` or an outer `break` or `return`. - self.diverges.set(Diverges::Maybe); - } - - self.tcx.mk_unit() + self.check_expr_while(cond, body, expr) } ExprKind::Loop(ref body, _, source) => { let coerce = match source { @@ -787,4 +766,34 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.tcx.mk_unit() } } + + fn check_expr_while( + &self, + cond: &'tcx hir::Expr, + body: &'tcx hir::Block, + expr: &'tcx hir::Expr + ) -> Ty<'tcx> { + let ctxt = BreakableCtxt { + // Cannot use break with a value from a while loop. + coerce: None, + may_break: false, // Will get updated if/when we find a `break`. + }; + + let (ctxt, ()) = self.with_breakable_ctxt(expr.hir_id, ctxt, || { + self.check_expr_has_type_or_error(&cond, self.tcx.types.bool); + let cond_diverging = self.diverges.get(); + self.check_block_no_value(&body); + + // We may never reach the body so it diverging means nothing. + self.diverges.set(cond_diverging); + }); + + if ctxt.may_break { + // No way to know whether it's diverging because + // of a `break` or an outer `break` or `return`. + self.diverges.set(Diverges::Maybe); + } + + self.tcx.mk_unit() + } }