diff options
Diffstat (limited to 'crates/hir_def/src/nameres/collector.rs')
-rw-r--r-- | crates/hir_def/src/nameres/collector.rs | 81 |
1 files changed, 48 insertions, 33 deletions
diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index 9996a0807..e51d89b43 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs | |||
@@ -13,7 +13,7 @@ use hir_expand::{ | |||
13 | builtin_macro::find_builtin_macro, | 13 | builtin_macro::find_builtin_macro, |
14 | name::{AsName, Name}, | 14 | name::{AsName, Name}, |
15 | proc_macro::ProcMacroExpander, | 15 | proc_macro::ProcMacroExpander, |
16 | HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, | 16 | HirFileId, MacroCallId, MacroDefId, MacroDefKind, |
17 | }; | 17 | }; |
18 | use hir_expand::{InFile, MacroCallLoc}; | 18 | use hir_expand::{InFile, MacroCallLoc}; |
19 | use rustc_hash::{FxHashMap, FxHashSet}; | 19 | use rustc_hash::{FxHashMap, FxHashSet}; |
@@ -24,11 +24,13 @@ use tt::{Leaf, TokenTree}; | |||
24 | use crate::{ | 24 | use crate::{ |
25 | attr::Attrs, | 25 | attr::Attrs, |
26 | db::DefDatabase, | 26 | db::DefDatabase, |
27 | item_attr_as_call_id, | ||
27 | item_scope::{ImportType, PerNsGlobImports}, | 28 | item_scope::{ImportType, PerNsGlobImports}, |
28 | item_tree::{ | 29 | item_tree::{ |
29 | self, FileItemTreeId, ItemTree, ItemTreeId, MacroCall, MacroRules, Mod, ModItem, ModKind, | 30 | self, FileItemTreeId, ItemTree, ItemTreeId, MacroCall, MacroRules, Mod, ModItem, ModKind, |
30 | StructDefKind, | 31 | StructDefKind, |
31 | }, | 32 | }, |
33 | macro_call_as_call_id, | ||
32 | nameres::{ | 34 | nameres::{ |
33 | diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint, | 35 | diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint, |
34 | BuiltinShadowMode, DefMap, ModuleData, ModuleOrigin, ResolveMode, | 36 | BuiltinShadowMode, DefMap, ModuleData, ModuleOrigin, ResolveMode, |
@@ -36,9 +38,9 @@ use crate::{ | |||
36 | path::{ImportAlias, ModPath, PathKind}, | 38 | path::{ImportAlias, ModPath, PathKind}, |
37 | per_ns::PerNs, | 39 | per_ns::PerNs, |
38 | visibility::{RawVisibility, Visibility}, | 40 | visibility::{RawVisibility, Visibility}, |
39 | AdtId, AsMacroCall, AstId, AstIdWithPath, ConstLoc, ContainerId, EnumLoc, EnumVariantId, | 41 | AdtId, AstId, AstIdWithPath, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, |
40 | FunctionLoc, ImplLoc, Intern, LocalModuleId, ModuleDefId, StaticLoc, StructLoc, TraitLoc, | 42 | ImplLoc, Intern, LocalModuleId, ModuleDefId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, |
41 | TypeAliasLoc, UnionLoc, | 43 | UnionLoc, UnresolvedMacro, |
42 | }; | 44 | }; |
43 | 45 | ||
44 | const GLOB_RECURSION_LIMIT: usize = 100; | 46 | const GLOB_RECURSION_LIMIT: usize = 100; |
@@ -790,8 +792,11 @@ impl DefCollector<'_> { | |||
790 | return false; | 792 | return false; |
791 | } | 793 | } |
792 | 794 | ||
793 | if let Some(call_id) = | 795 | match macro_call_as_call_id( |
794 | directive.ast_id.as_call_id(self.db, self.def_map.krate, |path| { | 796 | &directive.ast_id, |
797 | self.db, | ||
798 | self.def_map.krate, | ||
799 | |path| { | ||
795 | let resolved_res = self.def_map.resolve_path_fp_with_macro( | 800 | let resolved_res = self.def_map.resolve_path_fp_with_macro( |
796 | self.db, | 801 | self.db, |
797 | ResolveMode::Other, | 802 | ResolveMode::Other, |
@@ -800,24 +805,29 @@ impl DefCollector<'_> { | |||
800 | BuiltinShadowMode::Module, | 805 | BuiltinShadowMode::Module, |
801 | ); | 806 | ); |
802 | resolved_res.resolved_def.take_macros() | 807 | resolved_res.resolved_def.take_macros() |
803 | }) | 808 | }, |
804 | { | 809 | &mut |_err| (), |
805 | resolved.push((directive.module_id, call_id, directive.depth)); | 810 | ) { |
806 | res = ReachedFixedPoint::No; | 811 | Ok(Ok(call_id)) => { |
807 | return false; | 812 | resolved.push((directive.module_id, call_id, directive.depth)); |
813 | res = ReachedFixedPoint::No; | ||
814 | return false; | ||
815 | } | ||
816 | Err(UnresolvedMacro) | Ok(Err(_)) => {} | ||
808 | } | 817 | } |
809 | 818 | ||
810 | true | 819 | true |
811 | }); | 820 | }); |
812 | attribute_macros.retain(|directive| { | 821 | attribute_macros.retain(|directive| { |
813 | if let Some(call_id) = | 822 | match item_attr_as_call_id(&directive.ast_id, self.db, self.def_map.krate, |path| { |
814 | directive.ast_id.as_call_id(self.db, self.def_map.krate, |path| { | 823 | self.resolve_attribute_macro(&directive, &path) |
815 | self.resolve_attribute_macro(&directive, &path) | 824 | }) { |
816 | }) | 825 | Ok(call_id) => { |
817 | { | 826 | resolved.push((directive.module_id, call_id, 0)); |
818 | resolved.push((directive.module_id, call_id, 0)); | 827 | res = ReachedFixedPoint::No; |
819 | res = ReachedFixedPoint::No; | 828 | return false; |
820 | return false; | 829 | } |
830 | Err(UnresolvedMacro) => (), | ||
821 | } | 831 | } |
822 | 832 | ||
823 | true | 833 | true |
@@ -902,7 +912,8 @@ impl DefCollector<'_> { | |||
902 | 912 | ||
903 | for directive in &self.unexpanded_macros { | 913 | for directive in &self.unexpanded_macros { |
904 | let mut error = None; | 914 | let mut error = None; |
905 | directive.ast_id.as_call_id_with_errors( | 915 | match macro_call_as_call_id( |
916 | &directive.ast_id, | ||
906 | self.db, | 917 | self.db, |
907 | self.def_map.krate, | 918 | self.def_map.krate, |
908 | |path| { | 919 | |path| { |
@@ -918,15 +929,15 @@ impl DefCollector<'_> { | |||
918 | &mut |e| { | 929 | &mut |e| { |
919 | error.get_or_insert(e); | 930 | error.get_or_insert(e); |
920 | }, | 931 | }, |
921 | ); | 932 | ) { |
922 | 933 | Ok(_) => (), | |
923 | if let Some(err) = error { | 934 | Err(UnresolvedMacro) => { |
924 | self.def_map.diagnostics.push(DefDiagnostic::macro_error( | 935 | self.def_map.diagnostics.push(DefDiagnostic::unresolved_macro_call( |
925 | directive.module_id, | 936 | directive.module_id, |
926 | MacroCallKind::FnLike(directive.ast_id.ast_id), | 937 | directive.ast_id.ast_id, |
927 | err.to_string(), | 938 | )); |
928 | )); | 939 | } |
929 | } | 940 | }; |
930 | } | 941 | } |
931 | 942 | ||
932 | // Emit diagnostics for all remaining unresolved imports. | 943 | // Emit diagnostics for all remaining unresolved imports. |
@@ -1446,8 +1457,11 @@ impl ModCollector<'_, '_> { | |||
1446 | let mut ast_id = AstIdWithPath::new(self.file_id, mac.ast_id, mac.path.clone()); | 1457 | let mut ast_id = AstIdWithPath::new(self.file_id, mac.ast_id, mac.path.clone()); |
1447 | 1458 | ||
1448 | // Case 1: try to resolve in legacy scope and expand macro_rules | 1459 | // Case 1: try to resolve in legacy scope and expand macro_rules |
1449 | if let Some(macro_call_id) = | 1460 | if let Ok(Ok(macro_call_id)) = macro_call_as_call_id( |
1450 | ast_id.as_call_id(self.def_collector.db, self.def_collector.def_map.krate, |path| { | 1461 | &ast_id, |
1462 | self.def_collector.db, | ||
1463 | self.def_collector.def_map.krate, | ||
1464 | |path| { | ||
1451 | path.as_ident().and_then(|name| { | 1465 | path.as_ident().and_then(|name| { |
1452 | self.def_collector.def_map.with_ancestor_maps( | 1466 | self.def_collector.def_map.with_ancestor_maps( |
1453 | self.def_collector.db, | 1467 | self.def_collector.db, |
@@ -1455,8 +1469,9 @@ impl ModCollector<'_, '_> { | |||
1455 | &mut |map, module| map[module].scope.get_legacy_macro(&name), | 1469 | &mut |map, module| map[module].scope.get_legacy_macro(&name), |
1456 | ) | 1470 | ) |
1457 | }) | 1471 | }) |
1458 | }) | 1472 | }, |
1459 | { | 1473 | &mut |_err| (), |
1474 | ) { | ||
1460 | self.def_collector.unexpanded_macros.push(MacroDirective { | 1475 | self.def_collector.unexpanded_macros.push(MacroDirective { |
1461 | module_id: self.module_id, | 1476 | module_id: self.module_id, |
1462 | ast_id, | 1477 | ast_id, |