Skip to content

Commit

Permalink
typeck/expr.rs: extract out check_expr_while.
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril committed Jun 15, 2019
1 parent af800c7 commit 867ff1b
Showing 1 changed file with 31 additions and 22 deletions.
53 changes: 31 additions & 22 deletions src/librustc_typeck/check/expr.rs
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
}

0 comments on commit 867ff1b

Please sign in to comment.