From 8c8d0bb34f5495e0f260b5aaf3685ecb98406f32 Mon Sep 17 00:00:00 2001 From: Matthew Jasper Date: Sun, 9 Feb 2020 18:57:01 +0000 Subject: Add or- and parenthesized-patterns --- crates/ra_hir_def/src/body/lower.rs | 15 ++++++++++----- crates/ra_hir_def/src/body/scope.rs | 4 +--- crates/ra_hir_def/src/expr.rs | 5 +++-- 3 files changed, 14 insertions(+), 10 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index e656f9a41..fe0973fc7 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -164,9 +164,9 @@ where let match_expr = self.collect_expr_opt(condition.expr()); let placeholder_pat = self.missing_pat(); let arms = vec![ - MatchArm { pats: vec![pat], expr: then_branch, guard: None }, + MatchArm { pat, expr: then_branch, guard: None }, MatchArm { - pats: vec![placeholder_pat], + pat: placeholder_pat, expr: else_branch.unwrap_or_else(|| self.empty_block()), guard: None, }, @@ -203,8 +203,8 @@ where let placeholder_pat = self.missing_pat(); let break_ = self.alloc_expr_desugared(Expr::Break { expr: None }); let arms = vec![ - MatchArm { pats: vec![pat], expr: body, guard: None }, - MatchArm { pats: vec![placeholder_pat], expr: break_, guard: None }, + MatchArm { pat, expr: body, guard: None }, + MatchArm { pat: placeholder_pat, expr: break_, guard: None }, ]; let match_expr = self.alloc_expr_desugared(Expr::Match { expr: match_expr, arms }); @@ -250,7 +250,7 @@ where match_arm_list .arms() .map(|arm| MatchArm { - pats: arm.pats().map(|p| self.collect_pat(p)).collect(), + pat: self.collect_pat_opt(arm.pat()), expr: self.collect_expr_opt(arm.expr()), guard: arm .guard() @@ -587,6 +587,11 @@ where let path = p.path().and_then(|path| self.expander.parse_path(path)); path.map(Pat::Path).unwrap_or(Pat::Missing) } + ast::Pat::OrPat(p) => { + let pats = p.pats().map(|p| self.collect_pat(p)).collect(); + Pat::Or(pats) + } + ast::Pat::ParenPat(p) => return self.collect_pat_opt(p.pat()), ast::Pat::TuplePat(p) => { let args = p.args().map(|p| self.collect_pat(p)).collect(); Pat::Tuple(args) diff --git a/crates/ra_hir_def/src/body/scope.rs b/crates/ra_hir_def/src/body/scope.rs index a63552327..32c924acc 100644 --- a/crates/ra_hir_def/src/body/scope.rs +++ b/crates/ra_hir_def/src/body/scope.rs @@ -158,9 +158,7 @@ fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope compute_expr_scopes(*expr, body, scopes, scope); for arm in arms { let scope = scopes.new_scope(scope); - for pat in &arm.pats { - scopes.add_bindings(body, scope, *pat); - } + scopes.add_bindings(body, scope, arm.pat); scopes.set_scope(arm.expr, scope); compute_expr_scopes(arm.expr, body, scopes, scope); } diff --git a/crates/ra_hir_def/src/expr.rs b/crates/ra_hir_def/src/expr.rs index a75ef9970..5a84e08ed 100644 --- a/crates/ra_hir_def/src/expr.rs +++ b/crates/ra_hir_def/src/expr.rs @@ -202,7 +202,7 @@ pub enum Array { #[derive(Debug, Clone, Eq, PartialEq)] pub struct MatchArm { - pub pats: Vec, + pub pat: PatId, pub guard: Option, pub expr: ExprId, } @@ -382,6 +382,7 @@ pub enum Pat { Missing, Wild, Tuple(Vec), + Or(Vec), Record { path: Option, args: Vec, @@ -420,7 +421,7 @@ impl Pat { Pat::Bind { subpat, .. } => { subpat.iter().copied().for_each(f); } - Pat::Tuple(args) | Pat::TupleStruct { args, .. } => { + Pat::Or(args) | Pat::Tuple(args) | Pat::TupleStruct { args, .. } => { args.iter().copied().for_each(f); } Pat::Ref { pat, .. } => f(*pat), -- cgit v1.2.3