use hir_def::{
expr::{BindingAnnotation, Expr, Literal, Pat, PatId, RecordFieldPat},
path::Path,
- FieldId,
};
use hir_expand::name::Name;
-use super::{BindingMode, Expectation, InferenceContext};
+use super::{BindingMode, Expectation, InferenceContext, TypeMismatch};
use crate::{
- lower::lower_to_chalk_mutability,
- utils::{generics, variant_data},
- Interner, Substitution, Ty, TyKind,
+ lower::lower_to_chalk_mutability, static_lifetime, Interner, Substitution, Ty, TyBuilder,
+ TyExt, TyKind,
};
impl<'a> InferenceContext<'a> {
ellipsis: Option<usize>,
) -> Ty {
let (ty, def) = self.resolve_variant(path);
- let var_data = def.map(|it| variant_data(self.db.upcast(), it));
+ let var_data = def.map(|it| it.variant_data(self.db.upcast()));
if let Some(variant) = def {
self.write_variant_resolution(id.into(), variant);
}
self.unify(&ty, expected);
- let substs = ty.substs().cloned().unwrap_or_else(|| Substitution::empty(&Interner));
+ let substs =
+ ty.as_adt().map(|(_, s)| s.clone()).unwrap_or_else(|| Substitution::empty(&Interner));
let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default();
let (pre, post) = match ellipsis {
let expected_ty = var_data
.as_ref()
.and_then(|d| d.field(&Name::new_tuple_field(i)))
- .map_or(self.err_ty(), |field| field_tys[field].clone().subst(&substs));
+ .map_or(self.err_ty(), |field| {
+ field_tys[field].clone().substitute(&Interner, &substs)
+ });
let expected_ty = self.normalize_associated_types_in(expected_ty);
self.infer_pat(subpat, &expected_ty, default_bm);
}
id: PatId,
) -> Ty {
let (ty, def) = self.resolve_variant(path);
- let var_data = def.map(|it| variant_data(self.db.upcast(), it));
+ let var_data = def.map(|it| it.variant_data(self.db.upcast()));
if let Some(variant) = def {
self.write_variant_resolution(id.into(), variant);
}
self.unify(&ty, expected);
- let substs = ty.substs().cloned().unwrap_or_else(|| Substitution::empty(&Interner));
+ let substs =
+ ty.as_adt().map(|(_, s)| s.clone()).unwrap_or_else(|| Substitution::empty(&Interner));
let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default();
for subpat in subpats {
let matching_field = var_data.as_ref().and_then(|it| it.field(&subpat.name));
- if let Some(local_id) = matching_field {
- let field_def = FieldId { parent: def.unwrap(), local_id };
- self.result.record_pat_field_resolutions.insert(subpat.pat, field_def);
- }
-
- let expected_ty = matching_field
- .map_or(self.err_ty(), |field| field_tys[field].clone().subst(&substs));
+ let expected_ty = matching_field.map_or(self.err_ty(), |field| {
+ field_tys[field].clone().substitute(&Interner, &substs)
+ });
let expected_ty = self.normalize_associated_types_in(expected_ty);
self.infer_pat(subpat.pat, &expected_ty, default_bm);
}
pub(super) fn infer_pat(
&mut self,
pat: PatId,
- mut expected: &Ty,
+ expected: &Ty,
mut default_bm: BindingMode,
) -> Ty {
let body = Arc::clone(&self.body); // avoid borrow checker problem
+ let mut expected = self.resolve_ty_shallow(expected);
if is_non_ref_pat(&body, pat) {
- while let Some((inner, mutability)) = expected.as_reference() {
- expected = inner;
+ while let Some((inner, _lifetime, mutability)) = expected.as_reference() {
+ expected = self.resolve_ty_shallow(inner);
default_bm = match default_bm {
BindingMode::Move => BindingMode::Ref(mutability),
BindingMode::Ref(Mutability::Not) => BindingMode::Ref(Mutability::Not),
let ty = match &body[pat] {
&Pat::Tuple { ref args, ellipsis } => {
let expectations = match expected.as_tuple() {
- Some(parameters) => &*parameters.0,
+ Some(parameters) => &*parameters.as_slice(&Interner),
_ => &[],
};
- let (pre, post) = match ellipsis {
- Some(idx) => args.split_at(idx),
- None => (&args[..], &[][..]),
+ let ((pre, post), n_uncovered_patterns) = match ellipsis {
+ Some(idx) => {
+ (args.split_at(idx), expectations.len().saturating_sub(args.len()))
+ }
+ None => ((&args[..], &[][..]), 0),
};
- let n_uncovered_patterns = expectations.len().saturating_sub(args.len());
let err_ty = self.err_ty();
let mut expectations_iter =
expectations.iter().map(|a| a.assert_ty_ref(&Interner)).chain(repeat(&err_ty));
}
Pat::Or(ref pats) => {
if let Some((first_pat, rest)) = pats.split_first() {
- let ty = self.infer_pat(*first_pat, expected, default_bm);
+ let ty = self.infer_pat(*first_pat, &expected, default_bm);
for pat in rest {
- self.infer_pat(*pat, expected, default_bm);
+ self.infer_pat(*pat, &expected, default_bm);
}
ty
} else {
Pat::Ref { pat, mutability } => {
let mutability = lower_to_chalk_mutability(*mutability);
let expectation = match expected.as_reference() {
- Some((inner_ty, exp_mut)) => {
+ Some((inner_ty, _lifetime, exp_mut)) => {
if mutability != exp_mut {
// FIXME: emit type error?
}
_ => self.result.standard_types.unknown.clone(),
};
let subty = self.infer_pat(*pat, &expectation, default_bm);
- TyKind::Ref(mutability, subty).intern(&Interner)
+ TyKind::Ref(mutability, static_lifetime(), subty).intern(&Interner)
}
Pat::TupleStruct { path: p, args: subpats, ellipsis } => self.infer_tuple_struct_pat(
- p.as_ref(),
+ p.as_deref(),
subpats,
- expected,
+ &expected,
default_bm,
pat,
*ellipsis,
),
Pat::Record { path: p, args: fields, ellipsis: _ } => {
- self.infer_record_pat(p.as_ref(), fields, expected, default_bm, pat)
+ self.infer_record_pat(p.as_deref(), fields, &expected, default_bm, pat)
}
Pat::Path(path) => {
// FIXME use correct resolver for the surrounding expression
BindingMode::convert(*mode)
};
let inner_ty = if let Some(subpat) = subpat {
- self.infer_pat(*subpat, expected, default_bm)
+ self.infer_pat(*subpat, &expected, default_bm)
} else {
- expected.clone()
+ expected
};
let inner_ty = self.insert_type_vars_shallow(inner_ty);
let bound_ty = match mode {
BindingMode::Ref(mutability) => {
- TyKind::Ref(mutability, inner_ty.clone()).intern(&Interner)
+ TyKind::Ref(mutability, static_lifetime(), inner_ty.clone())
+ .intern(&Interner)
}
BindingMode::Move => inner_ty.clone(),
};
- let bound_ty = self.resolve_ty_as_possible(bound_ty);
self.write_pat_ty(pat, bound_ty);
return inner_ty;
}
Pat::Slice { prefix, slice, suffix } => {
- let (container_ty, elem_ty): (fn(_) -> _, _) = match expected.kind(&Interner) {
- TyKind::Array(st) => (TyKind::Array, st.clone()),
- TyKind::Slice(st) => (TyKind::Slice, st.clone()),
- _ => (TyKind::Slice, self.err_ty()),
+ let elem_ty = match expected.kind(&Interner) {
+ TyKind::Array(st, _) | TyKind::Slice(st) => st.clone(),
+ _ => self.err_ty(),
};
for pat_id in prefix.iter().chain(suffix) {
self.infer_pat(*pat_id, &elem_ty, default_bm);
}
- let pat_ty = container_ty(elem_ty).intern(&Interner);
+ let pat_ty = match expected.kind(&Interner) {
+ TyKind::Array(_, const_) => TyKind::Array(elem_ty, const_.clone()),
+ _ => TyKind::Slice(elem_ty),
+ }
+ .intern(&Interner);
if let Some(slice_pat_id) = slice {
self.infer_pat(*slice_pat_id, &pat_ty, default_bm);
}
let (inner_ty, alloc_ty) = match expected.as_adt() {
Some((adt, subst)) if adt == box_adt => (
subst.at(&Interner, 0).assert_ty_ref(&Interner).clone(),
- subst.interned(&Interner).get(1).and_then(|a| a.ty(&Interner).cloned()),
+ subst.as_slice(&Interner).get(1).and_then(|a| a.ty(&Interner).cloned()),
),
_ => (self.result.standard_types.unknown.clone(), None),
};
let inner_ty = self.infer_pat(*inner, &inner_ty, default_bm);
- let mut sb = Substitution::build_for_generics(&generics(
- self.db.upcast(),
- box_adt.into(),
- ));
- sb = sb.push(inner_ty);
- if sb.remaining() == 1 {
- sb = sb.push(match alloc_ty {
- Some(alloc_ty) if !alloc_ty.is_unknown() => alloc_ty,
- _ => match self.db.generic_defaults(box_adt.into()).get(1) {
- Some(alloc_ty) if !alloc_ty.value.is_unknown() => {
- alloc_ty.value.clone()
- }
- _ => self.table.new_type_var(),
- },
- });
+ let mut b = TyBuilder::adt(self.db, box_adt).push(inner_ty);
+
+ if let Some(alloc_ty) = alloc_ty {
+ b = b.push(alloc_ty);
}
- Ty::adt_ty(box_adt, sb.build())
+ b.fill_with_defaults(self.db, || self.table.new_type_var()).build()
}
None => self.err_ty(),
},
};
// use a new type variable if we got error type here
let ty = self.insert_type_vars_shallow(ty);
- if !self.unify(&ty, expected) {
- // FIXME record mismatch, we need to change the type of self.type_mismatches for that
+ if !self.unify(&ty, &expected) {
+ self.result
+ .type_mismatches
+ .insert(pat.into(), TypeMismatch { expected: expected, actual: ty.clone() });
}
- let ty = self.resolve_ty_as_possible(ty);
self.write_pat_ty(pat, ty.clone());
ty
}