Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir_def/src/body/lower.rs')
| -rw-r--r-- | crates/hir_def/src/body/lower.rs | 73 |
1 files changed, 11 insertions, 62 deletions
diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs index 7cbeef1488..06ad7ce4cd 100644 --- a/crates/hir_def/src/body/lower.rs +++ b/crates/hir_def/src/body/lower.rs @@ -28,7 +28,7 @@ use crate::{ db::DefDatabase, expr::{ dummy_expr_id, Array, BindingAnnotation, Expr, ExprId, Label, LabelId, Literal, MatchArm, - MatchGuard, Pat, PatId, RecordFieldPat, RecordLitField, Statement, + Pat, PatId, RecordFieldPat, RecordLitField, Statement, }, intern::Interned, item_scope::BuiltinShadowMode, @@ -155,9 +155,6 @@ impl ExprCollector<'_> { fn alloc_expr_desugared(&mut self, expr: Expr) -> ExprId { self.make_expr(expr, Err(SyntheticSyntax)) } - fn unit(&mut self) -> ExprId { - self.alloc_expr_desugared(Expr::Tuple { exprs: Box::default() }) - } fn missing_expr(&mut self) -> ExprId { self.alloc_expr_desugared(Expr::Missing) } @@ -215,33 +212,15 @@ impl ExprCollector<'_> { } }); - let condition = match e.condition() { - None => self.missing_expr(), - Some(condition) => match condition.pat() { - None => self.collect_expr_opt(condition.expr()), - // if let -- desugar to match - Some(pat) => { - let pat = self.collect_pat(pat); - let match_expr = self.collect_expr_opt(condition.expr()); - let placeholder_pat = self.missing_pat(); - let arms = vec![ - MatchArm { pat, expr: then_branch, guard: None }, - MatchArm { - pat: placeholder_pat, - expr: else_branch.unwrap_or_else(|| self.unit()), - guard: None, - }, - ] - .into(); - return Some( - self.alloc_expr(Expr::Match { expr: match_expr, arms }, syntax_ptr), - ); - } - }, - }; + let condition = self.collect_expr_opt(e.condition()); self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr) } + ast::Expr::LetExpr(e) => { + let pat = self.collect_pat_opt(e.pat()); + let expr = self.collect_expr_opt(e.expr()); + self.alloc_expr(Expr::Let { pat, expr }, syntax_ptr) + } ast::Expr::BlockExpr(e) => match e.modifier() { Some(ast::BlockModifier::Try(_)) => { let body = self.collect_block(e); @@ -282,31 +261,7 @@ impl ExprCollector<'_> { let label = e.label().map(|label| self.collect_label(label)); let body = self.collect_block_opt(e.loop_body()); - let condition = match e.condition() { - None => self.missing_expr(), - Some(condition) => match condition.pat() { - None => self.collect_expr_opt(condition.expr()), - // if let -- desugar to match - Some(pat) => { - cov_mark::hit!(infer_resolve_while_let); - let pat = self.collect_pat(pat); - let match_expr = self.collect_expr_opt(condition.expr()); - let placeholder_pat = self.missing_pat(); - let break_ = - self.alloc_expr_desugared(Expr::Break { expr: None, label: None }); - let arms = vec![ - MatchArm { pat, expr: body, guard: None }, - MatchArm { pat: placeholder_pat, expr: break_, guard: None }, - ] - .into(); - let match_expr = - self.alloc_expr_desugared(Expr::Match { expr: match_expr, arms }); - return Some( - self.alloc_expr(Expr::Loop { body: match_expr, label }, syntax_ptr), - ); - } - }, - }; + let condition = self.collect_expr_opt(e.condition()); self.alloc_expr(Expr::While { condition, body, label }, syntax_ptr) } @@ -352,15 +307,9 @@ impl ExprCollector<'_> { self.check_cfg(&arm).map(|()| MatchArm { pat: self.collect_pat_opt(arm.pat()), expr: self.collect_expr_opt(arm.expr()), - guard: arm.guard().map(|guard| match guard.pat() { - Some(pat) => MatchGuard::IfLet { - pat: self.collect_pat(pat), - expr: self.collect_expr_opt(guard.expr()), - }, - None => { - MatchGuard::If { expr: self.collect_expr_opt(guard.expr()) } - } - }), + guard: arm + .guard() + .map(|guard| self.collect_expr_opt(guard.condition())), }) }) .collect() |