2019-10-12 15:39:20 +00:00
|
|
|
//! Type inference for patterns.
|
|
|
|
|
|
|
|
use std::iter::repeat;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2019-11-27 09:13:07 +00:00
|
|
|
use hir_def::{
|
2020-06-24 09:57:28 +00:00
|
|
|
expr::{BindingAnnotation, Expr, Literal, Pat, PatId, RecordFieldPat},
|
2019-11-27 09:13:07 +00:00
|
|
|
path::Path,
|
|
|
|
type_ref::Mutability,
|
2020-04-25 12:23:34 +00:00
|
|
|
FieldId,
|
2019-11-27 09:13:07 +00:00
|
|
|
};
|
|
|
|
use hir_expand::name::Name;
|
2020-05-20 10:59:20 +00:00
|
|
|
use test_utils::mark;
|
2019-10-12 15:39:20 +00:00
|
|
|
|
2020-04-01 18:27:47 +00:00
|
|
|
use super::{BindingMode, Expectation, InferenceContext};
|
2020-03-13 15:05:46 +00:00
|
|
|
use crate::{utils::variant_data, Substs, Ty, TypeCtor};
|
2019-10-12 15:39:20 +00:00
|
|
|
|
2020-03-13 15:05:46 +00:00
|
|
|
impl<'a> InferenceContext<'a> {
|
2019-10-12 15:39:20 +00:00
|
|
|
fn infer_tuple_struct_pat(
|
|
|
|
&mut self,
|
|
|
|
path: Option<&Path>,
|
|
|
|
subpats: &[PatId],
|
|
|
|
expected: &Ty,
|
|
|
|
default_bm: BindingMode,
|
2020-03-24 11:40:58 +00:00
|
|
|
id: PatId,
|
2019-10-12 15:39:20 +00:00
|
|
|
) -> Ty {
|
|
|
|
let (ty, def) = self.resolve_variant(path);
|
2020-03-13 15:05:46 +00:00
|
|
|
let var_data = def.map(|it| variant_data(self.db.upcast(), it));
|
2020-03-24 11:40:58 +00:00
|
|
|
if let Some(variant) = def {
|
|
|
|
self.write_variant_resolution(id.into(), variant);
|
|
|
|
}
|
2019-10-12 15:39:20 +00:00
|
|
|
self.unify(&ty, expected);
|
|
|
|
|
|
|
|
let substs = ty.substs().unwrap_or_else(Substs::empty);
|
|
|
|
|
2020-02-18 12:53:02 +00:00
|
|
|
let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default();
|
2019-11-27 13:25:01 +00:00
|
|
|
|
2019-10-12 15:39:20 +00:00
|
|
|
for (i, &subpat) in subpats.iter().enumerate() {
|
2019-11-27 13:25:01 +00:00
|
|
|
let expected_ty = var_data
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|d| d.field(&Name::new_tuple_field(i)))
|
2020-01-31 15:52:43 +00:00
|
|
|
.map_or(Ty::Unknown, |field| field_tys[field].clone().subst(&substs));
|
2019-10-12 15:39:20 +00:00
|
|
|
let expected_ty = self.normalize_associated_types_in(expected_ty);
|
|
|
|
self.infer_pat(subpat, &expected_ty, default_bm);
|
|
|
|
}
|
|
|
|
|
|
|
|
ty
|
|
|
|
}
|
|
|
|
|
|
|
|
fn infer_record_pat(
|
|
|
|
&mut self,
|
|
|
|
path: Option<&Path>,
|
|
|
|
subpats: &[RecordFieldPat],
|
|
|
|
expected: &Ty,
|
|
|
|
default_bm: BindingMode,
|
|
|
|
id: PatId,
|
|
|
|
) -> Ty {
|
|
|
|
let (ty, def) = self.resolve_variant(path);
|
2020-03-13 15:05:46 +00:00
|
|
|
let var_data = def.map(|it| variant_data(self.db.upcast(), it));
|
2019-10-12 15:39:20 +00:00
|
|
|
if let Some(variant) = def {
|
|
|
|
self.write_variant_resolution(id.into(), variant);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.unify(&ty, expected);
|
|
|
|
|
|
|
|
let substs = ty.substs().unwrap_or_else(Substs::empty);
|
|
|
|
|
2020-02-18 12:53:02 +00:00
|
|
|
let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default();
|
2019-10-12 15:39:20 +00:00
|
|
|
for subpat in subpats {
|
2019-11-27 13:25:01 +00:00
|
|
|
let matching_field = var_data.as_ref().and_then(|it| it.field(&subpat.name));
|
2020-04-18 20:05:06 +00:00
|
|
|
if let Some(local_id) = matching_field {
|
2020-04-25 12:23:34 +00:00
|
|
|
let field_def = FieldId { parent: def.unwrap(), local_id };
|
2020-09-05 01:06:05 +00:00
|
|
|
self.result.record_pat_field_resolutions.insert(subpat.pat, field_def);
|
2020-04-18 20:05:06 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 13:25:01 +00:00
|
|
|
let expected_ty =
|
2020-01-31 15:52:43 +00:00
|
|
|
matching_field.map_or(Ty::Unknown, |field| field_tys[field].clone().subst(&substs));
|
2019-10-12 15:39:20 +00:00
|
|
|
let expected_ty = self.normalize_associated_types_in(expected_ty);
|
|
|
|
self.infer_pat(subpat.pat, &expected_ty, default_bm);
|
|
|
|
}
|
|
|
|
|
|
|
|
ty
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn infer_pat(
|
|
|
|
&mut self,
|
|
|
|
pat: PatId,
|
|
|
|
mut expected: &Ty,
|
|
|
|
mut default_bm: BindingMode,
|
|
|
|
) -> Ty {
|
|
|
|
let body = Arc::clone(&self.body); // avoid borrow checker problem
|
|
|
|
|
2020-06-24 09:57:28 +00:00
|
|
|
if is_non_ref_pat(&body, pat) {
|
2019-10-12 15:39:20 +00:00
|
|
|
while let Some((inner, mutability)) = expected.as_reference() {
|
|
|
|
expected = inner;
|
|
|
|
default_bm = match default_bm {
|
|
|
|
BindingMode::Move => BindingMode::Ref(mutability),
|
|
|
|
BindingMode::Ref(Mutability::Shared) => BindingMode::Ref(Mutability::Shared),
|
|
|
|
BindingMode::Ref(Mutability::Mut) => BindingMode::Ref(mutability),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if let Pat::Ref { .. } = &body[pat] {
|
2020-05-20 10:59:20 +00:00
|
|
|
mark::hit!(match_ergonomics_ref);
|
2019-10-12 15:39:20 +00:00
|
|
|
// When you encounter a `&pat` pattern, reset to Move.
|
|
|
|
// This is so that `w` is by value: `let (_, &w) = &(1, &2);`
|
|
|
|
default_bm = BindingMode::Move;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lose mutability.
|
|
|
|
let default_bm = default_bm;
|
|
|
|
let expected = expected;
|
|
|
|
|
|
|
|
let ty = match &body[pat] {
|
2020-11-24 12:41:21 +00:00
|
|
|
Pat::Tuple { ref args, ellipsis } => {
|
2019-10-12 15:39:20 +00:00
|
|
|
let expectations = match expected.as_tuple() {
|
|
|
|
Some(parameters) => &*parameters.0,
|
|
|
|
_ => &[],
|
|
|
|
};
|
|
|
|
|
2020-11-24 12:41:21 +00:00
|
|
|
let (pre, post) = match ellipsis {
|
|
|
|
&Some(idx) => args.split_at(idx),
|
|
|
|
None => (&args[..], &[][..]),
|
|
|
|
};
|
|
|
|
let uncovered_range = pre.len()..expectations.len().saturating_sub(post.len());
|
|
|
|
let mut expectations_iter = expectations.iter().chain(repeat(&Ty::Unknown));
|
|
|
|
let mut infer_pat = |(&pat, ty)| self.infer_pat(pat, ty, default_bm);
|
|
|
|
|
|
|
|
let mut inner_tys = Vec::with_capacity(expectations.len());
|
|
|
|
inner_tys.extend(pre.iter().zip(expectations_iter.by_ref()).map(&mut infer_pat));
|
|
|
|
inner_tys.extend(expectations_iter.by_ref().take(uncovered_range.len()).cloned());
|
|
|
|
inner_tys.extend(post.iter().zip(expectations_iter).map(infer_pat));
|
|
|
|
|
|
|
|
Ty::apply(
|
|
|
|
TypeCtor::Tuple { cardinality: inner_tys.len() as u16 },
|
|
|
|
Substs(inner_tys.into()),
|
|
|
|
)
|
2019-10-12 15:39:20 +00:00
|
|
|
}
|
2020-02-09 18:57:01 +00:00
|
|
|
Pat::Or(ref pats) => {
|
|
|
|
if let Some((first_pat, rest)) = pats.split_first() {
|
|
|
|
let ty = self.infer_pat(*first_pat, expected, default_bm);
|
|
|
|
for pat in rest {
|
|
|
|
self.infer_pat(*pat, expected, default_bm);
|
|
|
|
}
|
|
|
|
ty
|
|
|
|
} else {
|
|
|
|
Ty::Unknown
|
|
|
|
}
|
|
|
|
}
|
2019-10-12 15:39:20 +00:00
|
|
|
Pat::Ref { pat, mutability } => {
|
|
|
|
let expectation = match expected.as_reference() {
|
|
|
|
Some((inner_ty, exp_mut)) => {
|
|
|
|
if *mutability != exp_mut {
|
|
|
|
// FIXME: emit type error?
|
|
|
|
}
|
|
|
|
inner_ty
|
|
|
|
}
|
|
|
|
_ => &Ty::Unknown,
|
|
|
|
};
|
|
|
|
let subty = self.infer_pat(*pat, expectation, default_bm);
|
|
|
|
Ty::apply_one(TypeCtor::Ref(*mutability), subty)
|
|
|
|
}
|
2020-04-12 15:40:09 +00:00
|
|
|
Pat::TupleStruct { path: p, args: subpats, .. } => {
|
2020-03-24 11:40:58 +00:00
|
|
|
self.infer_tuple_struct_pat(p.as_ref(), subpats, expected, default_bm, pat)
|
2019-10-12 15:39:20 +00:00
|
|
|
}
|
2020-04-09 03:23:51 +00:00
|
|
|
Pat::Record { path: p, args: fields, ellipsis: _ } => {
|
2019-10-12 15:39:20 +00:00
|
|
|
self.infer_record_pat(p.as_ref(), fields, expected, default_bm, pat)
|
|
|
|
}
|
|
|
|
Pat::Path(path) => {
|
|
|
|
// FIXME use correct resolver for the surrounding expression
|
|
|
|
let resolver = self.resolver.clone();
|
|
|
|
self.infer_path(&resolver, &path, pat.into()).unwrap_or(Ty::Unknown)
|
|
|
|
}
|
|
|
|
Pat::Bind { mode, name: _, subpat } => {
|
|
|
|
let mode = if mode == &BindingAnnotation::Unannotated {
|
|
|
|
default_bm
|
|
|
|
} else {
|
|
|
|
BindingMode::convert(*mode)
|
|
|
|
};
|
|
|
|
let inner_ty = if let Some(subpat) = subpat {
|
|
|
|
self.infer_pat(*subpat, expected, default_bm)
|
|
|
|
} else {
|
|
|
|
expected.clone()
|
|
|
|
};
|
|
|
|
let inner_ty = self.insert_type_vars_shallow(inner_ty);
|
|
|
|
|
|
|
|
let bound_ty = match mode {
|
|
|
|
BindingMode::Ref(mutability) => {
|
|
|
|
Ty::apply_one(TypeCtor::Ref(mutability), inner_ty.clone())
|
|
|
|
}
|
|
|
|
BindingMode::Move => inner_ty.clone(),
|
|
|
|
};
|
2019-12-01 19:30:28 +00:00
|
|
|
let bound_ty = self.resolve_ty_as_possible(bound_ty);
|
2019-10-12 15:39:20 +00:00
|
|
|
self.write_pat_ty(pat, bound_ty);
|
|
|
|
return inner_ty;
|
|
|
|
}
|
2020-06-25 21:05:55 +00:00
|
|
|
Pat::Slice { prefix, slice, suffix } => {
|
2020-03-01 22:02:32 +00:00
|
|
|
let (container_ty, elem_ty) = match &expected {
|
2020-03-02 13:28:34 +00:00
|
|
|
ty_app!(TypeCtor::Array, st) => (TypeCtor::Array, st.as_single().clone()),
|
|
|
|
ty_app!(TypeCtor::Slice, st) => (TypeCtor::Slice, st.as_single().clone()),
|
2020-03-01 22:02:32 +00:00
|
|
|
_ => (TypeCtor::Slice, Ty::Unknown),
|
2020-03-01 20:13:05 +00:00
|
|
|
};
|
|
|
|
|
2020-03-01 22:02:32 +00:00
|
|
|
for pat_id in prefix.iter().chain(suffix) {
|
|
|
|
self.infer_pat(*pat_id, &elem_ty, default_bm);
|
|
|
|
}
|
|
|
|
|
2020-06-25 21:05:55 +00:00
|
|
|
let pat_ty = Ty::apply_one(container_ty, elem_ty);
|
|
|
|
if let Some(slice_pat_id) = slice {
|
|
|
|
self.infer_pat(*slice_pat_id, &pat_ty, default_bm);
|
|
|
|
}
|
|
|
|
|
|
|
|
pat_ty
|
2020-03-01 14:25:38 +00:00
|
|
|
}
|
2020-04-01 18:27:47 +00:00
|
|
|
Pat::Wild => expected.clone(),
|
|
|
|
Pat::Range { start, end } => {
|
|
|
|
let start_ty = self.infer_expr(*start, &Expectation::has_type(expected.clone()));
|
|
|
|
let end_ty = self.infer_expr(*end, &Expectation::has_type(start_ty));
|
|
|
|
end_ty
|
|
|
|
}
|
|
|
|
Pat::Lit(expr) => self.infer_expr(*expr, &Expectation::has_type(expected.clone())),
|
2020-09-12 19:18:57 +00:00
|
|
|
Pat::Box { inner } => match self.resolve_boxed_box() {
|
|
|
|
Some(box_adt) => {
|
|
|
|
let inner_expected = match expected.as_adt() {
|
|
|
|
Some((adt, substs)) if adt == box_adt => substs.as_single(),
|
|
|
|
_ => &Ty::Unknown,
|
|
|
|
};
|
|
|
|
|
|
|
|
let inner_ty = self.infer_pat(*inner, inner_expected, default_bm);
|
|
|
|
Ty::apply_one(TypeCtor::Adt(box_adt), inner_ty)
|
|
|
|
}
|
|
|
|
None => Ty::Unknown,
|
|
|
|
},
|
2020-04-01 18:27:47 +00:00
|
|
|
Pat::Missing => Ty::Unknown,
|
2019-10-12 15:39:20 +00:00
|
|
|
};
|
|
|
|
// use a new type variable if we got Ty::Unknown here
|
|
|
|
let ty = self.insert_type_vars_shallow(ty);
|
2020-02-21 15:56:34 +00:00
|
|
|
if !self.unify(&ty, expected) {
|
|
|
|
// FIXME record mismatch, we need to change the type of self.type_mismatches for that
|
|
|
|
}
|
2019-12-01 19:30:28 +00:00
|
|
|
let ty = self.resolve_ty_as_possible(ty);
|
2019-10-12 15:39:20 +00:00
|
|
|
self.write_pat_ty(pat, ty.clone());
|
|
|
|
ty
|
|
|
|
}
|
|
|
|
}
|
2020-06-24 09:57:28 +00:00
|
|
|
|
|
|
|
fn is_non_ref_pat(body: &hir_def::body::Body, pat: PatId) -> bool {
|
|
|
|
match &body[pat] {
|
|
|
|
Pat::Tuple { .. }
|
|
|
|
| Pat::TupleStruct { .. }
|
|
|
|
| Pat::Record { .. }
|
|
|
|
| Pat::Range { .. }
|
|
|
|
| Pat::Slice { .. } => true,
|
|
|
|
Pat::Or(pats) => pats.iter().all(|p| is_non_ref_pat(body, *p)),
|
|
|
|
// FIXME: Path/Lit might actually evaluate to ref, but inference is unimplemented.
|
|
|
|
Pat::Path(..) => true,
|
|
|
|
Pat::Lit(expr) => match body[*expr] {
|
|
|
|
Expr::Literal(Literal::String(..)) => false,
|
|
|
|
_ => true,
|
|
|
|
},
|
2020-09-12 19:18:57 +00:00
|
|
|
Pat::Wild | Pat::Bind { .. } | Pat::Ref { .. } | Pat::Box { .. } | Pat::Missing => false,
|
2020-06-24 09:57:28 +00:00
|
|
|
}
|
|
|
|
}
|