diff options
Diffstat (limited to 'crates/hir_def/src/nameres')
-rw-r--r-- | crates/hir_def/src/nameres/collector.rs | 48 |
1 files changed, 29 insertions, 19 deletions
diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index 1936348fb..b114a6fe4 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs | |||
@@ -221,17 +221,20 @@ impl DefCollector<'_> { | |||
221 | let item_tree = self.db.item_tree(file_id.into()); | 221 | let item_tree = self.db.item_tree(file_id.into()); |
222 | let module_id = self.def_map.root; | 222 | let module_id = self.def_map.root; |
223 | self.def_map.modules[module_id].origin = ModuleOrigin::CrateRoot { definition: file_id }; | 223 | self.def_map.modules[module_id].origin = ModuleOrigin::CrateRoot { definition: file_id }; |
224 | let mut root_collector = ModCollector { | 224 | if item_tree |
225 | def_collector: &mut *self, | 225 | .top_level_attrs(self.db, self.def_map.krate) |
226 | macro_depth: 0, | 226 | .cfg() |
227 | module_id, | 227 | .map_or(true, |cfg| self.cfg_options.check(&cfg) != Some(false)) |
228 | file_id: file_id.into(), | ||
229 | item_tree: &item_tree, | ||
230 | mod_dir: ModDir::root(), | ||
231 | }; | ||
232 | if item_tree.top_level_attrs().cfg().map_or(true, |cfg| root_collector.is_cfg_enabled(&cfg)) | ||
233 | { | 228 | { |
234 | root_collector.collect(item_tree.top_level_items()); | 229 | ModCollector { |
230 | def_collector: &mut *self, | ||
231 | macro_depth: 0, | ||
232 | module_id, | ||
233 | file_id: file_id.into(), | ||
234 | item_tree: &item_tree, | ||
235 | mod_dir: ModDir::root(), | ||
236 | } | ||
237 | .collect(item_tree.top_level_items()); | ||
235 | } | 238 | } |
236 | 239 | ||
237 | // main name resolution fixed-point loop. | 240 | // main name resolution fixed-point loop. |
@@ -905,6 +908,8 @@ struct ModCollector<'a, 'b> { | |||
905 | 908 | ||
906 | impl ModCollector<'_, '_> { | 909 | impl ModCollector<'_, '_> { |
907 | fn collect(&mut self, items: &[ModItem]) { | 910 | fn collect(&mut self, items: &[ModItem]) { |
911 | let krate = self.def_collector.def_map.krate; | ||
912 | |||
908 | // Note: don't assert that inserted value is fresh: it's simply not true | 913 | // Note: don't assert that inserted value is fresh: it's simply not true |
909 | // for macros. | 914 | // for macros. |
910 | self.def_collector.mod_dirs.insert(self.module_id, self.mod_dir.clone()); | 915 | self.def_collector.mod_dirs.insert(self.module_id, self.mod_dir.clone()); |
@@ -921,7 +926,7 @@ impl ModCollector<'_, '_> { | |||
921 | // `#[macro_use] extern crate` is hoisted to imports macros before collecting | 926 | // `#[macro_use] extern crate` is hoisted to imports macros before collecting |
922 | // any other items. | 927 | // any other items. |
923 | for item in items { | 928 | for item in items { |
924 | let attrs = self.item_tree.attrs((*item).into()); | 929 | let attrs = self.item_tree.attrs(self.def_collector.db, krate, (*item).into()); |
925 | if attrs.cfg().map_or(true, |cfg| self.is_cfg_enabled(&cfg)) { | 930 | if attrs.cfg().map_or(true, |cfg| self.is_cfg_enabled(&cfg)) { |
926 | if let ModItem::ExternCrate(id) = item { | 931 | if let ModItem::ExternCrate(id) = item { |
927 | let import = self.item_tree[*id].clone(); | 932 | let import = self.item_tree[*id].clone(); |
@@ -933,7 +938,7 @@ impl ModCollector<'_, '_> { | |||
933 | } | 938 | } |
934 | 939 | ||
935 | for &item in items { | 940 | for &item in items { |
936 | let attrs = self.item_tree.attrs(item.into()); | 941 | let attrs = self.item_tree.attrs(self.def_collector.db, krate, item.into()); |
937 | if let Some(cfg) = attrs.cfg() { | 942 | if let Some(cfg) = attrs.cfg() { |
938 | if !self.is_cfg_enabled(&cfg) { | 943 | if !self.is_cfg_enabled(&cfg) { |
939 | self.emit_unconfigured_diagnostic(item, &cfg); | 944 | self.emit_unconfigured_diagnostic(item, &cfg); |
@@ -946,7 +951,7 @@ impl ModCollector<'_, '_> { | |||
946 | 951 | ||
947 | let mut def = None; | 952 | let mut def = None; |
948 | match item { | 953 | match item { |
949 | ModItem::Mod(m) => self.collect_module(&self.item_tree[m], attrs), | 954 | ModItem::Mod(m) => self.collect_module(&self.item_tree[m], &attrs), |
950 | ModItem::Import(import_id) => { | 955 | ModItem::Import(import_id) => { |
951 | self.def_collector.unresolved_imports.push(ImportDirective { | 956 | self.def_collector.unresolved_imports.push(ImportDirective { |
952 | module_id: self.module_id, | 957 | module_id: self.module_id, |
@@ -975,7 +980,11 @@ impl ModCollector<'_, '_> { | |||
975 | 980 | ||
976 | // "Macro 2.0" is not currently supported by rust-analyzer, but libcore uses it | 981 | // "Macro 2.0" is not currently supported by rust-analyzer, but libcore uses it |
977 | // to define builtin macros, so we support at least that part. | 982 | // to define builtin macros, so we support at least that part. |
978 | let attrs = self.item_tree.attrs(ModItem::from(id).into()); | 983 | let attrs = self.item_tree.attrs( |
984 | self.def_collector.db, | ||
985 | krate, | ||
986 | ModItem::from(id).into(), | ||
987 | ); | ||
979 | if attrs.by_key("rustc_builtin_macro").exists() { | 988 | if attrs.by_key("rustc_builtin_macro").exists() { |
980 | let krate = self.def_collector.def_map.krate; | 989 | let krate = self.def_collector.def_map.krate; |
981 | let macro_id = find_builtin_macro(&mac.name, krate, ast_id) | 990 | let macro_id = find_builtin_macro(&mac.name, krate, ast_id) |
@@ -1012,7 +1021,7 @@ impl ModCollector<'_, '_> { | |||
1012 | ModItem::Function(id) => { | 1021 | ModItem::Function(id) => { |
1013 | let func = &self.item_tree[id]; | 1022 | let func = &self.item_tree[id]; |
1014 | 1023 | ||
1015 | self.collect_proc_macro_def(&func.name, attrs); | 1024 | self.collect_proc_macro_def(&func.name, &attrs); |
1016 | 1025 | ||
1017 | def = Some(DefData { | 1026 | def = Some(DefData { |
1018 | id: FunctionLoc { | 1027 | id: FunctionLoc { |
@@ -1032,7 +1041,7 @@ impl ModCollector<'_, '_> { | |||
1032 | // FIXME: check attrs to see if this is an attribute macro invocation; | 1041 | // FIXME: check attrs to see if this is an attribute macro invocation; |
1033 | // in which case we don't add the invocation, just a single attribute | 1042 | // in which case we don't add the invocation, just a single attribute |
1034 | // macro invocation | 1043 | // macro invocation |
1035 | self.collect_derives(attrs, it.ast_id.upcast()); | 1044 | self.collect_derives(&attrs, it.ast_id.upcast()); |
1036 | 1045 | ||
1037 | def = Some(DefData { | 1046 | def = Some(DefData { |
1038 | id: StructLoc { container, id: ItemTreeId::new(self.file_id, id) } | 1047 | id: StructLoc { container, id: ItemTreeId::new(self.file_id, id) } |
@@ -1049,7 +1058,7 @@ impl ModCollector<'_, '_> { | |||
1049 | // FIXME: check attrs to see if this is an attribute macro invocation; | 1058 | // FIXME: check attrs to see if this is an attribute macro invocation; |
1050 | // in which case we don't add the invocation, just a single attribute | 1059 | // in which case we don't add the invocation, just a single attribute |
1051 | // macro invocation | 1060 | // macro invocation |
1052 | self.collect_derives(attrs, it.ast_id.upcast()); | 1061 | self.collect_derives(&attrs, it.ast_id.upcast()); |
1053 | 1062 | ||
1054 | def = Some(DefData { | 1063 | def = Some(DefData { |
1055 | id: UnionLoc { container, id: ItemTreeId::new(self.file_id, id) } | 1064 | id: UnionLoc { container, id: ItemTreeId::new(self.file_id, id) } |
@@ -1066,7 +1075,7 @@ impl ModCollector<'_, '_> { | |||
1066 | // FIXME: check attrs to see if this is an attribute macro invocation; | 1075 | // FIXME: check attrs to see if this is an attribute macro invocation; |
1067 | // in which case we don't add the invocation, just a single attribute | 1076 | // in which case we don't add the invocation, just a single attribute |
1068 | // macro invocation | 1077 | // macro invocation |
1069 | self.collect_derives(attrs, it.ast_id.upcast()); | 1078 | self.collect_derives(&attrs, it.ast_id.upcast()); |
1070 | 1079 | ||
1071 | def = Some(DefData { | 1080 | def = Some(DefData { |
1072 | id: EnumLoc { container, id: ItemTreeId::new(self.file_id, id) } | 1081 | id: EnumLoc { container, id: ItemTreeId::new(self.file_id, id) } |
@@ -1303,8 +1312,9 @@ impl ModCollector<'_, '_> { | |||
1303 | } | 1312 | } |
1304 | 1313 | ||
1305 | fn collect_macro_rules(&mut self, id: FileItemTreeId<MacroRules>) { | 1314 | fn collect_macro_rules(&mut self, id: FileItemTreeId<MacroRules>) { |
1315 | let krate = self.def_collector.def_map.krate; | ||
1306 | let mac = &self.item_tree[id]; | 1316 | let mac = &self.item_tree[id]; |
1307 | let attrs = self.item_tree.attrs(ModItem::from(id).into()); | 1317 | let attrs = self.item_tree.attrs(self.def_collector.db, krate, ModItem::from(id).into()); |
1308 | let ast_id = InFile::new(self.file_id, mac.ast_id.upcast()); | 1318 | let ast_id = InFile::new(self.file_id, mac.ast_id.upcast()); |
1309 | 1319 | ||
1310 | let export_attr = attrs.by_key("macro_export"); | 1320 | let export_attr = attrs.by_key("macro_export"); |