1
Fork 0

Improve liveness analysis for generators

Liveness analysis for generators assumes that execution always continues
normally after a yield point, not accounting for the fact that generator
could be dropped before completion.

If generators captures any variables by reference, those variables could
be used within a generator, or when the generator completes, but also
after each yield point in the case the generator is dropped.

Account for the case when generator is dropped after yielding, but
before running to the completion. This effectively considers all
variables captured by reference to be used after a yield point.
This commit is contained in:
Tomasz Miąsko 2021-04-19 00:00:00 +00:00
parent f66e825f73
commit 9f6f8620e1
3 changed files with 107 additions and 23 deletions

View file

@ -435,7 +435,10 @@ impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> {
}
// live nodes required for interesting control flow:
hir::ExprKind::If(..) | hir::ExprKind::Match(..) | hir::ExprKind::Loop(..) => {
hir::ExprKind::If(..)
| hir::ExprKind::Match(..)
| hir::ExprKind::Loop(..)
| hir::ExprKind::Yield(..) => {
self.add_live_node_for_node(expr.hir_id, ExprNode(expr.span, expr.hir_id));
intravisit::walk_expr(self, expr);
}
@ -469,7 +472,6 @@ impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> {
| hir::ExprKind::InlineAsm(..)
| hir::ExprKind::LlvmInlineAsm(..)
| hir::ExprKind::Box(..)
| hir::ExprKind::Yield(..)
| hir::ExprKind::Type(..)
| hir::ExprKind::Err
| hir::ExprKind::Path(hir::QPath::TypeRelative(..))
@ -866,6 +868,13 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
// at the label ident
hir::ExprKind::Loop(ref blk, ..) => self.propagate_through_loop(expr, &blk, succ),
hir::ExprKind::Yield(ref e, ..) => {
let yield_ln = self.live_node(expr.hir_id, expr.span);
self.init_from_succ(yield_ln, succ);
self.merge_from_succ(yield_ln, self.exit_ln);
self.propagate_through_expr(e, yield_ln)
}
hir::ExprKind::If(ref cond, ref then, ref else_opt) => {
//
// (cond)
@ -1025,7 +1034,6 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
| hir::ExprKind::Type(ref e, _)
| hir::ExprKind::DropTemps(ref e)
| hir::ExprKind::Unary(_, ref e)
| hir::ExprKind::Yield(ref e, _)
| hir::ExprKind::Repeat(ref e, _) => self.propagate_through_expr(&e, succ),
hir::ExprKind::InlineAsm(ref asm) => {