From 0f2f3a21e7e624f920d182869896347af309e909 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 25 Jan 2019 01:31:32 +0300 Subject: Migrate trait & type to new ids --- crates/ra_hir/src/code_model_api.rs | 35 +++++------- crates/ra_hir/src/code_model_impl.rs | 15 ----- crates/ra_hir/src/code_model_impl/module.rs | 7 +-- crates/ra_hir/src/generics.rs | 23 +++----- crates/ra_hir/src/ids.rs | 38 +++++++------ crates/ra_hir/src/nameres/lower.rs | 85 +++++++---------------------- crates/ra_hir/src/source_binder.rs | 4 +- crates/ra_hir/src/ty.rs | 4 +- 8 files changed, 69 insertions(+), 142 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index d82dda79a..e2979617d 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -13,10 +13,9 @@ use crate::{ ty::{InferenceResult, VariantDef}, adt::VariantData, generics::GenericParams, - code_model_impl::def_id_to_ast, docs::{Documentation, Docs, docs_from_ast}, module_tree::ModuleId, - ids::{FunctionId, StructId, EnumId, EnumVariantId, AstItemDef, ConstId, StaticId}, + ids::{FunctionId, StructId, EnumId, EnumVariantId, AstItemDef, ConstId, StaticId, TraitId, TypeId}, }; /// hir::Crate describes a single crate. It's the main interface with which @@ -47,8 +46,6 @@ impl Crate { #[derive(Debug)] pub enum Def { - Trait(Trait), - Type(Type), Item, } @@ -68,6 +65,8 @@ pub enum ModuleDef { EnumVariant(EnumVariant), Const(Const), Static(Static), + Trait(Trait), + Type(Type), // Can't be directly declared, but can be imported. Def(DefId), } @@ -78,7 +77,9 @@ impl_froms!( Enum, EnumVariant, Const, - Static + Static, + Trait, + Type ); impl From for ModuleDef { @@ -428,22 +429,18 @@ impl Docs for Static { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct Trait { - pub(crate) def_id: DefId, + pub(crate) id: TraitId, } impl Trait { - pub(crate) fn new(def_id: DefId) -> Trait { - Trait { def_id } - } - pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc) { - def_id_to_ast(db, self.def_id) + self.id.source(db) } pub fn generic_params(&self, db: &impl HirDatabase) -> Arc { - db.generic_params(self.def_id.into()) + db.generic_params((*self).into()) } } @@ -453,22 +450,18 @@ impl Docs for Trait { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct Type { - pub(crate) def_id: DefId, + pub(crate) id: TypeId, } impl Type { - pub(crate) fn new(def_id: DefId) -> Type { - Type { def_id } - } - pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc) { - def_id_to_ast(db, self.def_id) + self.id.source(db) } pub fn generic_params(&self, db: &impl HirDatabase) -> Arc { - db.generic_params(self.def_id.into()) + db.generic_params((*self).into()) } } diff --git a/crates/ra_hir/src/code_model_impl.rs b/crates/ra_hir/src/code_model_impl.rs index 0cea9f7b6..1f28fab74 100644 --- a/crates/ra_hir/src/code_model_impl.rs +++ b/crates/ra_hir/src/code_model_impl.rs @@ -1,18 +1,3 @@ mod krate; // `crate` is invalid ident :( mod module; pub(crate) mod function; - -use ra_syntax::{AstNode, TreeArc}; - -use crate::{HirDatabase, DefId, HirFileId}; - -pub(crate) fn def_id_to_ast( - db: &impl HirDatabase, - def_id: DefId, -) -> (HirFileId, TreeArc) { - let (file_id, syntax) = def_id.source(db); - let ast = N::cast(&syntax) - .unwrap_or_else(|| panic!("def points to wrong source {:?} {:?}", def_id, syntax)) - .to_owned(); - (file_id, ast) -} diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs index 1518825c7..6419d3934 100644 --- a/crates/ra_hir/src/code_model_impl/module.rs +++ b/crates/ra_hir/src/code_model_impl/module.rs @@ -147,17 +147,12 @@ impl Module { None => PerNs::none(), } } - ModuleDef::Function(_) - | ModuleDef::Struct(_) - | ModuleDef::Const(_) - | ModuleDef::Static(_) - | ModuleDef::EnumVariant(_) => { + _ => { // could be an inherent method call in UFCS form // (`Struct::method`), or some other kind of associated // item... Which we currently don't handle (TODO) PerNs::none() } - ModuleDef::Def(_) => PerNs::none(), }; } curr_per_ns diff --git a/crates/ra_hir/src/generics.rs b/crates/ra_hir/src/generics.rs index a8cacbb4a..64c20a462 100644 --- a/crates/ra_hir/src/generics.rs +++ b/crates/ra_hir/src/generics.rs @@ -5,9 +5,9 @@ use std::sync::Arc; -use ra_syntax::ast::{self, AstNode, NameOwner, TypeParamsOwner}; +use ra_syntax::ast::{self, NameOwner, TypeParamsOwner}; -use crate::{db::HirDatabase, DefId, Name, AsName, Function, Struct, Enum}; +use crate::{db::HirDatabase, Name, AsName, Function, Struct, Enum, Trait, Type}; /// Data about a generic parameter (to a function, struct, impl, ...). #[derive(Clone, PartialEq, Eq, Debug)] @@ -27,15 +27,10 @@ pub enum GenericDef { Function(Function), Struct(Struct), Enum(Enum), - Def(DefId), -} -impl_froms!(GenericDef: Function, Struct, Enum); - -impl From for GenericDef { - fn from(def_id: DefId) -> GenericDef { - GenericDef::Def(def_id) - } + Trait(Trait), + Type(Type), } +impl_froms!(GenericDef: Function, Struct, Enum, Trait, Type); impl GenericParams { pub(crate) fn generic_params_query( @@ -47,12 +42,8 @@ impl GenericParams { GenericDef::Function(it) => generics.fill(&*it.source(db).1), GenericDef::Struct(it) => generics.fill(&*it.source(db).1), GenericDef::Enum(it) => generics.fill(&*it.source(db).1), - GenericDef::Def(def_id) => { - let (_file_id, node) = def_id.source(db); - if let Some(type_param_list) = node.children().find_map(ast::TypeParamList::cast) { - generics.fill_params(type_param_list) - } - } + GenericDef::Trait(it) => generics.fill(&*it.source(db).1), + GenericDef::Type(it) => generics.fill(&*it.source(db).1), } Arc::new(generics) diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index 2cc175bda..311c0b98a 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs @@ -9,7 +9,7 @@ use ra_arena::{Arena, RawId, ArenaId, impl_arena_id}; use crate::{ HirDatabase, Def, - Module, Trait, Type, + Module, }; #[derive(Debug, Default)] @@ -22,6 +22,8 @@ pub struct HirInterner { enum_variants: LocationIntener, EnumVariantId>, consts: LocationIntener, ConstId>, statics: LocationIntener, StaticId>, + traits: LocationIntener, TraitId>, + types: LocationIntener, TypeId>, } impl HirInterner { @@ -279,6 +281,24 @@ impl AstItemDef for StaticId { } } +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct TraitId(RawId); +impl_arena_id!(TraitId); +impl AstItemDef for TraitId { + fn interner(interner: &HirInterner) -> &LocationIntener, Self> { + &interner.traits + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct TypeId(RawId); +impl_arena_id!(TypeId); +impl AstItemDef for TypeId { + fn interner(interner: &HirInterner) -> &LocationIntener, Self> { + &interner.types + } +} + /// Def's are a core concept of hir. A `Def` is an Item (function, module, etc) /// in a specific module. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -294,8 +314,6 @@ pub struct DefLoc { #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub(crate) enum DefKind { - Trait, - Type, Item, // /// The constructor of a struct. E.g. if we have `struct Foo(usize)`, the // /// name `Foo` needs to resolve to different types depending on whether we @@ -317,23 +335,9 @@ impl DefId { pub fn resolve(self, db: &impl HirDatabase) -> Def { let loc = self.loc(db); match loc.kind { - DefKind::Trait => { - let def = Trait::new(self); - Def::Trait(def) - } - DefKind::Type => { - let def = Type::new(self); - Def::Type(def) - } DefKind::Item => Def::Item, } } - - pub(crate) fn source(self, db: &impl HirDatabase) -> (HirFileId, TreeArc) { - let loc = self.loc(db); - let syntax = db.file_item(loc.source_item_id); - (loc.source_item_id.file_id, syntax) - } } impl DefLoc { diff --git a/crates/ra_hir/src/nameres/lower.rs b/crates/ra_hir/src/nameres/lower.rs index 6a86e5fd4..b4fe99ea7 100644 --- a/crates/ra_hir/src/nameres/lower.rs +++ b/crates/ra_hir/src/nameres/lower.rs @@ -1,16 +1,16 @@ use std::sync::Arc; use ra_syntax::{ - SyntaxKind, AstNode, SourceFile, TreeArc, AstPtr, + AstNode, SourceFile, TreeArc, AstPtr, ast::{self, ModuleItemOwner, NameOwner}, }; use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap}; use rustc_hash::FxHashMap; use crate::{ - SourceItemId, Path, ModuleSource, HirDatabase, Name, SourceFileItems, - HirFileId, MacroCallLoc, AsName, PerNs, DefKind, DefLoc, Function, - ModuleDef, Module, Struct, Enum, Const, Static, + SourceItemId, Path, ModuleSource, HirDatabase, Name, + HirFileId, MacroCallLoc, AsName, PerNs, Function, + ModuleDef, Module, Struct, Enum, Const, Static, Trait, Type, ids::LocationCtx, }; @@ -115,7 +115,7 @@ impl LoweredModule { for item in items { match item { ast::ItemOrMacro::Item(it) => { - self.add_def_id(source_map, db, module, file_id, &file_items, it); + self.add_def_id(source_map, db, module, file_id, it); } ast::ItemOrMacro::Macro(macro_call) => { let item_id = file_items.id_of_unchecked(macro_call.syntax()); @@ -128,10 +128,9 @@ impl LoweredModule { }; let id = loc.id(db); let file_id = HirFileId::from(id); - let file_items = db.file_items(file_id); //FIXME: expand recursively for item in db.hir_source_file(file_id).items() { - self.add_def_id(source_map, db, module, file_id, &file_items, item); + self.add_def_id(source_map, db, module, file_id, item); } } } @@ -144,18 +143,16 @@ impl LoweredModule { db: &impl HirDatabase, module: Module, file_id: HirFileId, - file_items: &SourceFileItems, item: &ast::ModuleItem, ) { let ctx = LocationCtx::new(db, module, file_id); - let name = match item.kind() { + match item.kind() { ast::ModuleItemKind::StructDef(it) => { if let Some(name) = it.name() { let s = Struct { id: ctx.to_def(it) }; let s: ModuleDef = s.into(); self.declarations.insert(name.as_name(), PerNs::both(s, s)); } - return; } ast::ModuleItemKind::EnumDef(it) => { if let Some(name) = it.name() { @@ -163,7 +160,6 @@ impl LoweredModule { let e: ModuleDef = e.into(); self.declarations.insert(name.as_name(), PerNs::types(e)); } - return; } ast::ModuleItemKind::FnDef(it) => { if let Some(name) = it.name() { @@ -171,21 +167,29 @@ impl LoweredModule { self.declarations .insert(name.as_name(), PerNs::values(func.into())); } - return; } - ast::ModuleItemKind::TraitDef(it) => it.name(), - ast::ModuleItemKind::TypeDef(it) => it.name(), + ast::ModuleItemKind::TraitDef(it) => { + if let Some(name) = it.name() { + let t = Trait { id: ctx.to_def(it) }; + self.declarations + .insert(name.as_name(), PerNs::types(t.into())); + } + } + ast::ModuleItemKind::TypeDef(it) => { + if let Some(name) = it.name() { + let t = Type { id: ctx.to_def(it) }; + self.declarations + .insert(name.as_name(), PerNs::types(t.into())); + } + } ast::ModuleItemKind::ImplBlock(_) => { // impls don't define items - return; } ast::ModuleItemKind::UseItem(it) => { self.add_use_item(source_map, it); - return; } ast::ModuleItemKind::ExternCrateItem(_) => { // TODO - return; } ast::ModuleItemKind::ConstDef(it) => { if let Some(name) = it.name() { @@ -193,7 +197,6 @@ impl LoweredModule { self.declarations .insert(name.as_name(), PerNs::values(c.into())); } - return; } ast::ModuleItemKind::StaticDef(it) => { if let Some(name) = it.name() { @@ -201,17 +204,11 @@ impl LoweredModule { self.declarations .insert(name.as_name(), PerNs::values(s.into())); } - return; } ast::ModuleItemKind::Module(_) => { // modules are handled separately direclty by nameres - return; } }; - if let Some(name) = name { - let def_id = assign_def_id(db, module, file_id, file_items, item); - self.declarations.insert(name.as_name(), def_id); - } } fn add_use_item(&mut self, source_map: &mut ImportSourceMap, item: &ast::UseItem) { @@ -226,43 +223,3 @@ impl LoweredModule { }) } } - -fn assign_def_id( - db: &impl HirDatabase, - module: Module, - file_id: HirFileId, - file_items: &SourceFileItems, - item: &ast::ModuleItem, -) -> PerNs { - // depending on the item kind, the location can define something in - // the values namespace, the types namespace, or both - let kind = DefKind::for_syntax_kind(item.syntax().kind()); - let def_id = kind.map(|k| { - let item_id = file_items.id_of_unchecked(item.syntax()); - let def_loc = DefLoc { - kind: k, - module, - source_item_id: SourceItemId { - file_id, - item_id: Some(item_id), - }, - }; - def_loc.id(db).into() - }); - def_id -} - -impl DefKind { - fn for_syntax_kind(kind: SyntaxKind) -> PerNs { - match kind { - SyntaxKind::FN_DEF => unreachable!(), - SyntaxKind::STRUCT_DEF => unreachable!(), - SyntaxKind::ENUM_DEF => unreachable!(), - SyntaxKind::TRAIT_DEF => PerNs::types(DefKind::Trait), - SyntaxKind::TYPE_DEF => PerNs::types(DefKind::Type), - SyntaxKind::CONST_DEF => unreachable!(), - SyntaxKind::STATIC_DEF => unreachable!(), - _ => PerNs::none(), - } - } -} diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index ea8185853..dbe040805 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -145,10 +145,10 @@ pub fn macro_symbols(db: &impl HirDatabase, file_id: FileId) -> Vec<(SmolStr, Te .iter() .filter_map(|(_, it)| it.clone().take_types()) .filter_map(|it| match it { - ModuleDef::Def(it) => Some(it), + ModuleDef::Trait(it) => Some(it), _ => None, }) - .filter_map(|it| it.loc(db).source_item_id.file_id.as_macro_call_id()) + .filter_map(|it| it.source(db).0.as_macro_call_id()) { if let Some(exp) = db.expand_macro_invocation(macro_call_id) { let loc = macro_call_id.loc(db); diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 6d6150096..3801e498e 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -696,7 +696,9 @@ impl From for Option { ModuleDef::Const(_) | ModuleDef::Static(_) | ModuleDef::Def(_) - | ModuleDef::Module(_) => return None, + | ModuleDef::Module(_) + | ModuleDef::Trait(_) + | ModuleDef::Type(_) => return None, }; Some(res) } -- cgit v1.2.3