diff options
author | Edwin Cheng <[email protected]> | 2020-03-25 12:14:22 +0000 |
---|---|---|
committer | Edwin Cheng <[email protected]> | 2020-03-25 12:14:22 +0000 |
commit | 2adc9a8d5f8f7686a125a478330c67a2d46fba98 (patch) | |
tree | 5d41d254dc94ff7d8d20063a08df2b9b133d34d9 /crates | |
parent | 5bd3aa05d6963a633a1b5cea8187500657847b21 (diff) |
Remove collect proc_macro definitions
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_hir_def/src/nameres/collector.rs | 46 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/tests/macros.rs | 8 |
2 files changed, 25 insertions, 29 deletions
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 1dcf5a309..9c125f32f 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs | |||
@@ -65,6 +65,9 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: CrateDefMap) -> Cr | |||
65 | unexpanded_attribute_macros: Vec::new(), | 65 | unexpanded_attribute_macros: Vec::new(), |
66 | mod_dirs: FxHashMap::default(), | 66 | mod_dirs: FxHashMap::default(), |
67 | cfg_options, | 67 | cfg_options, |
68 | |||
69 | // FIXME: pass proc-macro from crate-graph | ||
70 | proc_macros: Default::default(), | ||
68 | }; | 71 | }; |
69 | collector.collect(); | 72 | collector.collect(); |
70 | collector.finish() | 73 | collector.finish() |
@@ -123,6 +126,7 @@ struct DefCollector<'a> { | |||
123 | unexpanded_attribute_macros: Vec<DeriveDirective>, | 126 | unexpanded_attribute_macros: Vec<DeriveDirective>, |
124 | mod_dirs: FxHashMap<LocalModuleId, ModDir>, | 127 | mod_dirs: FxHashMap<LocalModuleId, ModDir>, |
125 | cfg_options: &'a CfgOptions, | 128 | cfg_options: &'a CfgOptions, |
129 | proc_macros: Vec<(Name, ProcMacroExpander)>, | ||
126 | } | 130 | } |
127 | 131 | ||
128 | impl DefCollector<'_> { | 132 | impl DefCollector<'_> { |
@@ -178,6 +182,24 @@ impl DefCollector<'_> { | |||
178 | for directive in unresolved_imports { | 182 | for directive in unresolved_imports { |
179 | self.record_resolved_import(&directive) | 183 | self.record_resolved_import(&directive) |
180 | } | 184 | } |
185 | |||
186 | // Record proc-macros | ||
187 | self.collect_proc_macro(); | ||
188 | } | ||
189 | |||
190 | fn collect_proc_macro(&mut self) { | ||
191 | let proc_macros = std::mem::take(&mut self.proc_macros); | ||
192 | for (name, expander) in proc_macros { | ||
193 | let krate = self.def_map.krate; | ||
194 | |||
195 | let macro_id = MacroDefId { | ||
196 | ast_id: None, | ||
197 | krate: Some(krate), | ||
198 | kind: MacroDefKind::CustomDerive(expander), | ||
199 | }; | ||
200 | |||
201 | self.define_proc_macro(name.clone(), macro_id); | ||
202 | } | ||
181 | } | 203 | } |
182 | 204 | ||
183 | /// Define a macro with `macro_rules`. | 205 | /// Define a macro with `macro_rules`. |
@@ -801,7 +823,6 @@ impl ModCollector<'_, '_> { | |||
801 | // in which case we don't add the invocation, just a single attribute | 823 | // in which case we don't add the invocation, just a single attribute |
802 | // macro invocation | 824 | // macro invocation |
803 | self.collect_derives(attrs, def); | 825 | self.collect_derives(attrs, def); |
804 | self.collect_proc_macro(attrs); | ||
805 | 826 | ||
806 | let name = def.name.clone(); | 827 | let name = def.name.clone(); |
807 | let container = ContainerId::ModuleId(module); | 828 | let container = ContainerId::ModuleId(module); |
@@ -878,28 +899,6 @@ impl ModCollector<'_, '_> { | |||
878 | } | 899 | } |
879 | } | 900 | } |
880 | 901 | ||
881 | fn collect_proc_macro(&mut self, attrs: &Attrs) { | ||
882 | if let Some(derive_subtree) = attrs.by_key("proc_macro_derive").tt_values().next() { | ||
883 | if let Some(tt) = derive_subtree.token_trees.get(0) { | ||
884 | let ident = match &tt { | ||
885 | tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => ident, | ||
886 | _ => return, // anything else would be an error (which we currently ignore) | ||
887 | }; | ||
888 | let name = ident.as_name(); | ||
889 | let krate = self.def_collector.def_map.krate; | ||
890 | let expander = ProcMacroExpander::new(krate); | ||
891 | |||
892 | let macro_id = MacroDefId { | ||
893 | ast_id: None, | ||
894 | krate: Some(krate), | ||
895 | kind: MacroDefKind::CustomDerive(expander), | ||
896 | }; | ||
897 | |||
898 | self.def_collector.define_proc_macro(name.clone(), macro_id); | ||
899 | } | ||
900 | } | ||
901 | } | ||
902 | |||
903 | fn collect_macro(&mut self, mac: &raw::MacroData) { | 902 | fn collect_macro(&mut self, mac: &raw::MacroData) { |
904 | let mut ast_id = AstIdWithPath::new(self.file_id, mac.ast_id, mac.path.clone()); | 903 | let mut ast_id = AstIdWithPath::new(self.file_id, mac.ast_id, mac.path.clone()); |
905 | 904 | ||
@@ -1001,6 +1000,7 @@ mod tests { | |||
1001 | unexpanded_attribute_macros: Vec::new(), | 1000 | unexpanded_attribute_macros: Vec::new(), |
1002 | mod_dirs: FxHashMap::default(), | 1001 | mod_dirs: FxHashMap::default(), |
1003 | cfg_options: &CfgOptions::default(), | 1002 | cfg_options: &CfgOptions::default(), |
1003 | proc_macros: Default::default(), | ||
1004 | }; | 1004 | }; |
1005 | collector.collect(); | 1005 | collector.collect(); |
1006 | collector.def_map | 1006 | collector.def_map |
diff --git a/crates/ra_hir_ty/src/tests/macros.rs b/crates/ra_hir_ty/src/tests/macros.rs index e3b9fbac5..eb97288f1 100644 --- a/crates/ra_hir_ty/src/tests/macros.rs +++ b/crates/ra_hir_ty/src/tests/macros.rs | |||
@@ -642,9 +642,10 @@ mod clone { | |||
642 | 642 | ||
643 | #[test] | 643 | #[test] |
644 | fn infer_custom_derive_simple() { | 644 | fn infer_custom_derive_simple() { |
645 | // FIXME: this test current now do nothing | ||
645 | let (db, pos) = TestDB::with_position( | 646 | let (db, pos) = TestDB::with_position( |
646 | r#" | 647 | r#" |
647 | //- /main.rs crate:main deps:foo | 648 | //- /main.rs crate:main |
648 | use foo::Foo; | 649 | use foo::Foo; |
649 | 650 | ||
650 | #[derive(Foo)] | 651 | #[derive(Foo)] |
@@ -653,11 +654,6 @@ struct S{} | |||
653 | fn test() { | 654 | fn test() { |
654 | S{}<|>; | 655 | S{}<|>; |
655 | } | 656 | } |
656 | |||
657 | //- /lib.rs crate:foo | ||
658 | #[proc_macro_derive(Foo)] | ||
659 | pub fn derive_foo(_item: TokenStream) -> TokenStream { | ||
660 | } | ||
661 | "#, | 657 | "#, |
662 | ); | 658 | ); |
663 | assert_eq!("S", type_at_pos(&db, pos)); | 659 | assert_eq!("S", type_at_pos(&db, pos)); |