Skip to content

Commit 867ff1b

Browse files
committed
typeck/expr.rs: extract out check_expr_while.
1 parent af800c7 commit 867ff1b

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

src/librustc_typeck/check/expr.rs

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -93,28 +93,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
9393
self.check_expr_assign(expr, expected, lhs, rhs)
9494
}
9595
ExprKind::While(ref cond, ref body, _) => {
96-
let ctxt = BreakableCtxt {
97-
// cannot use break with a value from a while loop
98-
coerce: None,
99-
may_break: false, // Will get updated if/when we find a `break`.
100-
};
101-
102-
let (ctxt, ()) = self.with_breakable_ctxt(expr.hir_id, ctxt, || {
103-
self.check_expr_has_type_or_error(&cond, tcx.types.bool);
104-
let cond_diverging = self.diverges.get();
105-
self.check_block_no_value(&body);
106-
107-
// We may never reach the body so it diverging means nothing.
108-
self.diverges.set(cond_diverging);
109-
});
110-
111-
if ctxt.may_break {
112-
// No way to know whether it's diverging because
113-
// of a `break` or an outer `break` or `return`.
114-
self.diverges.set(Diverges::Maybe);
115-
}
116-
117-
self.tcx.mk_unit()
96+
self.check_expr_while(cond, body, expr)
11897
}
11998
ExprKind::Loop(ref body, _, source) => {
12099
let coerce = match source {
@@ -787,4 +766,34 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
787766
self.tcx.mk_unit()
788767
}
789768
}
769+
770+
fn check_expr_while(
771+
&self,
772+
cond: &'tcx hir::Expr,
773+
body: &'tcx hir::Block,
774+
expr: &'tcx hir::Expr
775+
) -> Ty<'tcx> {
776+
let ctxt = BreakableCtxt {
777+
// Cannot use break with a value from a while loop.
778+
coerce: None,
779+
may_break: false, // Will get updated if/when we find a `break`.
780+
};
781+
782+
let (ctxt, ()) = self.with_breakable_ctxt(expr.hir_id, ctxt, || {
783+
self.check_expr_has_type_or_error(&cond, self.tcx.types.bool);
784+
let cond_diverging = self.diverges.get();
785+
self.check_block_no_value(&body);
786+
787+
// We may never reach the body so it diverging means nothing.
788+
self.diverges.set(cond_diverging);
789+
});
790+
791+
if ctxt.may_break {
792+
// No way to know whether it's diverging because
793+
// of a `break` or an outer `break` or `return`.
794+
self.diverges.set(Diverges::Maybe);
795+
}
796+
797+
self.tcx.mk_unit()
798+
}
790799
}

0 commit comments

Comments
 (0)