From c35ef5013c3223986ae5111a3720ef8e85c80efc Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Wed, 25 Sep 2019 21:41:17 +0200 Subject: Resolve trait associated items E.g. `Default::default` or `::default`. --- crates/ra_hir/src/ty/infer/path.rs | 55 +++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 4 deletions(-) (limited to 'crates/ra_hir/src/ty/infer/path.rs') diff --git a/crates/ra_hir/src/ty/infer/path.rs b/crates/ra_hir/src/ty/infer/path.rs index 54aae4f0c..feb7481b2 100644 --- a/crates/ra_hir/src/ty/infer/path.rs +++ b/crates/ra_hir/src/ty/infer/path.rs @@ -1,6 +1,6 @@ //! Path expression resolution. -use super::{ExprOrPatId, InferenceContext}; +use super::{ExprOrPatId, InferenceContext, TraitRef}; use crate::{ db::HirDatabase, resolve::{ResolveValueResult, Resolver, TypeNs, ValueNs}, @@ -91,9 +91,17 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { let is_before_last = remaining_segments.len() == 1; match (def, is_before_last) { - (TypeNs::Trait(_trait), true) => { - // FIXME Associated item of trait, e.g. `Default::default` - None + (TypeNs::Trait(trait_), true) => { + 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, + trait_, + resolved_segment, + None, + ); + self.resolve_trait_assoc_item(trait_ref, segment, id) } (def, _) => { // Either we already have a type (e.g. `Vec::new`), or we have a @@ -120,6 +128,45 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } } + fn resolve_trait_assoc_item( + &mut self, + trait_ref: TraitRef, + segment: &crate::path::PathSegment, + id: ExprOrPatId, + ) -> Option<(ValueNs, Option)> { + let trait_ = trait_ref.trait_; + let item = trait_.items(self.db).iter().copied().find_map(|item| match item { + AssocItem::Function(func) => { + if segment.name == func.name(self.db) { + Some(AssocItem::Function(func)) + } else { + None + } + } + + AssocItem::Const(konst) => { + if konst.name(self.db).map_or(false, |n| n == segment.name) { + Some(AssocItem::Const(konst)) + } else { + None + } + } + AssocItem::TypeAlias(_) => None, + })?; + let def = match item { + AssocItem::Function(f) => ValueNs::Function(f), + AssocItem::Const(c) => ValueNs::Const(c), + AssocItem::TypeAlias(_) => unreachable!(), + }; + let generics = item.generic_params(self.db); + let mut substs = Vec::with_capacity(generics.count_params_including_parent()); + substs.extend(trait_ref.substs.iter().cloned()); + substs.extend(std::iter::repeat(Ty::Unknown).take(generics.params.len())); + + self.write_assoc_resolution(id, item); + Some((def, Some(substs.into()))) + } + fn resolve_ty_assoc_item( &mut self, ty: Ty, -- cgit v1.2.3