From 82e9b245587046d2a1ed432225b19023adbe3245 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 12 Dec 2019 14:34:03 +0100 Subject: Move traits to the new loc --- crates/ra_hir/src/code_model.rs | 2 +- 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 | 4 ++++ crates/ra_hir_def/src/data.rs | 6 +++--- crates/ra_hir_def/src/db.rs | 6 +++--- crates/ra_hir_def/src/docs.rs | 2 +- crates/ra_hir_def/src/generics.rs | 2 +- crates/ra_hir_def/src/keys.rs | 4 +++- crates/ra_hir_def/src/lib.rs | 30 +++++++++++++++++++++--------- crates/ra_hir_def/src/nameres/collector.rs | 9 +++++++-- crates/ra_hir_def/src/resolver.rs | 8 ++++---- crates/ra_hir_def/src/src.rs | 11 ++++++++++- crates/ra_hir_expand/src/builtin_macro.rs | 5 ++++- crates/ra_hir_ty/src/traits/chalk.rs | 6 ++---- 16 files changed, 70 insertions(+), 35 deletions(-) diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 117ca2fe3..78c7792bf 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -577,7 +577,7 @@ pub struct Trait { impl Trait { pub fn module(self, db: &impl DefDatabase) -> Module { - Module { id: self.id.module(db) } + Module { id: self.id.lookup(db).container } } pub fn name(self, db: &impl DefDatabase) -> Name { diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index b35188a21..0cf4bcae1 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -44,8 +44,10 @@ impl FromSource for Enum { impl FromSource for Trait { type Ast = ast::TraitDef; fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { - let id = from_source(db, src)?; - Some(Trait { 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::TRAIT].get(&src).copied().map(Trait::from) } } impl FromSource for Function { diff --git a/crates/ra_hir/src/has_source.rs b/crates/ra_hir/src/has_source.rs index a888fe995..c4c6d66b7 100644 --- a/crates/ra_hir/src/has_source.rs +++ b/crates/ra_hir/src/has_source.rs @@ -93,7 +93,7 @@ impl HasSource for Static { impl HasSource for Trait { type Ast = ast::TraitDef; fn source(self, db: &impl DefDatabase) -> InFile { - self.id.source(db) + self.id.lookup(db).source(db) } } impl HasSource for TypeAlias { diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs index 12d4e777a..3347d819c 100644 --- a/crates/ra_hir_def/src/attr.rs +++ b/crates/ra_hir_def/src/attr.rs @@ -60,7 +60,7 @@ impl Attrs { AdtId::EnumId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), AdtId::UnionId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), }, - AttrDefId::TraitId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), + AttrDefId::TraitId(it) => attrs_from_loc(it.lookup(db), db), AttrDefId::MacroDefId(it) => { it.ast_id.map_or_else(Default::default, |ast_id| attrs_from_ast(ast_id, db)) } diff --git a/crates/ra_hir_def/src/child_by_source.rs b/crates/ra_hir_def/src/child_by_source.rs index 821549bd5..88c2d3f24 100644 --- a/crates/ra_hir_def/src/child_by_source.rs +++ b/crates/ra_hir_def/src/child_by_source.rs @@ -94,6 +94,10 @@ impl ChildBySource for ModuleId { let src = ty.lookup(db).source(db); res[keys::TYPE_ALIAS].insert(src, ty) } + ModuleDefId::TraitId(trait_) => { + let src = trait_.lookup(db).source(db); + res[keys::TRAIT].insert(src, trait_) + } _ => (), } } diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs index 42821b9b1..b2dac183e 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs @@ -12,8 +12,8 @@ use crate::{ db::DefDatabase, src::HasSource, type_ref::{Mutability, TypeRef}, - AssocItemId, AstItemDef, ConstId, ConstLoc, ContainerId, FunctionId, FunctionLoc, ImplId, - Intern, Lookup, StaticId, TraitId, TypeAliasId, TypeAliasLoc, + AssocItemId, ConstId, ConstLoc, ContainerId, FunctionId, FunctionLoc, ImplId, Intern, Lookup, + StaticId, TraitId, TypeAliasId, TypeAliasLoc, }; #[derive(Debug, Clone, PartialEq, Eq)] @@ -94,7 +94,7 @@ pub struct TraitData { impl TraitData { pub(crate) fn trait_data_query(db: &impl DefDatabase, tr: TraitId) -> Arc { - let src = tr.source(db); + let src = tr.lookup(db).source(db); let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); let auto = src.value.is_auto(); let ast_id_map = db.ast_id_map(src.file_id); diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs index 8907aacca..f0c2ae559 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, ImplLoc, ItemLoc, ModuleId, StaticId, StaticLoc, StructId, TraitId, TypeAliasId, - TypeAliasLoc, UnionId, + ImplId, ImplLoc, ItemLoc, ModuleId, StaticId, StaticLoc, StructId, TraitId, TraitLoc, + TypeAliasId, TypeAliasLoc, UnionId, }; #[salsa::query_group(InternDatabaseStorage)] @@ -37,7 +37,7 @@ pub trait InternDatabase: SourceDatabase { #[salsa::interned] fn intern_static(&self, loc: StaticLoc) -> StaticId; #[salsa::interned] - fn intern_trait(&self, loc: ItemLoc) -> TraitId; + fn intern_trait(&self, loc: TraitLoc) -> TraitId; #[salsa::interned] fn intern_type_alias(&self, loc: TypeAliasLoc) -> TypeAliasId; #[salsa::interned] diff --git a/crates/ra_hir_def/src/docs.rs b/crates/ra_hir_def/src/docs.rs index 61727bd26..1921681fb 100644 --- a/crates/ra_hir_def/src/docs.rs +++ b/crates/ra_hir_def/src/docs.rs @@ -59,7 +59,7 @@ impl Documentation { let src = it.parent.child_source(db); docs_from_ast(&src.value[it.local_id]) } - AttrDefId::TraitId(it) => docs_from_ast(&it.source(db).value), + AttrDefId::TraitId(it) => docs_from_ast(&it.lookup(db).source(db).value), AttrDefId::MacroDefId(it) => docs_from_ast(&it.ast_id?.to_node(db)), AttrDefId::ConstId(it) => docs_from_ast(&it.lookup(db).source(db).value), AttrDefId::StaticId(it) => docs_from_ast(&it.lookup(db).source(db).value), diff --git a/crates/ra_hir_def/src/generics.rs b/crates/ra_hir_def/src/generics.rs index 5d1100945..cbfc3ff04 100644 --- a/crates/ra_hir_def/src/generics.rs +++ b/crates/ra_hir_def/src/generics.rs @@ -86,7 +86,7 @@ impl GenericParams { src.file_id } GenericDefId::TraitId(it) => { - let src = it.source(db); + let src = it.lookup(db).source(db); // traits get the Self type as an implicit first type parameter let self_param_id = diff --git a/crates/ra_hir_def/src/keys.rs b/crates/ra_hir_def/src/keys.rs index d318b2451..6ec58f5f9 100644 --- a/crates/ra_hir_def/src/keys.rs +++ b/crates/ra_hir_def/src/keys.rs @@ -8,7 +8,8 @@ use rustc_hash::FxHashMap; use crate::{ dyn_map::{DynMap, Policy}, - ConstId, EnumVariantId, FunctionId, ImplId, StaticId, StructFieldId, TypeAliasId, TypeParamId, + ConstId, EnumVariantId, FunctionId, ImplId, StaticId, StructFieldId, TraitId, TypeAliasId, + TypeParamId, }; type Key = crate::dyn_map::Key, V, AstPtrPolicy>; @@ -18,6 +19,7 @@ pub const CONST: Key = Key::new(); pub const STATIC: Key = Key::new(); pub const TYPE_ALIAS: Key = Key::new(); pub const IMPL: Key = Key::new(); +pub const TRAIT: Key = Key::new(); pub const ENUM_VARIANT: Key = Key::new(); pub const TUPLE_FIELD: Key = Key::new(); diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 5564b166b..0fcc2cde4 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -253,12 +253,24 @@ impl Lookup for StaticId { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct TraitId(salsa::InternId); impl_intern_key!(TraitId); -impl AstItemDef for TraitId { - fn intern(db: &impl InternDatabase, loc: ItemLoc) -> Self { - db.intern_trait(loc) + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct TraitLoc { + pub container: ModuleId, + pub ast_id: AstId, +} + +impl Intern for TraitLoc { + type ID = TraitId; + fn intern(self, db: &impl db::DefDatabase) -> TraitId { + db.intern_trait(self) } - fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc { - db.lookup_intern_trait(self) +} + +impl Lookup for TraitId { + type Data = TraitLoc; + fn lookup(&self, db: &impl db::DefDatabase) -> TraitLoc { + db.lookup_intern_trait(*self) } } @@ -492,7 +504,7 @@ impl HasModule for FunctionLoc { match self.container { ContainerId::ModuleId(it) => it, ContainerId::ImplId(it) => it.lookup(db).container, - ContainerId::TraitId(it) => it.module(db), + ContainerId::TraitId(it) => it.lookup(db).container, } } } @@ -502,7 +514,7 @@ impl HasModule for TypeAliasLoc { match self.container { ContainerId::ModuleId(it) => it, ContainerId::ImplId(it) => it.lookup(db).container, - ContainerId::TraitId(it) => it.module(db), + ContainerId::TraitId(it) => it.lookup(db).container, } } } @@ -512,7 +524,7 @@ impl HasModule for ConstLoc { match self.container { ContainerId::ModuleId(it) => it, ContainerId::ImplId(it) => it.lookup(db).container, - ContainerId::TraitId(it) => it.module(db), + ContainerId::TraitId(it) => it.lookup(db).container, } } } @@ -542,7 +554,7 @@ impl HasModule for GenericDefId { match self { GenericDefId::FunctionId(it) => it.lookup(db).module(db), GenericDefId::AdtId(it) => it.module(db), - GenericDefId::TraitId(it) => it.module(db), + GenericDefId::TraitId(it) => it.lookup(db).container, GenericDefId::TypeAliasId(it) => it.lookup(db).module(db), GenericDefId::ImplId(it) => it.lookup(db).container, GenericDefId::EnumVariantId(it) => it.parent.module(db), diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index b33507a9a..a7bdd620b 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -26,7 +26,7 @@ use crate::{ per_ns::PerNs, AdtId, AstId, AstItemDef, ConstLoc, ContainerId, EnumId, EnumVariantId, FunctionLoc, ImplLoc, Intern, LocalImportId, LocalModuleId, LocationCtx, ModuleDefId, ModuleId, StaticLoc, StructId, - TraitId, TypeAliasLoc, UnionId, + TraitLoc, TypeAliasLoc, UnionId, }; pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { @@ -796,7 +796,12 @@ where PerNs::values(def.into()) } - raw::DefKind::Trait(ast_id) => PerNs::types(TraitId::from_ast_id(ctx, ast_id).into()), + raw::DefKind::Trait(ast_id) => { + let def = TraitLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) } + .intern(self.def_collector.db); + + PerNs::types(def.into()) + } raw::DefKind::TypeAlias(ast_id) => { let def = TypeAliasLoc { container: ContainerId::ModuleId(module), diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs index f87b16b44..17b2169d2 100644 --- a/crates/ra_hir_def/src/resolver.rs +++ b/crates/ra_hir_def/src/resolver.rs @@ -17,9 +17,9 @@ use crate::{ nameres::{BuiltinShadowMode, CrateDefMap}, path::{Path, PathKind}, per_ns::PerNs, - AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, - GenericDefId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId, - StructId, TraitId, TypeAliasId, TypeParamId, VariantId, + AdtId, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, + HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId, + TypeAliasId, TypeParamId, VariantId, }; #[derive(Debug, Clone, Default)] @@ -524,7 +524,7 @@ impl HasResolver for ModuleId { impl HasResolver for TraitId { fn resolver(self, db: &impl DefDatabase) -> Resolver { - self.module(db).resolver(db).push_generic_params_scope(db, self.into()) + self.lookup(db).container.resolver(db).push_generic_params_scope(db, self.into()) } } diff --git a/crates/ra_hir_def/src/src.rs b/crates/ra_hir_def/src/src.rs index a5c4359a7..858e4861e 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, ImplLoc, StaticLoc, TypeAliasLoc}; +use crate::{db::DefDatabase, ConstLoc, FunctionLoc, ImplLoc, StaticLoc, TraitLoc, TypeAliasLoc}; pub trait HasSource { type Value; @@ -56,6 +56,15 @@ impl HasSource for ImplLoc { } } +impl HasSource for TraitLoc { + type Value = ast::TraitDef; + + 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_expand/src/builtin_macro.rs b/crates/ra_hir_expand/src/builtin_macro.rs index ec5ace757..be5f3cbe3 100644 --- a/crates/ra_hir_expand/src/builtin_macro.rs +++ b/crates/ra_hir_expand/src/builtin_macro.rs @@ -367,6 +367,9 @@ mod tests { BuiltinFnLikeExpander::FormatArgs, ); - assert_eq!(expanded, r#"std::fmt::Arguments::new_v1(&[] ,&[std::fmt::ArgumentV1::new(&(arg1(a,b,c)),std::fmt::Display::fmt),std::fmt::ArgumentV1::new(&(arg2),std::fmt::Display::fmt),])"#); + assert_eq!( + expanded, + r#"std::fmt::Arguments::new_v1(&[] ,&[std::fmt::ArgumentV1::new(&(arg1(a,b,c)),std::fmt::Display::fmt),std::fmt::ArgumentV1::new(&(arg2),std::fmt::Display::fmt),])"# + ); } } diff --git a/crates/ra_hir_ty/src/traits/chalk.rs b/crates/ra_hir_ty/src/traits/chalk.rs index ff299f511..fc21872b2 100644 --- a/crates/ra_hir_ty/src/traits/chalk.rs +++ b/crates/ra_hir_ty/src/traits/chalk.rs @@ -9,9 +9,7 @@ use chalk_ir::{ }; use chalk_rust_ir::{AssociatedTyDatum, AssociatedTyValue, ImplDatum, StructDatum, TraitDatum}; -use hir_def::{ - AssocItemId, AstItemDef, ContainerId, GenericDefId, ImplId, Lookup, TraitId, TypeAliasId, -}; +use hir_def::{AssocItemId, ContainerId, GenericDefId, ImplId, Lookup, TraitId, TypeAliasId}; use ra_db::{ salsa::{InternId, InternKey}, CrateId, @@ -593,7 +591,7 @@ pub(crate) fn trait_datum_query( let bound_vars = Substs::bound_vars(&generic_params); let flags = chalk_rust_ir::TraitFlags { auto: trait_data.auto, - upstream: trait_.module(db).krate != krate, + upstream: trait_.lookup(db).container.krate != krate, non_enumerable: true, coinductive: false, // only relevant for Chalk testing // FIXME set these flags correctly -- cgit v1.2.3