diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-12-16 16:52:46 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-12-16 16:52:46 +0000 |
commit | 63bbdb31e5148c804bbf940963c9c8f3481ad258 (patch) | |
tree | 2732cd2c3878257d9b55447830bc824447332c98 /crates/hir_def/src/nameres/collector.rs | |
parent | 423f3872246f1a67b49e248f3437cb46fdfc8138 (diff) | |
parent | d34611633b3b2404188b9e12b08c5def589808c2 (diff) |
Merge #6897
6897: Basic support for macros 2.0 r=jonas-schievink a=jonas-schievink
This adds support for (built-in-only) macros 2.0, and removes some hacks used for builtin derives, which are declared via macros 2.0 in libcore.
First steps for https://github.com/rust-analyzer/rust-analyzer/issues/2248.
Blocked on https://github.com/rust-analyzer/ungrammar/pull/16.
Co-authored-by: Jonas Schievink <[email protected]>
Co-authored-by: Jonas Schievink <[email protected]>
Diffstat (limited to 'crates/hir_def/src/nameres/collector.rs')
-rw-r--r-- | crates/hir_def/src/nameres/collector.rs | 45 |
1 files changed, 33 insertions, 12 deletions
diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index 85cc342c4..785895277 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs | |||
@@ -309,13 +309,13 @@ impl DefCollector<'_> { | |||
309 | let macro_def = match self.proc_macros.iter().find(|(n, _)| n == name) { | 309 | let macro_def = match self.proc_macros.iter().find(|(n, _)| n == name) { |
310 | Some((_, expander)) => MacroDefId { | 310 | Some((_, expander)) => MacroDefId { |
311 | ast_id: None, | 311 | ast_id: None, |
312 | krate: Some(self.def_map.krate), | 312 | krate: self.def_map.krate, |
313 | kind: MacroDefKind::ProcMacro(*expander), | 313 | kind: MacroDefKind::ProcMacro(*expander), |
314 | local_inner: false, | 314 | local_inner: false, |
315 | }, | 315 | }, |
316 | None => MacroDefId { | 316 | None => MacroDefId { |
317 | ast_id: None, | 317 | ast_id: None, |
318 | krate: Some(self.def_map.krate), | 318 | krate: self.def_map.krate, |
319 | kind: MacroDefKind::ProcMacro(ProcMacroExpander::dummy(self.def_map.krate)), | 319 | kind: MacroDefKind::ProcMacro(ProcMacroExpander::dummy(self.def_map.krate)), |
320 | local_inner: false, | 320 | local_inner: false, |
321 | }, | 321 | }, |
@@ -784,14 +784,6 @@ impl DefCollector<'_> { | |||
784 | directive: &DeriveDirective, | 784 | directive: &DeriveDirective, |
785 | path: &ModPath, | 785 | path: &ModPath, |
786 | ) -> Option<MacroDefId> { | 786 | ) -> Option<MacroDefId> { |
787 | if let Some(name) = path.as_ident() { | ||
788 | // FIXME this should actually be handled with the normal name | ||
789 | // resolution; the std lib defines built-in stubs for the derives, | ||
790 | // but these are new-style `macro`s, which we don't support yet | ||
791 | if let Some(def_id) = find_builtin_derive(name) { | ||
792 | return Some(def_id); | ||
793 | } | ||
794 | } | ||
795 | let resolved_res = self.def_map.resolve_path_fp_with_macro( | 787 | let resolved_res = self.def_map.resolve_path_fp_with_macro( |
796 | self.db, | 788 | self.db, |
797 | ResolveMode::Other, | 789 | ResolveMode::Other, |
@@ -976,6 +968,35 @@ impl ModCollector<'_, '_> { | |||
976 | } | 968 | } |
977 | ModItem::MacroCall(mac) => self.collect_macro_call(&self.item_tree[mac]), | 969 | ModItem::MacroCall(mac) => self.collect_macro_call(&self.item_tree[mac]), |
978 | ModItem::MacroRules(mac) => self.collect_macro_rules(&self.item_tree[mac]), | 970 | ModItem::MacroRules(mac) => self.collect_macro_rules(&self.item_tree[mac]), |
971 | ModItem::MacroDef(id) => { | ||
972 | let mac = &self.item_tree[id]; | ||
973 | let ast_id = InFile::new(self.file_id, mac.ast_id.upcast()); | ||
974 | |||
975 | // "Macro 2.0" is not currently supported by rust-analyzer, but libcore uses it | ||
976 | // to define builtin macros, so we support at least that part. | ||
977 | if mac.is_builtin { | ||
978 | let krate = self.def_collector.def_map.krate; | ||
979 | let macro_id = find_builtin_macro(&mac.name, krate, ast_id) | ||
980 | .or_else(|| find_builtin_derive(&mac.name, krate, ast_id)); | ||
981 | if let Some(macro_id) = macro_id { | ||
982 | let vis = self | ||
983 | .def_collector | ||
984 | .def_map | ||
985 | .resolve_visibility( | ||
986 | self.def_collector.db, | ||
987 | self.module_id, | ||
988 | &self.item_tree[mac.visibility], | ||
989 | ) | ||
990 | .unwrap_or(Visibility::Public); | ||
991 | self.def_collector.update( | ||
992 | self.module_id, | ||
993 | &[(Some(mac.name.clone()), PerNs::macros(macro_id, vis))], | ||
994 | vis, | ||
995 | ImportType::Named, | ||
996 | ); | ||
997 | } | ||
998 | } | ||
999 | } | ||
979 | ModItem::Impl(imp) => { | 1000 | ModItem::Impl(imp) => { |
980 | let module = ModuleId { | 1001 | let module = ModuleId { |
981 | krate: self.def_collector.def_map.krate, | 1002 | krate: self.def_collector.def_map.krate, |
@@ -1280,7 +1301,7 @@ impl ModCollector<'_, '_> { | |||
1280 | } | 1301 | } |
1281 | 1302 | ||
1282 | fn collect_macro_rules(&mut self, mac: &MacroRules) { | 1303 | fn collect_macro_rules(&mut self, mac: &MacroRules) { |
1283 | let ast_id = InFile::new(self.file_id, mac.ast_id); | 1304 | let ast_id = InFile::new(self.file_id, mac.ast_id.upcast()); |
1284 | 1305 | ||
1285 | // Case 1: builtin macros | 1306 | // Case 1: builtin macros |
1286 | if mac.is_builtin { | 1307 | if mac.is_builtin { |
@@ -1299,7 +1320,7 @@ impl ModCollector<'_, '_> { | |||
1299 | // Case 2: normal `macro_rules!` macro | 1320 | // Case 2: normal `macro_rules!` macro |
1300 | let macro_id = MacroDefId { | 1321 | let macro_id = MacroDefId { |
1301 | ast_id: Some(ast_id), | 1322 | ast_id: Some(ast_id), |
1302 | krate: Some(self.def_collector.def_map.krate), | 1323 | krate: self.def_collector.def_map.krate, |
1303 | kind: MacroDefKind::Declarative, | 1324 | kind: MacroDefKind::Declarative, |
1304 | local_inner: mac.is_local_inner, | 1325 | local_inner: mac.is_local_inner, |
1305 | }; | 1326 | }; |