aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-03-30 17:45:05 +0100
committerGitHub <[email protected]>2021-03-30 17:45:05 +0100
commitfd7c454d516e3af90d8c0d0d8a22655345fec2d7 (patch)
tree6e5292a731c7039d92c636bc66a27714845ffb1c
parent277db63a08d9910ffbcf1eded723c176941feff1 (diff)
parentb4bb7743810b1e6ac8b897a465c430e9ed8d5c44 (diff)
Merge #8186
8186: Lower traits to `TraitRef` instead of `TypeRef` r=matklad a=Veykril Co-authored-by: Lukas Wirth <[email protected]>
-rw-r--r--crates/hir/src/lib.rs13
-rw-r--r--crates/hir_def/src/data.rs10
-rw-r--r--crates/hir_def/src/item_tree.rs43
-rw-r--r--crates/hir_def/src/item_tree/lower.rs17
-rw-r--r--crates/hir_def/src/type_ref.rs17
-rw-r--r--crates/hir_ty/src/lower.rs12
-rw-r--r--crates/ide/src/doc_links.rs2
-rw-r--r--crates/ide/src/hover.rs6
-rw-r--r--crates/ide/src/references/rename.rs2
-rw-r--r--crates/ide/src/runnables.rs2
-rw-r--r--crates/ide_assists/src/handlers/generate_default_from_new.rs2
-rw-r--r--crates/ide_assists/src/handlers/generate_is_empty_from_len.rs2
-rw-r--r--crates/ide_assists/src/utils.rs4
-rw-r--r--crates/ide_completion/src/completions.rs2
-rw-r--r--crates/ide_completion/src/completions/pattern.rs2
-rw-r--r--crates/ide_completion/src/completions/qualified_path.rs2
-rw-r--r--crates/ide_db/src/helpers/import_assets.rs2
17 files changed, 100 insertions, 40 deletions
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index 4ee08ef21..97f162315 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -44,6 +44,7 @@ use hir_def::{
44 per_ns::PerNs, 44 per_ns::PerNs,
45 resolver::{HasResolver, Resolver}, 45 resolver::{HasResolver, Resolver},
46 src::HasSource as _, 46 src::HasSource as _,
47 type_ref::TraitRef,
47 AdtId, AssocContainerId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, 48 AdtId, AssocContainerId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId,
48 DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, ImplId, LifetimeParamId, 49 DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, ImplId, LifetimeParamId,
49 LocalEnumVariantId, LocalFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId, 50 LocalEnumVariantId, LocalFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId,
@@ -1573,9 +1574,9 @@ impl Impl {
1573 }; 1574 };
1574 1575
1575 let filter = |impl_def: &Impl| { 1576 let filter = |impl_def: &Impl| {
1576 let target_ty = impl_def.target_ty(db); 1577 let self_ty = impl_def.self_ty(db);
1577 let rref = target_ty.remove_ref(); 1578 let rref = self_ty.remove_ref();
1578 ty.equals_ctor(rref.as_ref().map_or(&target_ty.ty, |it| &it.ty)) 1579 ty.equals_ctor(rref.as_ref().map_or(&self_ty.ty, |it| &it.ty))
1579 }; 1580 };
1580 1581
1581 let mut all = Vec::new(); 1582 let mut all = Vec::new();
@@ -1613,16 +1614,16 @@ impl Impl {
1613 1614
1614 // FIXME: the return type is wrong. This should be a hir version of 1615 // FIXME: the return type is wrong. This should be a hir version of
1615 // `TraitRef` (ie, resolved `TypeRef`). 1616 // `TraitRef` (ie, resolved `TypeRef`).
1616 pub fn target_trait(self, db: &dyn HirDatabase) -> Option<TypeRef> { 1617 pub fn trait_(self, db: &dyn HirDatabase) -> Option<TraitRef> {
1617 db.impl_data(self.id).target_trait.clone() 1618 db.impl_data(self.id).target_trait.clone()
1618 } 1619 }
1619 1620
1620 pub fn target_ty(self, db: &dyn HirDatabase) -> Type { 1621 pub fn self_ty(self, db: &dyn HirDatabase) -> Type {
1621 let impl_data = db.impl_data(self.id); 1622 let impl_data = db.impl_data(self.id);
1622 let resolver = self.id.resolver(db.upcast()); 1623 let resolver = self.id.resolver(db.upcast());
1623 let krate = self.id.lookup(db.upcast()).container.krate(); 1624 let krate = self.id.lookup(db.upcast()).container.krate();
1624 let ctx = hir_ty::TyLoweringContext::new(db, &resolver); 1625 let ctx = hir_ty::TyLoweringContext::new(db, &resolver);
1625 let ty = ctx.lower_ty(&impl_data.target_type); 1626 let ty = ctx.lower_ty(&impl_data.self_ty);
1626 Type::new_with_resolver_inner(db, krate, &resolver, ty) 1627 Type::new_with_resolver_inner(db, krate, &resolver, ty)
1627 } 1628 }
1628 1629
diff --git a/crates/hir_def/src/data.rs b/crates/hir_def/src/data.rs
index 0be868ba2..214bcc648 100644
--- a/crates/hir_def/src/data.rs
+++ b/crates/hir_def/src/data.rs
@@ -10,7 +10,7 @@ use crate::{
10 body::Expander, 10 body::Expander,
11 db::DefDatabase, 11 db::DefDatabase,
12 item_tree::{AssocItem, FunctionQualifier, ItemTreeId, ModItem, Param}, 12 item_tree::{AssocItem, FunctionQualifier, ItemTreeId, ModItem, Param},
13 type_ref::{TypeBound, TypeRef}, 13 type_ref::{TraitRef, TypeBound, TypeRef},
14 visibility::RawVisibility, 14 visibility::RawVisibility,
15 AssocContainerId, AssocItemId, ConstId, ConstLoc, FunctionId, FunctionLoc, HasModule, ImplId, 15 AssocContainerId, AssocItemId, ConstId, ConstLoc, FunctionId, FunctionLoc, HasModule, ImplId,
16 Intern, Lookup, ModuleId, StaticId, TraitId, TypeAliasId, TypeAliasLoc, 16 Intern, Lookup, ModuleId, StaticId, TraitId, TypeAliasId, TypeAliasLoc,
@@ -156,8 +156,8 @@ impl TraitData {
156 156
157#[derive(Debug, Clone, PartialEq, Eq)] 157#[derive(Debug, Clone, PartialEq, Eq)]
158pub struct ImplData { 158pub struct ImplData {
159 pub target_trait: Option<TypeRef>, 159 pub target_trait: Option<TraitRef>,
160 pub target_type: TypeRef, 160 pub self_ty: TypeRef,
161 pub items: Vec<AssocItemId>, 161 pub items: Vec<AssocItemId>,
162 pub is_negative: bool, 162 pub is_negative: bool,
163} 163}
@@ -170,7 +170,7 @@ impl ImplData {
170 let item_tree = impl_loc.id.item_tree(db); 170 let item_tree = impl_loc.id.item_tree(db);
171 let impl_def = &item_tree[impl_loc.id.value]; 171 let impl_def = &item_tree[impl_loc.id.value];
172 let target_trait = impl_def.target_trait.map(|id| item_tree[id].clone()); 172 let target_trait = impl_def.target_trait.map(|id| item_tree[id].clone());
173 let target_type = item_tree[impl_def.target_type].clone(); 173 let self_ty = item_tree[impl_def.self_ty].clone();
174 let is_negative = impl_def.is_negative; 174 let is_negative = impl_def.is_negative;
175 let module_id = impl_loc.container; 175 let module_id = impl_loc.container;
176 let container = AssocContainerId::ImplId(id); 176 let container = AssocContainerId::ImplId(id);
@@ -187,7 +187,7 @@ impl ImplData {
187 ); 187 );
188 let items = items.into_iter().map(|(_, item)| item).collect(); 188 let items = items.into_iter().map(|(_, item)| item).collect();
189 189
190 Arc::new(ImplData { target_trait, target_type, items, is_negative }) 190 Arc::new(ImplData { target_trait, self_ty, items, is_negative })
191 } 191 }
192} 192}
193 193
diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs
index ca0048b16..5449bbf5d 100644
--- a/crates/hir_def/src/item_tree.rs
+++ b/crates/hir_def/src/item_tree.rs
@@ -31,7 +31,7 @@ use crate::{
31 db::DefDatabase, 31 db::DefDatabase,
32 generics::GenericParams, 32 generics::GenericParams,
33 path::{path, AssociatedTypeBinding, GenericArgs, ImportAlias, ModPath, Path, PathKind}, 33 path::{path, AssociatedTypeBinding, GenericArgs, ImportAlias, ModPath, Path, PathKind},
34 type_ref::{Mutability, TypeBound, TypeRef}, 34 type_ref::{Mutability, TraitRef, TypeBound, TypeRef},
35 visibility::RawVisibility, 35 visibility::RawVisibility,
36}; 36};
37 37
@@ -147,6 +147,7 @@ impl ItemTree {
147 vis, 147 vis,
148 generics, 148 generics,
149 type_refs, 149 type_refs,
150 trait_refs,
150 inner_items, 151 inner_items,
151 } = &mut **data; 152 } = &mut **data;
152 153
@@ -173,6 +174,7 @@ impl ItemTree {
173 generics.arena.shrink_to_fit(); 174 generics.arena.shrink_to_fit();
174 type_refs.arena.shrink_to_fit(); 175 type_refs.arena.shrink_to_fit();
175 type_refs.map.shrink_to_fit(); 176 type_refs.map.shrink_to_fit();
177 trait_refs.map.shrink_to_fit();
176 178
177 inner_items.shrink_to_fit(); 179 inner_items.shrink_to_fit();
178 } 180 }
@@ -295,6 +297,32 @@ impl TypeRefStorage {
295 } 297 }
296} 298}
297 299
300/// `TraitRef` interner.
301#[derive(Default, Debug, Eq, PartialEq)]
302struct TraitRefStorage {
303 arena: Arena<Arc<TraitRef>>,
304 map: FxHashMap<Arc<TraitRef>, Idx<Arc<TraitRef>>>,
305}
306
307impl TraitRefStorage {
308 // Note: We lie about the `Idx<TraitRef>` to hide the interner details.
309
310 fn intern(&mut self, ty: TraitRef) -> Idx<TraitRef> {
311 if let Some(id) = self.map.get(&ty) {
312 return Idx::from_raw(id.into_raw());
313 }
314
315 let ty = Arc::new(ty);
316 let idx = self.arena.alloc(ty.clone());
317 self.map.insert(ty, idx);
318 Idx::from_raw(idx.into_raw())
319 }
320
321 fn lookup(&self, id: Idx<TraitRef>) -> &TraitRef {
322 &self.arena[Idx::from_raw(id.into_raw())]
323 }
324}
325
298#[derive(Default, Debug, Eq, PartialEq)] 326#[derive(Default, Debug, Eq, PartialEq)]
299struct ItemTreeData { 327struct ItemTreeData {
300 imports: Arena<Import>, 328 imports: Arena<Import>,
@@ -319,6 +347,7 @@ struct ItemTreeData {
319 vis: ItemVisibilities, 347 vis: ItemVisibilities,
320 generics: GenericParamsStorage, 348 generics: GenericParamsStorage,
321 type_refs: TypeRefStorage, 349 type_refs: TypeRefStorage,
350 trait_refs: TraitRefStorage,
322 351
323 inner_items: FxHashMap<FileAstId<ast::BlockExpr>, SmallVec<[ModItem; 1]>>, 352 inner_items: FxHashMap<FileAstId<ast::BlockExpr>, SmallVec<[ModItem; 1]>>,
324} 353}
@@ -556,6 +585,14 @@ impl Index<Idx<TypeRef>> for ItemTree {
556 } 585 }
557} 586}
558 587
588impl Index<Idx<TraitRef>> for ItemTree {
589 type Output = TraitRef;
590
591 fn index(&self, id: Idx<TraitRef>) -> &Self::Output {
592 self.data().trait_refs.lookup(id)
593 }
594}
595
559impl<N: ItemTreeNode> Index<FileItemTreeId<N>> for ItemTree { 596impl<N: ItemTreeNode> Index<FileItemTreeId<N>> for ItemTree {
560 type Output = N; 597 type Output = N;
561 fn index(&self, id: FileItemTreeId<N>) -> &N { 598 fn index(&self, id: FileItemTreeId<N>) -> &N {
@@ -692,8 +729,8 @@ pub struct Trait {
692#[derive(Debug, Clone, Eq, PartialEq)] 729#[derive(Debug, Clone, Eq, PartialEq)]
693pub struct Impl { 730pub struct Impl {
694 pub generic_params: GenericParamsId, 731 pub generic_params: GenericParamsId,
695 pub target_trait: Option<Idx<TypeRef>>, 732 pub target_trait: Option<Idx<TraitRef>>,
696 pub target_type: Idx<TypeRef>, 733 pub self_ty: Idx<TypeRef>,
697 pub is_negative: bool, 734 pub is_negative: bool,
698 pub items: Box<[AssocItem]>, 735 pub items: Box<[AssocItem]>,
699 pub ast_id: FileAstId<ast::Impl>, 736 pub ast_id: FileAstId<ast::Impl>,
diff --git a/crates/hir_def/src/item_tree/lower.rs b/crates/hir_def/src/item_tree/lower.rs
index 3f558edd8..8d3862811 100644
--- a/crates/hir_def/src/item_tree/lower.rs
+++ b/crates/hir_def/src/item_tree/lower.rs
@@ -11,7 +11,7 @@ use syntax::{
11 11
12use crate::{ 12use crate::{
13 generics::{GenericParams, TypeParamData, TypeParamProvenance}, 13 generics::{GenericParams, TypeParamData, TypeParamProvenance},
14 type_ref::LifetimeRef, 14 type_ref::{LifetimeRef, TraitRef},
15}; 15};
16 16
17use super::*; 17use super::*;
@@ -536,8 +536,11 @@ impl Ctx {
536 fn lower_impl(&mut self, impl_def: &ast::Impl) -> Option<FileItemTreeId<Impl>> { 536 fn lower_impl(&mut self, impl_def: &ast::Impl) -> Option<FileItemTreeId<Impl>> {
537 let generic_params = 537 let generic_params =
538 self.lower_generic_params_and_inner_items(GenericsOwner::Impl, impl_def); 538 self.lower_generic_params_and_inner_items(GenericsOwner::Impl, impl_def);
539 let target_trait = impl_def.trait_().map(|tr| self.lower_type_ref(&tr)); 539 // FIXME: If trait lowering fails, due to a non PathType for example, we treat this impl
540 let target_type = self.lower_type_ref(&impl_def.self_ty()?); 540 // as if it was an non-trait impl. Ideally we want to create a unique missing ref that only
541 // equals itself.
542 let target_trait = impl_def.trait_().and_then(|tr| self.lower_trait_ref(&tr));
543 let self_ty = self.lower_type_ref(&impl_def.self_ty()?);
541 let is_negative = impl_def.excl_token().is_some(); 544 let is_negative = impl_def.excl_token().is_some();
542 545
543 // We cannot use `assoc_items()` here as that does not include macro calls. 546 // We cannot use `assoc_items()` here as that does not include macro calls.
@@ -554,7 +557,7 @@ impl Ctx {
554 }) 557 })
555 .collect(); 558 .collect();
556 let ast_id = self.source_ast_id_map.ast_id(impl_def); 559 let ast_id = self.source_ast_id_map.ast_id(impl_def);
557 let res = Impl { generic_params, target_trait, target_type, is_negative, items, ast_id }; 560 let res = Impl { generic_params, target_trait, self_ty, is_negative, items, ast_id };
558 Some(id(self.data().impls.alloc(res))) 561 Some(id(self.data().impls.alloc(res)))
559 } 562 }
560 563
@@ -740,10 +743,16 @@ impl Ctx {
740 self.data().vis.alloc(vis) 743 self.data().vis.alloc(vis)
741 } 744 }
742 745
746 fn lower_trait_ref(&mut self, trait_ref: &ast::Type) -> Option<Idx<TraitRef>> {
747 let trait_ref = TraitRef::from_ast(&self.body_ctx, trait_ref.clone())?;
748 Some(self.data().trait_refs.intern(trait_ref))
749 }
750
743 fn lower_type_ref(&mut self, type_ref: &ast::Type) -> Idx<TypeRef> { 751 fn lower_type_ref(&mut self, type_ref: &ast::Type) -> Idx<TypeRef> {
744 let tyref = TypeRef::from_ast(&self.body_ctx, type_ref.clone()); 752 let tyref = TypeRef::from_ast(&self.body_ctx, type_ref.clone());
745 self.data().type_refs.intern(tyref) 753 self.data().type_refs.intern(tyref)
746 } 754 }
755
747 fn lower_type_ref_opt(&mut self, type_ref: Option<ast::Type>) -> Idx<TypeRef> { 756 fn lower_type_ref_opt(&mut self, type_ref: Option<ast::Type>) -> Idx<TypeRef> {
748 match type_ref.map(|ty| self.lower_type_ref(&ty)) { 757 match type_ref.map(|ty| self.lower_type_ref(&ty)) {
749 Some(it) => it, 758 Some(it) => it,
diff --git a/crates/hir_def/src/type_ref.rs b/crates/hir_def/src/type_ref.rs
index 049b2e462..4c24aae94 100644
--- a/crates/hir_def/src/type_ref.rs
+++ b/crates/hir_def/src/type_ref.rs
@@ -51,6 +51,23 @@ impl Rawness {
51 } 51 }
52} 52}
53 53
54#[derive(Clone, PartialEq, Eq, Hash, Debug)]
55pub struct TraitRef {
56 pub path: Path,
57}
58
59impl TraitRef {
60 /// Converts an `ast::PathType` to a `hir::TraitRef`.
61 pub(crate) fn from_ast(ctx: &LowerCtx, node: ast::Type) -> Option<Self> {
62 // FIXME: Use `Path::from_src`
63 match node {
64 ast::Type::PathType(path) => {
65 path.path().and_then(|it| ctx.lower_path(it)).map(|path| TraitRef { path })
66 }
67 _ => None,
68 }
69 }
70}
54/// Compare ty::Ty 71/// Compare ty::Ty
55#[derive(Clone, PartialEq, Eq, Hash, Debug)] 72#[derive(Clone, PartialEq, Eq, Hash, Debug)]
56pub enum TypeRef { 73pub enum TypeRef {
diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs
index c87789d45..afbfa12d5 100644
--- a/crates/hir_ty/src/lower.rs
+++ b/crates/hir_ty/src/lower.rs
@@ -15,7 +15,7 @@ use hir_def::{
15 generics::{TypeParamProvenance, WherePredicate, WherePredicateTypeTarget}, 15 generics::{TypeParamProvenance, WherePredicate, WherePredicateTypeTarget},
16 path::{GenericArg, Path, PathSegment, PathSegments}, 16 path::{GenericArg, Path, PathSegment, PathSegments},
17 resolver::{HasResolver, Resolver, TypeNs}, 17 resolver::{HasResolver, Resolver, TypeNs},
18 type_ref::{TypeBound, TypeRef}, 18 type_ref::{TraitRef as HirTraitRef, TypeBound, TypeRef},
19 AdtId, AssocContainerId, AssocItemId, ConstId, ConstParamId, EnumId, EnumVariantId, FunctionId, 19 AdtId, AssocContainerId, AssocItemId, ConstId, ConstParamId, EnumId, EnumVariantId, FunctionId,
20 GenericDefId, HasModule, ImplId, LocalFieldId, Lookup, StaticId, StructId, TraitId, 20 GenericDefId, HasModule, ImplId, LocalFieldId, Lookup, StaticId, StructId, TraitId,
21 TypeAliasId, TypeParamId, UnionId, VariantId, 21 TypeAliasId, TypeParamId, UnionId, VariantId,
@@ -667,14 +667,10 @@ impl<'a> TyLoweringContext<'a> {
667 667
668 fn lower_trait_ref( 668 fn lower_trait_ref(
669 &self, 669 &self,
670 type_ref: &TypeRef, 670 trait_ref: &HirTraitRef,
671 explicit_self_ty: Option<Ty>, 671 explicit_self_ty: Option<Ty>,
672 ) -> Option<TraitRef> { 672 ) -> Option<TraitRef> {
673 let path = match type_ref { 673 self.lower_trait_ref_from_path(&trait_ref.path, explicit_self_ty)
674 TypeRef::Path(path) => path,
675 _ => return None,
676 };
677 self.lower_trait_ref_from_path(path, explicit_self_ty)
678 } 674 }
679 675
680 fn trait_ref_substs_from_path( 676 fn trait_ref_substs_from_path(
@@ -1253,7 +1249,7 @@ pub(crate) fn impl_self_ty_query(db: &dyn HirDatabase, impl_id: ImplId) -> Binde
1253 let generics = generics(db.upcast(), impl_id.into()); 1249 let generics = generics(db.upcast(), impl_id.into());
1254 let ctx = 1250 let ctx =
1255 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); 1251 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable);
1256 Binders::new(generics.len(), ctx.lower_ty(&impl_data.target_type)) 1252 Binders::new(generics.len(), ctx.lower_ty(&impl_data.self_ty))
1257} 1253}
1258 1254
1259pub(crate) fn const_param_ty_query(db: &dyn HirDatabase, def: ConstParamId) -> Ty { 1255pub(crate) fn const_param_ty_query(db: &dyn HirDatabase, def: ConstParamId) -> Ty {
diff --git a/crates/ide/src/doc_links.rs b/crates/ide/src/doc_links.rs
index 99276168f..67e2e5a1c 100644
--- a/crates/ide/src/doc_links.rs
+++ b/crates/ide/src/doc_links.rs
@@ -237,7 +237,7 @@ fn get_doc_link(db: &RootDatabase, definition: Definition) -> Option<String> {
237 .and_then(|assoc| match assoc.container(db) { 237 .and_then(|assoc| match assoc.container(db) {
238 AssocItemContainer::Trait(t) => Some(t.into()), 238 AssocItemContainer::Trait(t) => Some(t.into()),
239 AssocItemContainer::Impl(impld) => { 239 AssocItemContainer::Impl(impld) => {
240 impld.target_ty(db).as_adt().map(|adt| adt.into()) 240 impld.self_ty(db).as_adt().map(|adt| adt.into())
241 } 241 }
242 }) 242 })
243 .unwrap_or_else(|| f.clone().into()), 243 .unwrap_or_else(|| f.clone().into()),
diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs
index 02a1a5b37..7e35a1450 100644
--- a/crates/ide/src/hover.rs
+++ b/crates/ide/src/hover.rs
@@ -195,7 +195,7 @@ fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<Hov
195 let adt = match def { 195 let adt = match def {
196 Definition::ModuleDef(ModuleDef::Trait(it)) => return it.try_to_nav(db).map(to_action), 196 Definition::ModuleDef(ModuleDef::Trait(it)) => return it.try_to_nav(db).map(to_action),
197 Definition::ModuleDef(ModuleDef::Adt(it)) => Some(it), 197 Definition::ModuleDef(ModuleDef::Adt(it)) => Some(it),
198 Definition::SelfType(it) => it.target_ty(db).as_adt(), 198 Definition::SelfType(it) => it.self_ty(db).as_adt(),
199 _ => None, 199 _ => None,
200 }?; 200 }?;
201 adt.try_to_nav(db).map(to_action) 201 adt.try_to_nav(db).map(to_action)
@@ -318,7 +318,7 @@ fn definition_owner_name(db: &RootDatabase, def: &Definition) -> Option<String>
318 Definition::ModuleDef(md) => match md { 318 Definition::ModuleDef(md) => match md {
319 ModuleDef::Function(f) => match f.as_assoc_item(db)?.container(db) { 319 ModuleDef::Function(f) => match f.as_assoc_item(db)?.container(db) {
320 AssocItemContainer::Trait(t) => Some(t.name(db)), 320 AssocItemContainer::Trait(t) => Some(t.name(db)),
321 AssocItemContainer::Impl(i) => i.target_ty(db).as_adt().map(|adt| adt.name(db)), 321 AssocItemContainer::Impl(i) => i.self_ty(db).as_adt().map(|adt| adt.name(db)),
322 }, 322 },
323 ModuleDef::Variant(e) => Some(e.parent_enum(db).name(db)), 323 ModuleDef::Variant(e) => Some(e.parent_enum(db).name(db)),
324 _ => None, 324 _ => None,
@@ -376,7 +376,7 @@ fn hover_for_definition(
376 }, 376 },
377 Definition::Local(it) => hover_for_local(it, db), 377 Definition::Local(it) => hover_for_local(it, db),
378 Definition::SelfType(impl_def) => { 378 Definition::SelfType(impl_def) => {
379 impl_def.target_ty(db).as_adt().and_then(|adt| from_hir_fmt(db, adt, mod_path)) 379 impl_def.self_ty(db).as_adt().and_then(|adt| from_hir_fmt(db, adt, mod_path))
380 } 380 }
381 Definition::GenericParam(it) => from_hir_fmt(db, it, None), 381 Definition::GenericParam(it) => from_hir_fmt(db, it, None),
382 Definition::Label(it) => Some(Markup::fenced_block(&it.name(db))), 382 Definition::Label(it) => Some(Markup::fenced_block(&it.name(db))),
diff --git a/crates/ide/src/references/rename.rs b/crates/ide/src/references/rename.rs
index 26d6dc9c9..98456967a 100644
--- a/crates/ide/src/references/rename.rs
+++ b/crates/ide/src/references/rename.rs
@@ -307,7 +307,7 @@ fn rename_to_self(sema: &Semantics<RootDatabase>, local: hir::Local) -> RenameRe
307 hir::AssocItemContainer::Impl(impl_) => impl_, 307 hir::AssocItemContainer::Impl(impl_) => impl_,
308 }; 308 };
309 let first_param_ty = first_param.ty(); 309 let first_param_ty = first_param.ty();
310 let impl_ty = impl_.target_ty(sema.db); 310 let impl_ty = impl_.self_ty(sema.db);
311 let (ty, self_param) = if impl_ty.remove_ref().is_some() { 311 let (ty, self_param) = if impl_ty.remove_ref().is_some() {
312 // if the impl is a ref to the type we can just match the `&T` with self directly 312 // if the impl is a ref to the type we can just match the `&T` with self directly
313 (first_param_ty.clone(), "self") 313 (first_param_ty.clone(), "self")
diff --git a/crates/ide/src/runnables.rs b/crates/ide/src/runnables.rs
index 7e4c5a078..11bd385bb 100644
--- a/crates/ide/src/runnables.rs
+++ b/crates/ide/src/runnables.rs
@@ -298,7 +298,7 @@ fn module_def_doctest(sema: &Semantics<RootDatabase>, def: hir::ModuleDef) -> Op
298 // FIXME: this also looks very wrong 298 // FIXME: this also looks very wrong
299 if let Some(assoc_def) = assoc_def { 299 if let Some(assoc_def) = assoc_def {
300 if let hir::AssocItemContainer::Impl(imp) = assoc_def.container(sema.db) { 300 if let hir::AssocItemContainer::Impl(imp) = assoc_def.container(sema.db) {
301 let ty = imp.target_ty(sema.db); 301 let ty = imp.self_ty(sema.db);
302 if let Some(adt) = ty.as_adt() { 302 if let Some(adt) = ty.as_adt() {
303 let name = adt.name(sema.db); 303 let name = adt.name(sema.db);
304 let idx = path.rfind(':').map_or(0, |idx| idx + 1); 304 let idx = path.rfind(':').map_or(0, |idx| idx + 1);
diff --git a/crates/ide_assists/src/handlers/generate_default_from_new.rs b/crates/ide_assists/src/handlers/generate_default_from_new.rs
index 81c54ba3e..dc14552d6 100644
--- a/crates/ide_assists/src/handlers/generate_default_from_new.rs
+++ b/crates/ide_assists/src/handlers/generate_default_from_new.rs
@@ -92,7 +92,7 @@ fn is_default_implemented(ctx: &AssistContext, impl_: &Impl) -> bool {
92 None => return false, 92 None => return false,
93 }; 93 };
94 94
95 let ty = impl_def.target_ty(db); 95 let ty = impl_def.self_ty(db);
96 let krate = impl_def.module(db).krate(); 96 let krate = impl_def.module(db).krate();
97 let default = FamousDefs(&ctx.sema, Some(krate)).core_default_Default(); 97 let default = FamousDefs(&ctx.sema, Some(krate)).core_default_Default();
98 let default_trait = match default { 98 let default_trait = match default {
diff --git a/crates/ide_assists/src/handlers/generate_is_empty_from_len.rs b/crates/ide_assists/src/handlers/generate_is_empty_from_len.rs
index b8834d283..910010a04 100644
--- a/crates/ide_assists/src/handlers/generate_is_empty_from_len.rs
+++ b/crates/ide_assists/src/handlers/generate_is_empty_from_len.rs
@@ -91,7 +91,7 @@ fn get_impl_method(
91 91
92 let scope = ctx.sema.scope(impl_.syntax()); 92 let scope = ctx.sema.scope(impl_.syntax());
93 let krate = impl_def.module(db).krate(); 93 let krate = impl_def.module(db).krate();
94 let ty = impl_def.target_ty(db); 94 let ty = impl_def.self_ty(db);
95 let traits_in_scope = scope.traits_in_scope(); 95 let traits_in_scope = scope.traits_in_scope();
96 ty.iterate_method_candidates(db, krate, &traits_in_scope, Some(fn_name), |_, func| Some(func)) 96 ty.iterate_method_candidates(db, krate, &traits_in_scope, Some(fn_name), |_, func| Some(func))
97} 97}
diff --git a/crates/ide_assists/src/utils.rs b/crates/ide_assists/src/utils.rs
index 5f630ec75..d67524937 100644
--- a/crates/ide_assists/src/utils.rs
+++ b/crates/ide_assists/src/utils.rs
@@ -338,11 +338,11 @@ pub(crate) fn find_struct_impl(
338 // (we currently use the wrong type parameter) 338 // (we currently use the wrong type parameter)
339 // also we wouldn't want to use e.g. `impl S<u32>` 339 // also we wouldn't want to use e.g. `impl S<u32>`
340 340
341 let same_ty = match blk.target_ty(db).as_adt() { 341 let same_ty = match blk.self_ty(db).as_adt() {
342 Some(def) => def == struct_def, 342 Some(def) => def == struct_def,
343 None => false, 343 None => false,
344 }; 344 };
345 let not_trait_impl = blk.target_trait(db).is_none(); 345 let not_trait_impl = blk.trait_(db).is_none();
346 346
347 if !(same_ty && not_trait_impl) { 347 if !(same_ty && not_trait_impl) {
348 None 348 None
diff --git a/crates/ide_completion/src/completions.rs b/crates/ide_completion/src/completions.rs
index 6d572a836..e2994eed4 100644
--- a/crates/ide_completion/src/completions.rs
+++ b/crates/ide_completion/src/completions.rs
@@ -220,7 +220,7 @@ fn complete_enum_variants(
220 }; 220 };
221 221
222 if let Some(impl_) = ctx.impl_def.as_ref().and_then(|impl_| ctx.sema.to_def(impl_)) { 222 if let Some(impl_) = ctx.impl_def.as_ref().and_then(|impl_| ctx.sema.to_def(impl_)) {
223 if impl_.target_ty(ctx.db) == *ty { 223 if impl_.self_ty(ctx.db) == *ty {
224 for &variant in &variants { 224 for &variant in &variants {
225 let self_path = hir::ModPath::from_segments( 225 let self_path = hir::ModPath::from_segments(
226 hir::PathKind::Plain, 226 hir::PathKind::Plain,
diff --git a/crates/ide_completion/src/completions/pattern.rs b/crates/ide_completion/src/completions/pattern.rs
index b06498e6d..808d7ff7e 100644
--- a/crates/ide_completion/src/completions/pattern.rs
+++ b/crates/ide_completion/src/completions/pattern.rs
@@ -40,7 +40,7 @@ pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
40 _ => false, 40 _ => false,
41 }, 41 },
42 hir::ScopeDef::MacroDef(_) => true, 42 hir::ScopeDef::MacroDef(_) => true,
43 hir::ScopeDef::ImplSelfType(impl_) => match impl_.target_ty(ctx.db).as_adt() { 43 hir::ScopeDef::ImplSelfType(impl_) => match impl_.self_ty(ctx.db).as_adt() {
44 Some(hir::Adt::Struct(strukt)) => { 44 Some(hir::Adt::Struct(strukt)) => {
45 acc.add_struct_pat(ctx, strukt, Some(name.clone())); 45 acc.add_struct_pat(ctx, strukt, Some(name.clone()));
46 true 46 true
diff --git a/crates/ide_completion/src/completions/qualified_path.rs b/crates/ide_completion/src/completions/qualified_path.rs
index 105ff6013..1891eb5b3 100644
--- a/crates/ide_completion/src/completions/qualified_path.rs
+++ b/crates/ide_completion/src/completions/qualified_path.rs
@@ -117,7 +117,7 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
117 if let Some(krate) = ctx.krate { 117 if let Some(krate) = ctx.krate {
118 let ty = match resolution { 118 let ty = match resolution {
119 PathResolution::TypeParam(param) => param.ty(ctx.db), 119 PathResolution::TypeParam(param) => param.ty(ctx.db),
120 PathResolution::SelfType(impl_def) => impl_def.target_ty(ctx.db), 120 PathResolution::SelfType(impl_def) => impl_def.self_ty(ctx.db),
121 _ => return, 121 _ => return,
122 }; 122 };
123 123
diff --git a/crates/ide_db/src/helpers/import_assets.rs b/crates/ide_db/src/helpers/import_assets.rs
index 3deb0d159..8ce648367 100644
--- a/crates/ide_db/src/helpers/import_assets.rs
+++ b/crates/ide_db/src/helpers/import_assets.rs
@@ -361,7 +361,7 @@ fn item_for_path_search(db: &RootDatabase, item: ItemInNs) -> Option<ItemInNs> {
361 Some(assoc_item) => match assoc_item.container(db) { 361 Some(assoc_item) => match assoc_item.container(db) {
362 AssocItemContainer::Trait(trait_) => ItemInNs::from(ModuleDef::from(trait_)), 362 AssocItemContainer::Trait(trait_) => ItemInNs::from(ModuleDef::from(trait_)),
363 AssocItemContainer::Impl(impl_) => { 363 AssocItemContainer::Impl(impl_) => {
364 ItemInNs::from(ModuleDef::from(impl_.target_ty(db).as_adt()?)) 364 ItemInNs::from(ModuleDef::from(impl_.self_ty(db).as_adt()?))
365 } 365 }
366 }, 366 },
367 None => item, 367 None => item,