match single prefix slice

This commit is contained in:
Josh Mcguigan 2020-03-01 06:25:38 -08:00
parent 6db2da4993
commit f353625705
2 changed files with 48 additions and 1 deletions

View File

@ -12,7 +12,7 @@ use hir_expand::name::Name;
use test_utils::tested_by;
use super::{BindingMode, InferenceContext};
use crate::{db::HirDatabase, utils::variant_data, Substs, Ty, TypeCtor};
use crate::{db::HirDatabase, utils::variant_data, Substs, Ty, TypeCtor, ApplicationTy};
impl<'a, D: HirDatabase> InferenceContext<'a, D> {
fn infer_tuple_struct_pat(
@ -185,6 +185,20 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
self.write_pat_ty(pat, bound_ty);
return inner_ty;
}
Pat::Slice { prefix, slice, suffix } => {
if let Ty::Apply(ApplicationTy { ctor: TypeCtor::Slice, parameters }) = expected {
match (prefix.as_slice(), slice, suffix.as_slice()) {
([prefix_pat_id], None, []) => {
let ty = self.infer_pat(*prefix_pat_id, &parameters.0[0], default_bm);
Ty::apply_one(TypeCtor::Slice, ty)
},
_ => Ty::Unknown,
}
} else {
Ty::Unknown
}
}
_ => Ty::Unknown,
};
// use a new type variable if we got Ty::Unknown here

View File

@ -136,6 +136,39 @@ fn test() {
);
}
#[test]
fn infer_pattern_match_slice() {
assert_snapshot!(
infer(r#"
fn test() {
let slice: &[f64] = &[0.0];
match slice {
&[a] => {
a;
}
_ => {}
}
}
"#),
@r###"
[11; 129) '{ ... } }': ()
[21; 26) 'slice': &[f64]
[37; 43) '&[0.0]': &[f64; _]
[38; 43) '[0.0]': [f64; _]
[39; 42) '0.0': f64
[49; 127) 'match ... }': ()
[55; 60) 'slice': &[f64]
[71; 75) '&[a]': &[f64]
[72; 75) '[a]': [f64]
[73; 74) 'a': f64
[79; 105) '{ ... }': ()
[93; 94) 'a': f64
[114; 115) '_': &[f64]
[119; 121) '{}': ()
"###
);
}
#[test]
fn infer_adt_pattern() {
assert_snapshot!(