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