2019-09-23 16:53:52 +00:00
|
|
|
//! Path expression resolution.
|
|
|
|
|
2019-12-03 13:59:29 +00:00
|
|
|
use std::iter;
|
|
|
|
|
2019-11-21 12:39:09 +00:00
|
|
|
use hir_def::{
|
2019-11-27 14:46:02 +00:00
|
|
|
path::{Path, PathKind, PathSegment},
|
2019-11-27 19:12:09 +00:00
|
|
|
resolver::{ResolveValueResult, Resolver, TypeNs, ValueNs},
|
2019-11-27 09:31:40 +00:00
|
|
|
AssocItemId, ContainerId, Lookup,
|
2019-11-21 12:39:09 +00:00
|
|
|
};
|
2019-11-27 09:13:07 +00:00
|
|
|
use hir_expand::name::Name;
|
2019-10-30 14:24:36 +00:00
|
|
|
|
2019-11-27 14:46:02 +00:00
|
|
|
use crate::{db::HirDatabase, method_resolution, Substs, Ty, TypeWalk, ValueTyDefId};
|
2019-09-23 16:53:52 +00:00
|
|
|
|
2019-11-21 12:39:09 +00:00
|
|
|
use super::{ExprOrPatId, InferenceContext, TraitRef};
|
|
|
|
|
2019-09-23 16:53:52 +00:00
|
|
|
impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
|
|
|
pub(super) fn infer_path(
|
|
|
|
&mut self,
|
|
|
|
resolver: &Resolver,
|
|
|
|
path: &Path,
|
|
|
|
id: ExprOrPatId,
|
|
|
|
) -> Option<Ty> {
|
|
|
|
let ty = self.resolve_value_path(resolver, path, id)?;
|
|
|
|
let ty = self.insert_type_vars(ty);
|
|
|
|
let ty = self.normalize_associated_types_in(ty);
|
|
|
|
Some(ty)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_value_path(
|
|
|
|
&mut self,
|
|
|
|
resolver: &Resolver,
|
|
|
|
path: &Path,
|
|
|
|
id: ExprOrPatId,
|
|
|
|
) -> Option<Ty> {
|
2019-11-27 14:46:02 +00:00
|
|
|
let (value, self_subst) = if let PathKind::Type(type_ref) = &path.kind {
|
2019-09-23 16:53:52 +00:00
|
|
|
if path.segments.is_empty() {
|
|
|
|
// This can't actually happen syntax-wise
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let ty = self.make_ty(type_ref);
|
|
|
|
let remaining_segments_for_ty = &path.segments[..path.segments.len() - 1];
|
|
|
|
let ty = Ty::from_type_relative_path(self.db, resolver, ty, remaining_segments_for_ty);
|
|
|
|
self.resolve_ty_assoc_item(
|
|
|
|
ty,
|
2019-10-29 10:04:42 +00:00
|
|
|
&path.segments.last().expect("path had at least one segment").name,
|
2019-09-23 16:53:52 +00:00
|
|
|
id,
|
|
|
|
)?
|
|
|
|
} else {
|
|
|
|
let value_or_partial = resolver.resolve_path_in_value_ns(self.db, &path)?;
|
|
|
|
|
|
|
|
match value_or_partial {
|
|
|
|
ResolveValueResult::ValueNs(it) => (it, None),
|
|
|
|
ResolveValueResult::Partial(def, remaining_index) => {
|
|
|
|
self.resolve_assoc_item(def, path, remaining_index, id)?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-11-26 18:04:24 +00:00
|
|
|
let typable: ValueTyDefId = match value {
|
2019-09-23 16:53:52 +00:00
|
|
|
ValueNs::LocalBinding(pat) => {
|
|
|
|
let ty = self.result.type_of_pat.get(pat)?.clone();
|
2019-12-01 19:30:28 +00:00
|
|
|
let ty = self.resolve_ty_as_possible(ty);
|
2019-09-23 16:53:52 +00:00
|
|
|
return Some(ty);
|
|
|
|
}
|
2019-11-21 10:32:03 +00:00
|
|
|
ValueNs::FunctionId(it) => it.into(),
|
|
|
|
ValueNs::ConstId(it) => it.into(),
|
|
|
|
ValueNs::StaticId(it) => it.into(),
|
|
|
|
ValueNs::StructId(it) => it.into(),
|
|
|
|
ValueNs::EnumVariantId(it) => it.into(),
|
2019-09-23 16:53:52 +00:00
|
|
|
};
|
|
|
|
|
2019-11-26 18:04:24 +00:00
|
|
|
let mut ty = self.db.value_ty(typable);
|
2019-09-23 16:53:52 +00:00
|
|
|
if let Some(self_subst) = self_subst {
|
|
|
|
ty = ty.subst(&self_subst);
|
|
|
|
}
|
|
|
|
let substs = Ty::substs_from_path(self.db, &self.resolver, path, typable);
|
|
|
|
let ty = ty.subst(&substs);
|
|
|
|
Some(ty)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_assoc_item(
|
|
|
|
&mut self,
|
|
|
|
def: TypeNs,
|
|
|
|
path: &Path,
|
|
|
|
remaining_index: usize,
|
|
|
|
id: ExprOrPatId,
|
|
|
|
) -> Option<(ValueNs, Option<Substs>)> {
|
|
|
|
assert!(remaining_index < path.segments.len());
|
|
|
|
// there may be more intermediate segments between the resolved one and
|
|
|
|
// the end. Only the last segment needs to be resolved to a value; from
|
|
|
|
// the segments before that, we need to get either a type or a trait ref.
|
|
|
|
|
|
|
|
let resolved_segment = &path.segments[remaining_index - 1];
|
|
|
|
let remaining_segments = &path.segments[remaining_index..];
|
|
|
|
let is_before_last = remaining_segments.len() == 1;
|
|
|
|
|
|
|
|
match (def, is_before_last) {
|
2019-11-21 09:21:46 +00:00
|
|
|
(TypeNs::TraitId(trait_), true) => {
|
2019-09-25 19:41:17 +00:00
|
|
|
let segment =
|
|
|
|
remaining_segments.last().expect("there should be at least one segment here");
|
|
|
|
let trait_ref = TraitRef::from_resolved_path(
|
|
|
|
self.db,
|
|
|
|
&self.resolver,
|
2019-11-21 09:21:46 +00:00
|
|
|
trait_.into(),
|
2019-09-25 19:41:17 +00:00
|
|
|
resolved_segment,
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
self.resolve_trait_assoc_item(trait_ref, segment, id)
|
2019-09-23 16:53:52 +00:00
|
|
|
}
|
|
|
|
(def, _) => {
|
|
|
|
// Either we already have a type (e.g. `Vec::new`), or we have a
|
|
|
|
// trait but it's not the last segment, so the next segment
|
|
|
|
// should resolve to an associated type of that trait (e.g. `<T
|
|
|
|
// as Iterator>::Item::default`)
|
|
|
|
let remaining_segments_for_ty = &remaining_segments[..remaining_segments.len() - 1];
|
|
|
|
let ty = Ty::from_partly_resolved_hir_path(
|
|
|
|
self.db,
|
|
|
|
&self.resolver,
|
|
|
|
def,
|
|
|
|
resolved_segment,
|
|
|
|
remaining_segments_for_ty,
|
|
|
|
);
|
|
|
|
if let Ty::Unknown = ty {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2019-10-30 20:22:46 +00:00
|
|
|
let ty = self.insert_type_vars(ty);
|
|
|
|
let ty = self.normalize_associated_types_in(ty);
|
|
|
|
|
2019-09-23 16:53:52 +00:00
|
|
|
let segment =
|
|
|
|
remaining_segments.last().expect("there should be at least one segment here");
|
|
|
|
|
2019-10-29 10:04:42 +00:00
|
|
|
self.resolve_ty_assoc_item(ty, &segment.name, id)
|
2019-09-23 16:53:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-25 19:41:17 +00:00
|
|
|
fn resolve_trait_assoc_item(
|
|
|
|
&mut self,
|
|
|
|
trait_ref: TraitRef,
|
2019-10-30 14:24:36 +00:00
|
|
|
segment: &PathSegment,
|
2019-09-25 19:41:17 +00:00
|
|
|
id: ExprOrPatId,
|
|
|
|
) -> Option<(ValueNs, Option<Substs>)> {
|
|
|
|
let trait_ = trait_ref.trait_;
|
2019-11-27 09:31:40 +00:00
|
|
|
let item = self
|
|
|
|
.db
|
|
|
|
.trait_data(trait_)
|
|
|
|
.items
|
|
|
|
.iter()
|
|
|
|
.map(|(_name, id)| (*id).into())
|
|
|
|
.find_map(|item| match item {
|
|
|
|
AssocItemId::FunctionId(func) => {
|
|
|
|
if segment.name == self.db.function_data(func).name {
|
|
|
|
Some(AssocItemId::FunctionId(func))
|
|
|
|
} else {
|
|
|
|
None
|
2019-11-26 15:00:36 +00:00
|
|
|
}
|
2019-11-27 09:31:40 +00:00
|
|
|
}
|
2019-09-25 19:41:17 +00:00
|
|
|
|
2019-11-27 09:31:40 +00:00
|
|
|
AssocItemId::ConstId(konst) => {
|
|
|
|
if self.db.const_data(konst).name.as_ref().map_or(false, |n| n == &segment.name)
|
|
|
|
{
|
|
|
|
Some(AssocItemId::ConstId(konst))
|
|
|
|
} else {
|
|
|
|
None
|
2019-11-26 15:00:36 +00:00
|
|
|
}
|
2019-11-27 09:31:40 +00:00
|
|
|
}
|
|
|
|
AssocItemId::TypeAliasId(_) => None,
|
|
|
|
})?;
|
2019-09-25 19:41:17 +00:00
|
|
|
let def = match item {
|
2019-11-27 09:31:40 +00:00
|
|
|
AssocItemId::FunctionId(f) => ValueNs::FunctionId(f),
|
|
|
|
AssocItemId::ConstId(c) => ValueNs::ConstId(c),
|
|
|
|
AssocItemId::TypeAliasId(_) => unreachable!(),
|
2019-09-25 19:41:17 +00:00
|
|
|
};
|
2019-09-26 19:37:03 +00:00
|
|
|
let substs = Substs::build_for_def(self.db, item)
|
|
|
|
.use_parent_substs(&trait_ref.substs)
|
2019-10-29 10:04:42 +00:00
|
|
|
.fill_with_params()
|
2019-09-26 19:37:03 +00:00
|
|
|
.build();
|
2019-09-25 19:41:17 +00:00
|
|
|
|
|
|
|
self.write_assoc_resolution(id, item);
|
2019-09-26 19:37:03 +00:00
|
|
|
Some((def, Some(substs)))
|
2019-09-25 19:41:17 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 16:53:52 +00:00
|
|
|
fn resolve_ty_assoc_item(
|
|
|
|
&mut self,
|
|
|
|
ty: Ty,
|
2019-10-29 10:04:42 +00:00
|
|
|
name: &Name,
|
2019-09-23 16:53:52 +00:00
|
|
|
id: ExprOrPatId,
|
|
|
|
) -> Option<(ValueNs, Option<Substs>)> {
|
|
|
|
if let Ty::Unknown = ty {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2019-10-29 10:04:42 +00:00
|
|
|
let canonical_ty = self.canonicalizer().canonicalize_ty(ty.clone());
|
|
|
|
|
2019-10-31 18:28:33 +00:00
|
|
|
method_resolution::iterate_method_candidates(
|
|
|
|
&canonical_ty.value,
|
|
|
|
self.db,
|
|
|
|
&self.resolver.clone(),
|
|
|
|
Some(name),
|
|
|
|
method_resolution::LookupMode::Path,
|
|
|
|
move |_ty, item| {
|
2019-11-27 09:31:40 +00:00
|
|
|
let (def, container) = match item {
|
|
|
|
AssocItemId::FunctionId(f) => {
|
|
|
|
(ValueNs::FunctionId(f), f.lookup(self.db).container)
|
|
|
|
}
|
|
|
|
AssocItemId::ConstId(c) => (ValueNs::ConstId(c), c.lookup(self.db).container),
|
|
|
|
AssocItemId::TypeAliasId(_) => unreachable!(),
|
2019-10-31 18:28:33 +00:00
|
|
|
};
|
2019-11-27 09:31:40 +00:00
|
|
|
let substs = match container {
|
2019-12-02 17:12:49 +00:00
|
|
|
ContainerId::ImplId(impl_id) => {
|
2019-12-03 13:59:29 +00:00
|
|
|
let impl_substs = Substs::build_for_def(self.db, impl_id)
|
|
|
|
.fill(iter::repeat_with(|| self.table.new_type_var()))
|
|
|
|
.build();
|
|
|
|
let impl_self_ty = self.db.impl_self_ty(impl_id).subst(&impl_substs);
|
|
|
|
let substs = Substs::build_for_def(self.db, item)
|
|
|
|
.use_parent_substs(&impl_substs)
|
|
|
|
.fill_with_params()
|
|
|
|
.build();
|
|
|
|
self.unify(&impl_self_ty, &ty);
|
|
|
|
Some(substs)
|
2019-12-02 17:12:49 +00:00
|
|
|
}
|
2019-11-27 09:31:40 +00:00
|
|
|
ContainerId::TraitId(trait_) => {
|
2019-10-29 10:04:42 +00:00
|
|
|
// we're picking this method
|
2019-11-27 09:31:40 +00:00
|
|
|
let trait_substs = Substs::build_for_def(self.db, trait_)
|
2019-10-29 10:04:42 +00:00
|
|
|
.push(ty.clone())
|
2019-12-01 19:30:28 +00:00
|
|
|
.fill(std::iter::repeat_with(|| self.table.new_type_var()))
|
2019-10-29 10:04:42 +00:00
|
|
|
.build();
|
2019-10-31 18:28:33 +00:00
|
|
|
let substs = Substs::build_for_def(self.db, item)
|
2019-10-29 10:04:42 +00:00
|
|
|
.use_parent_substs(&trait_substs)
|
|
|
|
.fill_with_params()
|
|
|
|
.build();
|
|
|
|
self.obligations.push(super::Obligation::Trait(TraitRef {
|
2019-11-27 09:31:40 +00:00
|
|
|
trait_,
|
2019-10-29 10:04:42 +00:00
|
|
|
substs: trait_substs,
|
|
|
|
}));
|
2019-11-01 18:56:56 +00:00
|
|
|
Some(substs)
|
2019-10-29 10:04:42 +00:00
|
|
|
}
|
2019-11-27 09:31:40 +00:00
|
|
|
ContainerId::ModuleId(_) => None,
|
2019-11-01 18:56:56 +00:00
|
|
|
};
|
|
|
|
|
2019-11-27 09:31:40 +00:00
|
|
|
self.write_assoc_resolution(id, item.into());
|
2019-11-01 18:56:56 +00:00
|
|
|
Some((def, substs))
|
2019-10-31 18:28:33 +00:00
|
|
|
},
|
|
|
|
)
|
2019-10-29 10:04:42 +00:00
|
|
|
}
|
2019-09-23 16:53:52 +00:00
|
|
|
}
|