From 1db2cbcb8bd61b4f19f61cc6319343e5ad894515 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 25 Jan 2019 00:50:08 +0300 Subject: move consts&statics to new id --- crates/ra_hir/src/code_model_api.rs | 38 +++++++++++----------- crates/ra_hir/src/code_model_impl/module.rs | 6 +++- crates/ra_hir/src/ids.rs | 32 +++++++++++------- crates/ra_hir/src/nameres/lower.rs | 24 +++++++++++--- crates/ra_hir/src/ty.rs | 5 ++- crates/ra_ide_api/src/completion/complete_path.rs | 2 ++ .../ra_ide_api/src/completion/completion_item.rs | 4 +-- crates/ra_ide_api/src/navigation_target.rs | 22 ++++++++----- 8 files changed, 86 insertions(+), 47 deletions(-) diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 1fa591ea4..d82dda79a 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -16,7 +16,7 @@ use crate::{ code_model_impl::def_id_to_ast, docs::{Documentation, Docs, docs_from_ast}, module_tree::ModuleId, - ids::{FunctionId, StructId, EnumId, EnumVariantId, AstItemDef}, + ids::{FunctionId, StructId, EnumId, EnumVariantId, AstItemDef, ConstId, StaticId}, }; /// hir::Crate describes a single crate. It's the main interface with which @@ -47,8 +47,6 @@ impl Crate { #[derive(Debug)] pub enum Def { - Const(Const), - Static(Static), Trait(Trait), Type(Type), Item, @@ -67,11 +65,21 @@ pub enum ModuleDef { Function(Function), Struct(Struct), Enum(Enum), - // Can't be directly declared, but can be imported. EnumVariant(EnumVariant), + Const(Const), + Static(Static), + // Can't be directly declared, but can be imported. Def(DefId), } -impl_froms!(ModuleDef: Module, Function, Struct, Enum, EnumVariant); +impl_froms!( + ModuleDef: Module, + Function, + Struct, + Enum, + EnumVariant, + Const, + Static +); impl From for ModuleDef { fn from(it: DefId) -> ModuleDef { @@ -386,18 +394,14 @@ impl Docs for Function { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct Const { - pub(crate) def_id: DefId, + pub(crate) id: ConstId, } impl Const { - pub(crate) fn new(def_id: DefId) -> Const { - Const { def_id } - } - pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc) { - def_id_to_ast(db, self.def_id) + self.id.source(db) } } @@ -407,18 +411,14 @@ impl Docs for Const { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct Static { - pub(crate) def_id: DefId, + pub(crate) id: StaticId, } impl Static { - pub(crate) fn new(def_id: DefId) -> Static { - Static { def_id } - } - pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc) { - def_id_to_ast(db, self.def_id) + self.id.source(db) } } diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs index aa5e5d689..1518825c7 100644 --- a/crates/ra_hir/src/code_model_impl/module.rs +++ b/crates/ra_hir/src/code_model_impl/module.rs @@ -147,7 +147,11 @@ impl Module { None => PerNs::none(), } } - ModuleDef::Function(_) | ModuleDef::Struct(_) | ModuleDef::EnumVariant(_) => { + 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) diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index 4b4e07e58..9aae58bb6 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, Static, Const, + Module, Trait, Type, }; #[derive(Debug, Default)] @@ -20,6 +20,8 @@ pub struct HirInterner { structs: LocationIntener, StructId>, enums: LocationIntener, EnumId>, enum_variants: LocationIntener, EnumVariantId>, + consts: LocationIntener, ConstId>, + statics: LocationIntener, StaticId>, } impl HirInterner { @@ -246,6 +248,24 @@ impl AstItemDef for EnumVariantId { } } +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct ConstId(RawId); +impl_arena_id!(ConstId); +impl AstItemDef for ConstId { + fn interner(interner: &HirInterner) -> &LocationIntener, Self> { + &interner.consts + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct StaticId(RawId); +impl_arena_id!(StaticId); +impl AstItemDef for StaticId { + fn interner(interner: &HirInterner) -> &LocationIntener, Self> { + &interner.statics + } +} + /// 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)] @@ -261,8 +281,6 @@ pub struct DefLoc { #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub(crate) enum DefKind { - Const, - Static, Trait, Type, Item, @@ -286,14 +304,6 @@ impl DefId { pub fn resolve(self, db: &impl HirDatabase) -> Def { let loc = self.loc(db); match loc.kind { - DefKind::Const => { - let def = Const::new(self); - Def::Const(def) - } - DefKind::Static => { - let def = Static::new(self); - Def::Static(def) - } DefKind::Trait => { let def = Trait::new(self); Def::Trait(def) diff --git a/crates/ra_hir/src/nameres/lower.rs b/crates/ra_hir/src/nameres/lower.rs index 0056bdd5a..6a86e5fd4 100644 --- a/crates/ra_hir/src/nameres/lower.rs +++ b/crates/ra_hir/src/nameres/lower.rs @@ -10,7 +10,7 @@ use rustc_hash::FxHashMap; use crate::{ SourceItemId, Path, ModuleSource, HirDatabase, Name, SourceFileItems, HirFileId, MacroCallLoc, AsName, PerNs, DefKind, DefLoc, Function, - ModuleDef, Module, Struct, Enum, + ModuleDef, Module, Struct, Enum, Const, Static, ids::LocationCtx, }; @@ -187,8 +187,22 @@ impl LoweredModule { // TODO return; } - ast::ModuleItemKind::ConstDef(it) => it.name(), - ast::ModuleItemKind::StaticDef(it) => it.name(), + ast::ModuleItemKind::ConstDef(it) => { + if let Some(name) = it.name() { + let c = Const { id: ctx.to_def(it) }; + self.declarations + .insert(name.as_name(), PerNs::values(c.into())); + } + return; + } + ast::ModuleItemKind::StaticDef(it) => { + if let Some(name) = it.name() { + let s = Static { id: ctx.to_def(it) }; + self.declarations + .insert(name.as_name(), PerNs::values(s.into())); + } + return; + } ast::ModuleItemKind::Module(_) => { // modules are handled separately direclty by nameres return; @@ -246,8 +260,8 @@ impl DefKind { SyntaxKind::ENUM_DEF => unreachable!(), SyntaxKind::TRAIT_DEF => PerNs::types(DefKind::Trait), SyntaxKind::TYPE_DEF => PerNs::types(DefKind::Type), - SyntaxKind::CONST_DEF => PerNs::values(DefKind::Const), - SyntaxKind::STATIC_DEF => PerNs::values(DefKind::Static), + SyntaxKind::CONST_DEF => unreachable!(), + SyntaxKind::STATIC_DEF => unreachable!(), _ => PerNs::none(), } } diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 9a1a90eed..6d6150096 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -693,7 +693,10 @@ impl From for Option { ModuleDef::Struct(s) => s.into(), ModuleDef::Enum(e) => e.into(), ModuleDef::EnumVariant(v) => v.into(), - ModuleDef::Def(_) | ModuleDef::Module(_) => return None, + ModuleDef::Const(_) + | ModuleDef::Static(_) + | ModuleDef::Def(_) + | ModuleDef::Module(_) => return None, }; Some(res) } diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index 4768efdc1..bd5484516 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs @@ -43,6 +43,8 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { hir::ModuleDef::Function(_) | hir::ModuleDef::Struct(_) | hir::ModuleDef::Def(_) + | hir::ModuleDef::Const(_) + | hir::ModuleDef::Static(_) | hir::ModuleDef::EnumVariant(_) => return, }; } diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs index ee1f8bce0..2f8ec41d2 100644 --- a/crates/ra_ide_api/src/completion/completion_item.rs +++ b/crates/ra_ide_api/src/completion/completion_item.rs @@ -223,11 +223,11 @@ impl Builder { hir::ModuleDef::Struct(it) => (CompletionItemKind::Struct, it.docs(ctx.db)), hir::ModuleDef::Enum(it) => (CompletionItemKind::Enum, it.docs(ctx.db)), hir::ModuleDef::EnumVariant(it) => (CompletionItemKind::EnumVariant, it.docs(ctx.db)), + hir::ModuleDef::Const(it) => (CompletionItemKind::Const, it.docs(ctx.db)), + hir::ModuleDef::Static(it) => (CompletionItemKind::Static, it.docs(ctx.db)), hir::ModuleDef::Def(def_id) => match def_id.resolve(ctx.db) { hir::Def::Trait(it) => (CompletionItemKind::Trait, it.docs(ctx.db)), hir::Def::Type(it) => (CompletionItemKind::TypeAlias, it.docs(ctx.db)), - hir::Def::Const(it) => (CompletionItemKind::Const, it.docs(ctx.db)), - hir::Def::Static(it) => (CompletionItemKind::Static, it.docs(ctx.db)), _ => return self, }, }; diff --git a/crates/ra_ide_api/src/navigation_target.rs b/crates/ra_ide_api/src/navigation_target.rs index fc76338d2..8406fee31 100644 --- a/crates/ra_ide_api/src/navigation_target.rs +++ b/crates/ra_ide_api/src/navigation_target.rs @@ -121,6 +121,20 @@ impl NavigationTarget { &*node, )); } + hir::ModuleDef::Const(s) => { + let (file_id, node) = s.source(db); + return Some(NavigationTarget::from_named( + file_id.original_file(db), + &*node, + )); + } + hir::ModuleDef::Static(s) => { + let (file_id, node) = s.source(db); + return Some(NavigationTarget::from_named( + file_id.original_file(db), + &*node, + )); + } hir::ModuleDef::Enum(e) => { let (file_id, node) = e.source(db); return Some(NavigationTarget::from_named( @@ -146,14 +160,6 @@ impl NavigationTarget { let (file_id, node) = f.source(db); NavigationTarget::from_named(file_id.original_file(db), &*node) } - Def::Static(f) => { - let (file_id, node) = f.source(db); - NavigationTarget::from_named(file_id.original_file(db), &*node) - } - Def::Const(f) => { - let (file_id, node) = f.source(db); - NavigationTarget::from_named(file_id.original_file(db), &*node) - } Def::Item => return None, }; Some(res) -- cgit v1.2.3