diff options
Diffstat (limited to 'crates/hir_def/src/nameres/collector.rs')
-rw-r--r-- | crates/hir_def/src/nameres/collector.rs | 47 |
1 files changed, 15 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); |