]> git.lizzy.rs Git - rust.git/blob - crates/hir_def/src/nameres/collector.rs
Skip test/bench attr expansion in resolution instead of collection
[rust.git] / crates / hir_def / src / nameres / collector.rs
1 //! The core of the module-level name resolution algorithm.
2 //!
3 //! `DefCollector::collect` contains the fixed-point iteration loop which
4 //! resolves imports and expands macros.
5
6 use std::iter;
7
8 use base_db::{CrateId, Edition, FileId, ProcMacroId};
9 use cfg::{CfgExpr, CfgOptions};
10 use hir_expand::{
11     ast_id_map::FileAstId,
12     builtin_attr_macro::{find_builtin_attr, is_builtin_test_or_bench_attr},
13     builtin_derive_macro::find_builtin_derive,
14     builtin_fn_macro::find_builtin_macro,
15     name::{name, AsName, Name},
16     proc_macro::ProcMacroExpander,
17     ExpandTo, HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
18 };
19 use hir_expand::{InFile, MacroCallLoc};
20 use itertools::Itertools;
21 use la_arena::Idx;
22 use limit::Limit;
23 use rustc_hash::{FxHashMap, FxHashSet};
24 use syntax::ast;
25
26 use crate::{
27     attr::{Attr, AttrId, AttrInput, Attrs},
28     attr_macro_as_call_id, builtin_attr,
29     db::DefDatabase,
30     derive_macro_as_call_id,
31     intern::Interned,
32     item_scope::{ImportType, PerNsGlobImports},
33     item_tree::{
34         self, Fields, FileItemTreeId, ImportKind, ItemTree, ItemTreeId, MacroCall, MacroDef,
35         MacroRules, Mod, ModItem, ModKind, TreeId,
36     },
37     macro_call_as_call_id,
38     nameres::{
39         diagnostics::DefDiagnostic,
40         mod_resolution::ModDir,
41         path_resolution::ReachedFixedPoint,
42         proc_macro::{ProcMacroDef, ProcMacroKind},
43         BuiltinShadowMode, DefMap, ModuleData, ModuleOrigin, ResolveMode,
44     },
45     path::{ImportAlias, ModPath, PathKind},
46     per_ns::PerNs,
47     visibility::{RawVisibility, Visibility},
48     AdtId, AstId, AstIdWithPath, ConstLoc, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern,
49     LocalModuleId, ModuleDefId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc,
50     UnresolvedMacro,
51 };
52
53 static GLOB_RECURSION_LIMIT: Limit = Limit::new(100);
54 static EXPANSION_DEPTH_LIMIT: Limit = Limit::new(128);
55 static FIXED_POINT_LIMIT: Limit = Limit::new(8192);
56
57 pub(super) fn collect_defs(
58     db: &dyn DefDatabase,
59     mut def_map: DefMap,
60     block: Option<AstId<ast::BlockExpr>>,
61 ) -> DefMap {
62     let crate_graph = db.crate_graph();
63
64     let mut deps = FxHashMap::default();
65     // populate external prelude and dependency list
66     for dep in &crate_graph[def_map.krate].dependencies {
67         tracing::debug!("crate dep {:?} -> {:?}", dep.name, dep.crate_id);
68         let dep_def_map = db.crate_def_map(dep.crate_id);
69         let dep_root = dep_def_map.module_id(dep_def_map.root);
70
71         deps.insert(dep.as_name(), dep_root.into());
72
73         if dep.is_prelude() && block.is_none() {
74             def_map.extern_prelude.insert(dep.as_name(), dep_root.into());
75         }
76     }
77
78     let cfg_options = &crate_graph[def_map.krate].cfg_options;
79     let proc_macros = &crate_graph[def_map.krate].proc_macro;
80     let proc_macros = proc_macros
81         .iter()
82         .enumerate()
83         .map(|(idx, it)| {
84             // FIXME: a hacky way to create a Name from string.
85             let name = tt::Ident { text: it.name.clone(), id: tt::TokenId::unspecified() };
86             (name.as_name(), ProcMacroExpander::new(def_map.krate, ProcMacroId(idx as u32)))
87         })
88         .collect();
89
90     let mut collector = DefCollector {
91         db,
92         def_map,
93         deps,
94         glob_imports: FxHashMap::default(),
95         unresolved_imports: Vec::new(),
96         resolved_imports: Vec::new(),
97
98         unresolved_macros: Vec::new(),
99         mod_dirs: FxHashMap::default(),
100         cfg_options,
101         proc_macros,
102         exports_proc_macros: false,
103         from_glob_import: Default::default(),
104         skip_attrs: Default::default(),
105         derive_helpers_in_scope: Default::default(),
106         registered_attrs: Default::default(),
107         registered_tools: Default::default(),
108     };
109     match block {
110         Some(block) => {
111             collector.seed_with_inner(block);
112         }
113         None => {
114             collector.seed_with_top_level();
115         }
116     }
117     collector.collect();
118     let mut def_map = collector.finish();
119     def_map.shrink_to_fit();
120     def_map
121 }
122
123 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
124 enum PartialResolvedImport {
125     /// None of any namespaces is resolved
126     Unresolved,
127     /// One of namespaces is resolved
128     Indeterminate(PerNs),
129     /// All namespaces are resolved, OR it is came from other crate
130     Resolved(PerNs),
131 }
132
133 impl PartialResolvedImport {
134     fn namespaces(&self) -> PerNs {
135         match self {
136             PartialResolvedImport::Unresolved => PerNs::none(),
137             PartialResolvedImport::Indeterminate(ns) => *ns,
138             PartialResolvedImport::Resolved(ns) => *ns,
139         }
140     }
141 }
142
143 #[derive(Clone, Debug, Eq, PartialEq)]
144 enum ImportSource {
145     Import { id: ItemTreeId<item_tree::Import>, use_tree: Idx<ast::UseTree> },
146     ExternCrate(ItemTreeId<item_tree::ExternCrate>),
147 }
148
149 #[derive(Clone, Debug, Eq, PartialEq)]
150 struct Import {
151     path: Interned<ModPath>,
152     alias: Option<ImportAlias>,
153     visibility: RawVisibility,
154     kind: ImportKind,
155     is_prelude: bool,
156     is_extern_crate: bool,
157     is_macro_use: bool,
158     source: ImportSource,
159 }
160
161 impl Import {
162     fn from_use(
163         db: &dyn DefDatabase,
164         krate: CrateId,
165         tree: &ItemTree,
166         id: ItemTreeId<item_tree::Import>,
167     ) -> Vec<Self> {
168         let it = &tree[id.value];
169         let attrs = &tree.attrs(db, krate, ModItem::from(id.value).into());
170         let visibility = &tree[it.visibility];
171         let is_prelude = attrs.by_key("prelude_import").exists();
172
173         let mut res = Vec::new();
174         it.use_tree.expand(|idx, path, kind, alias| {
175             res.push(Self {
176                 path: Interned::new(path), // FIXME this makes little sense
177                 alias,
178                 visibility: visibility.clone(),
179                 kind,
180                 is_prelude,
181                 is_extern_crate: false,
182                 is_macro_use: false,
183                 source: ImportSource::Import { id, use_tree: idx },
184             });
185         });
186         res
187     }
188
189     fn from_extern_crate(
190         db: &dyn DefDatabase,
191         krate: CrateId,
192         tree: &ItemTree,
193         id: ItemTreeId<item_tree::ExternCrate>,
194     ) -> Self {
195         let it = &tree[id.value];
196         let attrs = &tree.attrs(db, krate, ModItem::from(id.value).into());
197         let visibility = &tree[it.visibility];
198         Self {
199             path: Interned::new(ModPath::from_segments(
200                 PathKind::Plain,
201                 iter::once(it.name.clone()),
202             )),
203             alias: it.alias.clone(),
204             visibility: visibility.clone(),
205             kind: ImportKind::Plain,
206             is_prelude: false,
207             is_extern_crate: true,
208             is_macro_use: attrs.by_key("macro_use").exists(),
209             source: ImportSource::ExternCrate(id),
210         }
211     }
212 }
213
214 #[derive(Clone, Debug, Eq, PartialEq)]
215 struct ImportDirective {
216     module_id: LocalModuleId,
217     import: Import,
218     status: PartialResolvedImport,
219 }
220
221 #[derive(Clone, Debug, Eq, PartialEq)]
222 struct MacroDirective {
223     module_id: LocalModuleId,
224     depth: usize,
225     kind: MacroDirectiveKind,
226 }
227
228 #[derive(Clone, Debug, Eq, PartialEq)]
229 enum MacroDirectiveKind {
230     FnLike { ast_id: AstIdWithPath<ast::MacroCall>, expand_to: ExpandTo },
231     Derive { ast_id: AstIdWithPath<ast::Item>, derive_attr: AttrId },
232     Attr { ast_id: AstIdWithPath<ast::Item>, attr: Attr, mod_item: ModItem },
233 }
234
235 struct DefData<'a> {
236     id: ModuleDefId,
237     name: &'a Name,
238     visibility: &'a RawVisibility,
239     has_constructor: bool,
240 }
241
242 /// Walks the tree of module recursively
243 struct DefCollector<'a> {
244     db: &'a dyn DefDatabase,
245     def_map: DefMap,
246     deps: FxHashMap<Name, ModuleDefId>,
247     glob_imports: FxHashMap<LocalModuleId, Vec<(LocalModuleId, Visibility)>>,
248     unresolved_imports: Vec<ImportDirective>,
249     resolved_imports: Vec<ImportDirective>,
250     unresolved_macros: Vec<MacroDirective>,
251     mod_dirs: FxHashMap<LocalModuleId, ModDir>,
252     cfg_options: &'a CfgOptions,
253     /// List of procedural macros defined by this crate. This is read from the dynamic library
254     /// built by the build system, and is the list of proc. macros we can actually expand. It is
255     /// empty when proc. macro support is disabled (in which case we still do name resolution for
256     /// them).
257     proc_macros: Vec<(Name, ProcMacroExpander)>,
258     exports_proc_macros: bool,
259     from_glob_import: PerNsGlobImports,
260     /// If we fail to resolve an attribute on a `ModItem`, we fall back to ignoring the attribute.
261     /// This map is used to skip all attributes up to and including the one that failed to resolve,
262     /// in order to not expand them twice.
263     ///
264     /// This also stores the attributes to skip when we resolve derive helpers and non-macro
265     /// non-builtin attributes in general.
266     skip_attrs: FxHashMap<InFile<ModItem>, AttrId>,
267     /// Tracks which custom derives are in scope for an item, to allow resolution of derive helper
268     /// attributes.
269     derive_helpers_in_scope: FxHashMap<AstId<ast::Item>, Vec<Name>>,
270     /// Custom attributes registered with `#![register_attr]`.
271     registered_attrs: Vec<String>,
272     /// Custom tool modules registered with `#![register_tool]`.
273     registered_tools: Vec<String>,
274 }
275
276 impl DefCollector<'_> {
277     fn seed_with_top_level(&mut self) {
278         let _p = profile::span("seed_with_top_level");
279
280         let file_id = self.db.crate_graph()[self.def_map.krate].root_file_id;
281         let item_tree = self.db.file_item_tree(file_id.into());
282         let module_id = self.def_map.root;
283
284         let attrs = item_tree.top_level_attrs(self.db, self.def_map.krate);
285         if attrs.cfg().map_or(true, |cfg| self.cfg_options.check(&cfg) != Some(false)) {
286             self.inject_prelude(&attrs);
287
288             // Process other crate-level attributes.
289             for attr in &*attrs {
290                 let attr_name = match attr.path.as_ident() {
291                     Some(name) => name,
292                     None => continue,
293                 };
294
295                 let registered_name = if *attr_name == hir_expand::name![register_attr]
296                     || *attr_name == hir_expand::name![register_tool]
297                 {
298                     match attr.input.as_deref() {
299                         Some(AttrInput::TokenTree(subtree, _)) => match &*subtree.token_trees {
300                             [tt::TokenTree::Leaf(tt::Leaf::Ident(name))] => name.as_name(),
301                             _ => continue,
302                         },
303                         _ => continue,
304                     }
305                 } else {
306                     continue;
307                 };
308
309                 if *attr_name == hir_expand::name![register_attr] {
310                     self.registered_attrs.push(registered_name.to_string());
311                     cov_mark::hit!(register_attr);
312                 } else {
313                     self.registered_tools.push(registered_name.to_string());
314                     cov_mark::hit!(register_tool);
315                 }
316             }
317
318             ModCollector {
319                 def_collector: &mut *self,
320                 macro_depth: 0,
321                 module_id,
322                 tree_id: TreeId::new(file_id.into(), None),
323                 item_tree: &item_tree,
324                 mod_dir: ModDir::root(),
325             }
326             .collect(item_tree.top_level_items());
327         }
328     }
329
330     fn seed_with_inner(&mut self, block: AstId<ast::BlockExpr>) {
331         let item_tree = self.db.file_item_tree(block.file_id);
332         let module_id = self.def_map.root;
333         if item_tree
334             .top_level_attrs(self.db, self.def_map.krate)
335             .cfg()
336             .map_or(true, |cfg| self.cfg_options.check(&cfg) != Some(false))
337         {
338             ModCollector {
339                 def_collector: &mut *self,
340                 macro_depth: 0,
341                 module_id,
342                 // FIXME: populate block once we have per-block ItemTrees
343                 tree_id: TreeId::new(block.file_id, None),
344                 item_tree: &item_tree,
345                 mod_dir: ModDir::root(),
346             }
347             .collect(item_tree.inner_items_of_block(block.value));
348         }
349     }
350
351     fn resolution_loop(&mut self) {
352         let _p = profile::span("DefCollector::resolution_loop");
353
354         // main name resolution fixed-point loop.
355         let mut i = 0;
356         'outer: loop {
357             loop {
358                 self.db.unwind_if_cancelled();
359                 {
360                     let _p = profile::span("resolve_imports loop");
361                     loop {
362                         if self.resolve_imports() == ReachedFixedPoint::Yes {
363                             break;
364                         }
365                     }
366                 }
367                 if self.resolve_macros() == ReachedFixedPoint::Yes {
368                     break;
369                 }
370
371                 i += 1;
372                 if FIXED_POINT_LIMIT.check(i).is_err() {
373                     tracing::error!("name resolution is stuck");
374                     break 'outer;
375                 }
376             }
377
378             if self.reseed_with_unresolved_attribute() == ReachedFixedPoint::Yes {
379                 break;
380             }
381         }
382     }
383
384     fn collect(&mut self) {
385         let _p = profile::span("DefCollector::collect");
386
387         self.resolution_loop();
388
389         // Resolve all indeterminate resolved imports again
390         // As some of the macros will expand newly import shadowing partial resolved imports
391         // FIXME: We maybe could skip this, if we handle the indeterminate imports in `resolve_imports`
392         // correctly
393         let partial_resolved = self.resolved_imports.iter().filter_map(|directive| {
394             if let PartialResolvedImport::Indeterminate(_) = directive.status {
395                 let mut directive = directive.clone();
396                 directive.status = PartialResolvedImport::Unresolved;
397                 Some(directive)
398             } else {
399                 None
400             }
401         });
402         self.unresolved_imports.extend(partial_resolved);
403         self.resolve_imports();
404
405         let unresolved_imports = std::mem::take(&mut self.unresolved_imports);
406         // show unresolved imports in completion, etc
407         for directive in &unresolved_imports {
408             self.record_resolved_import(directive)
409         }
410         self.unresolved_imports = unresolved_imports;
411
412         // FIXME: This condition should instead check if this is a `proc-macro` type crate.
413         if self.exports_proc_macros {
414             // A crate exporting procedural macros is not allowed to export anything else.
415             //
416             // Additionally, while the proc macro entry points must be `pub`, they are not publicly
417             // exported in type/value namespace. This function reduces the visibility of all items
418             // in the crate root that aren't proc macros.
419             let root = self.def_map.root;
420             let module_id = self.def_map.module_id(root);
421             let root = &mut self.def_map.modules[root];
422             root.scope.censor_non_proc_macros(module_id);
423         }
424     }
425
426     /// When the fixed-point loop reaches a stable state, we might still have some unresolved
427     /// attributes (or unexpanded attribute proc macros) left over. This takes one of them, and
428     /// feeds the item it's applied to back into name resolution.
429     ///
430     /// This effectively ignores the fact that the macro is there and just treats the items as
431     /// normal code.
432     ///
433     /// This improves UX when proc macros are turned off or don't work, and replicates the behavior
434     /// before we supported proc. attribute macros.
435     fn reseed_with_unresolved_attribute(&mut self) -> ReachedFixedPoint {
436         cov_mark::hit!(unresolved_attribute_fallback);
437
438         let mut unresolved_macros = std::mem::take(&mut self.unresolved_macros);
439         let pos = unresolved_macros.iter().position(|directive| {
440             if let MacroDirectiveKind::Attr { ast_id, mod_item, attr } = &directive.kind {
441                 self.skip_attrs.insert(ast_id.ast_id.with_value(*mod_item), attr.id);
442
443                 let file_id = ast_id.ast_id.file_id;
444                 let item_tree = self.db.file_item_tree(file_id);
445                 let mod_dir = self.mod_dirs[&directive.module_id].clone();
446                 ModCollector {
447                     def_collector: &mut *self,
448                     macro_depth: directive.depth,
449                     module_id: directive.module_id,
450                     tree_id: TreeId::new(file_id, None),
451                     item_tree: &item_tree,
452                     mod_dir,
453                 }
454                 .collect(&[*mod_item]);
455                 true
456             } else {
457                 false
458             }
459         });
460
461         if let Some(pos) = pos {
462             unresolved_macros.remove(pos);
463         }
464
465         // The collection above might add new unresolved macros (eg. derives), so merge the lists.
466         self.unresolved_macros.extend(unresolved_macros);
467
468         if pos.is_some() {
469             // Continue name resolution with the new data.
470             ReachedFixedPoint::No
471         } else {
472             ReachedFixedPoint::Yes
473         }
474     }
475
476     fn inject_prelude(&mut self, crate_attrs: &Attrs) {
477         // See compiler/rustc_builtin_macros/src/standard_library_imports.rs
478
479         if crate_attrs.by_key("no_core").exists() {
480             // libcore does not get a prelude.
481             return;
482         }
483
484         let krate = if crate_attrs.by_key("no_std").exists() {
485             name![core]
486         } else {
487             let std = name![std];
488             if self.def_map.extern_prelude().any(|(name, _)| *name == std) {
489                 std
490             } else {
491                 // If `std` does not exist for some reason, fall back to core. This mostly helps
492                 // keep r-a's own tests minimal.
493                 name![core]
494             }
495         };
496
497         let edition = match self.def_map.edition {
498             Edition::Edition2015 => name![rust_2015],
499             Edition::Edition2018 => name![rust_2018],
500             Edition::Edition2021 => name![rust_2021],
501         };
502
503         let path_kind = if self.def_map.edition == Edition::Edition2015 {
504             PathKind::Plain
505         } else {
506             PathKind::Abs
507         };
508         let path = ModPath::from_segments(
509             path_kind.clone(),
510             [krate.clone(), name![prelude], edition].into_iter(),
511         );
512         // Fall back to the older `std::prelude::v1` for compatibility with Rust <1.52.0
513         // FIXME remove this fallback
514         let fallback_path =
515             ModPath::from_segments(path_kind, [krate, name![prelude], name![v1]].into_iter());
516
517         for path in &[path, fallback_path] {
518             let (per_ns, _) = self.def_map.resolve_path(
519                 self.db,
520                 self.def_map.root,
521                 path,
522                 BuiltinShadowMode::Other,
523             );
524
525             match &per_ns.types {
526                 Some((ModuleDefId::ModuleId(m), _)) => {
527                     self.def_map.prelude = Some(*m);
528                     return;
529                 }
530                 _ => {
531                     tracing::debug!(
532                         "could not resolve prelude path `{}` to module (resolved to {:?})",
533                         path,
534                         per_ns.types
535                     );
536                 }
537             }
538         }
539     }
540
541     /// Adds a definition of procedural macro `name` to the root module.
542     ///
543     /// # Notes on procedural macro resolution
544     ///
545     /// Procedural macro functionality is provided by the build system: It has to build the proc
546     /// macro and pass the resulting dynamic library to rust-analyzer.
547     ///
548     /// When procedural macro support is enabled, the list of proc macros exported by a crate is
549     /// known before we resolve names in the crate. This list is stored in `self.proc_macros` and is
550     /// derived from the dynamic library.
551     ///
552     /// However, we *also* would like to be able to at least *resolve* macros on our own, without
553     /// help by the build system. So, when the macro isn't found in `self.proc_macros`, we instead
554     /// use a dummy expander that always errors. This comes with the drawback of macros potentially
555     /// going out of sync with what the build system sees (since we resolve using VFS state, but
556     /// Cargo builds only on-disk files). We could and probably should add diagnostics for that.
557     fn export_proc_macro(&mut self, def: ProcMacroDef, ast_id: AstId<ast::Fn>) {
558         let kind = def.kind.to_basedb_kind();
559         self.exports_proc_macros = true;
560         let macro_def = match self.proc_macros.iter().find(|(n, _)| n == &def.name) {
561             Some((_, expander)) => MacroDefId {
562                 krate: self.def_map.krate,
563                 kind: MacroDefKind::ProcMacro(*expander, kind, ast_id),
564                 local_inner: false,
565             },
566             None => MacroDefId {
567                 krate: self.def_map.krate,
568                 kind: MacroDefKind::ProcMacro(
569                     ProcMacroExpander::dummy(self.def_map.krate),
570                     kind,
571                     ast_id,
572                 ),
573                 local_inner: false,
574             },
575         };
576
577         self.define_proc_macro(def.name.clone(), macro_def);
578         self.def_map.exported_proc_macros.insert(macro_def, def);
579     }
580
581     /// Define a macro with `macro_rules`.
582     ///
583     /// It will define the macro in legacy textual scope, and if it has `#[macro_export]`,
584     /// then it is also defined in the root module scope.
585     /// You can `use` or invoke it by `crate::macro_name` anywhere, before or after the definition.
586     ///
587     /// It is surprising that the macro will never be in the current module scope.
588     /// These code fails with "unresolved import/macro",
589     /// ```rust,compile_fail
590     /// mod m { macro_rules! foo { () => {} } }
591     /// use m::foo as bar;
592     /// ```
593     ///
594     /// ```rust,compile_fail
595     /// macro_rules! foo { () => {} }
596     /// self::foo!();
597     /// crate::foo!();
598     /// ```
599     ///
600     /// Well, this code compiles, because the plain path `foo` in `use` is searched
601     /// in the legacy textual scope only.
602     /// ```rust
603     /// macro_rules! foo { () => {} }
604     /// use foo as bar;
605     /// ```
606     fn define_macro_rules(
607         &mut self,
608         module_id: LocalModuleId,
609         name: Name,
610         macro_: MacroDefId,
611         export: bool,
612     ) {
613         // Textual scoping
614         self.define_legacy_macro(module_id, name.clone(), macro_);
615
616         // Module scoping
617         // In Rust, `#[macro_export]` macros are unconditionally visible at the
618         // crate root, even if the parent modules is **not** visible.
619         if export {
620             self.update(
621                 self.def_map.root,
622                 &[(Some(name), PerNs::macros(macro_, Visibility::Public))],
623                 Visibility::Public,
624                 ImportType::Named,
625             );
626         }
627     }
628
629     /// Define a legacy textual scoped macro in module
630     ///
631     /// We use a map `legacy_macros` to store all legacy textual scoped macros visible per module.
632     /// It will clone all macros from parent legacy scope, whose definition is prior to
633     /// the definition of current module.
634     /// And also, `macro_use` on a module will import all legacy macros visible inside to
635     /// current legacy scope, with possible shadowing.
636     fn define_legacy_macro(&mut self, module_id: LocalModuleId, name: Name, mac: MacroDefId) {
637         // Always shadowing
638         self.def_map.modules[module_id].scope.define_legacy_macro(name, mac);
639     }
640
641     /// Define a macro 2.0 macro
642     ///
643     /// The scoped of macro 2.0 macro is equal to normal function
644     fn define_macro_def(
645         &mut self,
646         module_id: LocalModuleId,
647         name: Name,
648         macro_: MacroDefId,
649         vis: &RawVisibility,
650     ) {
651         let vis =
652             self.def_map.resolve_visibility(self.db, module_id, vis).unwrap_or(Visibility::Public);
653         self.update(module_id, &[(Some(name), PerNs::macros(macro_, vis))], vis, ImportType::Named);
654     }
655
656     /// Define a proc macro
657     ///
658     /// A proc macro is similar to normal macro scope, but it would not visible in legacy textual scoped.
659     /// And unconditionally exported.
660     fn define_proc_macro(&mut self, name: Name, macro_: MacroDefId) {
661         self.update(
662             self.def_map.root,
663             &[(Some(name), PerNs::macros(macro_, Visibility::Public))],
664             Visibility::Public,
665             ImportType::Named,
666         );
667     }
668
669     /// Import macros from `#[macro_use] extern crate`.
670     fn import_macros_from_extern_crate(
671         &mut self,
672         current_module_id: LocalModuleId,
673         extern_crate: &item_tree::ExternCrate,
674     ) {
675         tracing::debug!(
676             "importing macros from extern crate: {:?} ({:?})",
677             extern_crate,
678             self.def_map.edition,
679         );
680
681         let res = self.resolve_extern_crate(&extern_crate.name);
682
683         if let Some(ModuleDefId::ModuleId(m)) = res.take_types() {
684             if m == self.def_map.module_id(current_module_id) {
685                 cov_mark::hit!(ignore_macro_use_extern_crate_self);
686                 return;
687             }
688
689             cov_mark::hit!(macro_rules_from_other_crates_are_visible_with_macro_use);
690             self.import_all_macros_exported(current_module_id, m.krate);
691         }
692     }
693
694     /// Import all exported macros from another crate
695     ///
696     /// Exported macros are just all macros in the root module scope.
697     /// Note that it contains not only all `#[macro_export]` macros, but also all aliases
698     /// created by `use` in the root module, ignoring the visibility of `use`.
699     fn import_all_macros_exported(&mut self, current_module_id: LocalModuleId, krate: CrateId) {
700         let def_map = self.db.crate_def_map(krate);
701         for (name, def) in def_map[def_map.root].scope.macros() {
702             // `macro_use` only bring things into legacy scope.
703             self.define_legacy_macro(current_module_id, name.clone(), def);
704         }
705     }
706
707     /// Tries to resolve every currently unresolved import.
708     fn resolve_imports(&mut self) -> ReachedFixedPoint {
709         let mut res = ReachedFixedPoint::Yes;
710         let imports = std::mem::take(&mut self.unresolved_imports);
711         let imports = imports
712             .into_iter()
713             .filter_map(|mut directive| {
714                 directive.status = self.resolve_import(directive.module_id, &directive.import);
715                 match directive.status {
716                     PartialResolvedImport::Indeterminate(_) => {
717                         self.record_resolved_import(&directive);
718                         // FIXME: For avoid performance regression,
719                         // we consider an imported resolved if it is indeterminate (i.e not all namespace resolved)
720                         self.resolved_imports.push(directive);
721                         res = ReachedFixedPoint::No;
722                         None
723                     }
724                     PartialResolvedImport::Resolved(_) => {
725                         self.record_resolved_import(&directive);
726                         self.resolved_imports.push(directive);
727                         res = ReachedFixedPoint::No;
728                         None
729                     }
730                     PartialResolvedImport::Unresolved => Some(directive),
731                 }
732             })
733             .collect();
734         self.unresolved_imports = imports;
735         res
736     }
737
738     fn resolve_import(&self, module_id: LocalModuleId, import: &Import) -> PartialResolvedImport {
739         let _p = profile::span("resolve_import").detail(|| format!("{}", import.path));
740         tracing::debug!("resolving import: {:?} ({:?})", import, self.def_map.edition);
741         if import.is_extern_crate {
742             let name = import
743                 .path
744                 .as_ident()
745                 .expect("extern crate should have been desugared to one-element path");
746
747             let res = self.resolve_extern_crate(name);
748
749             if res.is_none() {
750                 PartialResolvedImport::Unresolved
751             } else {
752                 PartialResolvedImport::Resolved(res)
753             }
754         } else {
755             let res = self.def_map.resolve_path_fp_with_macro(
756                 self.db,
757                 ResolveMode::Import,
758                 module_id,
759                 &import.path,
760                 BuiltinShadowMode::Module,
761             );
762
763             let def = res.resolved_def;
764             if res.reached_fixedpoint == ReachedFixedPoint::No || def.is_none() {
765                 return PartialResolvedImport::Unresolved;
766             }
767
768             if let Some(krate) = res.krate {
769                 if krate != self.def_map.krate {
770                     return PartialResolvedImport::Resolved(
771                         def.filter_visibility(|v| matches!(v, Visibility::Public)),
772                     );
773                 }
774             }
775
776             // Check whether all namespace is resolved
777             if def.take_types().is_some()
778                 && def.take_values().is_some()
779                 && def.take_macros().is_some()
780             {
781                 PartialResolvedImport::Resolved(def)
782             } else {
783                 PartialResolvedImport::Indeterminate(def)
784             }
785         }
786     }
787
788     fn resolve_extern_crate(&self, name: &Name) -> PerNs {
789         let arc;
790         let root = match self.def_map.block {
791             Some(_) => {
792                 arc = self.def_map.crate_root(self.db).def_map(self.db);
793                 &*arc
794             }
795             None => &self.def_map,
796         };
797
798         if name == &name!(self) {
799             cov_mark::hit!(extern_crate_self_as);
800             PerNs::types(root.module_id(root.root()).into(), Visibility::Public)
801         } else {
802             self.deps.get(name).map_or(PerNs::none(), |&it| PerNs::types(it, Visibility::Public))
803         }
804     }
805
806     fn record_resolved_import(&mut self, directive: &ImportDirective) {
807         let _p = profile::span("record_resolved_import");
808
809         let module_id = directive.module_id;
810         let import = &directive.import;
811         let mut def = directive.status.namespaces();
812         let vis = self
813             .def_map
814             .resolve_visibility(self.db, module_id, &directive.import.visibility)
815             .unwrap_or(Visibility::Public);
816
817         match import.kind {
818             ImportKind::Plain | ImportKind::TypeOnly => {
819                 let name = match &import.alias {
820                     Some(ImportAlias::Alias(name)) => Some(name.clone()),
821                     Some(ImportAlias::Underscore) => None,
822                     None => match import.path.segments().last() {
823                         Some(last_segment) => Some(last_segment.clone()),
824                         None => {
825                             cov_mark::hit!(bogus_paths);
826                             return;
827                         }
828                     },
829                 };
830
831                 if import.kind == ImportKind::TypeOnly {
832                     def.values = None;
833                     def.macros = None;
834                 }
835
836                 tracing::debug!("resolved import {:?} ({:?}) to {:?}", name, import, def);
837
838                 // extern crates in the crate root are special-cased to insert entries into the extern prelude: rust-lang/rust#54658
839                 if import.is_extern_crate && module_id == self.def_map.root {
840                     if let (Some(def), Some(name)) = (def.take_types(), name.as_ref()) {
841                         self.def_map.extern_prelude.insert(name.clone(), def);
842                     }
843                 }
844
845                 self.update(module_id, &[(name, def)], vis, ImportType::Named);
846             }
847             ImportKind::Glob => {
848                 tracing::debug!("glob import: {:?}", import);
849                 match def.take_types() {
850                     Some(ModuleDefId::ModuleId(m)) => {
851                         if import.is_prelude {
852                             // Note: This dodgily overrides the injected prelude. The rustc
853                             // implementation seems to work the same though.
854                             cov_mark::hit!(std_prelude);
855                             self.def_map.prelude = Some(m);
856                         } else if m.krate != self.def_map.krate {
857                             cov_mark::hit!(glob_across_crates);
858                             // glob import from other crate => we can just import everything once
859                             let item_map = m.def_map(self.db);
860                             let scope = &item_map[m.local_id].scope;
861
862                             // Module scoped macros is included
863                             let items = scope
864                                 .resolutions()
865                                 // only keep visible names...
866                                 .map(|(n, res)| {
867                                     (n, res.filter_visibility(|v| v.is_visible_from_other_crate()))
868                                 })
869                                 .filter(|(_, res)| !res.is_none())
870                                 .collect::<Vec<_>>();
871
872                             self.update(module_id, &items, vis, ImportType::Glob);
873                         } else {
874                             // glob import from same crate => we do an initial
875                             // import, and then need to propagate any further
876                             // additions
877                             let def_map;
878                             let scope = if m.block == self.def_map.block_id() {
879                                 &self.def_map[m.local_id].scope
880                             } else {
881                                 def_map = m.def_map(self.db);
882                                 &def_map[m.local_id].scope
883                             };
884
885                             // Module scoped macros is included
886                             let items = scope
887                                 .resolutions()
888                                 // only keep visible names...
889                                 .map(|(n, res)| {
890                                     (
891                                         n,
892                                         res.filter_visibility(|v| {
893                                             v.is_visible_from_def_map(
894                                                 self.db,
895                                                 &self.def_map,
896                                                 module_id,
897                                             )
898                                         }),
899                                     )
900                                 })
901                                 .filter(|(_, res)| !res.is_none())
902                                 .collect::<Vec<_>>();
903
904                             self.update(module_id, &items, vis, ImportType::Glob);
905                             // record the glob import in case we add further items
906                             let glob = self.glob_imports.entry(m.local_id).or_default();
907                             if !glob.iter().any(|(mid, _)| *mid == module_id) {
908                                 glob.push((module_id, vis));
909                             }
910                         }
911                     }
912                     Some(ModuleDefId::AdtId(AdtId::EnumId(e))) => {
913                         cov_mark::hit!(glob_enum);
914                         // glob import from enum => just import all the variants
915
916                         // XXX: urgh, so this works by accident! Here, we look at
917                         // the enum data, and, in theory, this might require us to
918                         // look back at the crate_def_map, creating a cycle. For
919                         // example, `enum E { crate::some_macro!(); }`. Luckily, the
920                         // only kind of macro that is allowed inside enum is a
921                         // `cfg_macro`, and we don't need to run name resolution for
922                         // it, but this is sheer luck!
923                         let enum_data = self.db.enum_data(e);
924                         let resolutions = enum_data
925                             .variants
926                             .iter()
927                             .map(|(local_id, variant_data)| {
928                                 let name = variant_data.name.clone();
929                                 let variant = EnumVariantId { parent: e, local_id };
930                                 let res = PerNs::both(variant.into(), variant.into(), vis);
931                                 (Some(name), res)
932                             })
933                             .collect::<Vec<_>>();
934                         self.update(module_id, &resolutions, vis, ImportType::Glob);
935                     }
936                     Some(d) => {
937                         tracing::debug!("glob import {:?} from non-module/enum {:?}", import, d);
938                     }
939                     None => {
940                         tracing::debug!("glob import {:?} didn't resolve as type", import);
941                     }
942                 }
943             }
944         }
945     }
946
947     fn update(
948         &mut self,
949         module_id: LocalModuleId,
950         resolutions: &[(Option<Name>, PerNs)],
951         vis: Visibility,
952         import_type: ImportType,
953     ) {
954         self.db.unwind_if_cancelled();
955         self.update_recursive(module_id, resolutions, vis, import_type, 0)
956     }
957
958     fn update_recursive(
959         &mut self,
960         module_id: LocalModuleId,
961         resolutions: &[(Option<Name>, PerNs)],
962         // All resolutions are imported with this visibility; the visibilities in
963         // the `PerNs` values are ignored and overwritten
964         vis: Visibility,
965         import_type: ImportType,
966         depth: usize,
967     ) {
968         if GLOB_RECURSION_LIMIT.check(depth).is_err() {
969             // prevent stack overflows (but this shouldn't be possible)
970             panic!("infinite recursion in glob imports!");
971         }
972         let mut changed = false;
973
974         for (name, res) in resolutions {
975             match name {
976                 Some(name) => {
977                     let scope = &mut self.def_map.modules[module_id].scope;
978                     changed |= scope.push_res_with_import(
979                         &mut self.from_glob_import,
980                         (module_id, name.clone()),
981                         res.with_visibility(vis),
982                         import_type,
983                     );
984                 }
985                 None => {
986                     let tr = match res.take_types() {
987                         Some(ModuleDefId::TraitId(tr)) => tr,
988                         Some(other) => {
989                             tracing::debug!("non-trait `_` import of {:?}", other);
990                             continue;
991                         }
992                         None => continue,
993                     };
994                     let old_vis = self.def_map.modules[module_id].scope.unnamed_trait_vis(tr);
995                     let should_update = match old_vis {
996                         None => true,
997                         Some(old_vis) => {
998                             let max_vis = old_vis.max(vis, &self.def_map).unwrap_or_else(|| {
999                                 panic!("`Tr as _` imports with unrelated visibilities {:?} and {:?} (trait {:?})", old_vis, vis, tr);
1000                             });
1001
1002                             if max_vis == old_vis {
1003                                 false
1004                             } else {
1005                                 cov_mark::hit!(upgrade_underscore_visibility);
1006                                 true
1007                             }
1008                         }
1009                     };
1010
1011                     if should_update {
1012                         changed = true;
1013                         self.def_map.modules[module_id].scope.push_unnamed_trait(tr, vis);
1014                     }
1015                 }
1016             }
1017         }
1018
1019         if !changed {
1020             return;
1021         }
1022         let glob_imports = self
1023             .glob_imports
1024             .get(&module_id)
1025             .into_iter()
1026             .flat_map(|v| v.iter())
1027             .filter(|(glob_importing_module, _)| {
1028                 // we know all resolutions have the same visibility (`vis`), so we
1029                 // just need to check that once
1030                 vis.is_visible_from_def_map(self.db, &self.def_map, *glob_importing_module)
1031             })
1032             .cloned()
1033             .collect::<Vec<_>>();
1034
1035         for (glob_importing_module, glob_import_vis) in glob_imports {
1036             self.update_recursive(
1037                 glob_importing_module,
1038                 resolutions,
1039                 glob_import_vis,
1040                 ImportType::Glob,
1041                 depth + 1,
1042             );
1043         }
1044     }
1045
1046     fn resolve_macros(&mut self) -> ReachedFixedPoint {
1047         let mut macros = std::mem::take(&mut self.unresolved_macros);
1048         let mut resolved = Vec::new();
1049         let mut res = ReachedFixedPoint::Yes;
1050         macros.retain(|directive| {
1051             let resolver = |path| {
1052                 let resolved_res = self.def_map.resolve_path_fp_with_macro(
1053                     self.db,
1054                     ResolveMode::Other,
1055                     directive.module_id,
1056                     &path,
1057                     BuiltinShadowMode::Module,
1058                 );
1059                 resolved_res.resolved_def.take_macros()
1060             };
1061
1062             match &directive.kind {
1063                 MacroDirectiveKind::FnLike { ast_id, expand_to } => {
1064                     match macro_call_as_call_id(
1065                         ast_id,
1066                         *expand_to,
1067                         self.db,
1068                         self.def_map.krate,
1069                         &resolver,
1070                         &mut |_err| (),
1071                     ) {
1072                         Ok(Ok(call_id)) => {
1073                             resolved.push((directive.module_id, call_id, directive.depth));
1074                             res = ReachedFixedPoint::No;
1075                             return false;
1076                         }
1077                         Err(UnresolvedMacro { .. }) | Ok(Err(_)) => {}
1078                     }
1079                 }
1080                 MacroDirectiveKind::Derive { ast_id, derive_attr } => {
1081                     match derive_macro_as_call_id(
1082                         ast_id,
1083                         *derive_attr,
1084                         self.db,
1085                         self.def_map.krate,
1086                         &resolver,
1087                     ) {
1088                         Ok(call_id) => {
1089                             self.def_map.modules[directive.module_id].scope.add_derive_macro_invoc(
1090                                 ast_id.ast_id,
1091                                 call_id,
1092                                 *derive_attr,
1093                             );
1094
1095                             resolved.push((directive.module_id, call_id, directive.depth));
1096                             res = ReachedFixedPoint::No;
1097                             return false;
1098                         }
1099                         Err(UnresolvedMacro { .. }) => (),
1100                     }
1101                 }
1102                 MacroDirectiveKind::Attr { ast_id, mod_item, attr } => {
1103                     if let Some(ident) = ast_id.path.as_ident() {
1104                         if let Some(helpers) = self.derive_helpers_in_scope.get(&ast_id.ast_id) {
1105                             if helpers.contains(ident) {
1106                                 cov_mark::hit!(resolved_derive_helper);
1107
1108                                 // Resolved to derive helper. Collect the item's attributes again,
1109                                 // starting after the derive helper.
1110                                 let file_id = ast_id.ast_id.file_id;
1111                                 let item_tree = self.db.file_item_tree(file_id);
1112                                 let mod_dir = self.mod_dirs[&directive.module_id].clone();
1113                                 self.skip_attrs.insert(InFile::new(file_id, *mod_item), attr.id);
1114                                 ModCollector {
1115                                     def_collector: &mut *self,
1116                                     macro_depth: directive.depth,
1117                                     module_id: directive.module_id,
1118                                     tree_id: TreeId::new(file_id, None),
1119                                     item_tree: &item_tree,
1120                                     mod_dir,
1121                                 }
1122                                 .collect(&[*mod_item]);
1123
1124                                 // Remove the original directive since we resolved it.
1125                                 res = ReachedFixedPoint::No;
1126                                 return false;
1127                             }
1128                         }
1129                     }
1130
1131                     if !self.db.enable_proc_attr_macros() {
1132                         return true;
1133                     }
1134
1135                     // Not resolved to a derive helper, so try to resolve as a macro.
1136                     match attr_macro_as_call_id(
1137                         ast_id,
1138                         attr,
1139                         self.db,
1140                         self.def_map.krate,
1141                         &resolver,
1142                     ) {
1143                         Ok(call_id) => {
1144                             let loc: MacroCallLoc = self.db.lookup_intern_macro_call(call_id);
1145
1146                             // Skip #[test]/#[bench] expansion, which would merely result in more memory usage
1147                             // due to duplicating functions into macro expansions
1148                             if is_builtin_test_or_bench_attr(loc.def) {
1149                                 let file_id = ast_id.ast_id.file_id;
1150                                 let item_tree = self.db.file_item_tree(file_id);
1151                                 let mod_dir = self.mod_dirs[&directive.module_id].clone();
1152                                 self.skip_attrs.insert(InFile::new(file_id, *mod_item), attr.id);
1153                                 ModCollector {
1154                                     def_collector: &mut *self,
1155                                     macro_depth: directive.depth,
1156                                     module_id: directive.module_id,
1157                                     tree_id: TreeId::new(file_id, None),
1158                                     item_tree: &item_tree,
1159                                     mod_dir,
1160                                 }
1161                                 .collect(&[*mod_item]);
1162
1163                                 // Remove the original directive since we resolved it.
1164                                 res = ReachedFixedPoint::No;
1165                                 return false;
1166                             }
1167
1168                             if let MacroDefKind::ProcMacro(exp, ..) = loc.def.kind {
1169                                 if exp.is_dummy() {
1170                                     // Proc macros that cannot be expanded are treated as not
1171                                     // resolved, in order to fall back later.
1172                                     self.def_map.diagnostics.push(
1173                                         DefDiagnostic::unresolved_proc_macro(
1174                                             directive.module_id,
1175                                             loc.kind,
1176                                         ),
1177                                     );
1178
1179                                     let file_id = ast_id.ast_id.file_id;
1180                                     let item_tree = self.db.file_item_tree(file_id);
1181                                     let mod_dir = self.mod_dirs[&directive.module_id].clone();
1182                                     self.skip_attrs
1183                                         .insert(InFile::new(file_id, *mod_item), attr.id);
1184                                     ModCollector {
1185                                         def_collector: &mut *self,
1186                                         macro_depth: directive.depth,
1187                                         module_id: directive.module_id,
1188                                         tree_id: TreeId::new(file_id, None),
1189                                         item_tree: &item_tree,
1190                                         mod_dir,
1191                                     }
1192                                     .collect(&[*mod_item]);
1193
1194                                     // Remove the macro directive.
1195                                     return false;
1196                                 }
1197                             }
1198
1199                             self.def_map.modules[directive.module_id]
1200                                 .scope
1201                                 .add_attr_macro_invoc(ast_id.ast_id, call_id);
1202
1203                             resolved.push((directive.module_id, call_id, directive.depth));
1204                             res = ReachedFixedPoint::No;
1205                             return false;
1206                         }
1207                         Err(UnresolvedMacro { .. }) => (),
1208                     }
1209                 }
1210             }
1211
1212             true
1213         });
1214         // Attribute resolution can add unresolved macro invocations, so concatenate the lists.
1215         self.unresolved_macros.extend(macros);
1216
1217         for (module_id, macro_call_id, depth) in resolved {
1218             self.collect_macro_expansion(module_id, macro_call_id, depth);
1219         }
1220
1221         res
1222     }
1223
1224     fn collect_macro_expansion(
1225         &mut self,
1226         module_id: LocalModuleId,
1227         macro_call_id: MacroCallId,
1228         depth: usize,
1229     ) {
1230         if EXPANSION_DEPTH_LIMIT.check(depth).is_err() {
1231             cov_mark::hit!(macro_expansion_overflow);
1232             tracing::warn!("macro expansion is too deep");
1233             return;
1234         }
1235         let file_id = macro_call_id.as_file();
1236
1237         // First, fetch the raw expansion result for purposes of error reporting. This goes through
1238         // `macro_expand_error` to avoid depending on the full expansion result (to improve
1239         // incrementality).
1240         let loc: MacroCallLoc = self.db.lookup_intern_macro_call(macro_call_id);
1241         let err = self.db.macro_expand_error(macro_call_id);
1242         if let Some(err) = err {
1243             let diag = match err {
1244                 hir_expand::ExpandError::UnresolvedProcMacro => {
1245                     // Missing proc macros are non-fatal, so they are handled specially.
1246                     DefDiagnostic::unresolved_proc_macro(module_id, loc.kind.clone())
1247                 }
1248                 _ => DefDiagnostic::macro_error(module_id, loc.kind.clone(), err.to_string()),
1249             };
1250
1251             self.def_map.diagnostics.push(diag);
1252         }
1253
1254         // If we've just resolved a derive, record its helper attributes.
1255         if let MacroCallKind::Derive { ast_id, .. } = &loc.kind {
1256             if loc.def.krate != self.def_map.krate {
1257                 let def_map = self.db.crate_def_map(loc.def.krate);
1258                 if let Some(def) = def_map.exported_proc_macros.get(&loc.def) {
1259                     if let ProcMacroKind::CustomDerive { helpers } = &def.kind {
1260                         self.derive_helpers_in_scope
1261                             .entry(*ast_id)
1262                             .or_default()
1263                             .extend(helpers.iter().cloned());
1264                     }
1265                 }
1266             }
1267         }
1268
1269         // Then, fetch and process the item tree. This will reuse the expansion result from above.
1270         let item_tree = self.db.file_item_tree(file_id);
1271         let mod_dir = self.mod_dirs[&module_id].clone();
1272         ModCollector {
1273             def_collector: &mut *self,
1274             macro_depth: depth,
1275             tree_id: TreeId::new(file_id, None),
1276             module_id,
1277             item_tree: &item_tree,
1278             mod_dir,
1279         }
1280         .collect(item_tree.top_level_items());
1281     }
1282
1283     fn finish(mut self) -> DefMap {
1284         // Emit diagnostics for all remaining unexpanded macros.
1285
1286         let _p = profile::span("DefCollector::finish");
1287
1288         for directive in &self.unresolved_macros {
1289             match &directive.kind {
1290                 MacroDirectiveKind::FnLike { ast_id, expand_to } => {
1291                     match macro_call_as_call_id(
1292                         ast_id,
1293                         *expand_to,
1294                         self.db,
1295                         self.def_map.krate,
1296                         |path| {
1297                             let resolved_res = self.def_map.resolve_path_fp_with_macro(
1298                                 self.db,
1299                                 ResolveMode::Other,
1300                                 directive.module_id,
1301                                 &path,
1302                                 BuiltinShadowMode::Module,
1303                             );
1304                             resolved_res.resolved_def.take_macros()
1305                         },
1306                         &mut |_| (),
1307                     ) {
1308                         Ok(_) => (),
1309                         Err(UnresolvedMacro { path }) => {
1310                             self.def_map.diagnostics.push(DefDiagnostic::unresolved_macro_call(
1311                                 directive.module_id,
1312                                 ast_id.ast_id,
1313                                 path,
1314                             ));
1315                         }
1316                     }
1317                 }
1318                 MacroDirectiveKind::Derive { .. } | MacroDirectiveKind::Attr { .. } => {
1319                     // FIXME: we might want to diagnose this too
1320                 }
1321             }
1322         }
1323
1324         // Emit diagnostics for all remaining unresolved imports.
1325
1326         // We'd like to avoid emitting a diagnostics avalanche when some `extern crate` doesn't
1327         // resolve. We first emit diagnostics for unresolved extern crates and collect the missing
1328         // crate names. Then we emit diagnostics for unresolved imports, but only if the import
1329         // doesn't start with an unresolved crate's name. Due to renaming and reexports, this is a
1330         // heuristic, but it works in practice.
1331         let mut diagnosed_extern_crates = FxHashSet::default();
1332         for directive in &self.unresolved_imports {
1333             if let ImportSource::ExternCrate(krate) = directive.import.source {
1334                 let item_tree = krate.item_tree(self.db);
1335                 let extern_crate = &item_tree[krate.value];
1336
1337                 diagnosed_extern_crates.insert(extern_crate.name.clone());
1338
1339                 self.def_map.diagnostics.push(DefDiagnostic::unresolved_extern_crate(
1340                     directive.module_id,
1341                     InFile::new(krate.file_id(), extern_crate.ast_id),
1342                 ));
1343             }
1344         }
1345
1346         for directive in &self.unresolved_imports {
1347             if let ImportSource::Import { id: import, use_tree } = &directive.import.source {
1348                 if let (Some(krate), PathKind::Plain | PathKind::Abs) =
1349                     (directive.import.path.segments().first(), &directive.import.path.kind)
1350                 {
1351                     if diagnosed_extern_crates.contains(krate) {
1352                         continue;
1353                     }
1354                 }
1355
1356                 self.def_map.diagnostics.push(DefDiagnostic::unresolved_import(
1357                     directive.module_id,
1358                     *import,
1359                     *use_tree,
1360                 ));
1361             }
1362         }
1363
1364         self.def_map
1365     }
1366 }
1367
1368 /// Walks a single module, populating defs, imports and macros
1369 struct ModCollector<'a, 'b> {
1370     def_collector: &'a mut DefCollector<'b>,
1371     macro_depth: usize,
1372     module_id: LocalModuleId,
1373     tree_id: TreeId,
1374     item_tree: &'a ItemTree,
1375     mod_dir: ModDir,
1376 }
1377
1378 impl ModCollector<'_, '_> {
1379     fn collect(&mut self, items: &[ModItem]) {
1380         let krate = self.def_collector.def_map.krate;
1381
1382         // Note: don't assert that inserted value is fresh: it's simply not true
1383         // for macros.
1384         self.def_collector.mod_dirs.insert(self.module_id, self.mod_dir.clone());
1385
1386         // Prelude module is always considered to be `#[macro_use]`.
1387         if let Some(prelude_module) = self.def_collector.def_map.prelude {
1388             if prelude_module.krate != krate {
1389                 cov_mark::hit!(prelude_is_macro_use);
1390                 self.def_collector.import_all_macros_exported(self.module_id, prelude_module.krate);
1391             }
1392         }
1393
1394         // This should be processed eagerly instead of deferred to resolving.
1395         // `#[macro_use] extern crate` is hoisted to imports macros before collecting
1396         // any other items.
1397         for item in items {
1398             let attrs = self.item_tree.attrs(self.def_collector.db, krate, (*item).into());
1399             if attrs.cfg().map_or(true, |cfg| self.is_cfg_enabled(&cfg)) {
1400                 if let ModItem::ExternCrate(id) = item {
1401                     let import = self.item_tree[*id].clone();
1402                     let attrs = self.item_tree.attrs(
1403                         self.def_collector.db,
1404                         krate,
1405                         ModItem::from(*id).into(),
1406                     );
1407                     if attrs.by_key("macro_use").exists() {
1408                         self.def_collector.import_macros_from_extern_crate(self.module_id, &import);
1409                     }
1410                 }
1411             }
1412         }
1413
1414         for &item in items {
1415             let attrs = self.item_tree.attrs(self.def_collector.db, krate, item.into());
1416             if let Some(cfg) = attrs.cfg() {
1417                 if !self.is_cfg_enabled(&cfg) {
1418                     self.emit_unconfigured_diagnostic(item, &cfg);
1419                     continue;
1420                 }
1421             }
1422
1423             if let Err(()) = self.resolve_attributes(&attrs, item) {
1424                 // Do not process the item. It has at least one non-builtin attribute, so the
1425                 // fixed-point algorithm is required to resolve the rest of them.
1426                 continue;
1427             }
1428
1429             let module = self.def_collector.def_map.module_id(self.module_id);
1430
1431             let mut def = None;
1432             match item {
1433                 ModItem::Mod(m) => self.collect_module(&self.item_tree[m], &attrs),
1434                 ModItem::Import(import_id) => {
1435                     let module_id = self.module_id;
1436                     let imports = Import::from_use(
1437                         self.def_collector.db,
1438                         krate,
1439                         self.item_tree,
1440                         ItemTreeId::new(self.tree_id, import_id),
1441                     );
1442                     self.def_collector.unresolved_imports.extend(imports.into_iter().map(
1443                         |import| ImportDirective {
1444                             module_id,
1445                             import,
1446                             status: PartialResolvedImport::Unresolved,
1447                         },
1448                     ));
1449                 }
1450                 ModItem::ExternCrate(import_id) => {
1451                     self.def_collector.unresolved_imports.push(ImportDirective {
1452                         module_id: self.module_id,
1453                         import: Import::from_extern_crate(
1454                             self.def_collector.db,
1455                             krate,
1456                             self.item_tree,
1457                             ItemTreeId::new(self.tree_id, import_id),
1458                         ),
1459                         status: PartialResolvedImport::Unresolved,
1460                     })
1461                 }
1462                 ModItem::ExternBlock(block) => self.collect(&self.item_tree[block].children),
1463                 ModItem::MacroCall(mac) => self.collect_macro_call(&self.item_tree[mac]),
1464                 ModItem::MacroRules(id) => self.collect_macro_rules(id),
1465                 ModItem::MacroDef(id) => self.collect_macro_def(id),
1466                 ModItem::Impl(imp) => {
1467                     let module = self.def_collector.def_map.module_id(self.module_id);
1468                     let impl_id =
1469                         ImplLoc { container: module, id: ItemTreeId::new(self.tree_id, imp) }
1470                             .intern(self.def_collector.db);
1471                     self.def_collector.def_map.modules[self.module_id].scope.define_impl(impl_id)
1472                 }
1473                 ModItem::Function(id) => {
1474                     let func = &self.item_tree[id];
1475
1476                     let ast_id = InFile::new(self.file_id(), func.ast_id);
1477                     self.collect_proc_macro_def(&func.name, ast_id, &attrs);
1478
1479                     def = Some(DefData {
1480                         id: FunctionLoc {
1481                             container: module.into(),
1482                             id: ItemTreeId::new(self.tree_id, id),
1483                         }
1484                         .intern(self.def_collector.db)
1485                         .into(),
1486                         name: &func.name,
1487                         visibility: &self.item_tree[func.visibility],
1488                         has_constructor: false,
1489                     });
1490                 }
1491                 ModItem::Struct(id) => {
1492                     let it = &self.item_tree[id];
1493
1494                     def = Some(DefData {
1495                         id: StructLoc { container: module, id: ItemTreeId::new(self.tree_id, id) }
1496                             .intern(self.def_collector.db)
1497                             .into(),
1498                         name: &it.name,
1499                         visibility: &self.item_tree[it.visibility],
1500                         has_constructor: !matches!(it.fields, Fields::Record(_)),
1501                     });
1502                 }
1503                 ModItem::Union(id) => {
1504                     let it = &self.item_tree[id];
1505
1506                     def = Some(DefData {
1507                         id: UnionLoc { container: module, id: ItemTreeId::new(self.tree_id, id) }
1508                             .intern(self.def_collector.db)
1509                             .into(),
1510                         name: &it.name,
1511                         visibility: &self.item_tree[it.visibility],
1512                         has_constructor: false,
1513                     });
1514                 }
1515                 ModItem::Enum(id) => {
1516                     let it = &self.item_tree[id];
1517
1518                     def = Some(DefData {
1519                         id: EnumLoc { container: module, id: ItemTreeId::new(self.tree_id, id) }
1520                             .intern(self.def_collector.db)
1521                             .into(),
1522                         name: &it.name,
1523                         visibility: &self.item_tree[it.visibility],
1524                         has_constructor: false,
1525                     });
1526                 }
1527                 ModItem::Const(id) => {
1528                     let it = &self.item_tree[id];
1529                     let const_id = ConstLoc {
1530                         container: module.into(),
1531                         id: ItemTreeId::new(self.tree_id, id),
1532                     }
1533                     .intern(self.def_collector.db);
1534
1535                     match &it.name {
1536                         Some(name) => {
1537                             def = Some(DefData {
1538                                 id: const_id.into(),
1539                                 name,
1540                                 visibility: &self.item_tree[it.visibility],
1541                                 has_constructor: false,
1542                             });
1543                         }
1544                         None => {
1545                             // const _: T = ...;
1546                             self.def_collector.def_map.modules[self.module_id]
1547                                 .scope
1548                                 .define_unnamed_const(const_id);
1549                         }
1550                     }
1551                 }
1552                 ModItem::Static(id) => {
1553                     let it = &self.item_tree[id];
1554
1555                     def = Some(DefData {
1556                         id: StaticLoc { container: module, id: ItemTreeId::new(self.tree_id, id) }
1557                             .intern(self.def_collector.db)
1558                             .into(),
1559                         name: &it.name,
1560                         visibility: &self.item_tree[it.visibility],
1561                         has_constructor: false,
1562                     });
1563                 }
1564                 ModItem::Trait(id) => {
1565                     let it = &self.item_tree[id];
1566
1567                     def = Some(DefData {
1568                         id: TraitLoc { container: module, id: ItemTreeId::new(self.tree_id, id) }
1569                             .intern(self.def_collector.db)
1570                             .into(),
1571                         name: &it.name,
1572                         visibility: &self.item_tree[it.visibility],
1573                         has_constructor: false,
1574                     });
1575                 }
1576                 ModItem::TypeAlias(id) => {
1577                     let it = &self.item_tree[id];
1578
1579                     def = Some(DefData {
1580                         id: TypeAliasLoc {
1581                             container: module.into(),
1582                             id: ItemTreeId::new(self.tree_id, id),
1583                         }
1584                         .intern(self.def_collector.db)
1585                         .into(),
1586                         name: &it.name,
1587                         visibility: &self.item_tree[it.visibility],
1588                         has_constructor: false,
1589                     });
1590                 }
1591             }
1592
1593             if let Some(DefData { id, name, visibility, has_constructor }) = def {
1594                 self.def_collector.def_map.modules[self.module_id].scope.declare(id);
1595                 let vis = self
1596                     .def_collector
1597                     .def_map
1598                     .resolve_visibility(self.def_collector.db, self.module_id, visibility)
1599                     .unwrap_or(Visibility::Public);
1600                 self.def_collector.update(
1601                     self.module_id,
1602                     &[(Some(name.clone()), PerNs::from_def(id, vis, has_constructor))],
1603                     vis,
1604                     ImportType::Named,
1605                 )
1606             }
1607         }
1608     }
1609
1610     fn collect_module(&mut self, module: &Mod, attrs: &Attrs) {
1611         let path_attr = attrs.by_key("path").string_value();
1612         let is_macro_use = attrs.by_key("macro_use").exists();
1613         match &module.kind {
1614             // inline module, just recurse
1615             ModKind::Inline { items } => {
1616                 let module_id = self.push_child_module(
1617                     module.name.clone(),
1618                     AstId::new(self.file_id(), module.ast_id),
1619                     None,
1620                     &self.item_tree[module.visibility],
1621                 );
1622
1623                 if let Some(mod_dir) = self.mod_dir.descend_into_definition(&module.name, path_attr)
1624                 {
1625                     ModCollector {
1626                         def_collector: &mut *self.def_collector,
1627                         macro_depth: self.macro_depth,
1628                         module_id,
1629                         tree_id: self.tree_id,
1630                         item_tree: self.item_tree,
1631                         mod_dir,
1632                     }
1633                     .collect(&*items);
1634                     if is_macro_use {
1635                         self.import_all_legacy_macros(module_id);
1636                     }
1637                 }
1638             }
1639             // out of line module, resolve, parse and recurse
1640             ModKind::Outline {} => {
1641                 let ast_id = AstId::new(self.tree_id.file_id(), module.ast_id);
1642                 let db = self.def_collector.db;
1643                 match self.mod_dir.resolve_declaration(db, self.file_id(), &module.name, path_attr)
1644                 {
1645                     Ok((file_id, is_mod_rs, mod_dir)) => {
1646                         let item_tree = db.file_item_tree(file_id.into());
1647                         let is_enabled = item_tree
1648                             .top_level_attrs(db, self.def_collector.def_map.krate)
1649                             .cfg()
1650                             .map_or(true, |cfg| {
1651                                 self.def_collector.cfg_options.check(&cfg) != Some(false)
1652                             });
1653                         if is_enabled {
1654                             let module_id = self.push_child_module(
1655                                 module.name.clone(),
1656                                 ast_id,
1657                                 Some((file_id, is_mod_rs)),
1658                                 &self.item_tree[module.visibility],
1659                             );
1660                             ModCollector {
1661                                 def_collector: &mut *self.def_collector,
1662                                 macro_depth: self.macro_depth,
1663                                 module_id,
1664                                 tree_id: TreeId::new(file_id.into(), None),
1665                                 item_tree: &item_tree,
1666                                 mod_dir,
1667                             }
1668                             .collect(item_tree.top_level_items());
1669                             if is_macro_use
1670                                 || item_tree
1671                                     .top_level_attrs(db, self.def_collector.def_map.krate)
1672                                     .by_key("macro_use")
1673                                     .exists()
1674                             {
1675                                 self.import_all_legacy_macros(module_id);
1676                             }
1677                         }
1678                     }
1679                     Err(candidate) => {
1680                         self.def_collector.def_map.diagnostics.push(
1681                             DefDiagnostic::unresolved_module(self.module_id, ast_id, candidate),
1682                         );
1683                     }
1684                 };
1685             }
1686         }
1687     }
1688
1689     fn push_child_module(
1690         &mut self,
1691         name: Name,
1692         declaration: AstId<ast::Module>,
1693         definition: Option<(FileId, bool)>,
1694         visibility: &crate::visibility::RawVisibility,
1695     ) -> LocalModuleId {
1696         let vis = self
1697             .def_collector
1698             .def_map
1699             .resolve_visibility(self.def_collector.db, self.module_id, visibility)
1700             .unwrap_or(Visibility::Public);
1701         let modules = &mut self.def_collector.def_map.modules;
1702         let origin = match definition {
1703             None => ModuleOrigin::Inline { definition: declaration },
1704             Some((definition, is_mod_rs)) => {
1705                 ModuleOrigin::File { declaration, definition, is_mod_rs }
1706             }
1707         };
1708         let res = modules.alloc(ModuleData::new(origin, vis));
1709         modules[res].parent = Some(self.module_id);
1710         for (name, mac) in modules[self.module_id].scope.collect_legacy_macros() {
1711             modules[res].scope.define_legacy_macro(name, mac)
1712         }
1713         modules[self.module_id].children.insert(name.clone(), res);
1714         let module = self.def_collector.def_map.module_id(res);
1715         let def: ModuleDefId = module.into();
1716         self.def_collector.def_map.modules[self.module_id].scope.declare(def);
1717         self.def_collector.update(
1718             self.module_id,
1719             &[(Some(name), PerNs::from_def(def, vis, false))],
1720             vis,
1721             ImportType::Named,
1722         );
1723         res
1724     }
1725
1726     /// Resolves attributes on an item.
1727     ///
1728     /// Returns `Err` when some attributes could not be resolved to builtins and have been
1729     /// registered as unresolved.
1730     ///
1731     /// If `ignore_up_to` is `Some`, attributes preceding and including that attribute will be
1732     /// assumed to be resolved already.
1733     fn resolve_attributes(&mut self, attrs: &Attrs, mod_item: ModItem) -> Result<(), ()> {
1734         let mut ignore_up_to =
1735             self.def_collector.skip_attrs.get(&InFile::new(self.file_id(), mod_item)).copied();
1736         let iter = attrs
1737             .iter()
1738             .dedup_by(|a, b| {
1739                 // FIXME: this should not be required, all attributes on an item should have a
1740                 // unique ID!
1741                 // Still, this occurs because `#[cfg_attr]` can "expand" to multiple attributes:
1742                 //     #[cfg_attr(not(off), unresolved, unresolved)]
1743                 //     struct S;
1744                 // We should come up with a different way to ID attributes.
1745                 a.id == b.id
1746             })
1747             .skip_while(|attr| match ignore_up_to {
1748                 Some(id) if attr.id == id => {
1749                     ignore_up_to = None;
1750                     true
1751                 }
1752                 Some(_) => true,
1753                 None => false,
1754             });
1755
1756         for attr in iter {
1757             if attr.path.as_ident() == Some(&hir_expand::name![derive]) {
1758                 self.collect_derive(attr, mod_item);
1759             } else if self.is_builtin_or_registered_attr(&attr.path) {
1760                 continue;
1761             } else {
1762                 tracing::debug!("non-builtin attribute {}", attr.path);
1763
1764                 let ast_id = AstIdWithPath::new(
1765                     self.file_id(),
1766                     mod_item.ast_id(self.item_tree),
1767                     attr.path.as_ref().clone(),
1768                 );
1769                 self.def_collector.unresolved_macros.push(MacroDirective {
1770                     module_id: self.module_id,
1771                     depth: self.macro_depth + 1,
1772                     kind: MacroDirectiveKind::Attr { ast_id, attr: attr.clone(), mod_item },
1773                 });
1774
1775                 return Err(());
1776             }
1777         }
1778
1779         Ok(())
1780     }
1781
1782     fn is_builtin_or_registered_attr(&self, path: &ModPath) -> bool {
1783         if path.kind == PathKind::Plain {
1784             if let Some(tool_module) = path.segments().first() {
1785                 let tool_module = tool_module.to_smol_str();
1786                 let is_tool = builtin_attr::TOOL_MODULES
1787                     .iter()
1788                     .copied()
1789                     .chain(self.def_collector.registered_tools.iter().map(AsRef::as_ref))
1790                     .any(|m| tool_module == *m);
1791                 if is_tool {
1792                     return true;
1793                 }
1794             }
1795
1796             if let Some(name) = path.as_ident() {
1797                 let name = name.to_smol_str();
1798                 let is_inert = builtin_attr::INERT_ATTRIBUTES
1799                     .iter()
1800                     .copied()
1801                     .chain(self.def_collector.registered_attrs.iter().map(AsRef::as_ref))
1802                     .any(|attr| name == *attr);
1803                 return is_inert;
1804             }
1805         }
1806
1807         false
1808     }
1809
1810     fn collect_derive(&mut self, attr: &Attr, mod_item: ModItem) {
1811         let ast_id: FileAstId<ast::Item> = match mod_item {
1812             ModItem::Struct(it) => self.item_tree[it].ast_id.upcast(),
1813             ModItem::Union(it) => self.item_tree[it].ast_id.upcast(),
1814             ModItem::Enum(it) => self.item_tree[it].ast_id.upcast(),
1815             _ => {
1816                 // Cannot use derive on this item.
1817                 // FIXME: diagnose
1818                 return;
1819             }
1820         };
1821
1822         match attr.parse_derive() {
1823             Some(derive_macros) => {
1824                 for path in derive_macros {
1825                     let ast_id = AstIdWithPath::new(self.file_id(), ast_id, path);
1826                     self.def_collector.unresolved_macros.push(MacroDirective {
1827                         module_id: self.module_id,
1828                         depth: self.macro_depth + 1,
1829                         kind: MacroDirectiveKind::Derive { ast_id, derive_attr: attr.id },
1830                     });
1831                 }
1832             }
1833             None => {
1834                 // FIXME: diagnose
1835                 tracing::debug!("malformed derive: {:?}", attr);
1836             }
1837         }
1838     }
1839
1840     /// If `attrs` registers a procedural macro, collects its definition.
1841     fn collect_proc_macro_def(&mut self, func_name: &Name, ast_id: AstId<ast::Fn>, attrs: &Attrs) {
1842         // FIXME: this should only be done in the root module of `proc-macro` crates, not everywhere
1843         if let Some(proc_macro) = attrs.parse_proc_macro_decl(func_name) {
1844             self.def_collector.export_proc_macro(proc_macro, ast_id);
1845         }
1846     }
1847
1848     fn collect_macro_rules(&mut self, id: FileItemTreeId<MacroRules>) {
1849         let krate = self.def_collector.def_map.krate;
1850         let mac = &self.item_tree[id];
1851         let attrs = self.item_tree.attrs(self.def_collector.db, krate, ModItem::from(id).into());
1852         let ast_id = InFile::new(self.file_id(), mac.ast_id.upcast());
1853
1854         let export_attr = attrs.by_key("macro_export");
1855
1856         let is_export = export_attr.exists();
1857         let is_local_inner = if is_export {
1858             export_attr.tt_values().map(|it| &it.token_trees).flatten().any(|it| match it {
1859                 tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => {
1860                     ident.text.contains("local_inner_macros")
1861                 }
1862                 _ => false,
1863             })
1864         } else {
1865             false
1866         };
1867
1868         // Case 1: builtin macros
1869         if attrs.by_key("rustc_builtin_macro").exists() {
1870             // `#[rustc_builtin_macro = "builtin_name"]` overrides the `macro_rules!` name.
1871             let name;
1872             let name = match attrs.by_key("rustc_builtin_macro").string_value() {
1873                 Some(it) => {
1874                     // FIXME: a hacky way to create a Name from string.
1875                     name = tt::Ident { text: it.clone(), id: tt::TokenId::unspecified() }.as_name();
1876                     &name
1877                 }
1878                 None => {
1879                     match attrs.by_key("rustc_builtin_macro").tt_values().next().and_then(|tt| {
1880                         match tt.token_trees.first() {
1881                             Some(tt::TokenTree::Leaf(tt::Leaf::Ident(name))) => Some(name),
1882                             _ => None,
1883                         }
1884                     }) {
1885                         Some(ident) => {
1886                             name = ident.as_name();
1887                             &name
1888                         }
1889                         None => &mac.name,
1890                     }
1891                 }
1892             };
1893             let krate = self.def_collector.def_map.krate;
1894             match find_builtin_macro(name, krate, ast_id) {
1895                 Some(macro_id) => {
1896                     self.def_collector.define_macro_rules(
1897                         self.module_id,
1898                         mac.name.clone(),
1899                         macro_id,
1900                         is_export,
1901                     );
1902                     return;
1903                 }
1904                 None => {
1905                     self.def_collector
1906                         .def_map
1907                         .diagnostics
1908                         .push(DefDiagnostic::unimplemented_builtin_macro(self.module_id, ast_id));
1909                 }
1910             }
1911         }
1912
1913         // Case 2: normal `macro_rules!` macro
1914         let macro_id = MacroDefId {
1915             krate: self.def_collector.def_map.krate,
1916             kind: MacroDefKind::Declarative(ast_id),
1917             local_inner: is_local_inner,
1918         };
1919         self.def_collector.define_macro_rules(
1920             self.module_id,
1921             mac.name.clone(),
1922             macro_id,
1923             is_export,
1924         );
1925     }
1926
1927     fn collect_macro_def(&mut self, id: FileItemTreeId<MacroDef>) {
1928         let krate = self.def_collector.def_map.krate;
1929         let mac = &self.item_tree[id];
1930         let ast_id = InFile::new(self.file_id(), mac.ast_id.upcast());
1931
1932         // Case 1: builtin macros
1933         let attrs = self.item_tree.attrs(self.def_collector.db, krate, ModItem::from(id).into());
1934         if attrs.by_key("rustc_builtin_macro").exists() {
1935             let macro_id = find_builtin_macro(&mac.name, krate, ast_id)
1936                 .or_else(|| find_builtin_derive(&mac.name, krate, ast_id))
1937                 .or_else(|| find_builtin_attr(&mac.name, krate, ast_id));
1938
1939             match macro_id {
1940                 Some(macro_id) => {
1941                     self.def_collector.define_macro_def(
1942                         self.module_id,
1943                         mac.name.clone(),
1944                         macro_id,
1945                         &self.item_tree[mac.visibility],
1946                     );
1947                     return;
1948                 }
1949                 None => {
1950                     self.def_collector
1951                         .def_map
1952                         .diagnostics
1953                         .push(DefDiagnostic::unimplemented_builtin_macro(self.module_id, ast_id));
1954                 }
1955             }
1956         }
1957
1958         // Case 2: normal `macro`
1959         let macro_id = MacroDefId {
1960             krate: self.def_collector.def_map.krate,
1961             kind: MacroDefKind::Declarative(ast_id),
1962             local_inner: false,
1963         };
1964
1965         self.def_collector.define_macro_def(
1966             self.module_id,
1967             mac.name.clone(),
1968             macro_id,
1969             &self.item_tree[mac.visibility],
1970         );
1971     }
1972
1973     fn collect_macro_call(&mut self, mac: &MacroCall) {
1974         let ast_id = AstIdWithPath::new(self.file_id(), mac.ast_id, (*mac.path).clone());
1975
1976         // Case 1: try to resolve in legacy scope and expand macro_rules
1977         let mut error = None;
1978         match macro_call_as_call_id(
1979             &ast_id,
1980             mac.expand_to,
1981             self.def_collector.db,
1982             self.def_collector.def_map.krate,
1983             |path| {
1984                 path.as_ident().and_then(|name| {
1985                     self.def_collector.def_map.with_ancestor_maps(
1986                         self.def_collector.db,
1987                         self.module_id,
1988                         &mut |map, module| map[module].scope.get_legacy_macro(name),
1989                     )
1990                 })
1991             },
1992             &mut |err| {
1993                 error.get_or_insert(err);
1994             },
1995         ) {
1996             Ok(Ok(macro_call_id)) => {
1997                 // Legacy macros need to be expanded immediately, so that any macros they produce
1998                 // are in scope.
1999                 self.def_collector.collect_macro_expansion(
2000                     self.module_id,
2001                     macro_call_id,
2002                     self.macro_depth + 1,
2003                 );
2004
2005                 if let Some(err) = error {
2006                     self.def_collector.def_map.diagnostics.push(DefDiagnostic::macro_error(
2007                         self.module_id,
2008                         MacroCallKind::FnLike { ast_id: ast_id.ast_id, expand_to: mac.expand_to },
2009                         err.to_string(),
2010                     ));
2011                 }
2012
2013                 return;
2014             }
2015             Ok(Err(_)) => {
2016                 // Built-in macro failed eager expansion.
2017
2018                 self.def_collector.def_map.diagnostics.push(DefDiagnostic::macro_error(
2019                     self.module_id,
2020                     MacroCallKind::FnLike { ast_id: ast_id.ast_id, expand_to: mac.expand_to },
2021                     error.unwrap().to_string(),
2022                 ));
2023                 return;
2024             }
2025             Err(UnresolvedMacro { .. }) => (),
2026         }
2027
2028         // Case 2: resolve in module scope, expand during name resolution.
2029         self.def_collector.unresolved_macros.push(MacroDirective {
2030             module_id: self.module_id,
2031             depth: self.macro_depth + 1,
2032             kind: MacroDirectiveKind::FnLike { ast_id, expand_to: mac.expand_to },
2033         });
2034     }
2035
2036     fn import_all_legacy_macros(&mut self, module_id: LocalModuleId) {
2037         let macros = self.def_collector.def_map[module_id].scope.collect_legacy_macros();
2038         for (name, macro_) in macros {
2039             self.def_collector.define_legacy_macro(self.module_id, name.clone(), macro_);
2040         }
2041     }
2042
2043     fn is_cfg_enabled(&self, cfg: &CfgExpr) -> bool {
2044         self.def_collector.cfg_options.check(cfg) != Some(false)
2045     }
2046
2047     fn emit_unconfigured_diagnostic(&mut self, item: ModItem, cfg: &CfgExpr) {
2048         let ast_id = item.ast_id(self.item_tree);
2049
2050         let ast_id = InFile::new(self.file_id(), ast_id);
2051         self.def_collector.def_map.diagnostics.push(DefDiagnostic::unconfigured_code(
2052             self.module_id,
2053             ast_id,
2054             cfg.clone(),
2055             self.def_collector.cfg_options.clone(),
2056         ));
2057     }
2058
2059     fn file_id(&self) -> HirFileId {
2060         self.tree_id.file_id()
2061     }
2062 }
2063
2064 #[cfg(test)]
2065 mod tests {
2066     use crate::{db::DefDatabase, test_db::TestDB};
2067     use base_db::{fixture::WithFixture, SourceDatabase};
2068
2069     use super::*;
2070
2071     fn do_collect_defs(db: &dyn DefDatabase, def_map: DefMap) -> DefMap {
2072         let mut collector = DefCollector {
2073             db,
2074             def_map,
2075             deps: FxHashMap::default(),
2076             glob_imports: FxHashMap::default(),
2077             unresolved_imports: Vec::new(),
2078             resolved_imports: Vec::new(),
2079             unresolved_macros: Vec::new(),
2080             mod_dirs: FxHashMap::default(),
2081             cfg_options: &CfgOptions::default(),
2082             proc_macros: Default::default(),
2083             exports_proc_macros: false,
2084             from_glob_import: Default::default(),
2085             skip_attrs: Default::default(),
2086             derive_helpers_in_scope: Default::default(),
2087             registered_attrs: Default::default(),
2088             registered_tools: Default::default(),
2089         };
2090         collector.seed_with_top_level();
2091         collector.collect();
2092         collector.def_map
2093     }
2094
2095     fn do_resolve(not_ra_fixture: &str) -> DefMap {
2096         let (db, file_id) = TestDB::with_single_file(not_ra_fixture);
2097         let krate = db.test_crate();
2098
2099         let edition = db.crate_graph()[krate].edition;
2100         let module_origin = ModuleOrigin::CrateRoot { definition: file_id };
2101         let def_map = DefMap::empty(krate, edition, module_origin);
2102         do_collect_defs(&db, def_map)
2103     }
2104
2105     #[test]
2106     fn test_macro_expand_will_stop_1() {
2107         do_resolve(
2108             r#"
2109 macro_rules! foo {
2110     ($($ty:ty)*) => { foo!($($ty)*); }
2111 }
2112 foo!(KABOOM);
2113 "#,
2114         );
2115         do_resolve(
2116             r#"
2117 macro_rules! foo {
2118     ($($ty:ty)*) => { foo!(() $($ty)*); }
2119 }
2120 foo!(KABOOM);
2121 "#,
2122         );
2123     }
2124
2125     #[ignore]
2126     #[test]
2127     fn test_macro_expand_will_stop_2() {
2128         // FIXME: this test does succeed, but takes quite a while: 90 seconds in
2129         // the release mode. That's why the argument is not an ra_fixture --
2130         // otherwise injection highlighting gets stuck.
2131         //
2132         // We need to find a way to fail this faster.
2133         do_resolve(
2134             r#"
2135 macro_rules! foo {
2136     ($($ty:ty)*) => { foo!($($ty)* $($ty)*); }
2137 }
2138 foo!(KABOOM);
2139 "#,
2140         );
2141     }
2142 }