Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-db/src/path_transform.rs')
-rw-r--r--crates/ide-db/src/path_transform.rs40
1 files changed, 38 insertions, 2 deletions
diff --git a/crates/ide-db/src/path_transform.rs b/crates/ide-db/src/path_transform.rs
index fa9339f30f..fb4c0c1269 100644
--- a/crates/ide-db/src/path_transform.rs
+++ b/crates/ide-db/src/path_transform.rs
@@ -2,7 +2,7 @@
use crate::helpers::mod_path_to_ast;
use either::Either;
-use hir::{AsAssocItem, HirDisplay, SemanticsScope};
+use hir::{AsAssocItem, HirDisplay, ModuleDef, SemanticsScope};
use rustc_hash::FxHashMap;
use syntax::{
ast::{self, make, AstNode},
@@ -183,6 +183,7 @@ impl<'a> PathTransform<'a> {
lifetime_substs,
target_module,
source_scope: self.source_scope,
+ same_self_type: self.target_scope.has_same_self_type(self.source_scope),
};
ctx.transform_default_values(defaulted_params);
ctx
@@ -195,6 +196,7 @@ struct Ctx<'a> {
lifetime_substs: FxHashMap<LifetimeName, ast::Lifetime>,
target_module: hir::Module,
source_scope: &'a SemanticsScope<'a>,
+ same_self_type: bool,
}
fn postorder(item: &SyntaxNode) -> impl Iterator<Item = SyntaxNode> {
@@ -332,8 +334,42 @@ impl Ctx<'_> {
ted::replace(path.syntax(), subst.clone_subtree().clone_for_update());
}
}
+ hir::PathResolution::SelfType(imp) => {
+ // keep Self type if it does not need to be replaced
+ if self.same_self_type {
+ return None;
+ }
+
+ let ty = imp.self_ty(self.source_scope.db);
+ let ty_str = &ty
+ .display_source_code(
+ self.source_scope.db,
+ self.source_scope.module().into(),
+ true,
+ )
+ .ok()?;
+ let ast_ty = make::ty(&ty_str).clone_for_update();
+
+ if let Some(adt) = ty.as_adt() {
+ if let ast::Type::PathType(path_ty) = &ast_ty {
+ let found_path = self.target_module.find_use_path(
+ self.source_scope.db.upcast(),
+ ModuleDef::from(adt),
+ false,
+ true,
+ )?;
+
+ if let Some(qual) = mod_path_to_ast(&found_path).qualifier() {
+ let res = make::path_concat(qual, path_ty.path()?).clone_for_update();
+ ted::replace(path.syntax(), res.syntax());
+ return Some(());
+ }
+ }
+ }
+
+ ted::replace(path.syntax(), ast_ty.syntax());
+ }
hir::PathResolution::Local(_)
- | hir::PathResolution::SelfType(_)
| hir::PathResolution::Def(_)
| hir::PathResolution::BuiltinAttr(_)
| hir::PathResolution::ToolModule(_)