2019-09-23 16:53:52 +00:00
|
|
|
//! Path expression resolution.
|
|
|
|
|
2021-03-20 10:23:59 +00:00
|
|
|
use chalk_ir::cast::Cast;
|
2019-11-21 12:39:09 +00:00
|
|
|
use hir_def::{
|
2019-12-18 16:41:33 +00:00
|
|
|
path::{Path, PathSegment},
|
2019-11-27 19:12:09 +00:00
|
|
|
resolver::{ResolveValueResult, Resolver, TypeNs, ValueNs},
|
2021-12-07 16:31:26 +00:00
|
|
|
AdtId, AssocItemId, EnumVariantId, ItemContainerId, 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
|
|
|
|
2021-04-06 10:36:12 +00:00
|
|
|
use crate::{
|
2022-03-31 09:12:08 +00:00
|
|
|
builder::ParamKind,
|
|
|
|
consteval,
|
|
|
|
method_resolution::{self, VisibleFromModule},
|
|
|
|
GenericArgData, Interner, Substitution, TraitRefExt, Ty, TyBuilder, TyExt, TyKind,
|
|
|
|
ValueTyDefId,
|
2021-04-06 10:36:12 +00:00
|
|
|
};
|
2019-09-23 16:53:52 +00:00
|
|
|
|
2020-01-24 13:32:47 +00:00
|
|
|
use super::{ExprOrPatId, InferenceContext, TraitRef};
|
2019-11-21 12:39:09 +00:00
|
|
|
|
2020-03-13 15:05:46 +00:00
|
|
|
impl<'a> InferenceContext<'a> {
|
2019-09-23 16:53:52 +00:00
|
|
|
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-12-18 16:41:33 +00:00
|
|
|
let (value, self_subst) = if let Some(type_ref) = path.type_anchor() {
|
2019-12-13 11:12:36 +00:00
|
|
|
if path.segments().is_empty() {
|
2019-09-23 16:53:52 +00:00
|
|
|
// This can't actually happen syntax-wise
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let ty = self.make_ty(type_ref);
|
2019-12-13 11:12:36 +00:00
|
|
|
let remaining_segments_for_ty = path.segments().take(path.segments().len() - 1);
|
2021-06-13 03:54:16 +00:00
|
|
|
let ctx = crate::lower::TyLoweringContext::new(self.db, resolver);
|
2021-03-13 21:44:36 +00:00
|
|
|
let (ty, _) = ctx.lower_ty_relative_path(ty, None, remaining_segments_for_ty);
|
2019-09-23 16:53:52 +00:00
|
|
|
self.resolve_ty_assoc_item(
|
|
|
|
ty,
|
2021-06-13 03:54:16 +00:00
|
|
|
path.segments().last().expect("path had at least one segment").name,
|
2019-09-23 16:53:52 +00:00
|
|
|
id,
|
|
|
|
)?
|
|
|
|
} else {
|
2020-03-13 15:05:46 +00:00
|
|
|
let value_or_partial =
|
|
|
|
resolver.resolve_path_in_value_ns(self.db.upcast(), path.mod_path())?;
|
2019-09-23 16:53:52 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
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(),
|
2020-03-24 11:40:58 +00:00
|
|
|
ValueNs::StructId(it) => {
|
|
|
|
self.write_variant_resolution(id, it.into());
|
|
|
|
|
|
|
|
it.into()
|
|
|
|
}
|
|
|
|
ValueNs::EnumVariantId(it) => {
|
|
|
|
self.write_variant_resolution(id, it.into());
|
|
|
|
|
|
|
|
it.into()
|
|
|
|
}
|
2020-05-15 15:15:40 +00:00
|
|
|
ValueNs::ImplSelf(impl_id) => {
|
|
|
|
let generics = crate::utils::generics(self.db.upcast(), impl_id.into());
|
2022-03-09 18:50:24 +00:00
|
|
|
let substs = generics.placeholder_subst(self.db);
|
2021-12-19 16:58:39 +00:00
|
|
|
let ty = self.db.impl_self_ty(impl_id).substitute(Interner, &substs);
|
2020-06-19 18:33:04 +00:00
|
|
|
if let Some((AdtId::StructId(struct_id), substs)) = ty.as_adt() {
|
2021-12-19 16:58:39 +00:00
|
|
|
let ty = self.db.value_ty(struct_id.into()).substitute(Interner, &substs);
|
2020-05-15 15:15:40 +00:00
|
|
|
return Some(ty);
|
|
|
|
} else {
|
|
|
|
// FIXME: diagnostic, invalid Self reference
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
2021-01-01 09:06:42 +00:00
|
|
|
ValueNs::GenericParam(it) => return Some(self.db.const_param_ty(it)),
|
2019-09-23 16:53:52 +00:00
|
|
|
};
|
|
|
|
|
2021-12-19 16:58:39 +00:00
|
|
|
let parent_substs = self_subst.unwrap_or_else(|| Substitution::empty(Interner));
|
2020-01-25 22:38:33 +00:00
|
|
|
let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver);
|
2021-03-13 21:44:36 +00:00
|
|
|
let substs = ctx.substs_from_path(path, typable, true);
|
2022-03-09 18:50:24 +00:00
|
|
|
let mut it = substs.as_slice(Interner)[parent_substs.len(Interner)..].iter().cloned();
|
2021-04-04 10:48:10 +00:00
|
|
|
let ty = TyBuilder::value_ty(self.db, typable)
|
2020-01-25 22:38:33 +00:00
|
|
|
.use_parent_substs(&parent_substs)
|
2022-03-09 18:50:24 +00:00
|
|
|
.fill(|x| {
|
|
|
|
it.next().unwrap_or_else(|| match x {
|
|
|
|
ParamKind::Type => {
|
|
|
|
GenericArgData::Ty(TyKind::Error.intern(Interner)).intern(Interner)
|
|
|
|
}
|
2022-03-17 11:39:42 +00:00
|
|
|
ParamKind::Const(ty) => consteval::unknown_const_as_generic(ty.clone()),
|
2022-03-09 18:50:24 +00:00
|
|
|
})
|
|
|
|
})
|
2020-01-25 22:38:33 +00:00
|
|
|
.build();
|
2019-09-23 16:53:52 +00:00
|
|
|
Some(ty)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_assoc_item(
|
|
|
|
&mut self,
|
|
|
|
def: TypeNs,
|
|
|
|
path: &Path,
|
|
|
|
remaining_index: usize,
|
|
|
|
id: ExprOrPatId,
|
2021-03-15 20:02:34 +00:00
|
|
|
) -> Option<(ValueNs, Option<Substitution>)> {
|
2019-12-13 11:12:36 +00:00
|
|
|
assert!(remaining_index < path.segments().len());
|
2019-09-23 16:53:52 +00:00
|
|
|
// 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.
|
|
|
|
|
2019-12-13 11:12:36 +00:00
|
|
|
let resolved_segment = path.segments().get(remaining_index - 1).unwrap();
|
|
|
|
let remaining_segments = path.segments().skip(remaining_index);
|
2019-09-23 16:53:52 +00:00
|
|
|
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");
|
2020-01-25 22:38:33 +00:00
|
|
|
let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver);
|
2021-03-13 21:44:36 +00:00
|
|
|
let trait_ref =
|
|
|
|
ctx.lower_trait_ref_from_resolved_path(trait_, resolved_segment, None);
|
2019-09-25 19:41:17 +00:00
|
|
|
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`)
|
2019-12-13 11:12:36 +00:00
|
|
|
let remaining_segments_for_ty =
|
|
|
|
remaining_segments.take(remaining_segments.len() - 1);
|
2020-01-25 22:38:33 +00:00
|
|
|
let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver);
|
2021-03-13 21:44:36 +00:00
|
|
|
let (ty, _) = ctx.lower_partly_resolved_path(
|
2019-09-23 16:53:52 +00:00
|
|
|
def,
|
|
|
|
resolved_segment,
|
|
|
|
remaining_segments_for_ty,
|
2020-05-29 17:14:04 +00:00
|
|
|
true,
|
2019-09-23 16:53:52 +00:00
|
|
|
);
|
2021-12-19 16:58:39 +00:00
|
|
|
if let TyKind::Error = ty.kind(Interner) {
|
2019-09-23 16:53:52 +00:00
|
|
|
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");
|
|
|
|
|
2021-06-13 03:54:16 +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-12-13 11:12:36 +00:00
|
|
|
segment: PathSegment<'_>,
|
2019-09-25 19:41:17 +00:00
|
|
|
id: ExprOrPatId,
|
2021-03-15 20:02:34 +00:00
|
|
|
) -> Option<(ValueNs, Option<Substitution>)> {
|
2021-03-18 20:53:19 +00:00
|
|
|
let trait_ = trait_ref.hir_trait_id();
|
2020-02-18 12:53:02 +00:00
|
|
|
let item =
|
|
|
|
self.db.trait_data(trait_).items.iter().map(|(_name, id)| (*id)).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-09-25 19:41:17 +00:00
|
|
|
|
2020-02-18 12:53:02 +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
|
|
|
}
|
2020-02-18 12:53:02 +00:00
|
|
|
AssocItemId::TypeAliasId(_) => None,
|
2019-11-27 09:31:40 +00:00
|
|
|
}
|
|
|
|
})?;
|
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
|
|
|
};
|
|
|
|
|
|
|
|
self.write_assoc_resolution(id, item);
|
2021-03-18 20:53:19 +00:00
|
|
|
Some((def, Some(trait_ref.substitution)))
|
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,
|
2021-03-15 20:02:34 +00:00
|
|
|
) -> Option<(ValueNs, Option<Substitution>)> {
|
2021-12-19 16:58:39 +00:00
|
|
|
if let TyKind::Error = ty.kind(Interner) {
|
2019-09-23 16:53:52 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-05-15 15:15:40 +00:00
|
|
|
if let Some(result) = self.resolve_enum_variant_on_ty(&ty, name, id) {
|
|
|
|
return Some(result);
|
|
|
|
}
|
|
|
|
|
2021-04-11 09:20:45 +00:00
|
|
|
let canonical_ty = self.canonicalize(ty.clone());
|
2020-03-13 15:05:46 +00:00
|
|
|
let traits_in_scope = self.resolver.traits_in_scope(self.db.upcast());
|
2019-10-29 10:04:42 +00:00
|
|
|
|
2019-10-31 18:28:33 +00:00
|
|
|
method_resolution::iterate_method_candidates(
|
|
|
|
&canonical_ty.value,
|
|
|
|
self.db,
|
2021-05-16 13:50:28 +00:00
|
|
|
self.table.trait_env.clone(),
|
2020-01-14 13:20:33 +00:00
|
|
|
&traits_in_scope,
|
2022-03-31 09:12:08 +00:00
|
|
|
VisibleFromModule::Filter(self.resolver.module()),
|
2019-10-31 18:28:33 +00:00
|
|
|
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) => {
|
2020-03-13 15:05:46 +00:00
|
|
|
(ValueNs::FunctionId(f), f.lookup(self.db.upcast()).container)
|
|
|
|
}
|
|
|
|
AssocItemId::ConstId(c) => {
|
|
|
|
(ValueNs::ConstId(c), c.lookup(self.db.upcast()).container)
|
2019-11-27 09:31:40 +00:00
|
|
|
}
|
|
|
|
AssocItemId::TypeAliasId(_) => unreachable!(),
|
2019-10-31 18:28:33 +00:00
|
|
|
};
|
2019-11-27 09:31:40 +00:00
|
|
|
let substs = match container {
|
2021-12-07 16:31:26 +00:00
|
|
|
ItemContainerId::ImplId(impl_id) => {
|
2021-04-03 20:18:02 +00:00
|
|
|
let impl_substs = TyBuilder::subst_for_def(self.db, impl_id)
|
2022-03-17 11:39:42 +00:00
|
|
|
.fill_with_inference_vars(&mut self.table)
|
2019-12-03 13:59:29 +00:00
|
|
|
.build();
|
2021-04-05 16:49:26 +00:00
|
|
|
let impl_self_ty =
|
2021-12-19 16:58:39 +00:00
|
|
|
self.db.impl_self_ty(impl_id).substitute(Interner, &impl_substs);
|
2019-12-03 13:59:29 +00:00
|
|
|
self.unify(&impl_self_ty, &ty);
|
2020-01-25 22:38:33 +00:00
|
|
|
Some(impl_substs)
|
2019-12-02 17:12:49 +00:00
|
|
|
}
|
2021-12-07 16:31:26 +00:00
|
|
|
ItemContainerId::TraitId(trait_) => {
|
2019-10-29 10:04:42 +00:00
|
|
|
// we're picking this method
|
2021-04-03 19:50:52 +00:00
|
|
|
let trait_ref = TyBuilder::trait_ref(self.db, trait_)
|
2019-10-29 10:04:42 +00:00
|
|
|
.push(ty.clone())
|
2022-03-17 11:39:42 +00:00
|
|
|
.fill_with_inference_vars(&mut self.table)
|
2019-10-29 10:04:42 +00:00
|
|
|
.build();
|
2021-12-19 16:58:39 +00:00
|
|
|
self.push_obligation(trait_ref.clone().cast(Interner));
|
2021-04-03 19:50:52 +00:00
|
|
|
Some(trait_ref.substitution)
|
2019-10-29 10:04:42 +00:00
|
|
|
}
|
2021-12-07 16:31:26 +00:00
|
|
|
ItemContainerId::ModuleId(_) | ItemContainerId::ExternBlockId(_) => None,
|
2019-11-01 18:56:56 +00:00
|
|
|
};
|
|
|
|
|
2020-02-18 12:53:02 +00:00
|
|
|
self.write_assoc_resolution(id, item);
|
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
|
|
|
}
|
2020-05-15 15:15:40 +00:00
|
|
|
|
|
|
|
fn resolve_enum_variant_on_ty(
|
|
|
|
&mut self,
|
|
|
|
ty: &Ty,
|
|
|
|
name: &Name,
|
|
|
|
id: ExprOrPatId,
|
2021-03-15 20:02:34 +00:00
|
|
|
) -> Option<(ValueNs, Option<Substitution>)> {
|
2021-05-16 15:56:38 +00:00
|
|
|
let ty = self.resolve_ty_shallow(ty);
|
2020-05-15 15:15:40 +00:00
|
|
|
let (enum_id, subst) = match ty.as_adt() {
|
|
|
|
Some((AdtId::EnumId(e), subst)) => (e, subst),
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
let enum_data = self.db.enum_data(enum_id);
|
|
|
|
let local_id = enum_data.variant(name)?;
|
|
|
|
let variant = EnumVariantId { parent: enum_id, local_id };
|
|
|
|
self.write_variant_resolution(id, variant.into());
|
|
|
|
Some((ValueNs::EnumVariantId(variant), Some(subst.clone())))
|
|
|
|
}
|
2019-09-23 16:53:52 +00:00
|
|
|
}
|