From fb8b354dcc837d5eb9b81fc205e4282a203df177 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 26 Mar 2019 17:25:14 +0300 Subject: add typed ids --- crates/ra_hir/src/code_model_impl/module.rs | 18 +++++------ crates/ra_hir/src/lib.rs | 2 +- crates/ra_hir/src/nameres.rs | 20 ++++++------ crates/ra_hir/src/nameres/collector.rs | 22 ++++++------- crates/ra_hir/src/nameres/raw.rs | 13 ++++---- crates/ra_hir/src/source_binder.rs | 8 ++--- crates/ra_hir/src/source_id.rs | 50 ++++++++++++++++++++++++++++- 7 files changed, 88 insertions(+), 45 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs index 790e2b80f..0edb8ade5 100644 --- a/crates/ra_hir/src/code_model_impl/module.rs +++ b/crates/ra_hir/src/code_model_impl/module.rs @@ -1,18 +1,18 @@ use ra_db::FileId; -use ra_syntax::{ast, TreeArc, AstNode}; +use ra_syntax::{ast, TreeArc}; use crate::{ - Module, ModuleSource, Name, + Module, ModuleSource, Name, AstId, nameres::{CrateModuleId, ImportId}, HirDatabase, DefDatabase, - HirFileId, SourceItemId, + HirFileId, }; impl ModuleSource { pub(crate) fn new( db: &impl DefDatabase, file_id: Option, - decl_id: Option, + decl_id: Option>, ) -> ModuleSource { match (file_id, decl_id) { (Some(file_id), _) => { @@ -20,8 +20,7 @@ impl ModuleSource { ModuleSource::SourceFile(source_file) } (None, Some(item_id)) => { - let module = db.file_item(item_id); - let module = ast::Module::cast(&*module).unwrap(); + let module = item_id.to_node(db); assert!(module.item_list().is_some(), "expected inline module"); ModuleSource::Module(module.to_owned()) } @@ -55,7 +54,7 @@ impl Module { let decl_id = def_map[self.module_id].declaration; let file_id = def_map[self.module_id].definition; let module_source = ModuleSource::new(db, file_id, decl_id); - let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id); + let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id()); (file_id, module_source) } @@ -65,9 +64,8 @@ impl Module { ) -> Option<(HirFileId, TreeArc)> { let def_map = db.crate_def_map(self.krate); let decl = def_map[self.module_id].declaration?; - let syntax_node = db.file_item(decl); - let ast = ast::Module::cast(&syntax_node).unwrap().to_owned(); - Some((decl.file_id, ast)) + let ast = decl.to_node(db); + Some((decl.file_id(), ast)) } pub(crate) fn import_source_impl( diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index ac2585de0..3fc08c55c 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -48,7 +48,7 @@ mod marks; use crate::{ db::{HirDatabase, DefDatabase}, name::{AsName, KnownName}, - source_id::SourceFileItemId, + source_id::{SourceFileItemId, FileAstId, AstId}, }; pub use self::{ diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index e962bbd31..67b9d6986 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -59,13 +59,15 @@ use rustc_hash::FxHashMap; use ra_arena::{Arena, RawId, impl_arena_id}; use ra_db::{FileId, Edition}; use test_utils::tested_by; +use ra_syntax::ast; use crate::{ - ModuleDef, Name, Crate, Module, SourceItemId, + ModuleDef, Name, Crate, Module, DefDatabase, Path, PathKind, HirFileId, Trait, ids::MacroDefId, diagnostics::DiagnosticSink, nameres::diagnostics::DefDiagnostic, + AstId, }; pub(crate) use self::raw::{RawItems, ImportId, ImportSourceMap}; @@ -106,7 +108,7 @@ pub(crate) struct ModuleData { pub(crate) children: FxHashMap, pub(crate) scope: ModuleScope, /// None for root - pub(crate) declaration: Option, + pub(crate) declaration: Option>, /// None for inline modules. /// /// Note that non-inline modules, by definition, live inside non-macro file. @@ -225,7 +227,7 @@ impl CrateDefMap { pub(crate) fn find_module_by_source( &self, file_id: HirFileId, - decl_id: Option, + decl_id: Option>, ) -> Option { let (module_id, _module_data) = self.modules.iter().find(|(_module_id, module_data)| { if decl_id.is_some() { @@ -429,10 +431,10 @@ impl CrateDefMap { mod diagnostics { use relative_path::RelativePathBuf; - use ra_syntax::{AstPtr, AstNode, ast}; + use ra_syntax::{AstPtr, ast}; use crate::{ - SourceItemId, DefDatabase, + AstId, DefDatabase, nameres::CrateModuleId, diagnostics::{DiagnosticSink, UnresolvedModule}, }; @@ -441,7 +443,7 @@ mod diagnostics { pub(super) enum DefDiagnostic { UnresolvedModule { module: CrateModuleId, - declaration: SourceItemId, + declaration: AstId, candidate: RelativePathBuf, }, } @@ -458,10 +460,9 @@ mod diagnostics { if *module != target_module { return; } - let syntax = db.file_item(*declaration); - let decl = ast::Module::cast(&syntax).unwrap(); + let decl = declaration.to_node(db); sink.push(UnresolvedModule { - file: declaration.file_id, + file: declaration.file_id(), decl: AstPtr::new(&decl), candidate: candidate.clone(), }) @@ -469,5 +470,4 @@ mod diagnostics { } } } - } diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs index 4fb298155..e6fd8632a 100644 --- a/crates/ra_hir/src/nameres/collector.rs +++ b/crates/ra_hir/src/nameres/collector.rs @@ -3,6 +3,7 @@ use rustc_hash::FxHashMap; use relative_path::RelativePathBuf; use test_utils::tested_by; use ra_db::FileId; +use ra_syntax::ast; use crate::{ Function, Module, Struct, Enum, Const, Static, Trait, TypeAlias, @@ -15,6 +16,7 @@ use crate::{ raw, }, ids::{AstItemDef, LocationCtx, MacroCallLoc, MacroCallId, MacroDefId}, + AstId, }; pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { @@ -364,12 +366,9 @@ where fn collect_module(&mut self, module: &raw::ModuleData) { match module { // inline module, just recurse - raw::ModuleData::Definition { name, items, source_item_id } => { - let module_id = self.push_child_module( - name.clone(), - source_item_id.with_file_id(self.file_id), - None, - ); + raw::ModuleData::Definition { name, items, ast_id } => { + let module_id = + self.push_child_module(name.clone(), ast_id.with_file_id(self.file_id), None); ModCollector { def_collector: &mut *self.def_collector, module_id, @@ -379,13 +378,12 @@ where .collect(&*items); } // out of line module, resovle, parse and recurse - raw::ModuleData::Declaration { name, source_item_id } => { - let source_item_id = source_item_id.with_file_id(self.file_id); + raw::ModuleData::Declaration { name, ast_id } => { + let ast_id = ast_id.with_file_id(self.file_id); let is_root = self.def_collector.def_map.modules[self.module_id].parent.is_none(); match resolve_submodule(self.def_collector.db, self.file_id, name, is_root) { Ok(file_id) => { - let module_id = - self.push_child_module(name.clone(), source_item_id, Some(file_id)); + let module_id = self.push_child_module(name.clone(), ast_id, Some(file_id)); let raw_items = self.def_collector.db.raw_items(file_id.into()); ModCollector { def_collector: &mut *self.def_collector, @@ -398,7 +396,7 @@ where Err(candidate) => self.def_collector.def_map.diagnostics.push( DefDiagnostic::UnresolvedModule { module: self.module_id, - declaration: source_item_id, + declaration: ast_id, candidate, }, ), @@ -410,7 +408,7 @@ where fn push_child_module( &mut self, name: Name, - declaration: SourceItemId, + declaration: AstId, definition: Option, ) -> CrateModuleId { let modules = &mut self.def_collector.def_map.modules; diff --git a/crates/ra_hir/src/nameres/raw.rs b/crates/ra_hir/src/nameres/raw.rs index f32004601..09acd5a98 100644 --- a/crates/ra_hir/src/nameres/raw.rs +++ b/crates/ra_hir/src/nameres/raw.rs @@ -12,7 +12,7 @@ use ra_syntax::{ use crate::{ DefDatabase, Name, AsName, Path, HirFileId, ModuleSource, - SourceFileItemId, SourceFileItems, + SourceFileItemId, SourceFileItems, FileAstId, }; /// `RawItems` is a set of top-level items in a file (except for impls). @@ -115,8 +115,8 @@ impl_arena_id!(Module); #[derive(Debug, PartialEq, Eq)] pub(super) enum ModuleData { - Declaration { name: Name, source_item_id: SourceFileItemId }, - Definition { name: Name, source_item_id: SourceFileItemId, items: Vec }, + Declaration { name: Name, ast_id: FileAstId }, + Definition { name: Name, ast_id: FileAstId, items: Vec }, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -221,10 +221,9 @@ impl RawItemsCollector { Some(it) => it.as_name(), None => return, }; - let source_item_id = self.source_file_items.id_of_unchecked(module.syntax()); + let ast_id = self.source_file_items.ast_id(module); if module.has_semi() { - let item = - self.raw_items.modules.alloc(ModuleData::Declaration { name, source_item_id }); + let item = self.raw_items.modules.alloc(ModuleData::Declaration { name, ast_id }); self.push_item(current_module, RawItem::Module(item)); return; } @@ -232,7 +231,7 @@ impl RawItemsCollector { if let Some(item_list) = module.item_list() { let item = self.raw_items.modules.alloc(ModuleData::Definition { name, - source_item_id, + ast_id, items: Vec::new(), }); self.process_module(Some(item), item_list); diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index db9e3a22e..f9d2d0247 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -13,10 +13,10 @@ use ra_syntax::{ }; use crate::{ - HirDatabase, Function, Struct, Enum, SourceItemId, + HirDatabase, Function, Struct, Enum, AsName, Module, HirFileId, Crate, Trait, Resolver, ids::LocationCtx, - expr + expr, AstId }; /// Locates the module by `FileId`. Picks topmost module in the file. @@ -55,7 +55,7 @@ fn module_from_inline( assert!(!module.has_semi()); let file_id = file_id.into(); let file_items = db.file_items(file_id); - let item_id = file_items.id_of(file_id, module.syntax()).with_file_id(file_id); + let item_id = file_items.ast_id(module).with_file_id(file_id); module_from_source(db, file_id, Some(item_id)) } @@ -75,7 +75,7 @@ pub fn module_from_child_node( fn module_from_source( db: &impl HirDatabase, file_id: HirFileId, - decl_id: Option, + decl_id: Option>, ) -> Option { let source_root_id = db.file_source_root(file_id.as_original_file()); db.source_root_crates(source_root_id).iter().map(|&crate_id| Crate { crate_id }).find_map( diff --git a/crates/ra_hir/src/source_id.rs b/crates/ra_hir/src/source_id.rs index 62707ba6a..881cc590e 100644 --- a/crates/ra_hir/src/source_id.rs +++ b/crates/ra_hir/src/source_id.rs @@ -1,10 +1,55 @@ -use std::sync::Arc; +use std::{marker::PhantomData, sync::Arc}; use ra_arena::{Arena, RawId, impl_arena_id}; use ra_syntax::{SyntaxNodePtr, TreeArc, SyntaxNode, SourceFile, AstNode, ast}; use crate::{HirFileId, DefDatabase}; +#[derive(Debug, PartialEq, Eq)] +pub(crate) struct AstId { + file_id: HirFileId, + file_ast_id: FileAstId, +} + +impl Clone for AstId { + fn clone(&self) -> AstId { + *self + } +} + +impl Copy for AstId {} + +impl AstId { + pub(crate) fn file_id(&self) -> HirFileId { + self.file_id + } + + pub(crate) fn to_node(&self, db: &impl DefDatabase) -> TreeArc { + let syntax_node = db.file_item(self.file_ast_id.raw.with_file_id(self.file_id)); + N::cast(&syntax_node).unwrap().to_owned() + } +} + +#[derive(Debug, PartialEq, Eq)] +pub(crate) struct FileAstId { + raw: SourceFileItemId, + _ty: PhantomData, +} + +impl Clone for FileAstId { + fn clone(&self) -> FileAstId { + *self + } +} + +impl Copy for FileAstId {} + +impl FileAstId { + pub(crate) fn with_file_id(self, file_id: HirFileId) -> AstId { + AstId { file_id, file_ast_id: self } + } +} + /// Identifier of item within a specific file. This is stable over reparses, so /// it's OK to use it as a salsa key/value. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -90,6 +135,9 @@ impl SourceFileItems { self.arena.iter().map(|(_id, i)| i).collect::>(), ); } + pub(crate) fn ast_id(&self, item: &N) -> FileAstId { + FileAstId { raw: self.id_of_unchecked(item.syntax()), _ty: PhantomData } + } } impl std::ops::Index for SourceFileItems { -- cgit v1.2.3 From 071a19537d4399fd04d1e9594ab7878502a12d21 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 26 Mar 2019 18:03:17 +0300 Subject: strongy-typed ids for macros --- crates/ra_hir/src/ids.rs | 18 ++++++------------ crates/ra_hir/src/nameres/collector.rs | 24 ++++++++---------------- crates/ra_hir/src/nameres/raw.rs | 6 +++--- crates/ra_hir/src/source_id.rs | 4 ++-- 4 files changed, 19 insertions(+), 33 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index e73dd5d21..b503e0ee5 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs @@ -10,7 +10,7 @@ use ra_arena::{RawId, ArenaId, impl_arena_id}; use mbe::MacroRules; use crate::{ - Module, DefDatabase, SourceItemId, SourceFileItemId, + Module, DefDatabase, SourceItemId, SourceFileItemId, AstId, }; #[derive(Debug, Default)] @@ -68,7 +68,7 @@ impl HirFileId { HirFileIdRepr::File(file_id) => file_id, HirFileIdRepr::Macro(macro_call_id) => { let loc = macro_call_id.loc(db); - loc.source_item_id.file_id.original_file(db) + loc.ast_id.file_id().original_file(db) } } } @@ -96,8 +96,7 @@ impl HirFileId { fn parse_macro(db: &impl DefDatabase, macro_call_id: MacroCallId) -> Option> { let loc = macro_call_id.loc(db); - let syntax = db.file_item(loc.source_item_id); - let macro_call = ast::MacroCall::cast(&syntax).unwrap(); + let macro_call = loc.ast_id.to_node(db); let (macro_arg, _) = macro_call.token_tree().and_then(mbe::ast_to_token_tree)?; let macro_rules = db.macro_def(loc.def)?; @@ -124,15 +123,10 @@ impl From for HirFileId { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub enum MacroDefId { - MacroByExample { source_item_id: SourceItemId }, -} +pub struct MacroDefId(pub(crate) AstId); pub(crate) fn macro_def_query(db: &impl DefDatabase, id: MacroDefId) -> Option> { - let syntax_node = match id { - MacroDefId::MacroByExample { source_item_id } => db.file_item(source_item_id), - }; - let macro_call = ast::MacroCall::cast(&syntax_node).unwrap(); + let macro_call = id.0.to_node(db); let arg = macro_call.token_tree()?; let (tt, _) = mbe::ast_to_token_tree(arg)?; let rules = MacroRules::parse(&tt).ok()?; @@ -148,7 +142,7 @@ impl_arena_id!(MacroCallId); #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct MacroCallLoc { pub(crate) def: MacroDefId, - pub(crate) source_item_id: SourceItemId, + pub(crate) ast_id: AstId, } impl MacroCallId { diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs index e6fd8632a..b5f02ab80 100644 --- a/crates/ra_hir/src/nameres/collector.rs +++ b/crates/ra_hir/src/nameres/collector.rs @@ -7,7 +7,7 @@ use ra_syntax::ast; use crate::{ Function, Module, Struct, Enum, Const, Static, Trait, TypeAlias, - DefDatabase, HirFileId, Name, Path, SourceItemId, + DefDatabase, HirFileId, Name, Path, KnownName, nameres::{ Resolution, PerNs, ModuleDef, ReachedFixedPoint, ResolveMode, @@ -53,7 +53,7 @@ struct DefCollector { def_map: CrateDefMap, glob_imports: FxHashMap>, unresolved_imports: Vec<(CrateModuleId, raw::ImportId, raw::ImportData)>, - unexpanded_macros: Vec<(CrateModuleId, SourceItemId, Path)>, + unexpanded_macros: Vec<(CrateModuleId, AstId, Path)>, global_macro_scope: FxHashMap, } @@ -295,7 +295,7 @@ where let mut macros = std::mem::replace(&mut self.unexpanded_macros, Vec::new()); let mut resolved = Vec::new(); let mut res = ReachedFixedPoint::Yes; - macros.retain(|(module_id, source_item_id, path)| { + macros.retain(|(module_id, ast_id, path)| { if path.segments.len() != 2 { return true; } @@ -311,8 +311,7 @@ where res = ReachedFixedPoint::No; let def_map = self.db.crate_def_map(krate); if let Some(macro_id) = def_map.public_macros.get(&path.segments[1].name).cloned() { - let call_id = - MacroCallLoc { def: macro_id, source_item_id: *source_item_id }.id(self.db); + let call_id = MacroCallLoc { def: macro_id, ast_id: *ast_id }.id(self.db); resolved.push((*module_id, call_id)); } false @@ -456,34 +455,27 @@ where // Case 1: macro rules, define a macro in crate-global mutable scope if is_macro_rules(&mac.path) { if let Some(name) = &mac.name { - let macro_id = MacroDefId::MacroByExample { - source_item_id: mac.source_item_id.with_file_id(self.file_id), - }; + let macro_id = MacroDefId(mac.ast_id.with_file_id(self.file_id)); self.def_collector.define_macro(name.clone(), macro_id, mac.export) } return; } - let source_item_id = SourceItemId { file_id: self.file_id, item_id: mac.source_item_id }; + let ast_id = mac.ast_id.with_file_id(self.file_id); // Case 2: try to expand macro_rules from this crate, triggering // recursive item collection. if let Some(¯o_id) = mac.path.as_ident().and_then(|name| self.def_collector.global_macro_scope.get(name)) { - let macro_call_id = - MacroCallLoc { def: macro_id, source_item_id }.id(self.def_collector.db); + let macro_call_id = MacroCallLoc { def: macro_id, ast_id }.id(self.def_collector.db); self.def_collector.collect_macro_expansion(self.module_id, macro_call_id); return; } // Case 3: path to a macro from another crate, expand during name resolution - self.def_collector.unexpanded_macros.push(( - self.module_id, - source_item_id, - mac.path.clone(), - )) + self.def_collector.unexpanded_macros.push((self.module_id, ast_id, mac.path.clone())) } } diff --git a/crates/ra_hir/src/nameres/raw.rs b/crates/ra_hir/src/nameres/raw.rs index 09acd5a98..684bd1d50 100644 --- a/crates/ra_hir/src/nameres/raw.rs +++ b/crates/ra_hir/src/nameres/raw.rs @@ -160,7 +160,7 @@ impl_arena_id!(Macro); #[derive(Debug, PartialEq, Eq)] pub(super) struct MacroData { - pub(super) source_item_id: SourceFileItemId, + pub(super) ast_id: FileAstId, pub(super) path: Path, pub(super) name: Option, pub(super) export: bool, @@ -285,9 +285,9 @@ impl RawItemsCollector { }; let name = m.name().map(|it| it.as_name()); - let source_item_id = self.source_file_items.id_of_unchecked(m.syntax()); + let ast_id = self.source_file_items.ast_id(m); let export = m.has_atom_attr("macro_export"); - let m = self.raw_items.macros.alloc(MacroData { source_item_id, path, name, export }); + let m = self.raw_items.macros.alloc(MacroData { ast_id, path, name, export }); self.push_item(current_module, RawItem::Macro(m)); } diff --git a/crates/ra_hir/src/source_id.rs b/crates/ra_hir/src/source_id.rs index 881cc590e..1dadd76c5 100644 --- a/crates/ra_hir/src/source_id.rs +++ b/crates/ra_hir/src/source_id.rs @@ -5,7 +5,7 @@ use ra_syntax::{SyntaxNodePtr, TreeArc, SyntaxNode, SourceFile, AstNode, ast}; use crate::{HirFileId, DefDatabase}; -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, PartialEq, Eq, Hash)] pub(crate) struct AstId { file_id: HirFileId, file_ast_id: FileAstId, @@ -30,7 +30,7 @@ impl AstId { } } -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, PartialEq, Eq, Hash)] pub(crate) struct FileAstId { raw: SourceFileItemId, _ty: PhantomData, -- cgit v1.2.3 From 8f324773127c733b12d1c5ee98a3d9c6a5360db0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 26 Mar 2019 18:27:22 +0300 Subject: more type safety --- crates/ra_hir/src/ids.rs | 32 +++++++++-------------- crates/ra_hir/src/lib.rs | 2 +- crates/ra_hir/src/nameres/collector.rs | 23 ++++++++-------- crates/ra_hir/src/nameres/raw.rs | 48 +++++++++++++++++++++------------- crates/ra_hir/src/source_id.rs | 40 +++++++++++++++++++--------- 5 files changed, 82 insertions(+), 63 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index b503e0ee5..c2df5ce00 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs @@ -1,5 +1,4 @@ use std::{ - marker::PhantomData, hash::{Hash, Hasher}, sync::Arc, }; @@ -10,7 +9,7 @@ use ra_arena::{RawId, ArenaId, impl_arena_id}; use mbe::MacroRules; use crate::{ - Module, DefDatabase, SourceItemId, SourceFileItemId, AstId, + Module, DefDatabase, AstId, FileAstId, }; #[derive(Debug, Default)] @@ -123,6 +122,7 @@ impl From for HirFileId { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] + pub struct MacroDefId(pub(crate) AstId); pub(crate) fn macro_def_query(db: &impl DefDatabase, id: MacroDefId) -> Option> { @@ -161,26 +161,25 @@ impl MacroCallLoc { #[derive(Debug)] pub struct ItemLoc { pub(crate) module: Module, - raw: SourceItemId, - _ty: PhantomData, + ast_id: AstId, } impl PartialEq for ItemLoc { fn eq(&self, other: &Self) -> bool { - self.module == other.module && self.raw == other.raw + self.module == other.module && self.ast_id == other.ast_id } } impl Eq for ItemLoc {} impl Hash for ItemLoc { fn hash(&self, hasher: &mut H) { self.module.hash(hasher); - self.raw.hash(hasher); + self.ast_id.hash(hasher); } } impl Clone for ItemLoc { fn clone(&self) -> ItemLoc { - ItemLoc { module: self.module, raw: self.raw, _ty: PhantomData } + ItemLoc { module: self.module, ast_id: self.ast_id } } } @@ -208,25 +207,18 @@ pub(crate) trait AstItemDef: ArenaId + Clone { fn interner(interner: &HirInterner) -> &LocationInterner, Self>; fn from_ast(ctx: LocationCtx<&impl DefDatabase>, ast: &N) -> Self { let items = ctx.db.file_items(ctx.file_id); - let item_id = items.id_of(ctx.file_id, ast.syntax()); - Self::from_source_item_id_unchecked(ctx, item_id) + let item_id = items.ast_id(ast); + Self::from_ast_id(ctx, item_id) } - fn from_source_item_id_unchecked( - ctx: LocationCtx<&impl DefDatabase>, - item_id: SourceFileItemId, - ) -> Self { - let raw = SourceItemId { file_id: ctx.file_id, item_id }; - let loc = ItemLoc { module: ctx.module, raw, _ty: PhantomData }; - + fn from_ast_id(ctx: LocationCtx<&impl DefDatabase>, ast_id: FileAstId) -> Self { + let loc = ItemLoc { module: ctx.module, ast_id: ast_id.with_file_id(ctx.file_id) }; Self::interner(ctx.db.as_ref()).loc2id(&loc) } fn source(self, db: &impl DefDatabase) -> (HirFileId, TreeArc) { let int = Self::interner(db.as_ref()); let loc = int.id2loc(self); - let syntax = db.file_item(loc.raw); - let ast = - N::cast(&syntax).unwrap_or_else(|| panic!("invalid ItemLoc: {:?}", loc.raw)).to_owned(); - (loc.raw.file_id, ast) + let ast = loc.ast_id.to_node(db); + (loc.ast_id.file_id(), ast) } fn module(self, db: &impl DefDatabase) -> Module { let int = Self::interner(db.as_ref()); diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 3fc08c55c..b1f388b06 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -48,7 +48,7 @@ mod marks; use crate::{ db::{HirDatabase, DefDatabase}, name::{AsName, KnownName}, - source_id::{SourceFileItemId, FileAstId, AstId}, + source_id::{FileAstId, AstId}, }; pub use self::{ diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs index b5f02ab80..39cadc94a 100644 --- a/crates/ra_hir/src/nameres/collector.rs +++ b/crates/ra_hir/src/nameres/collector.rs @@ -429,23 +429,24 @@ where fn define_def(&mut self, def: &raw::DefData) { let module = Module { krate: self.def_collector.def_map.krate, module_id: self.module_id }; let ctx = LocationCtx::new(self.def_collector.db, module, self.file_id.into()); - macro_rules! id { - () => { - AstItemDef::from_source_item_id_unchecked(ctx, def.source_item_id) + + macro_rules! def { + ($kind:ident, $ast_id:ident) => { + $kind { id: AstItemDef::from_ast_id(ctx, $ast_id) }.into() }; } let name = def.name.clone(); let def: PerNs = match def.kind { - raw::DefKind::Function => PerNs::values(Function { id: id!() }.into()), - raw::DefKind::Struct => { - let s = Struct { id: id!() }.into(); + raw::DefKind::Function(ast_id) => PerNs::values(def!(Function, ast_id)), + raw::DefKind::Struct(ast_id) => { + let s = def!(Struct, ast_id); PerNs::both(s, s) } - raw::DefKind::Enum => PerNs::types(Enum { id: id!() }.into()), - raw::DefKind::Const => PerNs::values(Const { id: id!() }.into()), - raw::DefKind::Static => PerNs::values(Static { id: id!() }.into()), - raw::DefKind::Trait => PerNs::types(Trait { id: id!() }.into()), - raw::DefKind::TypeAlias => PerNs::types(TypeAlias { id: id!() }.into()), + raw::DefKind::Enum(ast_id) => PerNs::types(def!(Enum, ast_id)), + raw::DefKind::Const(ast_id) => PerNs::values(def!(Const, ast_id)), + raw::DefKind::Static(ast_id) => PerNs::values(def!(Static, ast_id)), + raw::DefKind::Trait(ast_id) => PerNs::types(def!(Trait, ast_id)), + raw::DefKind::TypeAlias(ast_id) => PerNs::types(def!(TypeAlias, ast_id)), }; let resolution = Resolution { def, import: None }; self.def_collector.update(self.module_id, None, &[(name, resolution)]) diff --git a/crates/ra_hir/src/nameres/raw.rs b/crates/ra_hir/src/nameres/raw.rs index 684bd1d50..984478adc 100644 --- a/crates/ra_hir/src/nameres/raw.rs +++ b/crates/ra_hir/src/nameres/raw.rs @@ -12,7 +12,7 @@ use ra_syntax::{ use crate::{ DefDatabase, Name, AsName, Path, HirFileId, ModuleSource, - SourceFileItemId, SourceFileItems, FileAstId, + SourceFileItems, FileAstId, }; /// `RawItems` is a set of top-level items in a file (except for impls). @@ -138,20 +138,19 @@ impl_arena_id!(Def); #[derive(Debug, PartialEq, Eq)] pub(super) struct DefData { - pub(super) source_item_id: SourceFileItemId, pub(super) name: Name, pub(super) kind: DefKind, } #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub(super) enum DefKind { - Function, - Struct, - Enum, - Const, - Static, - Trait, - TypeAlias, + Function(FileAstId), + Struct(FileAstId), + Enum(FileAstId), + Const(FileAstId), + Static(FileAstId), + Trait(FileAstId), + TypeAlias(FileAstId), } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -200,18 +199,31 @@ impl RawItemsCollector { // impls don't participate in name resolution return; } - ast::ModuleItemKind::StructDef(it) => (DefKind::Struct, it.name()), - ast::ModuleItemKind::EnumDef(it) => (DefKind::Enum, it.name()), - ast::ModuleItemKind::FnDef(it) => (DefKind::Function, it.name()), - ast::ModuleItemKind::TraitDef(it) => (DefKind::Trait, it.name()), - ast::ModuleItemKind::TypeAliasDef(it) => (DefKind::TypeAlias, it.name()), - ast::ModuleItemKind::ConstDef(it) => (DefKind::Const, it.name()), - ast::ModuleItemKind::StaticDef(it) => (DefKind::Static, it.name()), + ast::ModuleItemKind::StructDef(it) => { + (DefKind::Struct(self.source_file_items.ast_id(it)), it.name()) + } + ast::ModuleItemKind::EnumDef(it) => { + (DefKind::Enum(self.source_file_items.ast_id(it)), it.name()) + } + ast::ModuleItemKind::FnDef(it) => { + (DefKind::Function(self.source_file_items.ast_id(it)), it.name()) + } + ast::ModuleItemKind::TraitDef(it) => { + (DefKind::Trait(self.source_file_items.ast_id(it)), it.name()) + } + ast::ModuleItemKind::TypeAliasDef(it) => { + (DefKind::TypeAlias(self.source_file_items.ast_id(it)), it.name()) + } + ast::ModuleItemKind::ConstDef(it) => { + (DefKind::Const(self.source_file_items.ast_id(it)), it.name()) + } + ast::ModuleItemKind::StaticDef(it) => { + (DefKind::Static(self.source_file_items.ast_id(it)), it.name()) + } }; if let Some(name) = name { let name = name.as_name(); - let source_item_id = self.source_file_items.id_of_unchecked(item.syntax()); - let def = self.raw_items.defs.alloc(DefData { name, kind, source_item_id }); + let def = self.raw_items.defs.alloc(DefData { name, kind }); self.push_item(current_module, RawItem::Def(def)) } } diff --git a/crates/ra_hir/src/source_id.rs b/crates/ra_hir/src/source_id.rs index 1dadd76c5..2c855897a 100644 --- a/crates/ra_hir/src/source_id.rs +++ b/crates/ra_hir/src/source_id.rs @@ -1,11 +1,11 @@ -use std::{marker::PhantomData, sync::Arc}; +use std::{marker::PhantomData, sync::Arc, hash::{Hash, Hasher}}; use ra_arena::{Arena, RawId, impl_arena_id}; use ra_syntax::{SyntaxNodePtr, TreeArc, SyntaxNode, SourceFile, AstNode, ast}; use crate::{HirFileId, DefDatabase}; -#[derive(Debug, PartialEq, Eq, Hash)] +#[derive(Debug)] pub(crate) struct AstId { file_id: HirFileId, file_ast_id: FileAstId, @@ -16,9 +16,20 @@ impl Clone for AstId { *self } } - impl Copy for AstId {} +impl PartialEq for AstId { + fn eq(&self, other: &Self) -> bool { + (self.file_id, self.file_ast_id) == (other.file_id, other.file_ast_id) + } +} +impl Eq for AstId {} +impl Hash for AstId { + fn hash(&self, hasher: &mut H) { + (self.file_id, self.file_ast_id).hash(hasher); + } +} + impl AstId { pub(crate) fn file_id(&self) -> HirFileId { self.file_id @@ -30,7 +41,7 @@ impl AstId { } } -#[derive(Debug, PartialEq, Eq, Hash)] +#[derive(Debug)] pub(crate) struct FileAstId { raw: SourceFileItemId, _ty: PhantomData, @@ -41,9 +52,20 @@ impl Clone for FileAstId { *self } } - impl Copy for FileAstId {} +impl PartialEq for FileAstId { + fn eq(&self, other: &Self) -> bool { + self.raw == other.raw + } +} +impl Eq for FileAstId {} +impl Hash for FileAstId { + fn hash(&self, hasher: &mut H) { + self.raw.hash(hasher); + } +} + impl FileAstId { pub(crate) fn with_file_id(self, file_id: HirFileId) -> AstId { AstId { file_id, file_ast_id: self } @@ -116,14 +138,6 @@ impl SourceFileItems { fn alloc(&mut self, item: &SyntaxNode) -> SourceFileItemId { self.arena.alloc(SyntaxNodePtr::new(item)) } - pub(crate) fn id_of(&self, file_id: HirFileId, item: &SyntaxNode) -> SourceFileItemId { - assert_eq!( - self.file_id, file_id, - "SourceFileItems: wrong file, expected {:?}, got {:?}", - self.file_id, file_id - ); - self.id_of_unchecked(item) - } pub(crate) fn id_of_unchecked(&self, item: &SyntaxNode) -> SourceFileItemId { let ptr = SyntaxNodePtr::new(item); if let Some((id, _)) = self.arena.iter().find(|(_id, i)| **i == ptr) { -- cgit v1.2.3 From cffa3f960e401ac8100fd450b32c336c93181959 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 26 Mar 2019 18:32:46 +0300 Subject: make stuff private --- crates/ra_hir/src/source_id.rs | 40 ++++++++++++++-------------------------- 1 file changed, 14 insertions(+), 26 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/source_id.rs b/crates/ra_hir/src/source_id.rs index 2c855897a..04b7bb7b3 100644 --- a/crates/ra_hir/src/source_id.rs +++ b/crates/ra_hir/src/source_id.rs @@ -36,7 +36,9 @@ impl AstId { } pub(crate) fn to_node(&self, db: &impl DefDatabase) -> TreeArc { - let syntax_node = db.file_item(self.file_ast_id.raw.with_file_id(self.file_id)); + let source_item_id = + SourceItemId { file_id: self.file_id(), item_id: self.file_ast_id.raw }; + let syntax_node = db.file_item(source_item_id); N::cast(&syntax_node).unwrap().to_owned() } } @@ -75,19 +77,13 @@ impl FileAstId { /// Identifier of item within a specific file. This is stable over reparses, so /// it's OK to use it as a salsa key/value. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub(crate) struct SourceFileItemId(RawId); +struct SourceFileItemId(RawId); impl_arena_id!(SourceFileItemId); -impl SourceFileItemId { - pub(crate) fn with_file_id(self, file_id: HirFileId) -> SourceItemId { - SourceItemId { file_id, item_id: self } - } -} - #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct SourceItemId { - pub(crate) file_id: HirFileId, - pub(crate) item_id: SourceFileItemId, + file_id: HirFileId, + item_id: SourceFileItemId, } /// Maps items' `SyntaxNode`s to `SourceFileItemId`s and back. @@ -111,15 +107,16 @@ impl SourceFileItems { source_item_id: SourceItemId, ) -> TreeArc { let source_file = db.hir_parse(source_item_id.file_id); - db.file_items(source_item_id.file_id)[source_item_id.item_id] + db.file_items(source_item_id.file_id).arena[source_item_id.item_id] .to_node(&source_file) .to_owned() } - pub(crate) fn from_source_file( - source_file: &SourceFile, - file_id: HirFileId, - ) -> SourceFileItems { + pub(crate) fn ast_id(&self, item: &N) -> FileAstId { + FileAstId { raw: self.id_of_unchecked(item.syntax()), _ty: PhantomData } + } + + fn from_source_file(source_file: &SourceFile, file_id: HirFileId) -> SourceFileItems { let mut res = SourceFileItems { file_id, arena: Arena::default() }; // By walking the tree in bread-first order we make sure that parents // get lower ids then children. That is, adding a new child does not @@ -138,7 +135,8 @@ impl SourceFileItems { fn alloc(&mut self, item: &SyntaxNode) -> SourceFileItemId { self.arena.alloc(SyntaxNodePtr::new(item)) } - pub(crate) fn id_of_unchecked(&self, item: &SyntaxNode) -> SourceFileItemId { + + fn id_of_unchecked(&self, item: &SyntaxNode) -> SourceFileItemId { let ptr = SyntaxNodePtr::new(item); if let Some((id, _)) = self.arena.iter().find(|(_id, i)| **i == ptr) { return id; @@ -149,16 +147,6 @@ impl SourceFileItems { self.arena.iter().map(|(_id, i)| i).collect::>(), ); } - pub(crate) fn ast_id(&self, item: &N) -> FileAstId { - FileAstId { raw: self.id_of_unchecked(item.syntax()), _ty: PhantomData } - } -} - -impl std::ops::Index for SourceFileItems { - type Output = SyntaxNodePtr; - fn index(&self, idx: SourceFileItemId) -> &SyntaxNodePtr { - &self.arena[idx] - } } /// Walks the subtree in bfs order, calling `f` for each node. -- cgit v1.2.3 From b17217b34afbdbdc6b1d8ec480fcf06ec4bd587e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 26 Mar 2019 18:57:57 +0300 Subject: simplify --- crates/ra_hir/src/db.rs | 2 +- crates/ra_hir/src/ids.rs | 7 ++++--- crates/ra_hir/src/source_id.rs | 33 ++++++++++++++++----------------- 3 files changed, 21 insertions(+), 21 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index 3296b9b31..a7bbaefb8 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -22,7 +22,7 @@ pub trait DefDatabase: SourceDatabase + AsRef { #[salsa::invoke(crate::ids::macro_def_query)] fn macro_def(&self, macro_id: MacroDefId) -> Option>; - #[salsa::invoke(HirFileId::hir_parse)] + #[salsa::invoke(HirFileId::hir_parse_query)] fn hir_parse(&self, file_id: HirFileId) -> TreeArc; #[salsa::invoke(crate::adt::StructData::struct_data_query)] diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index c2df5ce00..81b3cfd22 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs @@ -82,7 +82,10 @@ impl HirFileId { } } - pub(crate) fn hir_parse(db: &impl DefDatabase, file_id: HirFileId) -> TreeArc { + pub(crate) fn hir_parse_query( + db: &impl DefDatabase, + file_id: HirFileId, + ) -> TreeArc { match file_id.0 { HirFileIdRepr::File(file_id) => db.parse(file_id), HirFileIdRepr::Macro(macro_call_id) => { @@ -122,7 +125,6 @@ impl From for HirFileId { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] - pub struct MacroDefId(pub(crate) AstId); pub(crate) fn macro_def_query(db: &impl DefDatabase, id: MacroDefId) -> Option> { @@ -152,7 +154,6 @@ impl MacroCallId { } impl MacroCallLoc { - #[allow(unused)] pub(crate) fn id(&self, db: &impl AsRef) -> MacroCallId { db.as_ref().macros.loc2id(&self) } diff --git a/crates/ra_hir/src/source_id.rs b/crates/ra_hir/src/source_id.rs index 04b7bb7b3..fb71417af 100644 --- a/crates/ra_hir/src/source_id.rs +++ b/crates/ra_hir/src/source_id.rs @@ -5,6 +5,7 @@ use ra_syntax::{SyntaxNodePtr, TreeArc, SyntaxNode, SourceFile, AstNode, ast}; use crate::{HirFileId, DefDatabase}; +/// `AstId` points to an AST node in any file #[derive(Debug)] pub(crate) struct AstId { file_id: HirFileId, @@ -43,6 +44,7 @@ impl AstId { } } +/// `AstId` points to an AST node in a specific file. #[derive(Debug)] pub(crate) struct FileAstId { raw: SourceFileItemId, @@ -89,7 +91,6 @@ pub struct SourceItemId { /// Maps items' `SyntaxNode`s to `SourceFileItemId`s and back. #[derive(Debug, PartialEq, Eq)] pub struct SourceFileItems { - file_id: HirFileId, arena: Arena, } @@ -99,7 +100,7 @@ impl SourceFileItems { file_id: HirFileId, ) -> Arc { let source_file = db.hir_parse(file_id); - Arc::new(SourceFileItems::from_source_file(&source_file, file_id)) + Arc::new(SourceFileItems::from_source_file(&source_file)) } pub(crate) fn file_item_query( @@ -113,11 +114,21 @@ impl SourceFileItems { } pub(crate) fn ast_id(&self, item: &N) -> FileAstId { - FileAstId { raw: self.id_of_unchecked(item.syntax()), _ty: PhantomData } + let ptr = SyntaxNodePtr::new(item.syntax()); + let raw = match self.arena.iter().find(|(_id, i)| **i == ptr) { + Some((it, _)) => it, + None => panic!( + "Can't find {:?} in SourceFileItems:\n{:?}", + item.syntax(), + self.arena.iter().map(|(_id, i)| i).collect::>(), + ), + }; + + FileAstId { raw, _ty: PhantomData } } - fn from_source_file(source_file: &SourceFile, file_id: HirFileId) -> SourceFileItems { - let mut res = SourceFileItems { file_id, arena: Arena::default() }; + fn from_source_file(source_file: &SourceFile) -> SourceFileItems { + let mut res = SourceFileItems { arena: Arena::default() }; // By walking the tree in bread-first order we make sure that parents // get lower ids then children. That is, adding a new child does not // change parent's id. This means that, say, adding a new function to a @@ -135,18 +146,6 @@ impl SourceFileItems { fn alloc(&mut self, item: &SyntaxNode) -> SourceFileItemId { self.arena.alloc(SyntaxNodePtr::new(item)) } - - fn id_of_unchecked(&self, item: &SyntaxNode) -> SourceFileItemId { - let ptr = SyntaxNodePtr::new(item); - if let Some((id, _)) = self.arena.iter().find(|(_id, i)| **i == ptr) { - return id; - } - panic!( - "Can't find {:?} in SourceFileItems:\n{:?}", - item, - self.arena.iter().map(|(_id, i)| i).collect::>(), - ); - } } /// Walks the subtree in bfs order, calling `f` for each node. -- cgit v1.2.3 From e28db444dfdc797002bd3561940cde2659b831de Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 26 Mar 2019 19:00:11 +0300 Subject: rename --- crates/ra_hir/src/db.rs | 10 +++---- crates/ra_hir/src/ids.rs | 2 +- crates/ra_hir/src/lib.rs | 2 +- crates/ra_hir/src/nameres/raw.rs | 24 ++++++++--------- crates/ra_hir/src/source_binder.rs | 4 +-- crates/ra_hir/src/source_id.rs | 54 +++++++++++++++----------------------- 6 files changed, 42 insertions(+), 54 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index a7bbaefb8..147005848 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -4,7 +4,7 @@ use ra_syntax::{SyntaxNode, TreeArc, SourceFile}; use ra_db::{SourceDatabase, salsa}; use crate::{ - HirFileId, MacroDefId, SourceFileItems, SourceItemId, Crate, Module, HirInterner, + HirFileId, MacroDefId, AstIdMap, ErasedFileAstId, Crate, Module, HirInterner, Function, FnSignature, ExprScopes, TypeAlias, Struct, Enum, StructField, Const, ConstSignature, Static, @@ -34,11 +34,11 @@ pub trait DefDatabase: SourceDatabase + AsRef { #[salsa::invoke(crate::traits::TraitData::trait_data_query)] fn trait_data(&self, t: Trait) -> Arc; - #[salsa::invoke(crate::source_id::SourceFileItems::file_items_query)] - fn file_items(&self, file_id: HirFileId) -> Arc; + #[salsa::invoke(crate::source_id::AstIdMap::ast_id_map_query)] + fn ast_id_map(&self, file_id: HirFileId) -> Arc; - #[salsa::invoke(crate::source_id::SourceFileItems::file_item_query)] - fn file_item(&self, source_item_id: SourceItemId) -> TreeArc; + #[salsa::invoke(crate::source_id::AstIdMap::file_item_query)] + fn ast_id_to_node(&self, file_id: HirFileId, ast_id: ErasedFileAstId) -> TreeArc; #[salsa::invoke(RawItems::raw_items_query)] fn raw_items(&self, file_id: HirFileId) -> Arc; diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index 81b3cfd22..d8a25e246 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs @@ -207,7 +207,7 @@ impl<'a, DB: DefDatabase> LocationCtx<&'a DB> { pub(crate) trait AstItemDef: ArenaId + Clone { fn interner(interner: &HirInterner) -> &LocationInterner, Self>; fn from_ast(ctx: LocationCtx<&impl DefDatabase>, ast: &N) -> Self { - let items = ctx.db.file_items(ctx.file_id); + let items = ctx.db.ast_id_map(ctx.file_id); let item_id = items.ast_id(ast); Self::from_ast_id(ctx, item_id) } diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index b1f388b06..7c603bbd3 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -54,7 +54,7 @@ use crate::{ pub use self::{ path::{Path, PathKind}, name::Name, - source_id::{SourceFileItems, SourceItemId}, + source_id::{AstIdMap, ErasedFileAstId}, ids::{HirFileId, MacroDefId, MacroCallId, MacroCallLoc, HirInterner}, nameres::{PerNs, Namespace}, ty::{Ty, ApplicationTy, TypeCtor, Substs, display::HirDisplay}, diff --git a/crates/ra_hir/src/nameres/raw.rs b/crates/ra_hir/src/nameres/raw.rs index 984478adc..0936229ac 100644 --- a/crates/ra_hir/src/nameres/raw.rs +++ b/crates/ra_hir/src/nameres/raw.rs @@ -12,7 +12,7 @@ use ra_syntax::{ use crate::{ DefDatabase, Name, AsName, Path, HirFileId, ModuleSource, - SourceFileItems, FileAstId, + AstIdMap, FileAstId, }; /// `RawItems` is a set of top-level items in a file (except for impls). @@ -60,7 +60,7 @@ impl RawItems { ) -> (Arc, Arc) { let mut collector = RawItemsCollector { raw_items: RawItems::default(), - source_file_items: db.file_items(file_id.into()), + source_ast_id_map: db.ast_id_map(file_id.into()), source_map: ImportSourceMap::default(), }; let source_file = db.hir_parse(file_id); @@ -167,7 +167,7 @@ pub(super) struct MacroData { struct RawItemsCollector { raw_items: RawItems, - source_file_items: Arc, + source_ast_id_map: Arc, source_map: ImportSourceMap, } @@ -200,25 +200,25 @@ impl RawItemsCollector { return; } ast::ModuleItemKind::StructDef(it) => { - (DefKind::Struct(self.source_file_items.ast_id(it)), it.name()) + (DefKind::Struct(self.source_ast_id_map.ast_id(it)), it.name()) } ast::ModuleItemKind::EnumDef(it) => { - (DefKind::Enum(self.source_file_items.ast_id(it)), it.name()) + (DefKind::Enum(self.source_ast_id_map.ast_id(it)), it.name()) } ast::ModuleItemKind::FnDef(it) => { - (DefKind::Function(self.source_file_items.ast_id(it)), it.name()) + (DefKind::Function(self.source_ast_id_map.ast_id(it)), it.name()) } ast::ModuleItemKind::TraitDef(it) => { - (DefKind::Trait(self.source_file_items.ast_id(it)), it.name()) + (DefKind::Trait(self.source_ast_id_map.ast_id(it)), it.name()) } ast::ModuleItemKind::TypeAliasDef(it) => { - (DefKind::TypeAlias(self.source_file_items.ast_id(it)), it.name()) + (DefKind::TypeAlias(self.source_ast_id_map.ast_id(it)), it.name()) } ast::ModuleItemKind::ConstDef(it) => { - (DefKind::Const(self.source_file_items.ast_id(it)), it.name()) + (DefKind::Const(self.source_ast_id_map.ast_id(it)), it.name()) } ast::ModuleItemKind::StaticDef(it) => { - (DefKind::Static(self.source_file_items.ast_id(it)), it.name()) + (DefKind::Static(self.source_ast_id_map.ast_id(it)), it.name()) } }; if let Some(name) = name { @@ -233,7 +233,7 @@ impl RawItemsCollector { Some(it) => it.as_name(), None => return, }; - let ast_id = self.source_file_items.ast_id(module); + let ast_id = self.source_ast_id_map.ast_id(module); if module.has_semi() { let item = self.raw_items.modules.alloc(ModuleData::Declaration { name, ast_id }); self.push_item(current_module, RawItem::Module(item)); @@ -297,7 +297,7 @@ impl RawItemsCollector { }; let name = m.name().map(|it| it.as_name()); - let ast_id = self.source_file_items.ast_id(m); + let ast_id = self.source_ast_id_map.ast_id(m); let export = m.has_atom_attr("macro_export"); let m = self.raw_items.macros.alloc(MacroData { ast_id, path, name, export }); self.push_item(current_module, RawItem::Macro(m)); diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index f9d2d0247..9dae4c3d1 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -54,8 +54,8 @@ fn module_from_inline( ) -> Option { assert!(!module.has_semi()); let file_id = file_id.into(); - let file_items = db.file_items(file_id); - let item_id = file_items.ast_id(module).with_file_id(file_id); + let ast_id_map = db.ast_id_map(file_id); + let item_id = ast_id_map.ast_id(module).with_file_id(file_id); module_from_source(db, file_id, Some(item_id)) } diff --git a/crates/ra_hir/src/source_id.rs b/crates/ra_hir/src/source_id.rs index fb71417af..0a8fb6d32 100644 --- a/crates/ra_hir/src/source_id.rs +++ b/crates/ra_hir/src/source_id.rs @@ -5,7 +5,9 @@ use ra_syntax::{SyntaxNodePtr, TreeArc, SyntaxNode, SourceFile, AstNode, ast}; use crate::{HirFileId, DefDatabase}; -/// `AstId` points to an AST node in any file +/// `AstId` points to an AST node in any file. +/// +/// It is stable across reparses, and can be used as salsa key/value. #[derive(Debug)] pub(crate) struct AstId { file_id: HirFileId, @@ -37,9 +39,7 @@ impl AstId { } pub(crate) fn to_node(&self, db: &impl DefDatabase) -> TreeArc { - let source_item_id = - SourceItemId { file_id: self.file_id(), item_id: self.file_ast_id.raw }; - let syntax_node = db.file_item(source_item_id); + let syntax_node = db.ast_id_to_node(self.file_id, self.file_ast_id.raw); N::cast(&syntax_node).unwrap().to_owned() } } @@ -47,7 +47,7 @@ impl AstId { /// `AstId` points to an AST node in a specific file. #[derive(Debug)] pub(crate) struct FileAstId { - raw: SourceFileItemId, + raw: ErasedFileAstId, _ty: PhantomData, } @@ -76,41 +76,29 @@ impl FileAstId { } } -/// Identifier of item within a specific file. This is stable over reparses, so -/// it's OK to use it as a salsa key/value. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -struct SourceFileItemId(RawId); -impl_arena_id!(SourceFileItemId); +pub struct ErasedFileAstId(RawId); +impl_arena_id!(ErasedFileAstId); -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct SourceItemId { - file_id: HirFileId, - item_id: SourceFileItemId, -} - -/// Maps items' `SyntaxNode`s to `SourceFileItemId`s and back. +/// Maps items' `SyntaxNode`s to `ErasedFileAstId`s and back. #[derive(Debug, PartialEq, Eq)] -pub struct SourceFileItems { - arena: Arena, +pub struct AstIdMap { + arena: Arena, } -impl SourceFileItems { - pub(crate) fn file_items_query( - db: &impl DefDatabase, - file_id: HirFileId, - ) -> Arc { +impl AstIdMap { + pub(crate) fn ast_id_map_query(db: &impl DefDatabase, file_id: HirFileId) -> Arc { let source_file = db.hir_parse(file_id); - Arc::new(SourceFileItems::from_source_file(&source_file)) + Arc::new(AstIdMap::from_source_file(&source_file)) } pub(crate) fn file_item_query( db: &impl DefDatabase, - source_item_id: SourceItemId, + file_id: HirFileId, + ast_id: ErasedFileAstId, ) -> TreeArc { - let source_file = db.hir_parse(source_item_id.file_id); - db.file_items(source_item_id.file_id).arena[source_item_id.item_id] - .to_node(&source_file) - .to_owned() + let source_file = db.hir_parse(file_id); + db.ast_id_map(file_id).arena[ast_id].to_node(&source_file).to_owned() } pub(crate) fn ast_id(&self, item: &N) -> FileAstId { @@ -118,7 +106,7 @@ impl SourceFileItems { let raw = match self.arena.iter().find(|(_id, i)| **i == ptr) { Some((it, _)) => it, None => panic!( - "Can't find {:?} in SourceFileItems:\n{:?}", + "Can't find {:?} in AstIdMap:\n{:?}", item.syntax(), self.arena.iter().map(|(_id, i)| i).collect::>(), ), @@ -127,8 +115,8 @@ impl SourceFileItems { FileAstId { raw, _ty: PhantomData } } - fn from_source_file(source_file: &SourceFile) -> SourceFileItems { - let mut res = SourceFileItems { arena: Arena::default() }; + fn from_source_file(source_file: &SourceFile) -> AstIdMap { + let mut res = AstIdMap { arena: Arena::default() }; // By walking the tree in bread-first order we make sure that parents // get lower ids then children. That is, adding a new child does not // change parent's id. This means that, say, adding a new function to a @@ -143,7 +131,7 @@ impl SourceFileItems { res } - fn alloc(&mut self, item: &SyntaxNode) -> SourceFileItemId { + fn alloc(&mut self, item: &SyntaxNode) -> ErasedFileAstId { self.arena.alloc(SyntaxNodePtr::new(item)) } } -- cgit v1.2.3 From 1325a31e34a3b4bf5104a743bcb8217ef5c4f3cd Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 26 Mar 2019 19:15:39 +0300 Subject: reduce visibility --- crates/ra_hir/src/code_model_api.rs | 4 ++-- crates/ra_hir/src/ids.rs | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 88c13566c..624c25c4d 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -13,7 +13,7 @@ use crate::{ adt::{EnumVariantId, StructFieldId, VariantDef}, generics::GenericParams, docs::{Documentation, Docs, docs_from_ast}, - ids::{FunctionId, StructId, EnumId, AstItemDef, ConstId, StaticId, TraitId, TypeId}, + ids::{FunctionId, StructId, EnumId, AstItemDef, ConstId, StaticId, TraitId, TypeAliasId}, impl_block::ImplBlock, resolve::Resolver, diagnostics::DiagnosticSink, @@ -672,7 +672,7 @@ impl Docs for Trait { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct TypeAlias { - pub(crate) id: TypeId, + pub(crate) id: TypeAliasId, } impl TypeAlias { diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index d8a25e246..eb9939df7 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs @@ -21,7 +21,7 @@ pub struct HirInterner { consts: LocationInterner, ConstId>, statics: LocationInterner, StaticId>, traits: LocationInterner, TraitId>, - types: LocationInterner, TypeId>, + types: LocationInterner, TypeAliasId>, } impl HirInterner { @@ -229,7 +229,7 @@ pub(crate) trait AstItemDef: ArenaId + Clone { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct FunctionId(RawId); +pub(crate) struct FunctionId(RawId); impl_arena_id!(FunctionId); impl AstItemDef for FunctionId { fn interner(interner: &HirInterner) -> &LocationInterner, Self> { @@ -238,7 +238,7 @@ impl AstItemDef for FunctionId { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct StructId(RawId); +pub(crate) struct StructId(RawId); impl_arena_id!(StructId); impl AstItemDef for StructId { fn interner(interner: &HirInterner) -> &LocationInterner, Self> { @@ -247,7 +247,7 @@ impl AstItemDef for StructId { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct EnumId(RawId); +pub(crate) struct EnumId(RawId); impl_arena_id!(EnumId); impl AstItemDef for EnumId { fn interner(interner: &HirInterner) -> &LocationInterner, Self> { @@ -256,7 +256,7 @@ impl AstItemDef for EnumId { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct ConstId(RawId); +pub(crate) struct ConstId(RawId); impl_arena_id!(ConstId); impl AstItemDef for ConstId { fn interner(interner: &HirInterner) -> &LocationInterner, Self> { @@ -265,7 +265,7 @@ impl AstItemDef for ConstId { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct StaticId(RawId); +pub(crate) struct StaticId(RawId); impl_arena_id!(StaticId); impl AstItemDef for StaticId { fn interner(interner: &HirInterner) -> &LocationInterner, Self> { @@ -274,7 +274,7 @@ impl AstItemDef for StaticId { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct TraitId(RawId); +pub(crate) struct TraitId(RawId); impl_arena_id!(TraitId); impl AstItemDef for TraitId { fn interner(interner: &HirInterner) -> &LocationInterner, Self> { @@ -283,9 +283,9 @@ impl AstItemDef for TraitId { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct TypeId(RawId); -impl_arena_id!(TypeId); -impl AstItemDef for TypeId { +pub(crate) struct TypeAliasId(RawId); +impl_arena_id!(TypeAliasId); +impl AstItemDef for TypeAliasId { fn interner(interner: &HirInterner) -> &LocationInterner, Self> { &interner.types } -- cgit v1.2.3