From 957c0171e63631f67d732f4ce53dc8bfe2602e71 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 20 Dec 2019 13:11:01 +0100 Subject: Remove more copy-paste --- crates/ra_hir_def/src/lib.rs | 87 +++++++++++++++----------------------------- 1 file changed, 30 insertions(+), 57 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 140eccf26..042fd3f8b 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -45,7 +45,7 @@ use std::hash::Hash; use hir_expand::{ast_id_map::FileAstId, AstId, HirFileId, InFile, MacroDefId}; use ra_arena::{impl_arena_id, RawId}; use ra_db::{impl_intern_key, salsa, CrateId}; -use ra_syntax::ast; +use ra_syntax::{ast, AstNode}; use crate::builtin_type::BuiltinType; @@ -65,16 +65,23 @@ pub struct ModuleId { pub struct LocalModuleId(RawId); impl_arena_id!(LocalModuleId); -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct FunctionId(salsa::InternId); -impl_intern_key!(FunctionId); +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct ItemLoc { + pub container: ContainerId, + pub ast_id: AstId, +} #[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct FunctionLoc { +pub struct AssocItemLoc { pub container: AssocContainerId, - pub ast_id: AstId, + pub ast_id: AstId, } +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct FunctionId(salsa::InternId); +impl_intern_key!(FunctionId); +type FunctionLoc = AssocItemLoc; + impl Intern for FunctionLoc { type ID = FunctionId; fn intern(self, db: &impl db::DefDatabase) -> FunctionId { @@ -92,12 +99,7 @@ impl Lookup for FunctionId { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct StructId(salsa::InternId); impl_intern_key!(StructId); - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct StructLoc { - pub container: ContainerId, - pub ast_id: AstId, -} +pub type StructLoc = ItemLoc; impl Intern for StructLoc { type ID = StructId; @@ -116,12 +118,7 @@ impl Lookup for StructId { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct UnionId(salsa::InternId); impl_intern_key!(UnionId); - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct UnionLoc { - pub container: ContainerId, - pub ast_id: AstId, -} +pub type UnionLoc = ItemLoc; impl Intern for UnionLoc { type ID = UnionId; @@ -140,12 +137,7 @@ impl Lookup for UnionId { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct EnumId(salsa::InternId); impl_intern_key!(EnumId); - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct EnumLoc { - pub container: ContainerId, - pub ast_id: AstId, -} +pub type EnumLoc = ItemLoc; impl Intern for EnumLoc { type ID = EnumId; @@ -185,11 +177,7 @@ impl_arena_id!(LocalStructFieldId); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct ConstId(salsa::InternId); impl_intern_key!(ConstId); -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct ConstLoc { - pub container: AssocContainerId, - pub ast_id: AstId, -} +type ConstLoc = AssocItemLoc; impl Intern for ConstLoc { type ID = ConstId; @@ -208,12 +196,7 @@ impl Lookup for ConstId { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct StaticId(salsa::InternId); impl_intern_key!(StaticId); - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct StaticLoc { - pub container: ContainerId, - pub ast_id: AstId, -} +pub type StaticLoc = ItemLoc; impl Intern for StaticLoc { type ID = StaticId; @@ -232,12 +215,7 @@ impl Lookup for StaticId { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct TraitId(salsa::InternId); impl_intern_key!(TraitId); - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct TraitLoc { - pub container: ContainerId, - pub ast_id: AstId, -} +pub type TraitLoc = ItemLoc; impl Intern for TraitLoc { type ID = TraitId; @@ -256,12 +234,7 @@ impl Lookup for TraitId { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct TypeAliasId(salsa::InternId); impl_intern_key!(TypeAliasId); - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct TypeAliasLoc { - pub container: AssocContainerId, - pub ast_id: AstId, -} +type TypeAliasLoc = AssocItemLoc; impl Intern for TypeAliasLoc { type ID = TypeAliasId; @@ -301,6 +274,16 @@ impl Lookup for ImplId { } } +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct TypeParamId { + pub parent: GenericDefId, + pub local_id: LocalTypeParamId, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct LocalTypeParamId(RawId); +impl_arena_id!(LocalTypeParamId); + macro_rules! impl_froms { ($e:ident: $($v:ident $(($($sv:ident),*))?),*) => { $( @@ -320,16 +303,6 @@ macro_rules! impl_froms { } } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct TypeParamId { - pub parent: GenericDefId, - pub local_id: LocalTypeParamId, -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct LocalTypeParamId(RawId); -impl_arena_id!(LocalTypeParamId); - #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum ContainerId { ModuleId(ModuleId), -- cgit v1.2.3 From d137df0137dab36819efada901e92cd2733f292b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 20 Dec 2019 13:19:41 +0100 Subject: Remove more copy-paste --- crates/ra_hir_def/src/lib.rs | 150 +++++++++---------------------------------- 1 file changed, 29 insertions(+), 121 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 042fd3f8b..faeb2fc8a 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -77,81 +77,45 @@ pub struct AssocItemLoc { pub ast_id: AstId, } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct FunctionId(salsa::InternId); -impl_intern_key!(FunctionId); -type FunctionLoc = AssocItemLoc; +macro_rules! impl_intern { + ($id:ident, $loc:ident, $intern:ident, $lookup:ident) => { + impl_intern_key!($id); + + impl Intern for $loc { + type ID = $id; + fn intern(self, db: &impl db::DefDatabase) -> $id { + db.$intern(self) + } + } -impl Intern for FunctionLoc { - type ID = FunctionId; - fn intern(self, db: &impl db::DefDatabase) -> FunctionId { - db.intern_function(self) - } + impl Lookup for $id { + type Data = $loc; + fn lookup(&self, db: &impl db::DefDatabase) -> $loc { + db.$lookup(*self) + } + } + }; } -impl Lookup for FunctionId { - type Data = FunctionLoc; - fn lookup(&self, db: &impl db::DefDatabase) -> FunctionLoc { - db.lookup_intern_function(*self) - } -} +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct FunctionId(salsa::InternId); +type FunctionLoc = AssocItemLoc; +impl_intern!(FunctionId, FunctionLoc, intern_function, lookup_intern_function); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct StructId(salsa::InternId); -impl_intern_key!(StructId); -pub type StructLoc = ItemLoc; - -impl Intern for StructLoc { - type ID = StructId; - fn intern(self, db: &impl db::DefDatabase) -> StructId { - db.intern_struct(self) - } -} - -impl Lookup for StructId { - type Data = StructLoc; - fn lookup(&self, db: &impl db::DefDatabase) -> StructLoc { - db.lookup_intern_struct(*self) - } -} +type StructLoc = ItemLoc; +impl_intern!(StructId, StructLoc, intern_struct, lookup_intern_struct); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct UnionId(salsa::InternId); -impl_intern_key!(UnionId); pub type UnionLoc = ItemLoc; - -impl Intern for UnionLoc { - type ID = UnionId; - fn intern(self, db: &impl db::DefDatabase) -> UnionId { - db.intern_union(self) - } -} - -impl Lookup for UnionId { - type Data = UnionLoc; - fn lookup(&self, db: &impl db::DefDatabase) -> UnionLoc { - db.lookup_intern_union(*self) - } -} +impl_intern!(UnionId, UnionLoc, intern_union, lookup_intern_union); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct EnumId(salsa::InternId); -impl_intern_key!(EnumId); pub type EnumLoc = ItemLoc; - -impl Intern for EnumLoc { - type ID = EnumId; - fn intern(self, db: &impl db::DefDatabase) -> EnumId { - db.intern_enum(self) - } -} - -impl Lookup for EnumId { - type Data = EnumLoc; - fn lookup(&self, db: &impl db::DefDatabase) -> EnumLoc { - db.lookup_intern_enum(*self) - } -} +impl_intern!(EnumId, EnumLoc, intern_enum, lookup_intern_enum); // FIXME: rename to `VariantId`, only enums can ave variants #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -176,79 +140,23 @@ impl_arena_id!(LocalStructFieldId); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct ConstId(salsa::InternId); -impl_intern_key!(ConstId); type ConstLoc = AssocItemLoc; - -impl Intern for ConstLoc { - type ID = ConstId; - fn intern(self, db: &impl db::DefDatabase) -> ConstId { - db.intern_const(self) - } -} - -impl Lookup for ConstId { - type Data = ConstLoc; - fn lookup(&self, db: &impl db::DefDatabase) -> ConstLoc { - db.lookup_intern_const(*self) - } -} +impl_intern!(ConstId, ConstLoc, intern_const, lookup_intern_const); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct StaticId(salsa::InternId); -impl_intern_key!(StaticId); pub type StaticLoc = ItemLoc; - -impl Intern for StaticLoc { - type ID = StaticId; - fn intern(self, db: &impl db::DefDatabase) -> StaticId { - db.intern_static(self) - } -} - -impl Lookup for StaticId { - type Data = StaticLoc; - fn lookup(&self, db: &impl db::DefDatabase) -> StaticLoc { - db.lookup_intern_static(*self) - } -} +impl_intern!(StaticId, StaticLoc, intern_static, lookup_intern_static); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct TraitId(salsa::InternId); -impl_intern_key!(TraitId); pub type TraitLoc = ItemLoc; - -impl Intern for TraitLoc { - type ID = TraitId; - fn intern(self, db: &impl db::DefDatabase) -> TraitId { - db.intern_trait(self) - } -} - -impl Lookup for TraitId { - type Data = TraitLoc; - fn lookup(&self, db: &impl db::DefDatabase) -> TraitLoc { - db.lookup_intern_trait(*self) - } -} +impl_intern!(TraitId, TraitLoc, intern_trait, lookup_intern_trait); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct TypeAliasId(salsa::InternId); -impl_intern_key!(TypeAliasId); type TypeAliasLoc = AssocItemLoc; - -impl Intern for TypeAliasLoc { - type ID = TypeAliasId; - fn intern(self, db: &impl db::DefDatabase) -> TypeAliasId { - db.intern_type_alias(self) - } -} - -impl Lookup for TypeAliasId { - type Data = TypeAliasLoc; - fn lookup(&self, db: &impl db::DefDatabase) -> TypeAliasLoc { - db.lookup_intern_type_alias(*self) - } -} +impl_intern!(TypeAliasId, TypeAliasLoc, intern_type_alias, lookup_intern_type_alias); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct ImplId(salsa::InternId); -- cgit v1.2.3 From 1234dda9ee60a19a83a9664c2e1208247566b49b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 20 Dec 2019 13:47:44 +0100 Subject: Use generic ItemLoc for impls --- crates/ra_hir/src/code_model.rs | 4 ++-- crates/ra_hir_def/src/lib.rs | 27 ++++----------------------- crates/ra_hir_def/src/nameres/collector.rs | 3 ++- crates/ra_hir_ty/src/method_resolution.rs | 2 +- crates/ra_hir_ty/src/traits/chalk.rs | 2 +- 5 files changed, 10 insertions(+), 28 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 35e1f752b..ecf883272 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -754,7 +754,7 @@ impl ImplBlock { let environment = TraitEnvironment::lower(db, &resolver); let ty = Ty::from_hir(db, &resolver, &impl_data.target_type); Type { - krate: self.id.lookup(db).container.krate, + krate: self.id.lookup(db).container.module(db).krate, ty: InEnvironment { value: ty, environment }, } } @@ -768,7 +768,7 @@ impl ImplBlock { } pub fn module(&self, db: &impl DefDatabase) -> Module { - self.id.lookup(db).container.into() + self.id.lookup(db).container.module(db).into() } pub fn krate(&self, db: &impl DefDatabase) -> Crate { diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index faeb2fc8a..d11b573e5 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -160,27 +160,8 @@ impl_intern!(TypeAliasId, TypeAliasLoc, intern_type_alias, lookup_intern_type_al #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct ImplId(salsa::InternId); -impl_intern_key!(ImplId); - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct ImplLoc { - pub container: ModuleId, - pub ast_id: AstId, -} - -impl Intern for ImplLoc { - type ID = ImplId; - fn intern(self, db: &impl db::DefDatabase) -> ImplId { - db.intern_impl(self) - } -} - -impl Lookup for ImplId { - type Data = ImplLoc; - fn lookup(&self, db: &impl db::DefDatabase) -> ImplLoc { - db.lookup_intern_impl(*self) - } -} +type ImplLoc = ItemLoc; +impl_intern!(ImplId, ImplLoc, intern_impl, lookup_intern_impl); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct TypeParamId { @@ -379,7 +360,7 @@ impl HasModule for AssocContainerId { fn module(&self, db: &impl db::DefDatabase) -> ModuleId { match *self { AssocContainerId::ContainerId(it) => it.module(db), - AssocContainerId::ImplId(it) => it.lookup(db).container, + AssocContainerId::ImplId(it) => it.lookup(db).container.module(db), AssocContainerId::TraitId(it) => it.lookup(db).container.module(db), } } @@ -431,7 +412,7 @@ impl HasModule for GenericDefId { GenericDefId::AdtId(it) => it.module(db), GenericDefId::TraitId(it) => it.lookup(db).container.module(db), GenericDefId::TypeAliasId(it) => it.lookup(db).module(db), - GenericDefId::ImplId(it) => it.lookup(db).container, + GenericDefId::ImplId(it) => it.lookup(db).container.module(db), GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container.module(db), GenericDefId::ConstId(it) => it.lookup(db).module(db), } diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index a1ea130e0..e68bf4868 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -661,9 +661,10 @@ where krate: self.def_collector.def_map.krate, local_id: self.module_id, }; + let container = ContainerId::ModuleId(module); let ast_id = self.raw_items[imp].ast_id; let impl_id = - ImplLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) } + ImplLoc { container, ast_id: AstId::new(self.file_id, ast_id) } .intern(self.def_collector.db); self.def_collector.def_map.modules[self.module_id].impls.push(impl_id) } diff --git a/crates/ra_hir_ty/src/method_resolution.rs b/crates/ra_hir_ty/src/method_resolution.rs index 1b2f4014c..92fb4c081 100644 --- a/crates/ra_hir_ty/src/method_resolution.rs +++ b/crates/ra_hir_ty/src/method_resolution.rs @@ -134,7 +134,7 @@ impl Ty { LangItemTarget::ImplBlockId(it) => Some(it), _ => None, }) - .map(|it| it.lookup(db).container.krate) + .map(|it| it.lookup(db).container.module(db).krate) .collect(); Some(res) } diff --git a/crates/ra_hir_ty/src/traits/chalk.rs b/crates/ra_hir_ty/src/traits/chalk.rs index dd4fa9664..5eb032d86 100644 --- a/crates/ra_hir_ty/src/traits/chalk.rs +++ b/crates/ra_hir_ty/src/traits/chalk.rs @@ -673,7 +673,7 @@ fn impl_block_datum( let bound_vars = Substs::bound_vars(&generic_params); let trait_ref = trait_ref.subst(&bound_vars); let trait_ = trait_ref.trait_; - let impl_type = if impl_id.lookup(db).container.krate == krate { + let impl_type = if impl_id.lookup(db).container.module(db).krate == krate { chalk_rust_ir::ImplType::Local } else { chalk_rust_ir::ImplType::External -- cgit v1.2.3