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