Skip to content

Commit f353625

Browse files
committed
match single prefix slice
1 parent 6db2da4 commit f353625

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

crates/ra_hir_ty/src/infer/pat.rs

Lines changed: 15 additions & 1 deletion
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};
15+
use crate::{db::HirDatabase, utils::variant_data, Substs, Ty, TypeCtor, ApplicationTy};
1616

1717
impl<'a, D: HirDatabase> InferenceContext<'a, D> {
1818
fn infer_tuple_struct_pat(
@@ -185,6 +185,20 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
185185
self.write_pat_ty(pat, bound_ty);
186186
return inner_ty;
187187
}
188+
Pat::Slice { prefix, slice, suffix } => {
189+
if let Ty::Apply(ApplicationTy { ctor: TypeCtor::Slice, parameters }) = expected {
190+
match (prefix.as_slice(), slice, suffix.as_slice()) {
191+
([prefix_pat_id], None, []) => {
192+
let ty = self.infer_pat(*prefix_pat_id, &parameters.0[0], default_bm);
193+
194+
Ty::apply_one(TypeCtor::Slice, ty)
195+
},
196+
_ => Ty::Unknown,
197+
}
198+
} else {
199+
Ty::Unknown
200+
}
201+
}
188202
_ => Ty::Unknown,
189203
};
190204
// use a new type variable if we got Ty::Unknown here

crates/ra_hir_ty/src/tests/patterns.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,39 @@ fn test() {
136136
);
137137
}
138138

139+
#[test]
140+
fn infer_pattern_match_slice() {
141+
assert_snapshot!(
142+
infer(r#"
143+
fn test() {
144+
let slice: &[f64] = &[0.0];
145+
match slice {
146+
&[a] => {
147+
a;
148+
}
149+
_ => {}
150+
}
151+
}
152+
"#),
153+
@r###"
154+
[11; 129) '{ ... } }': ()
155+
[21; 26) 'slice': &[f64]
156+
[37; 43) '&[0.0]': &[f64; _]
157+
[38; 43) '[0.0]': [f64; _]
158+
[39; 42) '0.0': f64
159+
[49; 127) 'match ... }': ()
160+
[55; 60) 'slice': &[f64]
161+
[71; 75) '&[a]': &[f64]
162+
[72; 75) '[a]': [f64]
163+
[73; 74) 'a': f64
164+
[79; 105) '{ ... }': ()
165+
[93; 94) 'a': f64
166+
[114; 115) '_': &[f64]
167+
[119; 121) '{}': ()
168+
"###
169+
);
170+
}
171+
139172
#[test]
140173
fn infer_adt_pattern() {
141174
assert_snapshot!(

0 commit comments

Comments
 (0)