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