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/nameres/collector.rs | 22 ++++++++++------------ crates/ra_hir/src/nameres/raw.rs | 13 ++++++------- 2 files changed, 16 insertions(+), 19 deletions(-) (limited to 'crates/ra_hir/src/nameres') 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); -- 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/nameres/collector.rs | 24 ++++++++---------------- crates/ra_hir/src/nameres/raw.rs | 6 +++--- 2 files changed, 11 insertions(+), 19 deletions(-) (limited to 'crates/ra_hir/src/nameres') 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)); } -- 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/nameres/collector.rs | 23 ++++++++-------- crates/ra_hir/src/nameres/raw.rs | 48 +++++++++++++++++++++------------- 2 files changed, 42 insertions(+), 29 deletions(-) (limited to 'crates/ra_hir/src/nameres') 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)) } } -- 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/nameres/raw.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'crates/ra_hir/src/nameres') 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)); -- cgit v1.2.3