diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-03-19 17:47:43 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-03-19 17:47:43 +0000 |
commit | 1ba03c6995015b3143a417ed07437f0c9028a97d (patch) | |
tree | ce3eb047dd9fe9005750a3b1417d95b1aa8fe01e /crates/ra_hir_def/src/nameres | |
parent | 988f1dda6bde576ec2457dd97a7525014609c771 (diff) | |
parent | f840fcb2f525c13809d6a736e434155edf075a06 (diff) |
Merge #3656
3656: Simplify arenas r=matklad a=matklad
At the moment, Arena is paranetrized by two types: index and data. The original motivation was to allow index to be defined in the downstream crate, so that you can add inherent impls to the index.
However, it seems like we've never actually used that capability, so perhaps we should switch to a generic Index impl? This PR tries this out, switching only `raw.rs` and parts of `hir_def`.
wdyt?
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir_def/src/nameres')
-rw-r--r-- | crates/ra_hir_def/src/nameres/collector.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres/raw.rs | 88 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres/tests/mod_resolution.rs | 8 |
3 files changed, 46 insertions, 52 deletions
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 7a042e69f..5b292c250 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs | |||
@@ -966,7 +966,7 @@ mod tests { | |||
966 | 966 | ||
967 | let def_map = { | 967 | let def_map = { |
968 | let edition = db.crate_graph()[krate].edition; | 968 | let edition = db.crate_graph()[krate].edition; |
969 | let mut modules: Arena<LocalModuleId, ModuleData> = Arena::default(); | 969 | let mut modules: Arena<ModuleData> = Arena::default(); |
970 | let root = modules.alloc(ModuleData::default()); | 970 | let root = modules.alloc(ModuleData::default()); |
971 | CrateDefMap { | 971 | CrateDefMap { |
972 | krate, | 972 | krate, |
diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 0e4931f58..1631e87b8 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs | |||
@@ -12,7 +12,7 @@ use hir_expand::{ | |||
12 | hygiene::Hygiene, | 12 | hygiene::Hygiene, |
13 | name::{AsName, Name}, | 13 | name::{AsName, Name}, |
14 | }; | 14 | }; |
15 | use ra_arena::{impl_arena_id, Arena, RawId}; | 15 | use ra_arena::{Arena, Idx}; |
16 | use ra_prof::profile; | 16 | use ra_prof::profile; |
17 | use ra_syntax::{ | 17 | use ra_syntax::{ |
18 | ast::{self, AttrsOwner, NameOwner, VisibilityOwner}, | 18 | ast::{self, AttrsOwner, NameOwner, VisibilityOwner}, |
@@ -34,11 +34,11 @@ use crate::{ | |||
34 | /// on most edits. | 34 | /// on most edits. |
35 | #[derive(Debug, Default, PartialEq, Eq)] | 35 | #[derive(Debug, Default, PartialEq, Eq)] |
36 | pub struct RawItems { | 36 | pub struct RawItems { |
37 | modules: Arena<Module, ModuleData>, | 37 | modules: Arena<ModuleData>, |
38 | imports: Arena<Import, ImportData>, | 38 | imports: Arena<ImportData>, |
39 | defs: Arena<Def, DefData>, | 39 | defs: Arena<DefData>, |
40 | macros: Arena<Macro, MacroData>, | 40 | macros: Arena<MacroData>, |
41 | impls: Arena<Impl, ImplData>, | 41 | impls: Arena<ImplData>, |
42 | /// items for top-level module | 42 | /// items for top-level module |
43 | items: Vec<RawItem>, | 43 | items: Vec<RawItem>, |
44 | } | 44 | } |
@@ -68,9 +68,9 @@ impl RawItems { | |||
68 | } | 68 | } |
69 | } | 69 | } |
70 | 70 | ||
71 | impl Index<Module> for RawItems { | 71 | impl Index<Idx<ModuleData>> for RawItems { |
72 | type Output = ModuleData; | 72 | type Output = ModuleData; |
73 | fn index(&self, idx: Module) -> &ModuleData { | 73 | fn index(&self, idx: Idx<ModuleData>) -> &ModuleData { |
74 | &self.modules[idx] | 74 | &self.modules[idx] |
75 | } | 75 | } |
76 | } | 76 | } |
@@ -82,23 +82,23 @@ impl Index<Import> for RawItems { | |||
82 | } | 82 | } |
83 | } | 83 | } |
84 | 84 | ||
85 | impl Index<Def> for RawItems { | 85 | impl Index<Idx<DefData>> for RawItems { |
86 | type Output = DefData; | 86 | type Output = DefData; |
87 | fn index(&self, idx: Def) -> &DefData { | 87 | fn index(&self, idx: Idx<DefData>) -> &DefData { |
88 | &self.defs[idx] | 88 | &self.defs[idx] |
89 | } | 89 | } |
90 | } | 90 | } |
91 | 91 | ||
92 | impl Index<Macro> for RawItems { | 92 | impl Index<Idx<MacroData>> for RawItems { |
93 | type Output = MacroData; | 93 | type Output = MacroData; |
94 | fn index(&self, idx: Macro) -> &MacroData { | 94 | fn index(&self, idx: Idx<MacroData>) -> &MacroData { |
95 | &self.macros[idx] | 95 | &self.macros[idx] |
96 | } | 96 | } |
97 | } | 97 | } |
98 | 98 | ||
99 | impl Index<Impl> for RawItems { | 99 | impl Index<Idx<ImplData>> for RawItems { |
100 | type Output = ImplData; | 100 | type Output = ImplData; |
101 | fn index(&self, idx: Impl) -> &ImplData { | 101 | fn index(&self, idx: Idx<ImplData>) -> &ImplData { |
102 | &self.impls[idx] | 102 | &self.impls[idx] |
103 | } | 103 | } |
104 | } | 104 | } |
@@ -111,17 +111,13 @@ pub(super) struct RawItem { | |||
111 | 111 | ||
112 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | 112 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] |
113 | pub(super) enum RawItemKind { | 113 | pub(super) enum RawItemKind { |
114 | Module(Module), | 114 | Module(Idx<ModuleData>), |
115 | Import(Import), | 115 | Import(Import), |
116 | Def(Def), | 116 | Def(Idx<DefData>), |
117 | Macro(Macro), | 117 | Macro(Idx<MacroData>), |
118 | Impl(Impl), | 118 | Impl(Idx<ImplData>), |
119 | } | 119 | } |
120 | 120 | ||
121 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
122 | pub(super) struct Module(RawId); | ||
123 | impl_arena_id!(Module); | ||
124 | |||
125 | #[derive(Debug, PartialEq, Eq)] | 121 | #[derive(Debug, PartialEq, Eq)] |
126 | pub(super) enum ModuleData { | 122 | pub(super) enum ModuleData { |
127 | Declaration { | 123 | Declaration { |
@@ -137,9 +133,7 @@ pub(super) enum ModuleData { | |||
137 | }, | 133 | }, |
138 | } | 134 | } |
139 | 135 | ||
140 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 136 | pub(crate) type Import = Idx<ImportData>; |
141 | pub(crate) struct Import(RawId); | ||
142 | impl_arena_id!(Import); | ||
143 | 137 | ||
144 | #[derive(Debug, Clone, PartialEq, Eq)] | 138 | #[derive(Debug, Clone, PartialEq, Eq)] |
145 | pub struct ImportData { | 139 | pub struct ImportData { |
@@ -152,9 +146,7 @@ pub struct ImportData { | |||
152 | pub(super) visibility: RawVisibility, | 146 | pub(super) visibility: RawVisibility, |
153 | } | 147 | } |
154 | 148 | ||
155 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 149 | // type Def = Idx<DefData>; |
156 | pub(super) struct Def(RawId); | ||
157 | impl_arena_id!(Def); | ||
158 | 150 | ||
159 | #[derive(Debug, PartialEq, Eq)] | 151 | #[derive(Debug, PartialEq, Eq)] |
160 | pub(super) struct DefData { | 152 | pub(super) struct DefData { |
@@ -190,10 +182,6 @@ impl DefKind { | |||
190 | } | 182 | } |
191 | } | 183 | } |
192 | 184 | ||
193 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
194 | pub(super) struct Macro(RawId); | ||
195 | impl_arena_id!(Macro); | ||
196 | |||
197 | #[derive(Debug, PartialEq, Eq)] | 185 | #[derive(Debug, PartialEq, Eq)] |
198 | pub(super) struct MacroData { | 186 | pub(super) struct MacroData { |
199 | pub(super) ast_id: FileAstId<ast::MacroCall>, | 187 | pub(super) ast_id: FileAstId<ast::MacroCall>, |
@@ -203,10 +191,6 @@ pub(super) struct MacroData { | |||
203 | pub(super) builtin: bool, | 191 | pub(super) builtin: bool, |
204 | } | 192 | } |
205 | 193 | ||
206 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
207 | pub(super) struct Impl(RawId); | ||
208 | impl_arena_id!(Impl); | ||
209 | |||
210 | #[derive(Debug, PartialEq, Eq)] | 194 | #[derive(Debug, PartialEq, Eq)] |
211 | pub(super) struct ImplData { | 195 | pub(super) struct ImplData { |
212 | pub(super) ast_id: FileAstId<ast::ImplDef>, | 196 | pub(super) ast_id: FileAstId<ast::ImplDef>, |
@@ -220,7 +204,11 @@ struct RawItemsCollector { | |||
220 | } | 204 | } |
221 | 205 | ||
222 | impl RawItemsCollector { | 206 | impl RawItemsCollector { |
223 | fn process_module(&mut self, current_module: Option<Module>, body: impl ast::ModuleItemOwner) { | 207 | fn process_module( |
208 | &mut self, | ||
209 | current_module: Option<Idx<ModuleData>>, | ||
210 | body: impl ast::ModuleItemOwner, | ||
211 | ) { | ||
224 | for item_or_macro in body.items_with_macros() { | 212 | for item_or_macro in body.items_with_macros() { |
225 | match item_or_macro { | 213 | match item_or_macro { |
226 | ast::ItemOrMacro::Macro(m) => self.add_macro(current_module, m), | 214 | ast::ItemOrMacro::Macro(m) => self.add_macro(current_module, m), |
@@ -229,7 +217,7 @@ impl RawItemsCollector { | |||
229 | } | 217 | } |
230 | } | 218 | } |
231 | 219 | ||
232 | fn add_item(&mut self, current_module: Option<Module>, item: ast::ModuleItem) { | 220 | fn add_item(&mut self, current_module: Option<Idx<ModuleData>>, item: ast::ModuleItem) { |
233 | let attrs = self.parse_attrs(&item); | 221 | let attrs = self.parse_attrs(&item); |
234 | let visibility = RawVisibility::from_ast_with_hygiene(item.visibility(), &self.hygiene); | 222 | let visibility = RawVisibility::from_ast_with_hygiene(item.visibility(), &self.hygiene); |
235 | let (kind, name) = match item { | 223 | let (kind, name) = match item { |
@@ -285,7 +273,7 @@ impl RawItemsCollector { | |||
285 | } | 273 | } |
286 | } | 274 | } |
287 | 275 | ||
288 | fn add_module(&mut self, current_module: Option<Module>, module: ast::Module) { | 276 | fn add_module(&mut self, current_module: Option<Idx<ModuleData>>, module: ast::Module) { |
289 | let name = match module.name() { | 277 | let name = match module.name() { |
290 | Some(it) => it.as_name(), | 278 | Some(it) => it.as_name(), |
291 | None => return, | 279 | None => return, |
@@ -315,7 +303,7 @@ impl RawItemsCollector { | |||
315 | tested_by!(name_res_works_for_broken_modules); | 303 | tested_by!(name_res_works_for_broken_modules); |
316 | } | 304 | } |
317 | 305 | ||
318 | fn add_use_item(&mut self, current_module: Option<Module>, use_item: ast::UseItem) { | 306 | fn add_use_item(&mut self, current_module: Option<Idx<ModuleData>>, use_item: ast::UseItem) { |
319 | // FIXME: cfg_attr | 307 | // FIXME: cfg_attr |
320 | let is_prelude = use_item.has_atom_attr("prelude_import"); | 308 | let is_prelude = use_item.has_atom_attr("prelude_import"); |
321 | let attrs = self.parse_attrs(&use_item); | 309 | let attrs = self.parse_attrs(&use_item); |
@@ -345,7 +333,7 @@ impl RawItemsCollector { | |||
345 | 333 | ||
346 | fn add_extern_crate_item( | 334 | fn add_extern_crate_item( |
347 | &mut self, | 335 | &mut self, |
348 | current_module: Option<Module>, | 336 | current_module: Option<Idx<ModuleData>>, |
349 | extern_crate: ast::ExternCrateItem, | 337 | extern_crate: ast::ExternCrateItem, |
350 | ) { | 338 | ) { |
351 | if let Some(name_ref) = extern_crate.name_ref() { | 339 | if let Some(name_ref) = extern_crate.name_ref() { |
@@ -371,7 +359,7 @@ impl RawItemsCollector { | |||
371 | } | 359 | } |
372 | } | 360 | } |
373 | 361 | ||
374 | fn add_macro(&mut self, current_module: Option<Module>, m: ast::MacroCall) { | 362 | fn add_macro(&mut self, current_module: Option<Idx<ModuleData>>, m: ast::MacroCall) { |
375 | let attrs = self.parse_attrs(&m); | 363 | let attrs = self.parse_attrs(&m); |
376 | let path = match m.path().and_then(|path| ModPath::from_src(path, &self.hygiene)) { | 364 | let path = match m.path().and_then(|path| ModPath::from_src(path, &self.hygiene)) { |
377 | Some(it) => it, | 365 | Some(it) => it, |
@@ -391,19 +379,29 @@ impl RawItemsCollector { | |||
391 | self.push_item(current_module, attrs, RawItemKind::Macro(m)); | 379 | self.push_item(current_module, attrs, RawItemKind::Macro(m)); |
392 | } | 380 | } |
393 | 381 | ||
394 | fn add_impl(&mut self, current_module: Option<Module>, imp: ast::ImplDef) { | 382 | fn add_impl(&mut self, current_module: Option<Idx<ModuleData>>, imp: ast::ImplDef) { |
395 | let attrs = self.parse_attrs(&imp); | 383 | let attrs = self.parse_attrs(&imp); |
396 | let ast_id = self.source_ast_id_map.ast_id(&imp); | 384 | let ast_id = self.source_ast_id_map.ast_id(&imp); |
397 | let imp = self.raw_items.impls.alloc(ImplData { ast_id }); | 385 | let imp = self.raw_items.impls.alloc(ImplData { ast_id }); |
398 | self.push_item(current_module, attrs, RawItemKind::Impl(imp)) | 386 | self.push_item(current_module, attrs, RawItemKind::Impl(imp)) |
399 | } | 387 | } |
400 | 388 | ||
401 | fn push_import(&mut self, current_module: Option<Module>, attrs: Attrs, data: ImportData) { | 389 | fn push_import( |
390 | &mut self, | ||
391 | current_module: Option<Idx<ModuleData>>, | ||
392 | attrs: Attrs, | ||
393 | data: ImportData, | ||
394 | ) { | ||
402 | let import = self.raw_items.imports.alloc(data); | 395 | let import = self.raw_items.imports.alloc(data); |
403 | self.push_item(current_module, attrs, RawItemKind::Import(import)) | 396 | self.push_item(current_module, attrs, RawItemKind::Import(import)) |
404 | } | 397 | } |
405 | 398 | ||
406 | fn push_item(&mut self, current_module: Option<Module>, attrs: Attrs, kind: RawItemKind) { | 399 | fn push_item( |
400 | &mut self, | ||
401 | current_module: Option<Idx<ModuleData>>, | ||
402 | attrs: Attrs, | ||
403 | kind: RawItemKind, | ||
404 | ) { | ||
407 | match current_module { | 405 | match current_module { |
408 | Some(module) => match &mut self.raw_items.modules[module] { | 406 | Some(module) => match &mut self.raw_items.modules[module] { |
409 | ModuleData::Definition { items, .. } => items, | 407 | ModuleData::Definition { items, .. } => items, |
diff --git a/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs b/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs index b502a4079..37fcdfb8c 100644 --- a/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs +++ b/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs | |||
@@ -710,9 +710,7 @@ fn unresolved_module_diagnostics() { | |||
710 | @r###" | 710 | @r###" |
711 | [ | 711 | [ |
712 | UnresolvedModule { | 712 | UnresolvedModule { |
713 | module: LocalModuleId( | 713 | module: Idx::<ModuleData>(0), |
714 | 0, | ||
715 | ), | ||
716 | declaration: InFile { | 714 | declaration: InFile { |
717 | file_id: HirFileId( | 715 | file_id: HirFileId( |
718 | FileId( | 716 | FileId( |
@@ -722,9 +720,7 @@ fn unresolved_module_diagnostics() { | |||
722 | ), | 720 | ), |
723 | ), | 721 | ), |
724 | value: FileAstId { | 722 | value: FileAstId { |
725 | raw: ErasedFileAstId( | 723 | raw: Idx::<SyntaxNodePtr>(1), |
726 | 1, | ||
727 | ), | ||
728 | _ty: PhantomData, | 724 | _ty: PhantomData, |
729 | }, | 725 | }, |
730 | }, | 726 | }, |