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