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