1 //! Path expression resolution.
6 path::{Path, PathSegment},
7 resolver::{ResolveValueResult, Resolver, TypeNs, ValueNs},
8 AdtId, AssocContainerId, AssocItemId, EnumVariantId, Lookup,
10 use hir_expand::name::Name;
13 method_resolution, to_chalk_trait_id, Interner, Substitution, Ty, TyKind, ValueTyDefId,
16 use super::{ExprOrPatId, InferenceContext, TraitRef};
18 impl<'a> InferenceContext<'a> {
19 pub(super) fn infer_path(
25 let ty = self.resolve_value_path(resolver, path, id)?;
26 let ty = self.insert_type_vars(ty);
27 let ty = self.normalize_associated_types_in(ty);
31 fn resolve_value_path(
37 let (value, self_subst) = if let Some(type_ref) = path.type_anchor() {
38 if path.segments().is_empty() {
39 // This can't actually happen syntax-wise
42 let ty = self.make_ty(type_ref);
43 let remaining_segments_for_ty = path.segments().take(path.segments().len() - 1);
44 let ctx = crate::lower::TyLoweringContext::new(self.db, &resolver);
45 let (ty, _) = ctx.lower_ty_relative_path(ty, None, remaining_segments_for_ty);
46 self.resolve_ty_assoc_item(
48 &path.segments().last().expect("path had at least one segment").name,
52 let value_or_partial =
53 resolver.resolve_path_in_value_ns(self.db.upcast(), path.mod_path())?;
55 match value_or_partial {
56 ResolveValueResult::ValueNs(it) => (it, None),
57 ResolveValueResult::Partial(def, remaining_index) => {
58 self.resolve_assoc_item(def, path, remaining_index, id)?
63 let typable: ValueTyDefId = match value {
64 ValueNs::LocalBinding(pat) => {
65 let ty = self.result.type_of_pat.get(pat)?.clone();
66 let ty = self.resolve_ty_as_possible(ty);
69 ValueNs::FunctionId(it) => it.into(),
70 ValueNs::ConstId(it) => it.into(),
71 ValueNs::StaticId(it) => it.into(),
72 ValueNs::StructId(it) => {
73 self.write_variant_resolution(id, it.into());
77 ValueNs::EnumVariantId(it) => {
78 self.write_variant_resolution(id, it.into());
82 ValueNs::ImplSelf(impl_id) => {
83 let generics = crate::utils::generics(self.db.upcast(), impl_id.into());
84 let substs = Substitution::type_params_for_generics(self.db, &generics);
85 let ty = self.db.impl_self_ty(impl_id).subst(&substs);
86 if let Some((AdtId::StructId(struct_id), substs)) = ty.as_adt() {
87 let ty = self.db.value_ty(struct_id.into()).subst(&substs);
90 // FIXME: diagnostic, invalid Self reference
94 ValueNs::GenericParam(it) => return Some(self.db.const_param_ty(it)),
97 let ty = self.db.value_ty(typable);
98 // self_subst is just for the parent
99 let parent_substs = self_subst.unwrap_or_else(Substitution::empty);
100 let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver);
101 let substs = ctx.substs_from_path(path, typable, true);
102 let full_substs = Substitution::builder(substs.len())
103 .use_parent_substs(&parent_substs)
104 .fill(substs.0[parent_substs.len()..].iter().cloned())
106 let ty = ty.subst(&full_substs);
110 fn resolve_assoc_item(
114 remaining_index: usize,
116 ) -> Option<(ValueNs, Option<Substitution>)> {
117 assert!(remaining_index < path.segments().len());
118 // there may be more intermediate segments between the resolved one and
119 // the end. Only the last segment needs to be resolved to a value; from
120 // the segments before that, we need to get either a type or a trait ref.
122 let resolved_segment = path.segments().get(remaining_index - 1).unwrap();
123 let remaining_segments = path.segments().skip(remaining_index);
124 let is_before_last = remaining_segments.len() == 1;
126 match (def, is_before_last) {
127 (TypeNs::TraitId(trait_), true) => {
129 remaining_segments.last().expect("there should be at least one segment here");
130 let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver);
132 ctx.lower_trait_ref_from_resolved_path(trait_, resolved_segment, None);
133 self.resolve_trait_assoc_item(trait_ref, segment, id)
136 // Either we already have a type (e.g. `Vec::new`), or we have a
137 // trait but it's not the last segment, so the next segment
138 // should resolve to an associated type of that trait (e.g. `<T
139 // as Iterator>::Item::default`)
140 let remaining_segments_for_ty =
141 remaining_segments.take(remaining_segments.len() - 1);
142 let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver);
143 let (ty, _) = ctx.lower_partly_resolved_path(
146 remaining_segments_for_ty,
149 if let TyKind::Unknown = ty.interned(&Interner) {
153 let ty = self.insert_type_vars(ty);
154 let ty = self.normalize_associated_types_in(ty);
157 remaining_segments.last().expect("there should be at least one segment here");
159 self.resolve_ty_assoc_item(ty, &segment.name, id)
164 fn resolve_trait_assoc_item(
167 segment: PathSegment<'_>,
169 ) -> Option<(ValueNs, Option<Substitution>)> {
170 let trait_ = trait_ref.hir_trait_id();
172 self.db.trait_data(trait_).items.iter().map(|(_name, id)| (*id)).find_map(|item| {
174 AssocItemId::FunctionId(func) => {
175 if segment.name == &self.db.function_data(func).name {
176 Some(AssocItemId::FunctionId(func))
182 AssocItemId::ConstId(konst) => {
188 .map_or(false, |n| n == segment.name)
190 Some(AssocItemId::ConstId(konst))
195 AssocItemId::TypeAliasId(_) => None,
198 let def = match item {
199 AssocItemId::FunctionId(f) => ValueNs::FunctionId(f),
200 AssocItemId::ConstId(c) => ValueNs::ConstId(c),
201 AssocItemId::TypeAliasId(_) => unreachable!(),
204 self.write_assoc_resolution(id, item);
205 Some((def, Some(trait_ref.substitution)))
208 fn resolve_ty_assoc_item(
213 ) -> Option<(ValueNs, Option<Substitution>)> {
214 if let TyKind::Unknown = ty.interned(&Interner) {
218 if let Some(result) = self.resolve_enum_variant_on_ty(&ty, name, id) {
222 let canonical_ty = self.canonicalizer().canonicalize_ty(ty.clone());
223 let krate = self.resolver.krate()?;
224 let traits_in_scope = self.resolver.traits_in_scope(self.db.upcast());
226 method_resolution::iterate_method_candidates(
229 self.trait_env.clone(),
233 method_resolution::LookupMode::Path,
235 let (def, container) = match item {
236 AssocItemId::FunctionId(f) => {
237 (ValueNs::FunctionId(f), f.lookup(self.db.upcast()).container)
239 AssocItemId::ConstId(c) => {
240 (ValueNs::ConstId(c), c.lookup(self.db.upcast()).container)
242 AssocItemId::TypeAliasId(_) => unreachable!(),
244 let substs = match container {
245 AssocContainerId::ImplId(impl_id) => {
246 let impl_substs = Substitution::build_for_def(self.db, impl_id)
247 .fill(iter::repeat_with(|| self.table.new_type_var()))
249 let impl_self_ty = self.db.impl_self_ty(impl_id).subst(&impl_substs);
250 self.unify(&impl_self_ty, &ty);
253 AssocContainerId::TraitId(trait_) => {
254 // we're picking this method
255 let trait_substs = Substitution::build_for_def(self.db, trait_)
257 .fill(std::iter::repeat_with(|| self.table.new_type_var()))
259 self.obligations.push(super::Obligation::Trait(TraitRef {
260 trait_id: to_chalk_trait_id(trait_),
261 substitution: trait_substs.clone(),
265 AssocContainerId::ModuleId(_) => None,
268 self.write_assoc_resolution(id, item);
274 fn resolve_enum_variant_on_ty(
279 ) -> Option<(ValueNs, Option<Substitution>)> {
280 let (enum_id, subst) = match ty.as_adt() {
281 Some((AdtId::EnumId(e), subst)) => (e, subst),
284 let enum_data = self.db.enum_data(enum_id);
285 let local_id = enum_data.variant(name)?;
286 let variant = EnumVariantId { parent: enum_id, local_id };
287 self.write_variant_resolution(id, variant.into());
288 Some((ValueNs::EnumVariantId(variant), Some(subst.clone())))