From f353625705854877a8347c3a46f15e294a9338e4 Mon Sep 17 00:00:00 2001 From: Josh Mcguigan Date: Sun, 1 Mar 2020 06:25:38 -0800 Subject: match single prefix slice --- crates/ra_hir_ty/src/infer/pat.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir_ty/src/infer') diff --git a/crates/ra_hir_ty/src/infer/pat.rs b/crates/ra_hir_ty/src/infer/pat.rs index bf8ea192b..b991720ff 100644 --- a/crates/ra_hir_ty/src/infer/pat.rs +++ b/crates/ra_hir_ty/src/infer/pat.rs @@ -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, ¶meters.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 -- cgit v1.2.3 From d0e282f6b1a8c698212f7f76de2ff122e36e17a2 Mon Sep 17 00:00:00 2001 From: Josh Mcguigan Date: Sun, 1 Mar 2020 07:08:05 -0800 Subject: handle arbitrary length slices --- crates/ra_hir_ty/src/infer/pat.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir_ty/src/infer') diff --git a/crates/ra_hir_ty/src/infer/pat.rs b/crates/ra_hir_ty/src/infer/pat.rs index b991720ff..3fe1a4abb 100644 --- a/crates/ra_hir_ty/src/infer/pat.rs +++ b/crates/ra_hir_ty/src/infer/pat.rs @@ -188,10 +188,12 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { 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, ¶meters.0[0], default_bm); + (prefix_pat_ids, None, []) => { + for pat_id in prefix_pat_ids { + self.infer_pat(*pat_id, parameters.as_single(), default_bm); + } - Ty::apply_one(TypeCtor::Slice, ty) + Ty::apply_one(TypeCtor::Slice, parameters.as_single().clone()) }, _ => Ty::Unknown, } -- cgit v1.2.3 From b9ef7a6b987eea4b0b14298fbbe44d912806f50f Mon Sep 17 00:00:00 2001 From: Josh Mcguigan Date: Sun, 1 Mar 2020 12:13:05 -0800 Subject: remove match statement, handle suffix --- crates/ra_hir_ty/src/infer/pat.rs | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) (limited to 'crates/ra_hir_ty/src/infer') diff --git a/crates/ra_hir_ty/src/infer/pat.rs b/crates/ra_hir_ty/src/infer/pat.rs index 3fe1a4abb..06b09af82 100644 --- a/crates/ra_hir_ty/src/infer/pat.rs +++ b/crates/ra_hir_ty/src/infer/pat.rs @@ -185,21 +185,18 @@ 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_ids, None, []) => { - for pat_id in prefix_pat_ids { - self.infer_pat(*pat_id, parameters.as_single(), default_bm); - } - - Ty::apply_one(TypeCtor::Slice, parameters.as_single().clone()) - }, - _ => Ty::Unknown, + Pat::Slice { prefix, slice: _slice, suffix } => { + let ty = if let Ty::Apply(ApplicationTy { ctor: TypeCtor::Slice, parameters }) = expected { + for pat_id in prefix.iter().chain(suffix) { + self.infer_pat(*pat_id, parameters.as_single(), default_bm); } + + parameters.as_single().clone() } else { Ty::Unknown - } + }; + + Ty::apply_one(TypeCtor::Slice, ty) } _ => Ty::Unknown, }; -- cgit v1.2.3 From f5efa17515a543c1405ecad2caf93ed25052500e Mon Sep 17 00:00:00 2001 From: Josh Mcguigan Date: Sun, 1 Mar 2020 14:02:32 -0800 Subject: handle array pattern matching type inference --- crates/ra_hir_ty/src/infer/pat.rs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'crates/ra_hir_ty/src/infer') diff --git a/crates/ra_hir_ty/src/infer/pat.rs b/crates/ra_hir_ty/src/infer/pat.rs index 06b09af82..623e52599 100644 --- a/crates/ra_hir_ty/src/infer/pat.rs +++ b/crates/ra_hir_ty/src/infer/pat.rs @@ -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, ApplicationTy}; +use crate::{db::HirDatabase, utils::variant_data, Substs, Ty, TypeCtor}; impl<'a, D: HirDatabase> InferenceContext<'a, D> { fn infer_tuple_struct_pat( @@ -186,17 +186,21 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { return inner_ty; } Pat::Slice { prefix, slice: _slice, suffix } => { - let ty = if let Ty::Apply(ApplicationTy { ctor: TypeCtor::Slice, parameters }) = expected { - for pat_id in prefix.iter().chain(suffix) { - self.infer_pat(*pat_id, parameters.as_single(), default_bm); - } - - parameters.as_single().clone() - } else { - Ty::Unknown + let (container_ty, elem_ty) = match &expected { + ty_app!(TypeCtor::Array, st) => { + (TypeCtor::Array, st.as_single().clone()) + }, + ty_app!(TypeCtor::Slice, st) => { + (TypeCtor::Slice, st.as_single().clone()) + }, + _ => (TypeCtor::Slice, Ty::Unknown), }; - Ty::apply_one(TypeCtor::Slice, ty) + for pat_id in prefix.iter().chain(suffix) { + self.infer_pat(*pat_id, &elem_ty, default_bm); + } + + Ty::apply_one(container_ty, elem_ty) } _ => Ty::Unknown, }; -- cgit v1.2.3