Fix drop handling for if let expressions

MIR lowering for `if let` expressions is now more complicated now that
`if let` exists in HIR. This PR adds a scope for the variables bound in
an `if let` expression and then uses an approach similar to how we
handle loops to ensure that we reliably drop the correct variables.
This commit is contained in:
Matthew Jasper 2021-09-01 22:52:17 +01:00
parent 50171c310c
commit ff8c0ef0e4
56 changed files with 543 additions and 483 deletions

View file

@ -391,21 +391,22 @@ fn resolve_expr<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, expr: &'tcx h
}
hir::ExprKind::If(ref cond, ref then, Some(ref otherwise)) => {
// FIXME(matthewjasper): ideally the scope we use here would only
// contain the condition and then expression. This works, but
// can result in some extra drop flags.
let expr_cx = visitor.cx;
visitor.enter_scope(Scope { id: then.hir_id.local_id, data: ScopeData::IfThen });
visitor.cx.var_parent = visitor.cx.parent;
visitor.visit_expr(cond);
visitor.cx.var_parent = prev_cx.var_parent;
visitor.visit_expr(then);
visitor.cx = expr_cx;
visitor.visit_expr(otherwise);
}
hir::ExprKind::If(ref cond, ref then, None) => {
let expr_cx = visitor.cx;
visitor.enter_scope(Scope { id: then.hir_id.local_id, data: ScopeData::IfThen });
visitor.cx.var_parent = visitor.cx.parent;
visitor.visit_expr(cond);
visitor.cx.var_parent = prev_cx.var_parent;
visitor.visit_expr(then);
visitor.cx = expr_cx;
}
_ => intravisit::walk_expr(visitor, expr),