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