]> git.lizzy.rs Git - rust.git/commitdiff
typeck/expr.rs: extract out check_expr_while.
authorMazdak Farrokhzad <twingoow@gmail.com>
Sat, 15 Jun 2019 00:19:23 +0000 (02:19 +0200)
committerMazdak Farrokhzad <twingoow@gmail.com>
Sat, 15 Jun 2019 00:19:23 +0000 (02:19 +0200)
src/librustc_typeck/check/expr.rs

index 7e8220573aec2f62a960c9c7959491b53f179863..6bf13f6a2f6a85471449926a710e45e04408ac92 100644 (file)
@@ -93,28 +93,7 @@ pub(super) fn check_expr_kind(
                 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 @@ fn check_expr_assign(
             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()
+    }
 }