about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-06-15 02:19:23 +0200
committerMazdak Farrokhzad <twingoow@gmail.com>2019-06-15 02:19:23 +0200
commit867ff1b00a801807c12b7a904e650eaddeadf64c (patch)
tree9fdae3419d32c106659fc1514454f3097c7918b5
parentaf800c7873b7dfa28a8583e2e89d4c573d525bb5 (diff)
downloadrust-867ff1b00a801807c12b7a904e650eaddeadf64c.tar.gz
rust-867ff1b00a801807c12b7a904e650eaddeadf64c.zip
typeck/expr.rs: extract out check_expr_while.
-rw-r--r--src/librustc_typeck/check/expr.rs53
1 files 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()
+    }
 }