diff options
author | Aleksey Kladov <[email protected]> | 2019-08-07 11:32:32 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-08-07 11:32:32 +0100 |
commit | 4d6475ada0c4176734f7e7f24cb1be8c5c8d1988 (patch) | |
tree | ffe6bcecc18f5ace50d31ecb5eba86699ba46363 /crates | |
parent | a2966944a80c7e017865e9705628e41b5bb4cd14 (diff) |
refactor if lowering
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_hir/src/expr.rs | 31 |
1 files changed, 14 insertions, 17 deletions
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 4dcea19a9..9fea70dda 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs | |||
@@ -558,35 +558,32 @@ where | |||
558 | let syntax_ptr = SyntaxNodePtr::new(expr.syntax()); | 558 | let syntax_ptr = SyntaxNodePtr::new(expr.syntax()); |
559 | match expr.kind() { | 559 | match expr.kind() { |
560 | ast::ExprKind::IfExpr(e) => { | 560 | ast::ExprKind::IfExpr(e) => { |
561 | let then_branch = self.collect_block_opt(e.then_branch()); | ||
562 | let else_branch = e.else_branch().map(|b| match b { | ||
563 | ast::ElseBranch::Block(it) => self.collect_block(it), | ||
564 | ast::ElseBranch::IfExpr(elif) => { | ||
565 | let expr: ast::Expr = ast::Expr::cast(elif.syntax().clone()).unwrap(); | ||
566 | self.collect_expr(expr) | ||
567 | } | ||
568 | }); | ||
569 | |||
561 | if let Some(pat) = e.condition().and_then(|c| c.pat()) { | 570 | if let Some(pat) = e.condition().and_then(|c| c.pat()) { |
562 | // if let -- desugar to match | 571 | // if let -- desugar to match |
563 | let pat = self.collect_pat(pat); | 572 | let pat = self.collect_pat(pat); |
564 | let match_expr = | 573 | let match_expr = |
565 | self.collect_expr_opt(e.condition().expect("checked above").expr()); | 574 | self.collect_expr_opt(e.condition().expect("checked above").expr()); |
566 | let then_branch = self.collect_block_opt(e.then_branch()); | ||
567 | let else_branch = e | ||
568 | .else_branch() | ||
569 | .map(|b| match b { | ||
570 | ast::ElseBranch::Block(it) => self.collect_block(it), | ||
571 | ast::ElseBranch::IfExpr(elif) => self.collect_expr(elif.into()), | ||
572 | }) | ||
573 | .unwrap_or_else(|| self.empty_block()); | ||
574 | let placeholder_pat = self.pats.alloc(Pat::Missing); | 575 | let placeholder_pat = self.pats.alloc(Pat::Missing); |
575 | let arms = vec![ | 576 | let arms = vec![ |
576 | MatchArm { pats: vec![pat], expr: then_branch, guard: None }, | 577 | MatchArm { pats: vec![pat], expr: then_branch, guard: None }, |
577 | MatchArm { pats: vec![placeholder_pat], expr: else_branch, guard: None }, | 578 | MatchArm { |
579 | pats: vec![placeholder_pat], | ||
580 | expr: else_branch.unwrap_or_else(|| self.empty_block()), | ||
581 | guard: None, | ||
582 | }, | ||
578 | ]; | 583 | ]; |
579 | self.alloc_expr(Expr::Match { expr: match_expr, arms }, syntax_ptr) | 584 | self.alloc_expr(Expr::Match { expr: match_expr, arms }, syntax_ptr) |
580 | } else { | 585 | } else { |
581 | let condition = self.collect_expr_opt(e.condition().and_then(|c| c.expr())); | 586 | let condition = self.collect_expr_opt(e.condition().and_then(|c| c.expr())); |
582 | let then_branch = self.collect_block_opt(e.then_branch()); | ||
583 | let else_branch = e.else_branch().map(|b| match b { | ||
584 | ast::ElseBranch::Block(it) => self.collect_block(it), | ||
585 | ast::ElseBranch::IfExpr(elif) => { | ||
586 | let expr: ast::Expr = ast::Expr::cast(elif.syntax().clone()).unwrap(); | ||
587 | self.collect_expr(expr) | ||
588 | } | ||
589 | }); | ||
590 | self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr) | 587 | self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr) |
591 | } | 588 | } |
592 | } | 589 | } |