Skip to content

Commit 0ebc240

Browse files
committed
Infer type for slice wildcard patterns
1 parent 3615347 commit 0ebc240

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

crates/ra_hir_ty/src/infer/pat.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl<'a> InferenceContext<'a> {
184184
self.write_pat_ty(pat, bound_ty);
185185
return inner_ty;
186186
}
187-
Pat::Slice { prefix, slice: _slice, suffix } => {
187+
Pat::Slice { prefix, slice, suffix } => {
188188
let (container_ty, elem_ty) = match &expected {
189189
ty_app!(TypeCtor::Array, st) => (TypeCtor::Array, st.as_single().clone()),
190190
ty_app!(TypeCtor::Slice, st) => (TypeCtor::Slice, st.as_single().clone()),
@@ -195,7 +195,12 @@ impl<'a> InferenceContext<'a> {
195195
self.infer_pat(*pat_id, &elem_ty, default_bm);
196196
}
197197

198-
Ty::apply_one(container_ty, elem_ty)
198+
let pat_ty = Ty::apply_one(container_ty, elem_ty);
199+
if let Some(slice_pat_id) = slice {
200+
self.infer_pat(*slice_pat_id, &pat_ty, default_bm);
201+
}
202+
203+
pat_ty
199204
}
200205
Pat::Wild => expected.clone(),
201206
Pat::Range { start, end } => {

crates/ra_hir_ty/src/tests/patterns.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,3 +627,28 @@ fn test() {
627627
"###
628628
);
629629
}
630+
631+
#[test]
632+
fn slice_tail_pattern() {
633+
assert_snapshot!(
634+
infer(r#"
635+
fn foo(params: &[i32]) {
636+
match params {
637+
[head, tail @ ..] => {
638+
}
639+
}
640+
}
641+
"#),
642+
@r###"
643+
7..13 'params': &[i32]
644+
23..92 '{ ... } }': ()
645+
29..90 'match ... }': ()
646+
35..41 'params': &[i32]
647+
52..69 '[head,... @ ..]': [i32]
648+
53..57 'head': &i32
649+
59..68 'tail @ ..': &[i32]
650+
66..68 '..': [i32]
651+
73..84 '{ }': ()
652+
"###
653+
);
654+
}

crates/ra_hir_ty/src/tests/regression.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,8 @@ fn foo(params: &[usize]) {
500500
31..78 'match ... }': ()
501501
37..43 'params': &[usize]
502502
54..66 '[ps @ .., _]': [usize]
503+
55..62 'ps @ ..': &[usize]
504+
60..62 '..': [usize]
503505
64..65 '_': usize
504506
70..72 '{}': ()
505507
"###

0 commit comments

Comments
 (0)