1 //! The core of the module-level name resolution algorithm.
3 //! `DefCollector::collect` contains the fixed-point iteration loop which
4 //! resolves imports and expands macros.
8 use base_db::{CrateId, Edition, FileId, ProcMacroId};
9 use cfg::{CfgExpr, CfgOptions};
11 builtin_attr_macro::find_builtin_attr,
12 builtin_derive_macro::find_builtin_derive,
13 builtin_fn_macro::find_builtin_macro,
14 name::{name, AsName, Name},
15 proc_macro::ProcMacroExpander,
16 ExpandTo, HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
18 use hir_expand::{InFile, MacroCallLoc};
19 use itertools::Itertools;
22 use rustc_hash::{FxHashMap, FxHashSet};
23 use syntax::{ast, SmolStr};
26 attr::{Attr, AttrId, AttrInput, Attrs},
27 attr_macro_as_call_id, builtin_attr,
29 derive_macro_as_call_id,
31 item_scope::{ImportType, PerNsGlobImports},
33 self, Fields, FileItemTreeId, ImportKind, ItemTree, ItemTreeId, MacroCall, MacroDef,
34 MacroRules, Mod, ModItem, ModKind, TreeId,
36 macro_call_as_call_id,
38 diagnostics::DefDiagnostic,
39 mod_resolution::ModDir,
40 path_resolution::ReachedFixedPoint,
41 proc_macro::{ProcMacroDef, ProcMacroKind},
42 BuiltinShadowMode, DefMap, ModuleData, ModuleOrigin, ResolveMode,
44 path::{ImportAlias, ModPath, PathKind},
46 visibility::{RawVisibility, Visibility},
47 AdtId, AstId, AstIdWithPath, ConstLoc, EnumLoc, EnumVariantId, ExternBlockLoc, FunctionLoc,
48 ImplLoc, Intern, ItemContainerId, LocalModuleId, ModuleDefId, StaticLoc, StructLoc, TraitLoc,
49 TypeAliasLoc, UnionLoc, UnresolvedMacro,
52 static GLOB_RECURSION_LIMIT: Limit = Limit::new(100);
53 static EXPANSION_DEPTH_LIMIT: Limit = Limit::new(128);
54 static FIXED_POINT_LIMIT: Limit = Limit::new(8192);
56 pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: DefMap, tree_id: TreeId) -> DefMap {
57 let crate_graph = db.crate_graph();
59 let mut deps = FxHashMap::default();
60 // populate external prelude and dependency list
61 for dep in &crate_graph[def_map.krate].dependencies {
62 tracing::debug!("crate dep {:?} -> {:?}", dep.name, dep.crate_id);
63 let dep_def_map = db.crate_def_map(dep.crate_id);
64 let dep_root = dep_def_map.module_id(dep_def_map.root);
66 deps.insert(dep.as_name(), dep_root.into());
68 if dep.is_prelude() && !tree_id.is_block() {
69 def_map.extern_prelude.insert(dep.as_name(), dep_root.into());
73 let cfg_options = &crate_graph[def_map.krate].cfg_options;
74 let proc_macros = &crate_graph[def_map.krate].proc_macro;
75 let proc_macros = proc_macros
79 // FIXME: a hacky way to create a Name from string.
80 let name = tt::Ident { text: it.name.clone(), id: tt::TokenId::unspecified() };
81 (name.as_name(), ProcMacroExpander::new(def_map.krate, ProcMacroId(idx as u32)))
85 let mut collector = DefCollector {
89 glob_imports: FxHashMap::default(),
90 unresolved_imports: Vec::new(),
91 resolved_imports: Vec::new(),
92 unresolved_macros: Vec::new(),
93 mod_dirs: FxHashMap::default(),
96 exports_proc_macros: false,
97 from_glob_import: Default::default(),
98 skip_attrs: Default::default(),
99 derive_helpers_in_scope: Default::default(),
100 registered_attrs: Default::default(),
101 registered_tools: Default::default(),
103 if tree_id.is_block() {
104 collector.seed_with_inner(tree_id);
106 collector.seed_with_top_level();
109 let mut def_map = collector.finish();
110 def_map.shrink_to_fit();
114 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
115 enum PartialResolvedImport {
116 /// None of any namespaces is resolved
118 /// One of namespaces is resolved
119 Indeterminate(PerNs),
120 /// All namespaces are resolved, OR it comes from other crate
124 impl PartialResolvedImport {
125 fn namespaces(self) -> PerNs {
127 PartialResolvedImport::Unresolved => PerNs::none(),
128 PartialResolvedImport::Indeterminate(ns) | PartialResolvedImport::Resolved(ns) => ns,
133 #[derive(Clone, Debug, Eq, PartialEq)]
135 Import { id: ItemTreeId<item_tree::Import>, use_tree: Idx<ast::UseTree> },
136 ExternCrate(ItemTreeId<item_tree::ExternCrate>),
139 #[derive(Clone, Debug, Eq, PartialEq)]
141 path: Interned<ModPath>,
142 alias: Option<ImportAlias>,
143 visibility: RawVisibility,
146 is_extern_crate: bool,
148 source: ImportSource,
153 db: &dyn DefDatabase,
156 id: ItemTreeId<item_tree::Import>,
158 let it = &tree[id.value];
159 let attrs = &tree.attrs(db, krate, ModItem::from(id.value).into());
160 let visibility = &tree[it.visibility];
161 let is_prelude = attrs.by_key("prelude_import").exists();
163 let mut res = Vec::new();
164 it.use_tree.expand(|idx, path, kind, alias| {
166 path: Interned::new(path), // FIXME this makes little sense
168 visibility: visibility.clone(),
171 is_extern_crate: false,
173 source: ImportSource::Import { id, use_tree: idx },
179 fn from_extern_crate(
180 db: &dyn DefDatabase,
183 id: ItemTreeId<item_tree::ExternCrate>,
185 let it = &tree[id.value];
186 let attrs = &tree.attrs(db, krate, ModItem::from(id.value).into());
187 let visibility = &tree[it.visibility];
189 path: Interned::new(ModPath::from_segments(
191 iter::once(it.name.clone()),
193 alias: it.alias.clone(),
194 visibility: visibility.clone(),
195 kind: ImportKind::Plain,
197 is_extern_crate: true,
198 is_macro_use: attrs.by_key("macro_use").exists(),
199 source: ImportSource::ExternCrate(id),
204 #[derive(Clone, Debug, Eq, PartialEq)]
205 struct ImportDirective {
206 module_id: LocalModuleId,
208 status: PartialResolvedImport,
211 #[derive(Clone, Debug, Eq, PartialEq)]
212 struct MacroDirective {
213 module_id: LocalModuleId,
215 kind: MacroDirectiveKind,
216 container: ItemContainerId,
219 #[derive(Clone, Debug, Eq, PartialEq)]
220 enum MacroDirectiveKind {
221 FnLike { ast_id: AstIdWithPath<ast::MacroCall>, expand_to: ExpandTo },
222 Derive { ast_id: AstIdWithPath<ast::Item>, derive_attr: AttrId, derive_pos: usize },
223 Attr { ast_id: AstIdWithPath<ast::Item>, attr: Attr, mod_item: ModItem, tree: TreeId },
226 /// Walks the tree of module recursively
227 struct DefCollector<'a> {
228 db: &'a dyn DefDatabase,
230 deps: FxHashMap<Name, ModuleDefId>,
231 glob_imports: FxHashMap<LocalModuleId, Vec<(LocalModuleId, Visibility)>>,
232 unresolved_imports: Vec<ImportDirective>,
233 resolved_imports: Vec<ImportDirective>,
234 unresolved_macros: Vec<MacroDirective>,
235 mod_dirs: FxHashMap<LocalModuleId, ModDir>,
236 cfg_options: &'a CfgOptions,
237 /// List of procedural macros defined by this crate. This is read from the dynamic library
238 /// built by the build system, and is the list of proc. macros we can actually expand. It is
239 /// empty when proc. macro support is disabled (in which case we still do name resolution for
241 proc_macros: Vec<(Name, ProcMacroExpander)>,
242 exports_proc_macros: bool,
243 from_glob_import: PerNsGlobImports,
244 /// If we fail to resolve an attribute on a `ModItem`, we fall back to ignoring the attribute.
245 /// This map is used to skip all attributes up to and including the one that failed to resolve,
246 /// in order to not expand them twice.
248 /// This also stores the attributes to skip when we resolve derive helpers and non-macro
249 /// non-builtin attributes in general.
250 skip_attrs: FxHashMap<InFile<ModItem>, AttrId>,
251 /// Tracks which custom derives are in scope for an item, to allow resolution of derive helper
253 derive_helpers_in_scope: FxHashMap<AstId<ast::Item>, Vec<Name>>,
254 /// Custom attributes registered with `#![register_attr]`.
255 registered_attrs: Vec<SmolStr>,
256 /// Custom tool modules registered with `#![register_tool]`.
257 registered_tools: Vec<SmolStr>,
260 impl DefCollector<'_> {
261 fn seed_with_top_level(&mut self) {
262 let _p = profile::span("seed_with_top_level");
264 let file_id = self.db.crate_graph()[self.def_map.krate].root_file_id;
265 let item_tree = self.db.file_item_tree(file_id.into());
266 let module_id = self.def_map.root;
268 let attrs = item_tree.top_level_attrs(self.db, self.def_map.krate);
269 if attrs.cfg().map_or(true, |cfg| self.cfg_options.check(&cfg) != Some(false)) {
270 self.inject_prelude(&attrs);
272 // Process other crate-level attributes.
273 for attr in &*attrs {
274 let attr_name = match attr.path.as_ident() {
279 let attr_is_register_like = *attr_name == hir_expand::name![register_attr]
280 || *attr_name == hir_expand::name![register_tool];
281 if !attr_is_register_like {
285 let registered_name = match attr.input.as_deref() {
286 Some(AttrInput::TokenTree(subtree, _)) => match &*subtree.token_trees {
287 [tt::TokenTree::Leaf(tt::Leaf::Ident(name))] => name.as_name(),
293 if *attr_name == hir_expand::name![register_attr] {
294 self.registered_attrs.push(registered_name.to_smol_str());
295 cov_mark::hit!(register_attr);
297 self.registered_tools.push(registered_name.to_smol_str());
298 cov_mark::hit!(register_tool);
306 tree_id: TreeId::new(file_id.into(), None),
307 item_tree: &item_tree,
308 mod_dir: ModDir::root(),
310 .collect_in_top_module(item_tree.top_level_items());
314 fn seed_with_inner(&mut self, tree_id: TreeId) {
315 let item_tree = tree_id.item_tree(self.db);
316 let module_id = self.def_map.root;
318 let is_cfg_enabled = item_tree
319 .top_level_attrs(self.db, self.def_map.krate)
321 .map_or(true, |cfg| self.cfg_options.check(&cfg) != Some(false));
328 item_tree: &item_tree,
329 mod_dir: ModDir::root(),
331 .collect_in_top_module(item_tree.top_level_items());
335 fn resolution_loop(&mut self) {
336 let _p = profile::span("DefCollector::resolution_loop");
338 // main name resolution fixed-point loop.
342 self.db.unwind_if_cancelled();
344 let _p = profile::span("resolve_imports loop");
346 if self.resolve_imports() == ReachedFixedPoint::Yes {
351 if self.resolve_macros() == ReachedFixedPoint::Yes {
356 if FIXED_POINT_LIMIT.check(i).is_err() {
357 tracing::error!("name resolution is stuck");
362 if self.reseed_with_unresolved_attribute() == ReachedFixedPoint::Yes {
368 fn collect(&mut self) {
369 let _p = profile::span("DefCollector::collect");
371 self.resolution_loop();
373 // Resolve all indeterminate resolved imports again
374 // As some of the macros will expand newly import shadowing partial resolved imports
375 // FIXME: We maybe could skip this, if we handle the indeterminate imports in `resolve_imports`
377 let partial_resolved = self.resolved_imports.iter().filter_map(|directive| {
378 if let PartialResolvedImport::Indeterminate(_) = directive.status {
379 let mut directive = directive.clone();
380 directive.status = PartialResolvedImport::Unresolved;
386 self.unresolved_imports.extend(partial_resolved);
387 self.resolve_imports();
389 let unresolved_imports = std::mem::take(&mut self.unresolved_imports);
390 // show unresolved imports in completion, etc
391 for directive in &unresolved_imports {
392 self.record_resolved_import(directive)
394 self.unresolved_imports = unresolved_imports;
396 // FIXME: This condition should instead check if this is a `proc-macro` type crate.
397 if self.exports_proc_macros {
398 // A crate exporting procedural macros is not allowed to export anything else.
400 // Additionally, while the proc macro entry points must be `pub`, they are not publicly
401 // exported in type/value namespace. This function reduces the visibility of all items
402 // in the crate root that aren't proc macros.
403 let root = self.def_map.root;
404 let module_id = self.def_map.module_id(root);
405 let root = &mut self.def_map.modules[root];
406 root.scope.censor_non_proc_macros(module_id);
410 /// When the fixed-point loop reaches a stable state, we might still have some unresolved
411 /// attributes (or unexpanded attribute proc macros) left over. This takes one of them, and
412 /// feeds the item it's applied to back into name resolution.
414 /// This effectively ignores the fact that the macro is there and just treats the items as
417 /// This improves UX when proc macros are turned off or don't work, and replicates the behavior
418 /// before we supported proc. attribute macros.
419 fn reseed_with_unresolved_attribute(&mut self) -> ReachedFixedPoint {
420 cov_mark::hit!(unresolved_attribute_fallback);
422 let mut unresolved_macros = std::mem::take(&mut self.unresolved_macros);
423 let pos = unresolved_macros.iter().position(|directive| {
424 if let MacroDirectiveKind::Attr { ast_id, mod_item, attr, tree } = &directive.kind {
425 self.skip_attrs.insert(ast_id.ast_id.with_value(*mod_item), attr.id);
427 let item_tree = tree.item_tree(self.db);
428 let mod_dir = self.mod_dirs[&directive.module_id].clone();
431 macro_depth: directive.depth,
432 module_id: directive.module_id,
434 item_tree: &item_tree,
437 .collect(&[*mod_item], directive.container);
444 if let Some(pos) = pos {
445 unresolved_macros.remove(pos);
448 // The collection above might add new unresolved macros (eg. derives), so merge the lists.
449 self.unresolved_macros.extend(unresolved_macros);
452 // Continue name resolution with the new data.
453 ReachedFixedPoint::No
455 ReachedFixedPoint::Yes
459 fn inject_prelude(&mut self, crate_attrs: &Attrs) {
460 // See compiler/rustc_builtin_macros/src/standard_library_imports.rs
462 if crate_attrs.by_key("no_core").exists() {
463 // libcore does not get a prelude.
467 let krate = if crate_attrs.by_key("no_std").exists() {
470 let std = name![std];
471 if self.def_map.extern_prelude().any(|(name, _)| *name == std) {
474 // If `std` does not exist for some reason, fall back to core. This mostly helps
475 // keep r-a's own tests minimal.
480 let edition = match self.def_map.edition {
481 Edition::Edition2015 => name![rust_2015],
482 Edition::Edition2018 => name![rust_2018],
483 Edition::Edition2021 => name![rust_2021],
486 let path_kind = if self.def_map.edition == Edition::Edition2015 {
491 let path = ModPath::from_segments(
493 [krate.clone(), name![prelude], edition].into_iter(),
495 // Fall back to the older `std::prelude::v1` for compatibility with Rust <1.52.0
496 // FIXME remove this fallback
498 ModPath::from_segments(path_kind, [krate, name![prelude], name![v1]].into_iter());
500 for path in &[path, fallback_path] {
501 let (per_ns, _) = self.def_map.resolve_path(
505 BuiltinShadowMode::Other,
509 Some((ModuleDefId::ModuleId(m), _)) => {
510 self.def_map.prelude = Some(m);
515 "could not resolve prelude path `{}` to module (resolved to {:?})",
524 /// Adds a definition of procedural macro `name` to the root module.
526 /// # Notes on procedural macro resolution
528 /// Procedural macro functionality is provided by the build system: It has to build the proc
529 /// macro and pass the resulting dynamic library to rust-analyzer.
531 /// When procedural macro support is enabled, the list of proc macros exported by a crate is
532 /// known before we resolve names in the crate. This list is stored in `self.proc_macros` and is
533 /// derived from the dynamic library.
535 /// However, we *also* would like to be able to at least *resolve* macros on our own, without
536 /// help by the build system. So, when the macro isn't found in `self.proc_macros`, we instead
537 /// use a dummy expander that always errors. This comes with the drawback of macros potentially
538 /// going out of sync with what the build system sees (since we resolve using VFS state, but
539 /// Cargo builds only on-disk files). We could and probably should add diagnostics for that.
540 fn export_proc_macro(&mut self, def: ProcMacroDef, ast_id: AstId<ast::Fn>) {
541 let kind = def.kind.to_basedb_kind();
542 self.exports_proc_macros = true;
543 let macro_def = match self.proc_macros.iter().find(|(n, _)| n == &def.name) {
544 Some(&(_, expander)) => MacroDefId {
545 krate: self.def_map.krate,
546 kind: MacroDefKind::ProcMacro(expander, kind, ast_id),
550 krate: self.def_map.krate,
551 kind: MacroDefKind::ProcMacro(
552 ProcMacroExpander::dummy(self.def_map.krate),
560 self.define_proc_macro(def.name.clone(), macro_def);
561 self.def_map.exported_proc_macros.insert(macro_def, def);
564 /// Define a macro with `macro_rules`.
566 /// It will define the macro in legacy textual scope, and if it has `#[macro_export]`,
567 /// then it is also defined in the root module scope.
568 /// You can `use` or invoke it by `crate::macro_name` anywhere, before or after the definition.
570 /// It is surprising that the macro will never be in the current module scope.
571 /// These code fails with "unresolved import/macro",
572 /// ```rust,compile_fail
573 /// mod m { macro_rules! foo { () => {} } }
574 /// use m::foo as bar;
577 /// ```rust,compile_fail
578 /// macro_rules! foo { () => {} }
583 /// Well, this code compiles, because the plain path `foo` in `use` is searched
584 /// in the legacy textual scope only.
586 /// macro_rules! foo { () => {} }
589 fn define_macro_rules(
591 module_id: LocalModuleId,
597 self.define_legacy_macro(module_id, name.clone(), macro_);
598 self.def_map.modules[module_id].scope.declare_macro(macro_);
601 // In Rust, `#[macro_export]` macros are unconditionally visible at the
602 // crate root, even if the parent modules is **not** visible.
606 &[(Some(name), PerNs::macros(macro_, Visibility::Public))],
613 /// Define a legacy textual scoped macro in module
615 /// We use a map `legacy_macros` to store all legacy textual scoped macros visible per module.
616 /// It will clone all macros from parent legacy scope, whose definition is prior to
617 /// the definition of current module.
618 /// And also, `macro_use` on a module will import all legacy macros visible inside to
619 /// current legacy scope, with possible shadowing.
620 fn define_legacy_macro(&mut self, module_id: LocalModuleId, name: Name, mac: MacroDefId) {
622 self.def_map.modules[module_id].scope.define_legacy_macro(name, mac);
625 /// Define a macro 2.0 macro
627 /// The scoped of macro 2.0 macro is equal to normal function
630 module_id: LocalModuleId,
636 self.def_map.resolve_visibility(self.db, module_id, vis).unwrap_or(Visibility::Public);
637 self.def_map.modules[module_id].scope.declare_macro(macro_);
638 self.update(module_id, &[(Some(name), PerNs::macros(macro_, vis))], vis, ImportType::Named);
641 /// Define a proc macro
643 /// A proc macro is similar to normal macro scope, but it would not visible in legacy textual scoped.
644 /// And unconditionally exported.
645 fn define_proc_macro(&mut self, name: Name, macro_: MacroDefId) {
646 self.def_map.modules[self.def_map.root].scope.declare_macro(macro_);
649 &[(Some(name), PerNs::macros(macro_, Visibility::Public))],
655 /// Import macros from `#[macro_use] extern crate`.
656 fn import_macros_from_extern_crate(
658 current_module_id: LocalModuleId,
659 extern_crate: &item_tree::ExternCrate,
662 "importing macros from extern crate: {:?} ({:?})",
664 self.def_map.edition,
667 let res = self.resolve_extern_crate(&extern_crate.name);
669 if let Some(ModuleDefId::ModuleId(m)) = res.take_types() {
670 if m == self.def_map.module_id(current_module_id) {
671 cov_mark::hit!(ignore_macro_use_extern_crate_self);
675 cov_mark::hit!(macro_rules_from_other_crates_are_visible_with_macro_use);
676 self.import_all_macros_exported(current_module_id, m.krate);
680 /// Import all exported macros from another crate
682 /// Exported macros are just all macros in the root module scope.
683 /// Note that it contains not only all `#[macro_export]` macros, but also all aliases
684 /// created by `use` in the root module, ignoring the visibility of `use`.
685 fn import_all_macros_exported(&mut self, current_module_id: LocalModuleId, krate: CrateId) {
686 let def_map = self.db.crate_def_map(krate);
687 for (name, def) in def_map[def_map.root].scope.macros() {
688 // `macro_use` only bring things into legacy scope.
689 self.define_legacy_macro(current_module_id, name.clone(), def);
693 /// Tries to resolve every currently unresolved import.
694 fn resolve_imports(&mut self) -> ReachedFixedPoint {
695 let mut res = ReachedFixedPoint::Yes;
696 let imports = std::mem::take(&mut self.unresolved_imports);
697 let imports = imports
699 .filter_map(|mut directive| {
700 directive.status = self.resolve_import(directive.module_id, &directive.import);
701 match directive.status {
702 PartialResolvedImport::Indeterminate(_) => {
703 self.record_resolved_import(&directive);
704 // FIXME: For avoid performance regression,
705 // we consider an imported resolved if it is indeterminate (i.e not all namespace resolved)
706 self.resolved_imports.push(directive);
707 res = ReachedFixedPoint::No;
710 PartialResolvedImport::Resolved(_) => {
711 self.record_resolved_import(&directive);
712 self.resolved_imports.push(directive);
713 res = ReachedFixedPoint::No;
716 PartialResolvedImport::Unresolved => Some(directive),
720 self.unresolved_imports = imports;
724 fn resolve_import(&self, module_id: LocalModuleId, import: &Import) -> PartialResolvedImport {
725 let _p = profile::span("resolve_import").detail(|| format!("{}", import.path));
726 tracing::debug!("resolving import: {:?} ({:?})", import, self.def_map.edition);
727 if import.is_extern_crate {
731 .expect("extern crate should have been desugared to one-element path");
733 let res = self.resolve_extern_crate(name);
736 PartialResolvedImport::Unresolved
738 PartialResolvedImport::Resolved(res)
741 let res = self.def_map.resolve_path_fp_with_macro(
746 BuiltinShadowMode::Module,
749 let def = res.resolved_def;
750 if res.reached_fixedpoint == ReachedFixedPoint::No || def.is_none() {
751 return PartialResolvedImport::Unresolved;
754 if let Some(krate) = res.krate {
755 if krate != self.def_map.krate {
756 return PartialResolvedImport::Resolved(
757 def.filter_visibility(|v| matches!(v, Visibility::Public)),
762 // Check whether all namespace is resolved
763 if def.take_types().is_some()
764 && def.take_values().is_some()
765 && def.take_macros().is_some()
767 PartialResolvedImport::Resolved(def)
769 PartialResolvedImport::Indeterminate(def)
774 fn resolve_extern_crate(&self, name: &Name) -> PerNs {
775 if *name == name!(self) {
776 cov_mark::hit!(extern_crate_self_as);
777 let root = match self.def_map.block {
779 let def_map = self.def_map.crate_root(self.db).def_map(self.db);
780 def_map.module_id(def_map.root())
782 None => self.def_map.module_id(self.def_map.root()),
784 PerNs::types(root.into(), Visibility::Public)
786 self.deps.get(name).map_or(PerNs::none(), |&it| PerNs::types(it, Visibility::Public))
790 fn record_resolved_import(&mut self, directive: &ImportDirective) {
791 let _p = profile::span("record_resolved_import");
793 let module_id = directive.module_id;
794 let import = &directive.import;
795 let mut def = directive.status.namespaces();
798 .resolve_visibility(self.db, module_id, &directive.import.visibility)
799 .unwrap_or(Visibility::Public);
802 ImportKind::Plain | ImportKind::TypeOnly => {
803 let name = match &import.alias {
804 Some(ImportAlias::Alias(name)) => Some(name),
805 Some(ImportAlias::Underscore) => None,
806 None => match import.path.segments().last() {
807 Some(last_segment) => Some(last_segment),
809 cov_mark::hit!(bogus_paths);
815 if import.kind == ImportKind::TypeOnly {
820 tracing::debug!("resolved import {:?} ({:?}) to {:?}", name, import, def);
822 // extern crates in the crate root are special-cased to insert entries into the extern prelude: rust-lang/rust#54658
823 if import.is_extern_crate && module_id == self.def_map.root {
824 if let (Some(def), Some(name)) = (def.take_types(), name) {
825 self.def_map.extern_prelude.insert(name.clone(), def);
829 self.update(module_id, &[(name.cloned(), def)], vis, ImportType::Named);
831 ImportKind::Glob => {
832 tracing::debug!("glob import: {:?}", import);
833 match def.take_types() {
834 Some(ModuleDefId::ModuleId(m)) => {
835 if import.is_prelude {
836 // Note: This dodgily overrides the injected prelude. The rustc
837 // implementation seems to work the same though.
838 cov_mark::hit!(std_prelude);
839 self.def_map.prelude = Some(m);
840 } else if m.krate != self.def_map.krate {
841 cov_mark::hit!(glob_across_crates);
842 // glob import from other crate => we can just import everything once
843 let item_map = m.def_map(self.db);
844 let scope = &item_map[m.local_id].scope;
846 // Module scoped macros is included
849 // only keep visible names...
851 (n, res.filter_visibility(|v| v.is_visible_from_other_crate()))
853 .filter(|(_, res)| !res.is_none())
854 .collect::<Vec<_>>();
856 self.update(module_id, &items, vis, ImportType::Glob);
858 // glob import from same crate => we do an initial
859 // import, and then need to propagate any further
862 let scope = if m.block == self.def_map.block_id() {
863 &self.def_map[m.local_id].scope
865 def_map = m.def_map(self.db);
866 &def_map[m.local_id].scope
869 // Module scoped macros is included
872 // only keep visible names...
876 res.filter_visibility(|v| {
877 v.is_visible_from_def_map(
885 .filter(|(_, res)| !res.is_none())
886 .collect::<Vec<_>>();
888 self.update(module_id, &items, vis, ImportType::Glob);
889 // record the glob import in case we add further items
890 let glob = self.glob_imports.entry(m.local_id).or_default();
891 if !glob.iter().any(|(mid, _)| *mid == module_id) {
892 glob.push((module_id, vis));
896 Some(ModuleDefId::AdtId(AdtId::EnumId(e))) => {
897 cov_mark::hit!(glob_enum);
898 // glob import from enum => just import all the variants
900 // XXX: urgh, so this works by accident! Here, we look at
901 // the enum data, and, in theory, this might require us to
902 // look back at the crate_def_map, creating a cycle. For
903 // example, `enum E { crate::some_macro!(); }`. Luckily, the
904 // only kind of macro that is allowed inside enum is a
905 // `cfg_macro`, and we don't need to run name resolution for
906 // it, but this is sheer luck!
907 let enum_data = self.db.enum_data(e);
908 let resolutions = enum_data
911 .map(|(local_id, variant_data)| {
912 let name = variant_data.name.clone();
913 let variant = EnumVariantId { parent: e, local_id };
914 let res = PerNs::both(variant.into(), variant.into(), vis);
917 .collect::<Vec<_>>();
918 self.update(module_id, &resolutions, vis, ImportType::Glob);
921 tracing::debug!("glob import {:?} from non-module/enum {:?}", import, d);
924 tracing::debug!("glob import {:?} didn't resolve as type", import);
933 module_id: LocalModuleId,
934 resolutions: &[(Option<Name>, PerNs)],
936 import_type: ImportType,
938 self.db.unwind_if_cancelled();
939 self.update_recursive(module_id, resolutions, vis, import_type, 0)
944 module_id: LocalModuleId,
945 resolutions: &[(Option<Name>, PerNs)],
946 // All resolutions are imported with this visibility; the visibilities in
947 // the `PerNs` values are ignored and overwritten
949 import_type: ImportType,
952 if GLOB_RECURSION_LIMIT.check(depth).is_err() {
953 // prevent stack overflows (but this shouldn't be possible)
954 panic!("infinite recursion in glob imports!");
956 let mut changed = false;
958 for (name, res) in resolutions {
961 let scope = &mut self.def_map.modules[module_id].scope;
962 changed |= scope.push_res_with_import(
963 &mut self.from_glob_import,
964 (module_id, name.clone()),
965 res.with_visibility(vis),
970 let tr = match res.take_types() {
971 Some(ModuleDefId::TraitId(tr)) => tr,
973 tracing::debug!("non-trait `_` import of {:?}", other);
978 let old_vis = self.def_map.modules[module_id].scope.unnamed_trait_vis(tr);
979 let should_update = match old_vis {
982 let max_vis = old_vis.max(vis, &self.def_map).unwrap_or_else(|| {
983 panic!("`Tr as _` imports with unrelated visibilities {:?} and {:?} (trait {:?})", old_vis, vis, tr);
986 if max_vis == old_vis {
989 cov_mark::hit!(upgrade_underscore_visibility);
997 self.def_map.modules[module_id].scope.push_unnamed_trait(tr, vis);
1006 let glob_imports = self
1010 .flat_map(|v| v.iter())
1011 .filter(|(glob_importing_module, _)| {
1012 // we know all resolutions have the same visibility (`vis`), so we
1013 // just need to check that once
1014 vis.is_visible_from_def_map(self.db, &self.def_map, *glob_importing_module)
1017 .collect::<Vec<_>>();
1019 for (glob_importing_module, glob_import_vis) in glob_imports {
1020 self.update_recursive(
1021 glob_importing_module,
1030 fn resolve_macros(&mut self) -> ReachedFixedPoint {
1031 let mut macros = std::mem::take(&mut self.unresolved_macros);
1032 let mut resolved = Vec::new();
1033 let mut res = ReachedFixedPoint::Yes;
1034 macros.retain(|directive| {
1035 let resolver = |path| {
1036 let resolved_res = self.def_map.resolve_path_fp_with_macro(
1039 directive.module_id,
1041 BuiltinShadowMode::Module,
1043 resolved_res.resolved_def.take_macros()
1046 match &directive.kind {
1047 MacroDirectiveKind::FnLike { ast_id, expand_to } => {
1048 let call_id = macro_call_as_call_id(
1056 if let Ok(Ok(call_id)) = call_id {
1058 directive.module_id,
1061 directive.container,
1063 res = ReachedFixedPoint::No;
1067 MacroDirectiveKind::Derive { ast_id, derive_attr, derive_pos } => {
1068 let call_id = derive_macro_as_call_id(
1075 if let Ok(call_id) = call_id {
1076 self.def_map.modules[directive.module_id].scope.set_derive_macro_invoc(
1084 directive.module_id,
1087 directive.container,
1089 res = ReachedFixedPoint::No;
1093 MacroDirectiveKind::Attr { ast_id: file_ast_id, mod_item, attr, tree } => {
1094 let &AstIdWithPath { ast_id, ref path } = file_ast_id;
1095 let file_id = ast_id.file_id;
1097 let mut recollect_without = |collector: &mut Self| {
1098 // Remove the original directive since we resolved it.
1099 let mod_dir = collector.mod_dirs[&directive.module_id].clone();
1100 collector.skip_attrs.insert(InFile::new(file_id, *mod_item), attr.id);
1102 let item_tree = tree.item_tree(self.db);
1104 def_collector: collector,
1105 macro_depth: directive.depth,
1106 module_id: directive.module_id,
1108 item_tree: &item_tree,
1111 .collect(&[*mod_item], directive.container);
1112 res = ReachedFixedPoint::No;
1116 if let Some(ident) = path.as_ident() {
1117 if let Some(helpers) = self.derive_helpers_in_scope.get(&ast_id) {
1118 if helpers.contains(ident) {
1119 cov_mark::hit!(resolved_derive_helper);
1120 // Resolved to derive helper. Collect the item's attributes again,
1121 // starting after the derive helper.
1122 return recollect_without(self);
1127 let def = resolver(path.clone()).filter(MacroDefId::is_attribute);
1130 Some(MacroDefId { kind:MacroDefKind::BuiltInAttr(expander, _),.. })
1131 if expander.is_derive()
1133 // Resolved to `#[derive]`
1136 ModItem::Struct(_) | ModItem::Union(_) | ModItem::Enum(_) => (),
1138 let diag = DefDiagnostic::invalid_derive_target(
1139 directive.module_id,
1143 self.def_map.diagnostics.push(diag);
1144 return recollect_without(self);
1148 match attr.parse_derive() {
1149 Some(derive_macros) => {
1151 for (idx, path) in derive_macros.enumerate() {
1152 let ast_id = AstIdWithPath::new(file_id, ast_id.value, path);
1153 self.unresolved_macros.push(MacroDirective {
1154 module_id: directive.module_id,
1155 depth: directive.depth + 1,
1156 kind: MacroDirectiveKind::Derive {
1158 derive_attr: attr.id,
1161 container: directive.container,
1166 self.def_map.modules[directive.module_id]
1168 .init_derive_attribute(ast_id, attr.id, len + 1);
1171 let diag = DefDiagnostic::malformed_derive(
1172 directive.module_id,
1176 self.def_map.diagnostics.push(diag);
1180 return recollect_without(self);
1183 if !self.db.enable_proc_attr_macros() {
1187 // Not resolved to a derive helper or the derive attribute, so try to resolve as a normal attribute.
1188 match attr_macro_as_call_id(file_ast_id, attr, self.db, self.def_map.krate, def)
1191 let loc: MacroCallLoc = self.db.lookup_intern_macro_call(call_id);
1193 // Skip #[test]/#[bench] expansion, which would merely result in more memory usage
1194 // due to duplicating functions into macro expansions
1197 MacroDefKind::BuiltInAttr(expander, _)
1198 if expander.is_test() || expander.is_bench()
1200 return recollect_without(self);
1203 if let MacroDefKind::ProcMacro(exp, ..) = loc.def.kind {
1205 // Proc macros that cannot be expanded are treated as not
1206 // resolved, in order to fall back later.
1207 self.def_map.diagnostics.push(
1208 DefDiagnostic::unresolved_proc_macro(
1209 directive.module_id,
1214 return recollect_without(self);
1218 self.def_map.modules[directive.module_id]
1220 .add_attr_macro_invoc(ast_id, call_id);
1223 directive.module_id,
1226 directive.container,
1228 res = ReachedFixedPoint::No;
1231 Err(UnresolvedMacro { .. }) => (),
1238 // Attribute resolution can add unresolved macro invocations, so concatenate the lists.
1239 self.unresolved_macros.extend(macros);
1241 for (module_id, macro_call_id, depth, container) in resolved {
1242 self.collect_macro_expansion(module_id, macro_call_id, depth, container);
1248 fn collect_macro_expansion(
1250 module_id: LocalModuleId,
1251 macro_call_id: MacroCallId,
1253 container: ItemContainerId,
1255 if EXPANSION_DEPTH_LIMIT.check(depth).is_err() {
1256 cov_mark::hit!(macro_expansion_overflow);
1257 tracing::warn!("macro expansion is too deep");
1260 let file_id = macro_call_id.as_file();
1262 // First, fetch the raw expansion result for purposes of error reporting. This goes through
1263 // `macro_expand_error` to avoid depending on the full expansion result (to improve
1265 let loc: MacroCallLoc = self.db.lookup_intern_macro_call(macro_call_id);
1266 let err = self.db.macro_expand_error(macro_call_id);
1267 if let Some(err) = err {
1268 let diag = match err {
1269 hir_expand::ExpandError::UnresolvedProcMacro => {
1270 // Missing proc macros are non-fatal, so they are handled specially.
1271 DefDiagnostic::unresolved_proc_macro(module_id, loc.kind.clone())
1273 _ => DefDiagnostic::macro_error(module_id, loc.kind.clone(), err.to_string()),
1276 self.def_map.diagnostics.push(diag);
1279 // If we've just resolved a derive, record its helper attributes.
1280 if let MacroCallKind::Derive { ast_id, .. } = &loc.kind {
1281 if loc.def.krate != self.def_map.krate {
1282 let def_map = self.db.crate_def_map(loc.def.krate);
1283 if let Some(def) = def_map.exported_proc_macros.get(&loc.def) {
1284 if let ProcMacroKind::CustomDerive { helpers } = &def.kind {
1285 self.derive_helpers_in_scope
1288 .extend(helpers.iter().cloned());
1294 // Then, fetch and process the item tree. This will reuse the expansion result from above.
1295 let item_tree = self.db.file_item_tree(file_id);
1296 let mod_dir = self.mod_dirs[&module_id].clone();
1298 def_collector: &mut *self,
1300 tree_id: TreeId::new(file_id, None),
1302 item_tree: &item_tree,
1305 .collect(item_tree.top_level_items(), container);
1308 fn finish(mut self) -> DefMap {
1309 // Emit diagnostics for all remaining unexpanded macros.
1311 let _p = profile::span("DefCollector::finish");
1313 for directive in &self.unresolved_macros {
1314 match &directive.kind {
1315 MacroDirectiveKind::FnLike { ast_id, expand_to } => {
1316 let macro_call_as_call_id = macro_call_as_call_id(
1322 let resolved_res = self.def_map.resolve_path_fp_with_macro(
1325 directive.module_id,
1327 BuiltinShadowMode::Module,
1329 resolved_res.resolved_def.take_macros()
1333 if let Err(UnresolvedMacro { path }) = macro_call_as_call_id {
1334 self.def_map.diagnostics.push(DefDiagnostic::unresolved_macro_call(
1335 directive.module_id,
1341 MacroDirectiveKind::Derive { .. } | MacroDirectiveKind::Attr { .. } => {
1342 // FIXME: we might want to diagnose this too
1347 // Emit diagnostics for all remaining unresolved imports.
1349 // We'd like to avoid emitting a diagnostics avalanche when some `extern crate` doesn't
1350 // resolve. We first emit diagnostics for unresolved extern crates and collect the missing
1351 // crate names. Then we emit diagnostics for unresolved imports, but only if the import
1352 // doesn't start with an unresolved crate's name. Due to renaming and reexports, this is a
1353 // heuristic, but it works in practice.
1354 let mut diagnosed_extern_crates = FxHashSet::default();
1355 for directive in &self.unresolved_imports {
1356 if let ImportSource::ExternCrate(krate) = directive.import.source {
1357 let item_tree = krate.item_tree(self.db);
1358 let extern_crate = &item_tree[krate.value];
1360 diagnosed_extern_crates.insert(extern_crate.name.clone());
1362 self.def_map.diagnostics.push(DefDiagnostic::unresolved_extern_crate(
1363 directive.module_id,
1364 InFile::new(krate.file_id(), extern_crate.ast_id),
1369 for directive in &self.unresolved_imports {
1370 if let ImportSource::Import { id: import, use_tree } = directive.import.source {
1372 (directive.import.path.segments().first(), &directive.import.path.kind),
1373 (Some(krate), PathKind::Plain | PathKind::Abs) if diagnosed_extern_crates.contains(krate)
1378 self.def_map.diagnostics.push(DefDiagnostic::unresolved_import(
1379 directive.module_id,
1390 /// Walks a single module, populating defs, imports and macros
1391 struct ModCollector<'a, 'b> {
1392 def_collector: &'a mut DefCollector<'b>,
1394 module_id: LocalModuleId,
1396 item_tree: &'a ItemTree,
1400 impl ModCollector<'_, '_> {
1401 fn collect_in_top_module(&mut self, items: &[ModItem]) {
1402 let module = self.def_collector.def_map.module_id(self.module_id);
1403 self.collect(items, module.into())
1406 fn collect(&mut self, items: &[ModItem], container: ItemContainerId) {
1407 struct DefData<'a> {
1410 visibility: &'a RawVisibility,
1411 has_constructor: bool,
1414 let krate = self.def_collector.def_map.krate;
1416 // Note: don't assert that inserted value is fresh: it's simply not true
1418 self.def_collector.mod_dirs.insert(self.module_id, self.mod_dir.clone());
1420 // Prelude module is always considered to be `#[macro_use]`.
1421 if let Some(prelude_module) = self.def_collector.def_map.prelude {
1422 if prelude_module.krate != krate {
1423 cov_mark::hit!(prelude_is_macro_use);
1424 self.def_collector.import_all_macros_exported(self.module_id, prelude_module.krate);
1428 // This should be processed eagerly instead of deferred to resolving.
1429 // `#[macro_use] extern crate` is hoisted to imports macros before collecting
1431 for &item in items {
1432 let attrs = self.item_tree.attrs(self.def_collector.db, krate, item.into());
1433 if attrs.cfg().map_or(true, |cfg| self.is_cfg_enabled(&cfg)) {
1434 if let ModItem::ExternCrate(id) = item {
1435 let import = &self.item_tree[id];
1436 let attrs = self.item_tree.attrs(
1437 self.def_collector.db,
1439 ModItem::from(id).into(),
1441 if attrs.by_key("macro_use").exists() {
1442 self.def_collector.import_macros_from_extern_crate(self.module_id, import);
1448 for &item in items {
1449 let attrs = self.item_tree.attrs(self.def_collector.db, krate, item.into());
1450 if let Some(cfg) = attrs.cfg() {
1451 if !self.is_cfg_enabled(&cfg) {
1452 self.emit_unconfigured_diagnostic(item, &cfg);
1457 if let Err(()) = self.resolve_attributes(&attrs, item, container) {
1458 // Do not process the item. It has at least one non-builtin attribute, so the
1459 // fixed-point algorithm is required to resolve the rest of them.
1463 let module = self.def_collector.def_map.module_id(self.module_id);
1467 ModItem::Mod(m) => self.collect_module(&self.item_tree[m], &attrs),
1468 ModItem::Import(import_id) => {
1469 let module_id = self.module_id;
1470 let imports = Import::from_use(
1471 self.def_collector.db,
1474 ItemTreeId::new(self.tree_id, import_id),
1476 self.def_collector.unresolved_imports.extend(imports.into_iter().map(
1477 |import| ImportDirective {
1480 status: PartialResolvedImport::Unresolved,
1484 ModItem::ExternCrate(import_id) => {
1485 self.def_collector.unresolved_imports.push(ImportDirective {
1486 module_id: self.module_id,
1487 import: Import::from_extern_crate(
1488 self.def_collector.db,
1491 ItemTreeId::new(self.tree_id, import_id),
1493 status: PartialResolvedImport::Unresolved,
1496 ModItem::ExternBlock(block) => self.collect(
1497 &self.item_tree[block].children,
1498 ItemContainerId::ExternBlockId(
1501 id: ItemTreeId::new(self.tree_id, block),
1503 .intern(self.def_collector.db),
1506 ModItem::MacroCall(mac) => self.collect_macro_call(&self.item_tree[mac], container),
1507 ModItem::MacroRules(id) => self.collect_macro_rules(id),
1508 ModItem::MacroDef(id) => self.collect_macro_def(id),
1509 ModItem::Impl(imp) => {
1510 let module = self.def_collector.def_map.module_id(self.module_id);
1512 ImplLoc { container: module, id: ItemTreeId::new(self.tree_id, imp) }
1513 .intern(self.def_collector.db);
1514 self.def_collector.def_map.modules[self.module_id].scope.define_impl(impl_id)
1516 ModItem::Function(id) => {
1517 let func = &self.item_tree[id];
1519 let ast_id = InFile::new(self.file_id(), func.ast_id);
1520 self.collect_proc_macro_def(&func.name, ast_id, &attrs);
1522 def = Some(DefData {
1523 id: FunctionLoc { container, id: ItemTreeId::new(self.tree_id, id) }
1524 .intern(self.def_collector.db)
1527 visibility: &self.item_tree[func.visibility],
1528 has_constructor: false,
1531 ModItem::Struct(id) => {
1532 let it = &self.item_tree[id];
1534 def = Some(DefData {
1535 id: StructLoc { container: module, id: ItemTreeId::new(self.tree_id, id) }
1536 .intern(self.def_collector.db)
1539 visibility: &self.item_tree[it.visibility],
1540 has_constructor: !matches!(it.fields, Fields::Record(_)),
1543 ModItem::Union(id) => {
1544 let it = &self.item_tree[id];
1546 def = Some(DefData {
1547 id: UnionLoc { container: module, id: ItemTreeId::new(self.tree_id, id) }
1548 .intern(self.def_collector.db)
1551 visibility: &self.item_tree[it.visibility],
1552 has_constructor: false,
1555 ModItem::Enum(id) => {
1556 let it = &self.item_tree[id];
1558 def = Some(DefData {
1559 id: EnumLoc { container: module, id: ItemTreeId::new(self.tree_id, id) }
1560 .intern(self.def_collector.db)
1563 visibility: &self.item_tree[it.visibility],
1564 has_constructor: false,
1567 ModItem::Const(id) => {
1568 let it = &self.item_tree[id];
1569 let const_id = ConstLoc { container, id: ItemTreeId::new(self.tree_id, id) }
1570 .intern(self.def_collector.db);
1574 def = Some(DefData {
1575 id: const_id.into(),
1577 visibility: &self.item_tree[it.visibility],
1578 has_constructor: false,
1582 // const _: T = ...;
1583 self.def_collector.def_map.modules[self.module_id]
1585 .define_unnamed_const(const_id);
1589 ModItem::Static(id) => {
1590 let it = &self.item_tree[id];
1592 def = Some(DefData {
1593 id: StaticLoc { container, id: ItemTreeId::new(self.tree_id, id) }
1594 .intern(self.def_collector.db)
1597 visibility: &self.item_tree[it.visibility],
1598 has_constructor: false,
1601 ModItem::Trait(id) => {
1602 let it = &self.item_tree[id];
1604 def = Some(DefData {
1605 id: TraitLoc { container: module, id: ItemTreeId::new(self.tree_id, id) }
1606 .intern(self.def_collector.db)
1609 visibility: &self.item_tree[it.visibility],
1610 has_constructor: false,
1613 ModItem::TypeAlias(id) => {
1614 let it = &self.item_tree[id];
1616 def = Some(DefData {
1617 id: TypeAliasLoc { container, id: ItemTreeId::new(self.tree_id, id) }
1618 .intern(self.def_collector.db)
1621 visibility: &self.item_tree[it.visibility],
1622 has_constructor: false,
1627 if let Some(DefData { id, name, visibility, has_constructor }) = def {
1628 self.def_collector.def_map.modules[self.module_id].scope.declare(id);
1632 .resolve_visibility(self.def_collector.db, self.module_id, visibility)
1633 .unwrap_or(Visibility::Public);
1634 self.def_collector.update(
1636 &[(Some(name.clone()), PerNs::from_def(id, vis, has_constructor))],
1644 fn collect_module(&mut self, module: &Mod, attrs: &Attrs) {
1645 let path_attr = attrs.by_key("path").string_value();
1646 let is_macro_use = attrs.by_key("macro_use").exists();
1647 match &module.kind {
1648 // inline module, just recurse
1649 ModKind::Inline { items } => {
1650 let module_id = self.push_child_module(
1651 module.name.clone(),
1652 AstId::new(self.file_id(), module.ast_id),
1654 &self.item_tree[module.visibility],
1657 if let Some(mod_dir) = self.mod_dir.descend_into_definition(&module.name, path_attr)
1660 def_collector: &mut *self.def_collector,
1661 macro_depth: self.macro_depth,
1663 tree_id: self.tree_id,
1664 item_tree: self.item_tree,
1667 .collect_in_top_module(&*items);
1669 self.import_all_legacy_macros(module_id);
1673 // out of line module, resolve, parse and recurse
1674 ModKind::Outline {} => {
1675 let ast_id = AstId::new(self.tree_id.file_id(), module.ast_id);
1676 let db = self.def_collector.db;
1677 match self.mod_dir.resolve_declaration(db, self.file_id(), &module.name, path_attr)
1679 Ok((file_id, is_mod_rs, mod_dir)) => {
1680 let item_tree = db.file_item_tree(file_id.into());
1681 let is_enabled = item_tree
1682 .top_level_attrs(db, self.def_collector.def_map.krate)
1684 .map_or(true, |cfg| self.is_cfg_enabled(&cfg));
1686 let module_id = self.push_child_module(
1687 module.name.clone(),
1689 Some((file_id, is_mod_rs)),
1690 &self.item_tree[module.visibility],
1693 def_collector: &mut *self.def_collector,
1694 macro_depth: self.macro_depth,
1696 tree_id: TreeId::new(file_id.into(), None),
1697 item_tree: &item_tree,
1700 .collect_in_top_module(item_tree.top_level_items());
1701 let is_macro_use = is_macro_use
1703 .top_level_attrs(db, self.def_collector.def_map.krate)
1704 .by_key("macro_use")
1707 self.import_all_legacy_macros(module_id);
1712 self.def_collector.def_map.diagnostics.push(
1713 DefDiagnostic::unresolved_module(self.module_id, ast_id, candidate),
1721 fn push_child_module(
1724 declaration: AstId<ast::Module>,
1725 definition: Option<(FileId, bool)>,
1726 visibility: &crate::visibility::RawVisibility,
1727 ) -> LocalModuleId {
1731 .resolve_visibility(self.def_collector.db, self.module_id, visibility)
1732 .unwrap_or(Visibility::Public);
1733 let modules = &mut self.def_collector.def_map.modules;
1734 let origin = match definition {
1735 None => ModuleOrigin::Inline { definition: declaration },
1736 Some((definition, is_mod_rs)) => {
1737 ModuleOrigin::File { declaration, definition, is_mod_rs }
1741 let res = modules.alloc(ModuleData::new(origin, vis));
1742 modules[res].parent = Some(self.module_id);
1743 for (name, mac) in modules[self.module_id].scope.collect_legacy_macros() {
1744 modules[res].scope.define_legacy_macro(name, mac)
1746 modules[self.module_id].children.insert(name.clone(), res);
1748 let module = self.def_collector.def_map.module_id(res);
1749 let def = ModuleDefId::from(module);
1751 self.def_collector.def_map.modules[self.module_id].scope.declare(def);
1752 self.def_collector.update(
1754 &[(Some(name), PerNs::from_def(def, vis, false))],
1761 /// Resolves attributes on an item.
1763 /// Returns `Err` when some attributes could not be resolved to builtins and have been
1764 /// registered as unresolved.
1766 /// If `ignore_up_to` is `Some`, attributes preceding and including that attribute will be
1767 /// assumed to be resolved already.
1768 fn resolve_attributes(
1772 container: ItemContainerId,
1773 ) -> Result<(), ()> {
1774 let mut ignore_up_to =
1775 self.def_collector.skip_attrs.get(&InFile::new(self.file_id(), mod_item)).copied();
1779 // FIXME: this should not be required, all attributes on an item should have a
1781 // Still, this occurs because `#[cfg_attr]` can "expand" to multiple attributes:
1782 // #[cfg_attr(not(off), unresolved, unresolved)]
1784 // We should come up with a different way to ID attributes.
1787 .skip_while(|attr| match ignore_up_to {
1788 Some(id) if attr.id == id => {
1789 ignore_up_to = None;
1797 if self.is_builtin_or_registered_attr(&attr.path) {
1800 tracing::debug!("non-builtin attribute {}", attr.path);
1802 let ast_id = AstIdWithPath::new(
1804 mod_item.ast_id(self.item_tree),
1805 attr.path.as_ref().clone(),
1807 self.def_collector.unresolved_macros.push(MacroDirective {
1808 module_id: self.module_id,
1809 depth: self.macro_depth + 1,
1810 kind: MacroDirectiveKind::Attr {
1825 fn is_builtin_or_registered_attr(&self, path: &ModPath) -> bool {
1826 if path.kind != PathKind::Plain {
1830 let segments = path.segments();
1832 if let Some(name) = segments.first() {
1833 let name = name.to_smol_str();
1834 let pred = |n: &_| *n == name;
1836 let registered = self.def_collector.registered_tools.iter().map(SmolStr::as_str);
1837 let is_tool = builtin_attr::TOOL_MODULES.iter().copied().chain(registered).any(pred);
1838 // FIXME: tool modules can be shadowed by actual modules
1843 if segments.len() == 1 {
1844 let registered = self.def_collector.registered_attrs.iter().map(SmolStr::as_str);
1845 let is_inert = builtin_attr::INERT_ATTRIBUTES
1856 /// If `attrs` registers a procedural macro, collects its definition.
1857 fn collect_proc_macro_def(&mut self, func_name: &Name, ast_id: AstId<ast::Fn>, attrs: &Attrs) {
1858 // FIXME: this should only be done in the root module of `proc-macro` crates, not everywhere
1859 if let Some(proc_macro) = attrs.parse_proc_macro_decl(func_name) {
1860 self.def_collector.export_proc_macro(proc_macro, ast_id);
1864 fn collect_macro_rules(&mut self, id: FileItemTreeId<MacroRules>) {
1865 let krate = self.def_collector.def_map.krate;
1866 let mac = &self.item_tree[id];
1867 let attrs = self.item_tree.attrs(self.def_collector.db, krate, ModItem::from(id).into());
1868 let ast_id = InFile::new(self.file_id(), mac.ast_id.upcast());
1870 let export_attr = attrs.by_key("macro_export");
1872 let is_export = export_attr.exists();
1873 let is_local_inner = if is_export {
1874 export_attr.tt_values().flat_map(|it| &it.token_trees).any(|it| match it {
1875 tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => {
1876 ident.text.contains("local_inner_macros")
1884 // Case 1: builtin macros
1885 if attrs.by_key("rustc_builtin_macro").exists() {
1886 // `#[rustc_builtin_macro = "builtin_name"]` overrides the `macro_rules!` name.
1888 let name = match attrs.by_key("rustc_builtin_macro").string_value() {
1890 // FIXME: a hacky way to create a Name from string.
1891 name = tt::Ident { text: it.clone(), id: tt::TokenId::unspecified() }.as_name();
1896 attrs.by_key("rustc_builtin_macro").tt_values().next().and_then(|tt| {
1897 match tt.token_trees.first() {
1898 Some(tt::TokenTree::Leaf(tt::Leaf::Ident(name))) => Some(name),
1902 match explicit_name {
1904 name = ident.as_name();
1911 let krate = self.def_collector.def_map.krate;
1912 match find_builtin_macro(name, krate, ast_id) {
1914 self.def_collector.define_macro_rules(
1926 .push(DefDiagnostic::unimplemented_builtin_macro(self.module_id, ast_id));
1931 // Case 2: normal `macro_rules!` macro
1932 let macro_id = MacroDefId {
1933 krate: self.def_collector.def_map.krate,
1934 kind: MacroDefKind::Declarative(ast_id),
1935 local_inner: is_local_inner,
1937 self.def_collector.define_macro_rules(
1945 fn collect_macro_def(&mut self, id: FileItemTreeId<MacroDef>) {
1946 let krate = self.def_collector.def_map.krate;
1947 let mac = &self.item_tree[id];
1948 let ast_id = InFile::new(self.file_id(), mac.ast_id.upcast());
1950 // Case 1: builtin macros
1951 let attrs = self.item_tree.attrs(self.def_collector.db, krate, ModItem::from(id).into());
1952 if attrs.by_key("rustc_builtin_macro").exists() {
1953 let macro_id = find_builtin_macro(&mac.name, krate, ast_id)
1954 .or_else(|| find_builtin_derive(&mac.name, krate, ast_id))
1955 .or_else(|| find_builtin_attr(&mac.name, krate, ast_id));
1959 self.def_collector.define_macro_def(
1963 &self.item_tree[mac.visibility],
1971 .push(DefDiagnostic::unimplemented_builtin_macro(self.module_id, ast_id));
1976 // Case 2: normal `macro`
1977 let macro_id = MacroDefId {
1978 krate: self.def_collector.def_map.krate,
1979 kind: MacroDefKind::Declarative(ast_id),
1983 self.def_collector.define_macro_def(
1987 &self.item_tree[mac.visibility],
1991 fn collect_macro_call(&mut self, mac: &MacroCall, container: ItemContainerId) {
1992 let ast_id = AstIdWithPath::new(self.file_id(), mac.ast_id, ModPath::clone(&mac.path));
1994 // Case 1: try to resolve in legacy scope and expand macro_rules
1995 let mut error = None;
1996 match macro_call_as_call_id(
1999 self.def_collector.db,
2000 self.def_collector.def_map.krate,
2002 path.as_ident().and_then(|name| {
2003 self.def_collector.def_map.with_ancestor_maps(
2004 self.def_collector.db,
2006 &mut |map, module| map[module].scope.get_legacy_macro(name),
2011 error.get_or_insert(err);
2014 Ok(Ok(macro_call_id)) => {
2015 // Legacy macros need to be expanded immediately, so that any macros they produce
2017 self.def_collector.collect_macro_expansion(
2020 self.macro_depth + 1,
2024 if let Some(err) = error {
2025 self.def_collector.def_map.diagnostics.push(DefDiagnostic::macro_error(
2027 MacroCallKind::FnLike { ast_id: ast_id.ast_id, expand_to: mac.expand_to },
2035 // Built-in macro failed eager expansion.
2037 self.def_collector.def_map.diagnostics.push(DefDiagnostic::macro_error(
2039 MacroCallKind::FnLike { ast_id: ast_id.ast_id, expand_to: mac.expand_to },
2040 error.unwrap().to_string(),
2044 Err(UnresolvedMacro { .. }) => (),
2047 // Case 2: resolve in module scope, expand during name resolution.
2048 self.def_collector.unresolved_macros.push(MacroDirective {
2049 module_id: self.module_id,
2050 depth: self.macro_depth + 1,
2051 kind: MacroDirectiveKind::FnLike { ast_id, expand_to: mac.expand_to },
2056 fn import_all_legacy_macros(&mut self, module_id: LocalModuleId) {
2057 let macros = self.def_collector.def_map[module_id].scope.collect_legacy_macros();
2058 for (name, macro_) in macros {
2059 self.def_collector.define_legacy_macro(self.module_id, name.clone(), macro_);
2063 fn is_cfg_enabled(&self, cfg: &CfgExpr) -> bool {
2064 self.def_collector.cfg_options.check(cfg) != Some(false)
2067 fn emit_unconfigured_diagnostic(&mut self, item: ModItem, cfg: &CfgExpr) {
2068 let ast_id = item.ast_id(self.item_tree);
2070 let ast_id = InFile::new(self.file_id(), ast_id);
2071 self.def_collector.def_map.diagnostics.push(DefDiagnostic::unconfigured_code(
2075 self.def_collector.cfg_options.clone(),
2079 fn file_id(&self) -> HirFileId {
2080 self.tree_id.file_id()
2086 use crate::{db::DefDatabase, test_db::TestDB};
2087 use base_db::{fixture::WithFixture, SourceDatabase};
2091 fn do_collect_defs(db: &dyn DefDatabase, def_map: DefMap) -> DefMap {
2092 let mut collector = DefCollector {
2095 deps: FxHashMap::default(),
2096 glob_imports: FxHashMap::default(),
2097 unresolved_imports: Vec::new(),
2098 resolved_imports: Vec::new(),
2099 unresolved_macros: Vec::new(),
2100 mod_dirs: FxHashMap::default(),
2101 cfg_options: &CfgOptions::default(),
2102 proc_macros: Default::default(),
2103 exports_proc_macros: false,
2104 from_glob_import: Default::default(),
2105 skip_attrs: Default::default(),
2106 derive_helpers_in_scope: Default::default(),
2107 registered_attrs: Default::default(),
2108 registered_tools: Default::default(),
2110 collector.seed_with_top_level();
2111 collector.collect();
2115 fn do_resolve(not_ra_fixture: &str) -> DefMap {
2116 let (db, file_id) = TestDB::with_single_file(not_ra_fixture);
2117 let krate = db.test_crate();
2119 let edition = db.crate_graph()[krate].edition;
2120 let module_origin = ModuleOrigin::CrateRoot { definition: file_id };
2121 let def_map = DefMap::empty(krate, edition, module_origin);
2122 do_collect_defs(&db, def_map)
2126 fn test_macro_expand_will_stop_1() {
2130 ($($ty:ty)*) => { foo!($($ty)*); }
2138 ($($ty:ty)*) => { foo!(() $($ty)*); }
2147 fn test_macro_expand_will_stop_2() {
2148 // FIXME: this test does succeed, but takes quite a while: 90 seconds in
2149 // the release mode. That's why the argument is not an ra_fixture --
2150 // otherwise injection highlighting gets stuck.
2152 // We need to find a way to fail this faster.
2156 ($($ty:ty)*) => { foo!($($ty)* $($ty)*); }