diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index bf7589e84adc4..fff9350899d0b 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -1,4 +1,6 @@ -use super::{ImplTraitContext, LoweringContext, ParamMode, ParenthesizedGenericArgs}; +use super::{ + ConditionScope, ImplTraitContext, LoweringContext, ParamMode, ParenthesizedGenericArgs, +}; use rustc_ast::attr; use rustc_ast::ptr::P as AstP; @@ -15,6 +17,8 @@ use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned}; use rustc_span::symbol::{sym, Ident, Symbol}; use rustc_span::{hygiene::ForLoopLoc, DUMMY_SP}; +use std::mem; + impl<'hir> LoweringContext<'_, 'hir> { fn lower_exprs(&mut self, exprs: &[AstP]) -> &'hir [hir::Expr<'hir>] { self.arena.alloc_from_iter(exprs.iter().map(|x| self.lower_expr_mut(x))) @@ -87,7 +91,18 @@ impl<'hir> LoweringContext<'_, 'hir> { hir::ExprKind::AddrOf(k, m, ohs) } ExprKind::Let(ref pat, ref scrutinee, span) => { - hir::ExprKind::Let(self.lower_pat(pat), self.lower_expr(scrutinee), span) + let source = match self.condition_scope { + Some(ConditionScope::Guard) => hir::LetSource::Guard, + Some(ConditionScope::If) => hir::LetSource::If, + Some(ConditionScope::While) => hir::LetSource::While, + None => hir::LetSource::Local, + }; + hir::ExprKind::Let( + self.lower_pat(pat), + self.lower_expr(scrutinee), + span, + source, + ) } ExprKind::If(ref cond, ref then, ref else_opt) => { self.lower_expr_if(cond, then, else_opt.as_deref()) @@ -354,14 +369,11 @@ impl<'hir> LoweringContext<'_, 'hir> { then: &Block, else_opt: Option<&Expr>, ) -> hir::ExprKind<'hir> { - let lowered_cond = self.lower_expr(cond); + let lowered_cond = self.with_condition_scope(ConditionScope::If, |t| t.lower_expr(cond)); let new_cond = self.manage_let_cond(lowered_cond); - let then_expr = self.lower_block_expr(then); - if let Some(rslt) = else_opt { - hir::ExprKind::If(new_cond, self.arena.alloc(then_expr), Some(self.lower_expr(rslt))) - } else { - hir::ExprKind::If(new_cond, self.arena.alloc(then_expr), None) - } + let then_expr = self.arena.alloc(self.lower_block_expr(then)); + let else_opt = else_opt.map(|e| self.lower_expr(e)); + hir::ExprKind::If(new_cond, then_expr, else_opt) } // If `cond` kind is `let`, returns `let`. Otherwise, wraps and returns `cond` @@ -400,7 +412,7 @@ impl<'hir> LoweringContext<'_, 'hir> { body: &Block, opt_label: Option