Skip to content

Commit f5efa17

Browse files
committed
handle array pattern matching type inference
1 parent b9ef7a6 commit f5efa17

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

crates/ra_hir_ty/src/infer/pat.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use hir_expand::name::Name;
1212
use test_utils::tested_by;
1313

1414
use super::{BindingMode, InferenceContext};
15-
use crate::{db::HirDatabase, utils::variant_data, Substs, Ty, TypeCtor, ApplicationTy};
15+
use crate::{db::HirDatabase, utils::variant_data, Substs, Ty, TypeCtor};
1616

1717
impl<'a, D: HirDatabase> InferenceContext<'a, D> {
1818
fn infer_tuple_struct_pat(
@@ -186,17 +186,21 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
186186
return inner_ty;
187187
}
188188
Pat::Slice { prefix, slice: _slice, suffix } => {
189-
let ty = if let Ty::Apply(ApplicationTy { ctor: TypeCtor::Slice, parameters }) = expected {
190-
for pat_id in prefix.iter().chain(suffix) {
191-
self.infer_pat(*pat_id, parameters.as_single(), default_bm);
192-
}
193-
194-
parameters.as_single().clone()
195-
} else {
196-
Ty::Unknown
189+
let (container_ty, elem_ty) = match &expected {
190+
ty_app!(TypeCtor::Array, st) => {
191+
(TypeCtor::Array, st.as_single().clone())
192+
},
193+
ty_app!(TypeCtor::Slice, st) => {
194+
(TypeCtor::Slice, st.as_single().clone())
195+
},
196+
_ => (TypeCtor::Slice, Ty::Unknown),
197197
};
198198

199-
Ty::apply_one(TypeCtor::Slice, ty)
199+
for pat_id in prefix.iter().chain(suffix) {
200+
self.infer_pat(*pat_id, &elem_ty, default_bm);
201+
}
202+
203+
Ty::apply_one(container_ty, elem_ty)
200204
}
201205
_ => Ty::Unknown,
202206
};

crates/ra_hir_ty/src/tests/patterns.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ fn test(x: &i32) {
5454
[144; 145) 'e': {unknown}
5555
[158; 205) 'if let... }': ()
5656
[165; 170) '[val]': [{unknown}]
57+
[166; 169) 'val': {unknown}
5758
[173; 176) 'opt': [{unknown}]
5859
[177; 205) '{ ... }': ()
5960
[191; 192) 'h': {unknown}
@@ -184,6 +185,46 @@ fn test() {
184185
);
185186
}
186187

188+
#[test]
189+
fn infer_pattern_match_arr() {
190+
assert_snapshot!(
191+
infer(r#"
192+
fn test() {
193+
let arr: [f64; 2] = [0.0, 1.0];
194+
match arr {
195+
[1.0, a] => {
196+
a;
197+
},
198+
[b, c] => {
199+
b;
200+
c;
201+
}
202+
}
203+
}
204+
"#),
205+
@r###"
206+
[11; 180) '{ ... } }': ()
207+
[21; 24) 'arr': [f64; _]
208+
[37; 47) '[0.0, 1.0]': [f64; _]
209+
[38; 41) '0.0': f64
210+
[43; 46) '1.0': f64
211+
[53; 178) 'match ... }': ()
212+
[59; 62) 'arr': [f64; _]
213+
[73; 81) '[1.0, a]': [f64; _]
214+
[74; 77) '1.0': f64
215+
[79; 80) 'a': f64
216+
[85; 111) '{ ... }': ()
217+
[99; 100) 'a': f64
218+
[121; 127) '[b, c]': [f64; _]
219+
[122; 123) 'b': f64
220+
[125; 126) 'c': f64
221+
[131; 172) '{ ... }': ()
222+
[145; 146) 'b': f64
223+
[160; 161) 'c': f64
224+
"###
225+
);
226+
}
227+
187228
#[test]
188229
fn infer_adt_pattern() {
189230
assert_snapshot!(

0 commit comments

Comments
 (0)