diff options
Diffstat (limited to 'crates/hir_def/src/nameres')
-rw-r--r-- | crates/hir_def/src/nameres/collector.rs | 47 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/proc_macro.rs | 71 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/tests/macros.rs | 26 |
3 files changed, 112 insertions, 32 deletions
diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index d0fefb5af..dcedf7766 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs | |||
@@ -18,7 +18,6 @@ use hir_expand::{ | |||
18 | use hir_expand::{InFile, MacroCallLoc}; | 18 | use hir_expand::{InFile, MacroCallLoc}; |
19 | use rustc_hash::{FxHashMap, FxHashSet}; | 19 | use rustc_hash::{FxHashMap, FxHashSet}; |
20 | use syntax::ast; | 20 | use syntax::ast; |
21 | use tt::{Leaf, TokenTree}; | ||
22 | 21 | ||
23 | use crate::{ | 22 | use crate::{ |
24 | attr::Attrs, | 23 | attr::Attrs, |
@@ -42,6 +41,8 @@ use crate::{ | |||
42 | UnresolvedMacro, | 41 | UnresolvedMacro, |
43 | }; | 42 | }; |
44 | 43 | ||
44 | use super::proc_macro::ProcMacroDef; | ||
45 | |||
45 | const GLOB_RECURSION_LIMIT: usize = 100; | 46 | const GLOB_RECURSION_LIMIT: usize = 100; |
46 | const EXPANSION_DEPTH_LIMIT: usize = 128; | 47 | const EXPANSION_DEPTH_LIMIT: usize = 128; |
47 | const FIXED_POINT_LIMIT: usize = 8192; | 48 | const FIXED_POINT_LIMIT: usize = 8192; |
@@ -353,24 +354,23 @@ impl DefCollector<'_> { | |||
353 | /// use a dummy expander that always errors. This comes with the drawback of macros potentially | 354 | /// use a dummy expander that always errors. This comes with the drawback of macros potentially |
354 | /// going out of sync with what the build system sees (since we resolve using VFS state, but | 355 | /// going out of sync with what the build system sees (since we resolve using VFS state, but |
355 | /// Cargo builds only on-disk files). We could and probably should add diagnostics for that. | 356 | /// Cargo builds only on-disk files). We could and probably should add diagnostics for that. |
356 | fn resolve_proc_macro(&mut self, name: &Name) { | 357 | fn export_proc_macro(&mut self, def: ProcMacroDef, ast_id: AstId<ast::Fn>) { |
357 | self.exports_proc_macros = true; | 358 | self.exports_proc_macros = true; |
358 | let macro_def = match self.proc_macros.iter().find(|(n, _)| n == name) { | 359 | let macro_def = match self.proc_macros.iter().find(|(n, _)| n == &def.name) { |
359 | Some((_, expander)) => MacroDefId { | 360 | Some((_, expander)) => MacroDefId { |
360 | ast_id: None, | ||
361 | krate: self.def_map.krate, | 361 | krate: self.def_map.krate, |
362 | kind: MacroDefKind::ProcMacro(*expander), | 362 | kind: MacroDefKind::ProcMacro(*expander, ast_id), |
363 | local_inner: false, | 363 | local_inner: false, |
364 | }, | 364 | }, |
365 | None => MacroDefId { | 365 | None => MacroDefId { |
366 | ast_id: None, | ||
367 | krate: self.def_map.krate, | 366 | krate: self.def_map.krate, |
368 | kind: MacroDefKind::ProcMacro(ProcMacroExpander::dummy(self.def_map.krate)), | 367 | kind: MacroDefKind::ProcMacro(ProcMacroExpander::dummy(self.def_map.krate), ast_id), |
369 | local_inner: false, | 368 | local_inner: false, |
370 | }, | 369 | }, |
371 | }; | 370 | }; |
372 | 371 | ||
373 | self.define_proc_macro(name.clone(), macro_def); | 372 | self.define_proc_macro(def.name.clone(), macro_def); |
373 | self.def_map.exported_proc_macros.insert(macro_def, def); | ||
374 | } | 374 | } |
375 | 375 | ||
376 | /// Define a macro with `macro_rules`. | 376 | /// Define a macro with `macro_rules`. |
@@ -1118,7 +1118,8 @@ impl ModCollector<'_, '_> { | |||
1118 | ModItem::Function(id) => { | 1118 | ModItem::Function(id) => { |
1119 | let func = &self.item_tree[id]; | 1119 | let func = &self.item_tree[id]; |
1120 | 1120 | ||
1121 | self.collect_proc_macro_def(&func.name, &attrs); | 1121 | let ast_id = InFile::new(self.file_id, func.ast_id); |
1122 | self.collect_proc_macro_def(&func.name, ast_id, &attrs); | ||
1122 | 1123 | ||
1123 | def = Some(DefData { | 1124 | def = Some(DefData { |
1124 | id: FunctionLoc { | 1125 | id: FunctionLoc { |
@@ -1385,28 +1386,11 @@ impl ModCollector<'_, '_> { | |||
1385 | } | 1386 | } |
1386 | 1387 | ||
1387 | /// If `attrs` registers a procedural macro, collects its definition. | 1388 | /// If `attrs` registers a procedural macro, collects its definition. |
1388 | fn collect_proc_macro_def(&mut self, func_name: &Name, attrs: &Attrs) { | 1389 | fn collect_proc_macro_def(&mut self, func_name: &Name, ast_id: AstId<ast::Fn>, attrs: &Attrs) { |
1389 | // FIXME: this should only be done in the root module of `proc-macro` crates, not everywhere | 1390 | // FIXME: this should only be done in the root module of `proc-macro` crates, not everywhere |
1390 | // FIXME: distinguish the type of macro | 1391 | if let Some(proc_macro) = attrs.parse_proc_macro_decl(func_name) { |
1391 | let macro_name = if attrs.by_key("proc_macro").exists() | 1392 | self.def_collector.export_proc_macro(proc_macro, ast_id); |
1392 | || attrs.by_key("proc_macro_attribute").exists() | 1393 | } |
1393 | { | ||
1394 | func_name.clone() | ||
1395 | } else { | ||
1396 | let derive = attrs.by_key("proc_macro_derive"); | ||
1397 | if let Some(arg) = derive.tt_values().next() { | ||
1398 | if let [TokenTree::Leaf(Leaf::Ident(trait_name)), ..] = &*arg.token_trees { | ||
1399 | trait_name.as_name() | ||
1400 | } else { | ||
1401 | log::trace!("malformed `#[proc_macro_derive]`: {}", arg); | ||
1402 | return; | ||
1403 | } | ||
1404 | } else { | ||
1405 | return; | ||
1406 | } | ||
1407 | }; | ||
1408 | |||
1409 | self.def_collector.resolve_proc_macro(¯o_name); | ||
1410 | } | 1394 | } |
1411 | 1395 | ||
1412 | fn collect_macro_rules(&mut self, id: FileItemTreeId<MacroRules>) { | 1396 | fn collect_macro_rules(&mut self, id: FileItemTreeId<MacroRules>) { |
@@ -1445,9 +1429,8 @@ impl ModCollector<'_, '_> { | |||
1445 | 1429 | ||
1446 | // Case 2: normal `macro_rules!` macro | 1430 | // Case 2: normal `macro_rules!` macro |
1447 | let macro_id = MacroDefId { | 1431 | let macro_id = MacroDefId { |
1448 | ast_id: Some(ast_id), | ||
1449 | krate: self.def_collector.def_map.krate, | 1432 | krate: self.def_collector.def_map.krate, |
1450 | kind: MacroDefKind::Declarative, | 1433 | kind: MacroDefKind::Declarative(ast_id), |
1451 | local_inner: is_local_inner, | 1434 | local_inner: is_local_inner, |
1452 | }; | 1435 | }; |
1453 | self.def_collector.define_macro(self.module_id, mac.name.clone(), macro_id, is_export); | 1436 | self.def_collector.define_macro(self.module_id, mac.name.clone(), macro_id, is_export); |
diff --git a/crates/hir_def/src/nameres/proc_macro.rs b/crates/hir_def/src/nameres/proc_macro.rs new file mode 100644 index 000000000..156598f19 --- /dev/null +++ b/crates/hir_def/src/nameres/proc_macro.rs | |||
@@ -0,0 +1,71 @@ | |||
1 | //! Nameres-specific procedural macro data and helpers. | ||
2 | |||
3 | use hir_expand::name::{AsName, Name}; | ||
4 | use tt::{Leaf, TokenTree}; | ||
5 | |||
6 | use crate::attr::Attrs; | ||
7 | |||
8 | #[derive(Debug, PartialEq, Eq)] | ||
9 | pub(super) struct ProcMacroDef { | ||
10 | pub(super) name: Name, | ||
11 | pub(super) kind: ProcMacroKind, | ||
12 | } | ||
13 | |||
14 | #[derive(Debug, PartialEq, Eq)] | ||
15 | pub(super) enum ProcMacroKind { | ||
16 | CustomDerive { helpers: Box<[Name]> }, | ||
17 | FnLike, | ||
18 | Attr, | ||
19 | } | ||
20 | |||
21 | impl Attrs { | ||
22 | #[rustfmt::skip] | ||
23 | pub(super) fn parse_proc_macro_decl(&self, func_name: &Name) -> Option<ProcMacroDef> { | ||
24 | if self.by_key("proc_macro").exists() { | ||
25 | Some(ProcMacroDef { name: func_name.clone(), kind: ProcMacroKind::FnLike }) | ||
26 | } else if self.by_key("proc_macro_attribute").exists() { | ||
27 | Some(ProcMacroDef { name: func_name.clone(), kind: ProcMacroKind::Attr }) | ||
28 | } else if self.by_key("proc_macro_derive").exists() { | ||
29 | let derive = self.by_key("proc_macro_derive").tt_values().next().unwrap(); | ||
30 | |||
31 | match &*derive.token_trees { | ||
32 | // `#[proc_macro_derive(Trait)]` | ||
33 | [TokenTree::Leaf(Leaf::Ident(trait_name))] => Some(ProcMacroDef { | ||
34 | name: trait_name.as_name(), | ||
35 | kind: ProcMacroKind::CustomDerive { helpers: Box::new([]) }, | ||
36 | }), | ||
37 | |||
38 | // `#[proc_macro_derive(Trait, attibutes(helper1, helper2, ...))]` | ||
39 | [ | ||
40 | TokenTree::Leaf(Leaf::Ident(trait_name)), | ||
41 | TokenTree::Leaf(Leaf::Punct(comma)), | ||
42 | TokenTree::Leaf(Leaf::Ident(attributes)), | ||
43 | TokenTree::Subtree(helpers) | ||
44 | ] if comma.char == ',' && attributes.text == "attributes" => | ||
45 | { | ||
46 | let helpers = helpers.token_trees.iter() | ||
47 | .filter(|tt| !matches!(tt, TokenTree::Leaf(Leaf::Punct(comma)) if comma.char == ',')) | ||
48 | .map(|tt| { | ||
49 | match tt { | ||
50 | TokenTree::Leaf(Leaf::Ident(helper)) => Some(helper.as_name()), | ||
51 | _ => None | ||
52 | } | ||
53 | }) | ||
54 | .collect::<Option<Box<[_]>>>()?; | ||
55 | |||
56 | Some(ProcMacroDef { | ||
57 | name: trait_name.as_name(), | ||
58 | kind: ProcMacroKind::CustomDerive { helpers }, | ||
59 | }) | ||
60 | } | ||
61 | |||
62 | _ => { | ||
63 | log::trace!("malformed `#[proc_macro_derive]`: {}", derive); | ||
64 | None | ||
65 | } | ||
66 | } | ||
67 | } else { | ||
68 | None | ||
69 | } | ||
70 | } | ||
71 | } | ||
diff --git a/crates/hir_def/src/nameres/tests/macros.rs b/crates/hir_def/src/nameres/tests/macros.rs index f65a655bf..d59d3c0db 100644 --- a/crates/hir_def/src/nameres/tests/macros.rs +++ b/crates/hir_def/src/nameres/tests/macros.rs | |||
@@ -1,4 +1,5 @@ | |||
1 | use super::*; | 1 | use super::*; |
2 | use crate::nameres::proc_macro::{ProcMacroDef, ProcMacroKind}; | ||
2 | 3 | ||
3 | #[test] | 4 | #[test] |
4 | fn macro_rules_are_globally_visible() { | 5 | fn macro_rules_are_globally_visible() { |
@@ -790,3 +791,28 @@ fn proc_macro_censoring() { | |||
790 | "#]], | 791 | "#]], |
791 | ); | 792 | ); |
792 | } | 793 | } |
794 | |||
795 | #[test] | ||
796 | fn collects_derive_helpers() { | ||
797 | let def_map = compute_crate_def_map( | ||
798 | r" | ||
799 | struct TokenStream; | ||
800 | |||
801 | #[proc_macro_derive(AnotherTrait, attributes(helper_attr))] | ||
802 | pub fn derive_macro_2(_item: TokenStream) -> TokenStream { | ||
803 | TokenStream | ||
804 | } | ||
805 | ", | ||
806 | ); | ||
807 | |||
808 | assert_eq!(def_map.exported_proc_macros.len(), 1); | ||
809 | match def_map.exported_proc_macros.values().next() { | ||
810 | Some(ProcMacroDef { kind: ProcMacroKind::CustomDerive { helpers }, .. }) => { | ||
811 | match &**helpers { | ||
812 | [attr] => assert_eq!(attr.to_string(), "helper_attr"), | ||
813 | _ => unreachable!(), | ||
814 | } | ||
815 | } | ||
816 | _ => unreachable!(), | ||
817 | } | ||
818 | } | ||