Skip to content

Commit f8d6d6f

Browse files
Merge #3074
3074: Or patterns r=matthewjasper a=matthewjasper Works towards #2458 Co-authored-by: Matthew Jasper <mjjasper1@gmail.com>
2 parents 29f5e7e + 49b53cd commit f8d6d6f

File tree

21 files changed

+427
-113
lines changed

21 files changed

+427
-113
lines changed

crates/ra_assists/src/handlers/fill_match_arms.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ pub(crate) fn fill_match_arms(ctx: AssistCtx) -> Option<Assist> {
7575
}
7676

7777
fn is_trivial(arm: &ast::MatchArm) -> bool {
78-
arm.pats().any(|pat| match pat {
79-
ast::Pat::PlaceholderPat(..) => true,
78+
match arm.pat() {
79+
Some(ast::Pat::PlaceholderPat(..)) => true,
8080
_ => false,
81-
})
81+
}
8282
}
8383

8484
fn resolve_enum_def(

crates/ra_assists/src/handlers/merge_match_arms.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub(crate) fn merge_match_arms(ctx: AssistCtx) -> Option<Assist> {
7575
} else {
7676
arms_to_merge
7777
.iter()
78-
.flat_map(ast::MatchArm::pats)
78+
.filter_map(ast::MatchArm::pat)
7979
.map(|x| x.syntax().to_string())
8080
.collect::<Vec<String>>()
8181
.join(" | ")
@@ -96,10 +96,10 @@ pub(crate) fn merge_match_arms(ctx: AssistCtx) -> Option<Assist> {
9696
}
9797

9898
fn contains_placeholder(a: &ast::MatchArm) -> bool {
99-
a.pats().any(|x| match x {
100-
ra_syntax::ast::Pat::PlaceholderPat(..) => true,
99+
match a.pat() {
100+
Some(ra_syntax::ast::Pat::PlaceholderPat(..)) => true,
101101
_ => false,
102-
})
102+
}
103103
}
104104

105105
fn next_arm(arm: &ast::MatchArm) -> Option<ast::MatchArm> {

crates/ra_assists/src/handlers/move_guard.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub(crate) fn move_guard_to_arm_body(ctx: AssistCtx) -> Option<Assist> {
9090
// ```
9191
pub(crate) fn move_arm_cond_to_match_guard(ctx: AssistCtx) -> Option<Assist> {
9292
let match_arm: MatchArm = ctx.find_node_at_offset::<MatchArm>()?;
93-
let last_match_pat = match_arm.pats().last()?;
93+
let match_pat = match_arm.pat()?;
9494

9595
let arm_body = match_arm.expr()?;
9696
let if_expr: IfExpr = IfExpr::cast(arm_body.syntax().clone())?;
@@ -122,8 +122,8 @@ pub(crate) fn move_arm_cond_to_match_guard(ctx: AssistCtx) -> Option<Assist> {
122122
_ => edit.replace(if_expr.syntax().text_range(), then_block.syntax().text()),
123123
}
124124

125-
edit.insert(last_match_pat.syntax().text_range().end(), buf);
126-
edit.set_cursor(last_match_pat.syntax().text_range().end() + TextUnit::from(1));
125+
edit.insert(match_pat.syntax().text_range().end(), buf);
126+
edit.set_cursor(match_pat.syntax().text_range().end() + TextUnit::from(1));
127127
},
128128
)
129129
}

crates/ra_hir_def/src/body/lower.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ where
164164
let match_expr = self.collect_expr_opt(condition.expr());
165165
let placeholder_pat = self.missing_pat();
166166
let arms = vec![
167-
MatchArm { pats: vec![pat], expr: then_branch, guard: None },
167+
MatchArm { pat, expr: then_branch, guard: None },
168168
MatchArm {
169-
pats: vec![placeholder_pat],
169+
pat: placeholder_pat,
170170
expr: else_branch.unwrap_or_else(|| self.empty_block()),
171171
guard: None,
172172
},
@@ -203,8 +203,8 @@ where
203203
let placeholder_pat = self.missing_pat();
204204
let break_ = self.alloc_expr_desugared(Expr::Break { expr: None });
205205
let arms = vec![
206-
MatchArm { pats: vec![pat], expr: body, guard: None },
207-
MatchArm { pats: vec![placeholder_pat], expr: break_, guard: None },
206+
MatchArm { pat, expr: body, guard: None },
207+
MatchArm { pat: placeholder_pat, expr: break_, guard: None },
208208
];
209209
let match_expr =
210210
self.alloc_expr_desugared(Expr::Match { expr: match_expr, arms });
@@ -250,7 +250,7 @@ where
250250
match_arm_list
251251
.arms()
252252
.map(|arm| MatchArm {
253-
pats: arm.pats().map(|p| self.collect_pat(p)).collect(),
253+
pat: self.collect_pat_opt(arm.pat()),
254254
expr: self.collect_expr_opt(arm.expr()),
255255
guard: arm
256256
.guard()
@@ -587,6 +587,11 @@ where
587587
let path = p.path().and_then(|path| self.expander.parse_path(path));
588588
path.map(Pat::Path).unwrap_or(Pat::Missing)
589589
}
590+
ast::Pat::OrPat(p) => {
591+
let pats = p.pats().map(|p| self.collect_pat(p)).collect();
592+
Pat::Or(pats)
593+
}
594+
ast::Pat::ParenPat(p) => return self.collect_pat_opt(p.pat()),
590595
ast::Pat::TuplePat(p) => {
591596
let args = p.args().map(|p| self.collect_pat(p)).collect();
592597
Pat::Tuple(args)

crates/ra_hir_def/src/body/scope.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,7 @@ fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope
158158
compute_expr_scopes(*expr, body, scopes, scope);
159159
for arm in arms {
160160
let scope = scopes.new_scope(scope);
161-
for pat in &arm.pats {
162-
scopes.add_bindings(body, scope, *pat);
163-
}
161+
scopes.add_bindings(body, scope, arm.pat);
164162
scopes.set_scope(arm.expr, scope);
165163
compute_expr_scopes(arm.expr, body, scopes, scope);
166164
}

crates/ra_hir_def/src/expr.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ pub enum Array {
202202

203203
#[derive(Debug, Clone, Eq, PartialEq)]
204204
pub struct MatchArm {
205-
pub pats: Vec<PatId>,
205+
pub pat: PatId,
206206
pub guard: Option<ExprId>,
207207
pub expr: ExprId,
208208
}
@@ -382,6 +382,7 @@ pub enum Pat {
382382
Missing,
383383
Wild,
384384
Tuple(Vec<PatId>),
385+
Or(Vec<PatId>),
385386
Record {
386387
path: Option<Path>,
387388
args: Vec<RecordFieldPat>,
@@ -420,7 +421,7 @@ impl Pat {
420421
Pat::Bind { subpat, .. } => {
421422
subpat.iter().copied().for_each(f);
422423
}
423-
Pat::Tuple(args) | Pat::TupleStruct { args, .. } => {
424+
Pat::Or(args) | Pat::Tuple(args) | Pat::TupleStruct { args, .. } => {
424425
args.iter().copied().for_each(f);
425426
}
426427
Pat::Ref { pat, .. } => f(*pat),

crates/ra_hir_ty/src/infer/expr.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
168168
let mut result_ty = self.table.new_maybe_never_type_var();
169169

170170
for arm in arms {
171-
for &pat in &arm.pats {
172-
let _pat_ty = self.infer_pat(pat, &input_ty, BindingMode::default());
173-
}
171+
let _pat_ty = self.infer_pat(arm.pat, &input_ty, BindingMode::default());
174172
if let Some(guard_expr) = arm.guard {
175173
self.infer_expr(
176174
guard_expr,

crates/ra_hir_ty/src/infer/pat.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
8282

8383
let is_non_ref_pat = match &body[pat] {
8484
Pat::Tuple(..)
85+
| Pat::Or(..)
8586
| Pat::TupleStruct { .. }
8687
| Pat::Record { .. }
8788
| Pat::Range { .. }
@@ -126,6 +127,17 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
126127

127128
Ty::apply(TypeCtor::Tuple { cardinality: args.len() as u16 }, Substs(inner_tys))
128129
}
130+
Pat::Or(ref pats) => {
131+
if let Some((first_pat, rest)) = pats.split_first() {
132+
let ty = self.infer_pat(*first_pat, expected, default_bm);
133+
for pat in rest {
134+
self.infer_pat(*pat, expected, default_bm);
135+
}
136+
ty
137+
} else {
138+
Ty::Unknown
139+
}
140+
}
129141
Pat::Ref { pat, mutability } => {
130142
let expectation = match expected.as_reference() {
131143
Some((inner_ty, exp_mut)) => {

crates/ra_ide/src/inlay_hints.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ fn get_inlay_hints(
8080
},
8181
ast::MatchArmList(it) => {
8282
it.arms()
83-
.map(|match_arm| match_arm.pats())
84-
.flatten()
83+
.filter_map(|match_arm| match_arm.pat())
8584
.for_each(|root_pat| get_pat_type_hints(acc, db, &analyzer, root_pat, true, max_inlay_hint_length));
8685
},
8786
ast::CallExpr(it) => {
@@ -202,6 +201,7 @@ fn get_leaf_pats(root_pat: ast::Pat) -> Vec<ast::Pat> {
202201
Some(pat) => pats_to_process.push_back(pat),
203202
_ => leaf_pats.push(maybe_leaf_pat),
204203
},
204+
ast::Pat::OrPat(ref_pat) => pats_to_process.extend(ref_pat.pats()),
205205
ast::Pat::TuplePat(tuple_pat) => pats_to_process.extend(tuple_pat.args()),
206206
ast::Pat::RecordPat(record_pat) => {
207207
if let Some(pat_list) = record_pat.record_field_pat_list() {
@@ -222,6 +222,7 @@ fn get_leaf_pats(root_pat: ast::Pat) -> Vec<ast::Pat> {
222222
ast::Pat::TupleStructPat(tuple_struct_pat) => {
223223
pats_to_process.extend(tuple_struct_pat.args())
224224
}
225+
ast::Pat::ParenPat(inner_pat) => pats_to_process.extend(inner_pat.pat()),
225226
ast::Pat::RefPat(ref_pat) => pats_to_process.extend(ref_pat.pat()),
226227
_ => (),
227228
}

crates/ra_parser/src/grammar/expressions/atom.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ fn for_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
336336
fn cond(p: &mut Parser) {
337337
let m = p.start();
338338
if p.eat(T![let]) {
339-
patterns::pattern_list(p);
339+
patterns::pattern_top(p);
340340
p.expect(T![=]);
341341
}
342342
expr_no_struct(p);
@@ -430,7 +430,7 @@ fn match_arm(p: &mut Parser) -> BlockLike {
430430
// }
431431
attributes::outer_attributes(p);
432432

433-
patterns::pattern_list_r(p, TokenSet::EMPTY);
433+
patterns::pattern_top_r(p, TokenSet::EMPTY);
434434
if p.at(T![if]) {
435435
match_guard(p);
436436
}

0 commit comments

Comments
 (0)