aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir')
-rw-r--r--crates/ra_hir/src/code_model.rs1
-rw-r--r--crates/ra_hir/src/source_analyzer.rs60
2 files changed, 6 insertions, 55 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index cf3b04511..a56b8ab04 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -696,7 +696,6 @@ impl AssocItem {
696 AssocItem::TypeAlias(t) => t.module(db), 696 AssocItem::TypeAlias(t) => t.module(db),
697 } 697 }
698 } 698 }
699
700 pub fn container(self, db: &impl DefDatabase) -> AssocItemContainer { 699 pub fn container(self, db: &impl DefDatabase) -> AssocItemContainer {
701 let container = match self { 700 let container = match self {
702 AssocItem::Function(it) => it.id.lookup(db).container, 701 AssocItem::Function(it) => it.id.lookup(db).container,
diff --git a/crates/ra_hir/src/source_analyzer.rs b/crates/ra_hir/src/source_analyzer.rs
index 49e1a10e0..bb9a35c5d 100644
--- a/crates/ra_hir/src/source_analyzer.rs
+++ b/crates/ra_hir/src/source_analyzer.rs
@@ -20,10 +20,7 @@ use hir_def::{
20use hir_expand::{ 20use hir_expand::{
21 hygiene::Hygiene, name::AsName, AstId, HirFileId, InFile, MacroCallId, MacroCallKind, 21 hygiene::Hygiene, name::AsName, AstId, HirFileId, InFile, MacroCallId, MacroCallKind,
22}; 22};
23use hir_ty::{ 23use hir_ty::{InEnvironment, InferenceResult, TraitEnvironment};
24 method_resolution::{iterate_method_candidates, LookupMode},
25 Canonical, InEnvironment, InferenceResult, TraitEnvironment,
26};
27use ra_syntax::{ 24use ra_syntax::{
28 ast::{self, AstNode}, 25 ast::{self, AstNode},
29 AstPtr, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextRange, TextUnit, 26 AstPtr, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextRange, TextUnit,
@@ -31,8 +28,8 @@ use ra_syntax::{
31use rustc_hash::FxHashSet; 28use rustc_hash::FxHashSet;
32 29
33use crate::{ 30use crate::{
34 db::HirDatabase, Adt, AssocItem, Const, DefWithBody, EnumVariant, Function, Local, MacroDef, 31 db::HirDatabase, Adt, Const, DefWithBody, EnumVariant, Function, Local, MacroDef, Name, Path,
35 ModuleDef, Name, Path, ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, 32 ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam,
36}; 33};
37 34
38/// `SourceAnalyzer` is a convenience wrapper which exposes HIR API in terms of 35/// `SourceAnalyzer` is a convenience wrapper which exposes HIR API in terms of
@@ -292,11 +289,9 @@ impl SourceAnalyzer {
292 289
293 pub fn resolve_path(&self, db: &impl HirDatabase, path: &ast::Path) -> Option<PathResolution> { 290 pub fn resolve_path(&self, db: &impl HirDatabase, path: &ast::Path) -> Option<PathResolution> {
294 if let Some(path_expr) = path.syntax().parent().and_then(ast::PathExpr::cast) { 291 if let Some(path_expr) = path.syntax().parent().and_then(ast::PathExpr::cast) {
295 let path_resolution = self 292 let expr_id = self.expr_id(&path_expr.into())?;
296 .resolve_as_full_path(path_expr.clone()) 293 if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_expr(expr_id) {
297 .or_else(|| self.resolve_as_path_to_method(db, &path_expr)); 294 return Some(PathResolution::AssocItem(assoc.into()));
298 if path_resolution.is_some() {
299 return path_resolution;
300 } 295 }
301 } 296 }
302 if let Some(path_pat) = path.syntax().parent().and_then(ast::PathPat::cast) { 297 if let Some(path_pat) = path.syntax().parent().and_then(ast::PathPat::cast) {
@@ -310,49 +305,6 @@ impl SourceAnalyzer {
310 self.resolve_hir_path(db, &hir_path) 305 self.resolve_hir_path(db, &hir_path)
311 } 306 }
312 307
313 fn resolve_as_full_path(&self, path_expr: ast::PathExpr) -> Option<PathResolution> {
314 let expr_id = self.expr_id(&path_expr.into())?;
315 self.infer
316 .as_ref()?
317 .assoc_resolutions_for_expr(expr_id)
318 .map(|assoc| PathResolution::AssocItem(assoc.into()))
319 }
320
321 fn resolve_as_path_to_method(
322 &self,
323 db: &impl HirDatabase,
324 path_expr: &ast::PathExpr,
325 ) -> Option<PathResolution> {
326 let full_path = path_expr.path()?;
327 let path_to_method = full_path.qualifier()?;
328 let method_name = full_path.segment()?.syntax().to_string();
329 match self.resolve_path(db, &path_to_method)? {
330 PathResolution::Def(ModuleDef::Adt(adt)) => {
331 let ty = adt.ty(db);
332 iterate_method_candidates(
333 &Canonical { value: ty.ty.value, num_vars: 0 },
334 db,
335 ty.ty.environment,
336 self.resolver.krate()?,
337 &self.resolver.traits_in_scope(db),
338 None,
339 LookupMode::Path,
340 |_, assoc_item_id| {
341 let assoc = assoc_item_id.into();
342 if let AssocItem::Function(function) = assoc {
343 if function.name(db).to_string() == method_name {
344 return Some(assoc);
345 }
346 }
347 None
348 },
349 )
350 }
351 _ => None,
352 }
353 .map(PathResolution::AssocItem)
354 }
355
356 fn resolve_local_name(&self, name_ref: &ast::NameRef) -> Option<ScopeEntryWithSyntax> { 308 fn resolve_local_name(&self, name_ref: &ast::NameRef) -> Option<ScopeEntryWithSyntax> {
357 let name = name_ref.as_name(); 309 let name = name_ref.as_name();
358 let source_map = self.body_source_map.as_ref()?; 310 let source_map = self.body_source_map.as_ref()?;