1
Fork 0

Auto merge of #88572 - matthewjasper:if-let-scoping-fix, r=oli-obk

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.

Closes #88307
cc `@flip1995` `@richkadel` `@c410-f3r`
This commit is contained in:
bors 2021-09-03 20:31:43 +00:00
commit b7404c898a
62 changed files with 617 additions and 583 deletions

View file

@ -94,6 +94,7 @@ impl fmt::Debug for Scope {
ScopeData::CallSite => write!(fmt, "CallSite({:?})", self.id),
ScopeData::Arguments => write!(fmt, "Arguments({:?})", self.id),
ScopeData::Destruction => write!(fmt, "Destruction({:?})", self.id),
ScopeData::IfThen => write!(fmt, "IfThen({:?})", self.id),
ScopeData::Remainder(fsi) => write!(
fmt,
"Remainder {{ block: {:?}, first_statement_index: {}}}",
@ -120,6 +121,10 @@ pub enum ScopeData {
/// Scope of destructors for temporaries of node-id.
Destruction,
/// Scope of the condition and then block of an if expression
/// Used for variables introduced in an if-let expression.
IfThen,
/// Scope following a `let id = expr;` binding in a block.
Remainder(FirstStatementIndex),
}

View file

@ -223,6 +223,7 @@ pub enum ExprKind<'tcx> {
},
/// An `if` expression.
If {
if_then_scope: region::Scope,
cond: ExprId,
then: ExprId,
else_opt: Option<ExprId>,