From 7b0644d81e52d00a7a6795b187f356213ff68225 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 12 Dec 2019 14:09:13 +0100 Subject: Switch to the new location for impls --- Cargo.lock | 1 + crates/ra_hir/src/code_model.rs | 7 +++++-- crates/ra_hir/src/from_source.rs | 6 ++++-- crates/ra_hir/src/has_source.rs | 2 +- crates/ra_hir_def/src/attr.rs | 2 +- crates/ra_hir_def/src/child_by_source.rs | 5 +++++ crates/ra_hir_def/src/data.rs | 2 +- crates/ra_hir_def/src/db.rs | 6 +++--- crates/ra_hir_def/src/generics.rs | 2 +- crates/ra_hir_def/src/keys.rs | 6 ++++-- crates/ra_hir_def/src/lib.rs | 30 +++++++++++++++++++++--------- crates/ra_hir_def/src/nameres/collector.rs | 10 ++++++---- crates/ra_hir_def/src/resolver.rs | 3 ++- crates/ra_hir_def/src/src.rs | 11 ++++++++++- crates/ra_hir_ty/src/method_resolution.rs | 6 +++--- crates/ra_hir_ty/src/traits/chalk.rs | 2 +- 16 files changed, 69 insertions(+), 32 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 13bbbb74c..af874945c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1123,6 +1123,7 @@ dependencies = [ "rowan 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "rustc_lexer 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.103 (registry+https://github.com/rust-lang/crates.io-index)", "smol_str 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", "test_utils 0.1.0", "walkdir 2.2.9 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index c013ff99b..117ca2fe3 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -809,7 +809,10 @@ impl ImplBlock { let resolver = self.id.resolver(db); let environment = TraitEnvironment::lower(db, &resolver); let ty = Ty::from_hir(db, &resolver, &impl_data.target_type); - Type { krate: self.id.module(db).krate, ty: InEnvironment { value: ty, environment } } + Type { + krate: self.id.lookup(db).container.krate, + ty: InEnvironment { value: ty, environment }, + } } pub fn items(&self, db: &impl DefDatabase) -> Vec { @@ -821,7 +824,7 @@ impl ImplBlock { } pub fn module(&self, db: &impl DefDatabase) -> Module { - self.id.module(db).into() + self.id.lookup(db).container.into() } pub fn krate(&self, db: &impl DefDatabase) -> Crate { diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index 307f3d5bf..b35188a21 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -107,8 +107,10 @@ impl FromSource for MacroDef { impl FromSource for ImplBlock { type Ast = ast::ImplBlock; fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { - let id = from_source(db, src)?; - Some(ImplBlock { id }) + // XXX: use `.parent()` to avoid finding ourselves + let parent = src.value.syntax().parent()?; + let container = Container::find(db, src.with_value(parent).as_ref())?; + container.child_by_source(db)[keys::IMPL].get(&src).copied().map(ImplBlock::from) } } diff --git a/crates/ra_hir/src/has_source.rs b/crates/ra_hir/src/has_source.rs index b09582f93..a888fe995 100644 --- a/crates/ra_hir/src/has_source.rs +++ b/crates/ra_hir/src/has_source.rs @@ -114,7 +114,7 @@ impl HasSource for MacroDef { impl HasSource for ImplBlock { type Ast = ast::ImplBlock; fn source(self, db: &impl DefDatabase) -> InFile { - self.id.source(db) + self.id.lookup(db).source(db) } } impl HasSource for Import { diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs index 2f8f02d82..12d4e777a 100644 --- a/crates/ra_hir_def/src/attr.rs +++ b/crates/ra_hir_def/src/attr.rs @@ -64,7 +64,7 @@ impl Attrs { AttrDefId::MacroDefId(it) => { it.ast_id.map_or_else(Default::default, |ast_id| attrs_from_ast(ast_id, db)) } - AttrDefId::ImplId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), + AttrDefId::ImplId(it) => attrs_from_loc(it.lookup(db), db), AttrDefId::ConstId(it) => attrs_from_loc(it.lookup(db), db), AttrDefId::StaticId(it) => attrs_from_loc(it.lookup(db), db), AttrDefId::FunctionId(it) => attrs_from_loc(it.lookup(db), db), diff --git a/crates/ra_hir_def/src/child_by_source.rs b/crates/ra_hir_def/src/child_by_source.rs index a3574a9db..821549bd5 100644 --- a/crates/ra_hir_def/src/child_by_source.rs +++ b/crates/ra_hir_def/src/child_by_source.rs @@ -98,6 +98,11 @@ impl ChildBySource for ModuleId { } } + for &impl_ in crate_def_map[self.local_id].impls.iter() { + let src = impl_.lookup(db).source(db); + res[keys::IMPL].insert(src, impl_) + } + res } } diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs index 095d7064a..42821b9b1 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs @@ -167,7 +167,7 @@ pub struct ImplData { impl ImplData { pub(crate) fn impl_data_query(db: &impl DefDatabase, id: ImplId) -> Arc { - let src = id.source(db); + let src = id.lookup(db).source(db); let items = db.ast_id_map(src.file_id); let target_trait = src.value.target_trait().map(TypeRef::from_ast); diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs index ef5611ffc..8907aacca 100644 --- a/crates/ra_hir_def/src/db.rs +++ b/crates/ra_hir_def/src/db.rs @@ -18,8 +18,8 @@ use crate::{ CrateDefMap, }, AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, FunctionId, FunctionLoc, GenericDefId, - ImplId, ItemLoc, ModuleId, StaticId, StaticLoc, StructId, TraitId, TypeAliasId, TypeAliasLoc, - UnionId, + ImplId, ImplLoc, ItemLoc, ModuleId, StaticId, StaticLoc, StructId, TraitId, TypeAliasId, + TypeAliasLoc, UnionId, }; #[salsa::query_group(InternDatabaseStorage)] @@ -41,7 +41,7 @@ pub trait InternDatabase: SourceDatabase { #[salsa::interned] fn intern_type_alias(&self, loc: TypeAliasLoc) -> TypeAliasId; #[salsa::interned] - fn intern_impl(&self, loc: ItemLoc) -> ImplId; + fn intern_impl(&self, loc: ImplLoc) -> ImplId; } #[salsa::query_group(DefDatabaseStorage)] diff --git a/crates/ra_hir_def/src/generics.rs b/crates/ra_hir_def/src/generics.rs index 976cf72d0..5d1100945 100644 --- a/crates/ra_hir_def/src/generics.rs +++ b/crates/ra_hir_def/src/generics.rs @@ -109,7 +109,7 @@ impl GenericParams { // type-parameter, but rather is a type-alias for impl's target // type, so this is handled by the resolver. GenericDefId::ImplId(it) => { - let src = it.source(db); + let src = it.lookup(db).source(db); generics.fill(&mut sm, &src.value); src.file_id } diff --git a/crates/ra_hir_def/src/keys.rs b/crates/ra_hir_def/src/keys.rs index be702a4f8..d318b2451 100644 --- a/crates/ra_hir_def/src/keys.rs +++ b/crates/ra_hir_def/src/keys.rs @@ -8,7 +8,7 @@ use rustc_hash::FxHashMap; use crate::{ dyn_map::{DynMap, Policy}, - ConstId, EnumVariantId, FunctionId, StaticId, StructFieldId, TypeAliasId, TypeParamId, + ConstId, EnumVariantId, FunctionId, ImplId, StaticId, StructFieldId, TypeAliasId, TypeParamId, }; type Key = crate::dyn_map::Key, V, AstPtrPolicy>; @@ -16,8 +16,10 @@ type Key = crate::dyn_map::Key, V, AstPtrPolicy>; pub const FUNCTION: Key = Key::new(); pub const CONST: Key = Key::new(); pub const STATIC: Key = Key::new(); -pub const ENUM_VARIANT: Key = Key::new(); pub const TYPE_ALIAS: Key = Key::new(); +pub const IMPL: Key = Key::new(); + +pub const ENUM_VARIANT: Key = Key::new(); pub const TUPLE_FIELD: Key = Key::new(); pub const RECORD_FIELD: Key = Key::new(); pub const TYPE_PARAM: Key = Key::new(); diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 569da4f28..5564b166b 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -289,12 +289,24 @@ impl Lookup for TypeAliasId { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct ImplId(salsa::InternId); impl_intern_key!(ImplId); -impl AstItemDef for ImplId { - fn intern(db: &impl InternDatabase, loc: ItemLoc) -> Self { - db.intern_impl(loc) + +#[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) } - fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc { - db.lookup_intern_impl(self) +} + +impl Lookup for ImplId { + type Data = ImplLoc; + fn lookup(&self, db: &impl db::DefDatabase) -> ImplLoc { + db.lookup_intern_impl(*self) } } @@ -479,7 +491,7 @@ impl HasModule for FunctionLoc { fn module(&self, db: &impl db::DefDatabase) -> ModuleId { match self.container { ContainerId::ModuleId(it) => it, - ContainerId::ImplId(it) => it.module(db), + ContainerId::ImplId(it) => it.lookup(db).container, ContainerId::TraitId(it) => it.module(db), } } @@ -489,7 +501,7 @@ impl HasModule for TypeAliasLoc { fn module(&self, db: &impl db::DefDatabase) -> ModuleId { match self.container { ContainerId::ModuleId(it) => it, - ContainerId::ImplId(it) => it.module(db), + ContainerId::ImplId(it) => it.lookup(db).container, ContainerId::TraitId(it) => it.module(db), } } @@ -499,7 +511,7 @@ impl HasModule for ConstLoc { fn module(&self, db: &impl db::DefDatabase) -> ModuleId { match self.container { ContainerId::ModuleId(it) => it, - ContainerId::ImplId(it) => it.module(db), + ContainerId::ImplId(it) => it.lookup(db).container, ContainerId::TraitId(it) => it.module(db), } } @@ -532,7 +544,7 @@ impl HasModule for GenericDefId { GenericDefId::AdtId(it) => it.module(db), GenericDefId::TraitId(it) => it.module(db), GenericDefId::TypeAliasId(it) => it.lookup(db).module(db), - GenericDefId::ImplId(it) => it.module(db), + GenericDefId::ImplId(it) => it.lookup(db).container, GenericDefId::EnumVariantId(it) => it.parent.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 a80067979..b33507a9a 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -24,7 +24,7 @@ use crate::{ }, path::{Path, PathKind}, per_ns::PerNs, - AdtId, AstId, AstItemDef, ConstLoc, ContainerId, EnumId, EnumVariantId, FunctionLoc, ImplId, + AdtId, AstId, AstItemDef, ConstLoc, ContainerId, EnumId, EnumVariantId, FunctionLoc, ImplLoc, Intern, LocalImportId, LocalModuleId, LocationCtx, ModuleDefId, ModuleId, StaticLoc, StructId, TraitId, TypeAliasLoc, UnionId, }; @@ -661,9 +661,11 @@ where krate: self.def_collector.def_map.krate, local_id: self.module_id, }; - let ctx = LocationCtx::new(self.def_collector.db, module, self.file_id); - let imp_id = ImplId::from_ast_id(ctx, self.raw_items[imp].ast_id); - self.def_collector.def_map.modules[self.module_id].impls.push(imp_id) + let ast_id = self.raw_items[imp].ast_id; + let impl_id = + ImplLoc { container: module, 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_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs index 9484a61d5..f87b16b44 100644 --- a/crates/ra_hir_def/src/resolver.rs +++ b/crates/ra_hir_def/src/resolver.rs @@ -564,7 +564,8 @@ impl HasResolver for TypeAliasId { impl HasResolver for ImplId { fn resolver(self, db: &impl DefDatabase) -> Resolver { - self.module(db) + self.lookup(db) + .container .resolver(db) .push_generic_params_scope(db, self.into()) .push_impl_block_scope(self) diff --git a/crates/ra_hir_def/src/src.rs b/crates/ra_hir_def/src/src.rs index 27caa02cc..a5c4359a7 100644 --- a/crates/ra_hir_def/src/src.rs +++ b/crates/ra_hir_def/src/src.rs @@ -4,7 +4,7 @@ use hir_expand::InFile; use ra_arena::map::ArenaMap; use ra_syntax::ast; -use crate::{db::DefDatabase, ConstLoc, FunctionLoc, StaticLoc, TypeAliasLoc}; +use crate::{db::DefDatabase, ConstLoc, FunctionLoc, ImplLoc, StaticLoc, TypeAliasLoc}; pub trait HasSource { type Value; @@ -47,6 +47,15 @@ impl HasSource for StaticLoc { } } +impl HasSource for ImplLoc { + type Value = ast::ImplBlock; + + fn source(&self, db: &impl DefDatabase) -> InFile { + let node = self.ast_id.to_node(db); + InFile::new(self.ast_id.file_id, node) + } +} + pub trait HasChildSource { type ChildId; type Value; diff --git a/crates/ra_hir_ty/src/method_resolution.rs b/crates/ra_hir_ty/src/method_resolution.rs index 7220d6e0a..848e306e9 100644 --- a/crates/ra_hir_ty/src/method_resolution.rs +++ b/crates/ra_hir_ty/src/method_resolution.rs @@ -6,8 +6,8 @@ use std::sync::Arc; use arrayvec::ArrayVec; use hir_def::{ - lang_item::LangItemTarget, resolver::Resolver, type_ref::Mutability, AssocItemId, AstItemDef, - FunctionId, HasModule, ImplId, Lookup, TraitId, + lang_item::LangItemTarget, resolver::Resolver, type_ref::Mutability, AssocItemId, FunctionId, + HasModule, ImplId, Lookup, TraitId, }; use hir_expand::name::Name; use ra_db::CrateId; @@ -134,7 +134,7 @@ impl Ty { LangItemTarget::ImplBlockId(it) => Some(it), _ => None, }) - .map(|it| it.module(db).krate) + .map(|it| it.lookup(db).container.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 1e7ff93d5..ff299f511 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.module(db).krate == krate { + let impl_type = if impl_id.lookup(db).container.krate == krate { chalk_rust_ir::ImplType::Local } else { chalk_rust_ir::ImplType::External -- cgit v1.2.3