2019-11-24 15:05:12 +00:00
|
|
|
//! The core of the module-level name resolution algorithm.
|
|
|
|
//!
|
|
|
|
//! `DefCollector::collect` contains the fixed-point iteration loop which
|
|
|
|
//! resolves imports and expands macros.
|
2019-09-30 08:58:53 +00:00
|
|
|
|
2019-10-31 15:45:10 +00:00
|
|
|
use hir_expand::{
|
2020-06-15 17:16:14 +00:00
|
|
|
ast_id_map::FileAstId,
|
2019-12-05 14:10:33 +00:00
|
|
|
builtin_derive::find_builtin_derive,
|
2019-11-10 03:03:24 +00:00
|
|
|
builtin_macro::find_builtin_macro,
|
2019-12-13 21:01:06 +00:00
|
|
|
name::{name, AsName, Name},
|
2020-03-18 09:47:59 +00:00
|
|
|
proc_macro::ProcMacroExpander,
|
2020-02-17 04:57:24 +00:00
|
|
|
HirFileId, MacroCallId, MacroDefId, MacroDefKind,
|
2019-10-31 07:31:29 +00:00
|
|
|
};
|
2019-09-29 22:52:15 +00:00
|
|
|
use ra_cfg::CfgOptions;
|
2020-03-18 12:56:46 +00:00
|
|
|
use ra_db::{CrateId, FileId, ProcMacroId};
|
2020-03-26 16:41:44 +00:00
|
|
|
use ra_syntax::ast;
|
2019-12-08 12:33:42 +00:00
|
|
|
use rustc_hash::FxHashMap;
|
2020-05-20 10:59:20 +00:00
|
|
|
use test_utils::mark;
|
2019-03-02 20:59:04 +00:00
|
|
|
|
|
|
|
use crate::{
|
2019-11-22 08:27:47 +00:00
|
|
|
attr::Attrs,
|
2019-11-23 11:44:43 +00:00
|
|
|
db::DefDatabase,
|
2020-06-26 01:34:39 +00:00
|
|
|
item_scope::{ImportType, PerNsGlobImports},
|
2020-06-24 13:36:18 +00:00
|
|
|
item_tree::{
|
|
|
|
self, FileItemTreeId, ItemTree, ItemTreeId, MacroCall, Mod, ModItem, ModKind, StructDefKind,
|
|
|
|
},
|
2019-03-23 17:41:59 +00:00
|
|
|
nameres::{
|
2019-11-08 21:17:17 +00:00
|
|
|
diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint,
|
2020-06-12 21:24:26 +00:00
|
|
|
BuiltinShadowMode, CrateDefMap, ModuleData, ModuleOrigin, ResolveMode,
|
2019-03-23 17:41:59 +00:00
|
|
|
},
|
2020-02-02 13:04:06 +00:00
|
|
|
path::{ImportAlias, ModPath, PathKind},
|
2019-11-23 13:53:16 +00:00
|
|
|
per_ns::PerNs,
|
2020-06-15 17:16:14 +00:00
|
|
|
visibility::{RawVisibility, Visibility},
|
2020-02-17 04:57:24 +00:00
|
|
|
AdtId, AsMacroCall, AstId, AstIdWithPath, ConstLoc, ContainerId, EnumLoc, EnumVariantId,
|
|
|
|
FunctionLoc, ImplLoc, Intern, LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc,
|
|
|
|
TraitLoc, TypeAliasLoc, UnionLoc,
|
2019-03-02 20:59:04 +00:00
|
|
|
};
|
|
|
|
|
2020-03-13 15:05:46 +00:00
|
|
|
pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap {
|
2019-10-31 15:45:10 +00:00
|
|
|
let crate_graph = db.crate_graph();
|
|
|
|
|
2019-03-13 13:04:28 +00:00
|
|
|
// populate external prelude
|
2020-03-09 10:11:59 +00:00
|
|
|
for dep in &crate_graph[def_map.krate].dependencies {
|
2019-10-31 15:45:10 +00:00
|
|
|
log::debug!("crate dep {:?} -> {:?}", dep.name, dep.crate_id);
|
2020-06-19 16:02:54 +00:00
|
|
|
let dep_def_map = db.crate_def_map(dep.crate_id);
|
2019-10-31 15:45:10 +00:00
|
|
|
def_map.extern_prelude.insert(
|
|
|
|
dep.as_name(),
|
2019-11-27 18:31:51 +00:00
|
|
|
ModuleId { krate: dep.crate_id, local_id: dep_def_map.root }.into(),
|
2019-10-31 15:45:10 +00:00
|
|
|
);
|
|
|
|
|
2019-03-13 13:04:28 +00:00
|
|
|
// look for the prelude
|
2019-11-10 21:15:47 +00:00
|
|
|
// If the dependency defines a prelude, we overwrite an already defined
|
|
|
|
// prelude. This is necessary to import the "std" prelude if a crate
|
|
|
|
// depends on both "core" and "std".
|
|
|
|
if dep_def_map.prelude.is_some() {
|
|
|
|
def_map.prelude = dep_def_map.prelude;
|
2019-03-13 13:04:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-09 10:14:51 +00:00
|
|
|
let cfg_options = &crate_graph[def_map.krate].cfg_options;
|
2020-03-18 12:56:46 +00:00
|
|
|
let proc_macros = &crate_graph[def_map.krate].proc_macro;
|
|
|
|
let proc_macros = proc_macros
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(idx, it)| {
|
|
|
|
// FIXME: a hacky way to create a Name from string.
|
2020-03-26 16:41:44 +00:00
|
|
|
let name = tt::Ident { text: it.name.clone(), id: tt::TokenId::unspecified() };
|
|
|
|
(name.as_name(), ProcMacroExpander::new(def_map.krate, ProcMacroId(idx as u32)))
|
2020-03-18 12:56:46 +00:00
|
|
|
})
|
|
|
|
.collect();
|
2019-09-29 22:52:15 +00:00
|
|
|
|
2019-03-02 20:59:04 +00:00
|
|
|
let mut collector = DefCollector {
|
|
|
|
db,
|
2019-03-13 13:04:28 +00:00
|
|
|
def_map,
|
|
|
|
glob_imports: FxHashMap::default(),
|
2019-03-02 20:59:04 +00:00
|
|
|
unresolved_imports: Vec::new(),
|
2019-12-07 11:20:41 +00:00
|
|
|
resolved_imports: Vec::new(),
|
|
|
|
|
2019-03-02 20:59:04 +00:00
|
|
|
unexpanded_macros: Vec::new(),
|
2019-12-05 14:10:33 +00:00
|
|
|
unexpanded_attribute_macros: Vec::new(),
|
2019-10-10 11:45:05 +00:00
|
|
|
mod_dirs: FxHashMap::default(),
|
2019-09-29 22:52:15 +00:00
|
|
|
cfg_options,
|
2020-03-18 12:56:46 +00:00
|
|
|
proc_macros,
|
2020-06-26 01:34:39 +00:00
|
|
|
from_glob_import: Default::default(),
|
2019-03-02 20:59:04 +00:00
|
|
|
};
|
|
|
|
collector.collect();
|
2019-03-13 13:38:02 +00:00
|
|
|
collector.finish()
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-07 11:20:41 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
|
|
|
enum PartialResolvedImport {
|
|
|
|
/// None of any namespaces is resolved
|
|
|
|
Unresolved,
|
|
|
|
/// One of namespaces is resolved
|
|
|
|
Indeterminate(PerNs),
|
|
|
|
/// All namespaces are resolved, OR it is came from other crate
|
|
|
|
Resolved(PerNs),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialResolvedImport {
|
|
|
|
fn namespaces(&self) -> PerNs {
|
|
|
|
match self {
|
|
|
|
PartialResolvedImport::Unresolved => PerNs::none(),
|
|
|
|
PartialResolvedImport::Indeterminate(ns) => *ns,
|
|
|
|
PartialResolvedImport::Resolved(ns) => *ns,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 13:07:06 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
struct Import {
|
|
|
|
pub path: ModPath,
|
|
|
|
pub alias: Option<ImportAlias>,
|
|
|
|
pub visibility: RawVisibility,
|
|
|
|
pub is_glob: bool,
|
|
|
|
pub is_prelude: bool,
|
|
|
|
pub is_extern_crate: bool,
|
|
|
|
pub is_macro_use: bool,
|
|
|
|
}
|
|
|
|
|
2020-06-24 13:36:18 +00:00
|
|
|
impl Import {
|
|
|
|
fn from_use(tree: &ItemTree, id: FileItemTreeId<item_tree::Import>) -> Self {
|
|
|
|
let it = &tree[id];
|
|
|
|
let visibility = &tree[it.visibility];
|
2020-06-22 13:07:06 +00:00
|
|
|
Self {
|
2020-06-24 13:36:18 +00:00
|
|
|
path: it.path.clone(),
|
|
|
|
alias: it.alias.clone(),
|
|
|
|
visibility: visibility.clone(),
|
2020-06-22 13:07:06 +00:00
|
|
|
is_glob: it.is_glob,
|
|
|
|
is_prelude: it.is_prelude,
|
|
|
|
is_extern_crate: false,
|
|
|
|
is_macro_use: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 13:36:18 +00:00
|
|
|
fn from_extern_crate(tree: &ItemTree, id: FileItemTreeId<item_tree::ExternCrate>) -> Self {
|
|
|
|
let it = &tree[id];
|
|
|
|
let visibility = &tree[it.visibility];
|
2020-06-22 13:07:06 +00:00
|
|
|
Self {
|
2020-06-24 13:36:18 +00:00
|
|
|
path: it.path.clone(),
|
|
|
|
alias: it.alias.clone(),
|
|
|
|
visibility: visibility.clone(),
|
2020-06-22 13:07:06 +00:00
|
|
|
is_glob: false,
|
|
|
|
is_prelude: false,
|
|
|
|
is_extern_crate: true,
|
|
|
|
is_macro_use: it.is_macro_use,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-07 11:20:41 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
struct ImportDirective {
|
|
|
|
module_id: LocalModuleId,
|
2020-06-12 21:24:26 +00:00
|
|
|
import: Import,
|
2019-12-07 11:20:41 +00:00
|
|
|
status: PartialResolvedImport,
|
|
|
|
}
|
|
|
|
|
2019-12-08 12:33:42 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
struct MacroDirective {
|
|
|
|
module_id: LocalModuleId,
|
2020-02-17 04:57:24 +00:00
|
|
|
ast_id: AstIdWithPath<ast::MacroCall>,
|
2019-12-08 12:33:42 +00:00
|
|
|
legacy: Option<MacroCallId>,
|
2020-03-13 12:57:44 +00:00
|
|
|
depth: usize,
|
2019-12-08 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 04:57:24 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
struct DeriveDirective {
|
|
|
|
module_id: LocalModuleId,
|
|
|
|
ast_id: AstIdWithPath<ast::ModuleItem>,
|
|
|
|
}
|
|
|
|
|
2020-06-15 17:16:14 +00:00
|
|
|
struct DefData<'a> {
|
|
|
|
id: ModuleDefId,
|
|
|
|
name: &'a Name,
|
|
|
|
visibility: &'a RawVisibility,
|
|
|
|
has_constructor: bool,
|
|
|
|
}
|
|
|
|
|
2019-03-02 20:59:04 +00:00
|
|
|
/// Walks the tree of module recursively
|
2020-03-13 15:05:46 +00:00
|
|
|
struct DefCollector<'a> {
|
|
|
|
db: &'a dyn DefDatabase,
|
2019-03-02 20:59:04 +00:00
|
|
|
def_map: CrateDefMap,
|
2019-12-26 15:00:10 +00:00
|
|
|
glob_imports: FxHashMap<LocalModuleId, Vec<(LocalModuleId, Visibility)>>,
|
2019-12-07 11:20:41 +00:00
|
|
|
unresolved_imports: Vec<ImportDirective>,
|
|
|
|
resolved_imports: Vec<ImportDirective>,
|
2019-12-08 12:33:42 +00:00
|
|
|
unexpanded_macros: Vec<MacroDirective>,
|
2020-02-17 04:57:24 +00:00
|
|
|
unexpanded_attribute_macros: Vec<DeriveDirective>,
|
2019-11-23 13:49:53 +00:00
|
|
|
mod_dirs: FxHashMap<LocalModuleId, ModDir>,
|
2019-09-29 22:52:15 +00:00
|
|
|
cfg_options: &'a CfgOptions,
|
2020-03-25 12:14:22 +00:00
|
|
|
proc_macros: Vec<(Name, ProcMacroExpander)>,
|
2020-06-26 01:34:39 +00:00
|
|
|
from_glob_import: PerNsGlobImports,
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2020-03-13 15:05:46 +00:00
|
|
|
impl DefCollector<'_> {
|
2019-03-02 20:59:04 +00:00
|
|
|
fn collect(&mut self) {
|
2020-03-09 10:11:59 +00:00
|
|
|
let file_id = self.db.crate_graph()[self.def_map.krate].root_file_id;
|
2020-06-12 21:24:26 +00:00
|
|
|
let item_tree = self.db.item_tree(file_id.into());
|
2019-03-02 20:59:04 +00:00
|
|
|
let module_id = self.def_map.root;
|
2019-12-05 13:33:29 +00:00
|
|
|
self.def_map.modules[module_id].origin = ModuleOrigin::CrateRoot { definition: file_id };
|
2019-03-02 20:59:04 +00:00
|
|
|
ModCollector {
|
|
|
|
def_collector: &mut *self,
|
2020-03-13 12:57:44 +00:00
|
|
|
macro_depth: 0,
|
2019-03-02 20:59:04 +00:00
|
|
|
module_id,
|
|
|
|
file_id: file_id.into(),
|
2020-06-12 21:24:26 +00:00
|
|
|
item_tree: &item_tree,
|
2019-10-10 11:45:05 +00:00
|
|
|
mod_dir: ModDir::root(),
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
2020-06-12 21:24:26 +00:00
|
|
|
.collect(item_tree.top_level_items());
|
2019-03-02 20:59:04 +00:00
|
|
|
|
|
|
|
// main name resolution fixed-point loop.
|
|
|
|
let mut i = 0;
|
|
|
|
loop {
|
2019-05-29 19:13:03 +00:00
|
|
|
self.db.check_canceled();
|
2019-12-07 11:20:41 +00:00
|
|
|
self.resolve_imports();
|
|
|
|
|
|
|
|
match self.resolve_macros() {
|
|
|
|
ReachedFixedPoint::Yes => break,
|
|
|
|
ReachedFixedPoint::No => i += 1,
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
2020-02-14 21:15:27 +00:00
|
|
|
if i == 10000 {
|
2019-09-17 18:04:15 +00:00
|
|
|
log::error!("name resolution is stuck");
|
2019-03-02 20:59:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-03-14 08:53:40 +00:00
|
|
|
|
2019-12-07 11:20:41 +00:00
|
|
|
// Resolve all indeterminate resolved imports again
|
|
|
|
// As some of the macros will expand newly import shadowing partial resolved imports
|
|
|
|
// FIXME: We maybe could skip this, if we handle the Indetermine imports in `resolve_imports`
|
|
|
|
// correctly
|
|
|
|
let partial_resolved = self.resolved_imports.iter().filter_map(|directive| {
|
|
|
|
if let PartialResolvedImport::Indeterminate(_) = directive.status {
|
|
|
|
let mut directive = directive.clone();
|
|
|
|
directive.status = PartialResolvedImport::Unresolved;
|
|
|
|
Some(directive)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
|
|
|
self.unresolved_imports.extend(partial_resolved);
|
|
|
|
self.resolve_imports();
|
|
|
|
|
2019-03-14 08:53:40 +00:00
|
|
|
let unresolved_imports = std::mem::replace(&mut self.unresolved_imports, Vec::new());
|
|
|
|
// show unresolved imports in completion, etc
|
2019-12-07 11:20:41 +00:00
|
|
|
for directive in unresolved_imports {
|
|
|
|
self.record_resolved_import(&directive)
|
2019-03-14 08:53:40 +00:00
|
|
|
}
|
2020-03-25 12:14:22 +00:00
|
|
|
|
|
|
|
// Record proc-macros
|
|
|
|
self.collect_proc_macro();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn collect_proc_macro(&mut self) {
|
|
|
|
let proc_macros = std::mem::take(&mut self.proc_macros);
|
|
|
|
for (name, expander) in proc_macros {
|
|
|
|
let krate = self.def_map.krate;
|
|
|
|
|
|
|
|
let macro_id = MacroDefId {
|
|
|
|
ast_id: None,
|
|
|
|
krate: Some(krate),
|
|
|
|
kind: MacroDefKind::CustomDerive(expander),
|
2020-05-01 03:23:03 +00:00
|
|
|
local_inner: false,
|
2020-03-25 12:14:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
self.define_proc_macro(name.clone(), macro_id);
|
|
|
|
}
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 12:54:02 +00:00
|
|
|
/// Define a macro with `macro_rules`.
|
|
|
|
///
|
|
|
|
/// It will define the macro in legacy textual scope, and if it has `#[macro_export]`,
|
|
|
|
/// then it is also defined in the root module scope.
|
|
|
|
/// You can `use` or invoke it by `crate::macro_name` anywhere, before or after the definition.
|
|
|
|
///
|
|
|
|
/// It is surprising that the macro will never be in the current module scope.
|
|
|
|
/// These code fails with "unresolved import/macro",
|
|
|
|
/// ```rust,compile_fail
|
|
|
|
/// mod m { macro_rules! foo { () => {} } }
|
|
|
|
/// use m::foo as bar;
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ```rust,compile_fail
|
|
|
|
/// macro_rules! foo { () => {} }
|
|
|
|
/// self::foo!();
|
|
|
|
/// crate::foo!();
|
|
|
|
/// ```
|
|
|
|
///
|
2019-11-17 15:35:05 +00:00
|
|
|
/// Well, this code compiles, because the plain path `foo` in `use` is searched
|
2019-09-09 12:54:02 +00:00
|
|
|
/// in the legacy textual scope only.
|
|
|
|
/// ```rust
|
|
|
|
/// macro_rules! foo { () => {} }
|
|
|
|
/// use foo as bar;
|
|
|
|
/// ```
|
2019-06-08 17:42:02 +00:00
|
|
|
fn define_macro(
|
|
|
|
&mut self,
|
2019-11-23 13:49:53 +00:00
|
|
|
module_id: LocalModuleId,
|
2019-06-08 17:42:02 +00:00
|
|
|
name: Name,
|
2019-10-31 15:45:10 +00:00
|
|
|
macro_: MacroDefId,
|
2019-06-08 17:42:02 +00:00
|
|
|
export: bool,
|
|
|
|
) {
|
2019-09-09 12:54:02 +00:00
|
|
|
// Textual scoping
|
|
|
|
self.define_legacy_macro(module_id, name.clone(), macro_);
|
2019-06-08 17:42:02 +00:00
|
|
|
|
2019-09-09 12:54:02 +00:00
|
|
|
// Module scoping
|
2019-06-08 17:42:02 +00:00
|
|
|
// In Rust, `#[macro_export]` macros are unconditionally visible at the
|
|
|
|
// crate root, even if the parent modules is **not** visible.
|
2019-03-26 10:09:39 +00:00
|
|
|
if export {
|
2019-12-25 14:00:10 +00:00
|
|
|
self.update(
|
|
|
|
self.def_map.root,
|
2019-12-26 15:00:10 +00:00
|
|
|
&[(name, PerNs::macros(macro_, Visibility::Public))],
|
|
|
|
Visibility::Public,
|
2020-06-25 16:42:12 +00:00
|
|
|
ImportType::Named,
|
2019-12-25 14:00:10 +00:00
|
|
|
);
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
2019-09-06 16:55:58 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 16:37:54 +00:00
|
|
|
/// Define a legacy textual scoped macro in module
|
2019-09-06 16:55:58 +00:00
|
|
|
///
|
2019-12-20 15:19:28 +00:00
|
|
|
/// We use a map `legacy_macros` to store all legacy textual scoped macros visible per module.
|
2019-09-07 16:37:54 +00:00
|
|
|
/// It will clone all macros from parent legacy scope, whose definition is prior to
|
2019-09-06 16:55:58 +00:00
|
|
|
/// the definition of current module.
|
2019-12-20 15:19:28 +00:00
|
|
|
/// And also, `macro_use` on a module will import all legacy macros visible inside to
|
2019-09-07 16:37:54 +00:00
|
|
|
/// current legacy scope, with possible shadowing.
|
2019-12-20 16:09:13 +00:00
|
|
|
fn define_legacy_macro(&mut self, module_id: LocalModuleId, name: Name, mac: MacroDefId) {
|
2019-09-06 16:55:58 +00:00
|
|
|
// Always shadowing
|
2019-12-20 16:09:13 +00:00
|
|
|
self.def_map.modules[module_id].scope.define_legacy_macro(name, mac);
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 09:47:59 +00:00
|
|
|
/// Define a proc macro
|
|
|
|
///
|
|
|
|
/// A proc macro is similar to normal macro scope, but it would not visiable in legacy textual scoped.
|
|
|
|
/// And unconditionally exported.
|
|
|
|
fn define_proc_macro(&mut self, name: Name, macro_: MacroDefId) {
|
|
|
|
self.update(
|
|
|
|
self.def_map.root,
|
|
|
|
&[(name, PerNs::macros(macro_, Visibility::Public))],
|
|
|
|
Visibility::Public,
|
2020-06-25 16:42:12 +00:00
|
|
|
ImportType::Named,
|
2020-03-18 09:47:59 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-09-05 03:35:13 +00:00
|
|
|
/// Import macros from `#[macro_use] extern crate`.
|
2019-09-06 16:55:58 +00:00
|
|
|
fn import_macros_from_extern_crate(
|
|
|
|
&mut self,
|
2019-11-23 13:49:53 +00:00
|
|
|
current_module_id: LocalModuleId,
|
2020-06-22 13:07:06 +00:00
|
|
|
import: &item_tree::ExternCrate,
|
2019-09-06 16:55:58 +00:00
|
|
|
) {
|
2019-09-05 03:35:13 +00:00
|
|
|
log::debug!(
|
|
|
|
"importing macros from extern crate: {:?} ({:?})",
|
|
|
|
import,
|
|
|
|
self.def_map.edition,
|
|
|
|
);
|
|
|
|
|
|
|
|
let res = self.def_map.resolve_name_in_extern_prelude(
|
|
|
|
&import
|
|
|
|
.path
|
|
|
|
.as_ident()
|
|
|
|
.expect("extern crate should have been desugared to one-element path"),
|
|
|
|
);
|
|
|
|
|
2019-10-31 15:45:10 +00:00
|
|
|
if let Some(ModuleDefId::ModuleId(m)) = res.take_types() {
|
2020-05-20 10:59:20 +00:00
|
|
|
mark::hit!(macro_rules_from_other_crates_are_visible_with_macro_use);
|
2019-10-31 15:45:10 +00:00
|
|
|
self.import_all_macros_exported(current_module_id, m.krate);
|
2019-09-05 08:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-05 03:35:13 +00:00
|
|
|
|
2019-09-09 12:54:02 +00:00
|
|
|
/// Import all exported macros from another crate
|
|
|
|
///
|
|
|
|
/// Exported macros are just all macros in the root module scope.
|
|
|
|
/// Note that it contains not only all `#[macro_export]` macros, but also all aliases
|
|
|
|
/// created by `use` in the root module, ignoring the visibility of `use`.
|
2019-11-23 13:49:53 +00:00
|
|
|
fn import_all_macros_exported(&mut self, current_module_id: LocalModuleId, krate: CrateId) {
|
2019-09-09 12:54:02 +00:00
|
|
|
let def_map = self.db.crate_def_map(krate);
|
|
|
|
for (name, def) in def_map[def_map.root].scope.macros() {
|
|
|
|
// `macro_use` only bring things into legacy scope.
|
|
|
|
self.define_legacy_macro(current_module_id, name.clone(), def);
|
2019-09-05 03:35:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-07 11:20:41 +00:00
|
|
|
/// Import resolution
|
|
|
|
///
|
|
|
|
/// This is a fix point algorithm. We resolve imports until no forward
|
|
|
|
/// progress in resolving imports is made
|
|
|
|
fn resolve_imports(&mut self) {
|
|
|
|
let mut n_previous_unresolved = self.unresolved_imports.len() + 1;
|
|
|
|
|
|
|
|
while self.unresolved_imports.len() < n_previous_unresolved {
|
|
|
|
n_previous_unresolved = self.unresolved_imports.len();
|
|
|
|
let imports = std::mem::replace(&mut self.unresolved_imports, Vec::new());
|
|
|
|
for mut directive in imports {
|
|
|
|
directive.status = self.resolve_import(directive.module_id, &directive.import);
|
|
|
|
match directive.status {
|
|
|
|
PartialResolvedImport::Indeterminate(_) => {
|
|
|
|
self.record_resolved_import(&directive);
|
|
|
|
// FIXME: For avoid performance regression,
|
|
|
|
// we consider an imported resolved if it is indeterminate (i.e not all namespace resolved)
|
|
|
|
self.resolved_imports.push(directive)
|
|
|
|
}
|
|
|
|
PartialResolvedImport::Resolved(_) => {
|
|
|
|
self.record_resolved_import(&directive);
|
|
|
|
self.resolved_imports.push(directive)
|
|
|
|
}
|
|
|
|
PartialResolvedImport::Unresolved => {
|
|
|
|
self.unresolved_imports.push(directive);
|
|
|
|
}
|
|
|
|
}
|
2019-03-13 13:04:28 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 21:24:26 +00:00
|
|
|
fn resolve_import(&self, module_id: LocalModuleId, import: &Import) -> PartialResolvedImport {
|
2019-03-13 13:04:28 +00:00
|
|
|
log::debug!("resolving import: {:?} ({:?})", import, self.def_map.edition);
|
|
|
|
if import.is_extern_crate {
|
|
|
|
let res = self.def_map.resolve_name_in_extern_prelude(
|
|
|
|
&import
|
|
|
|
.path
|
|
|
|
.as_ident()
|
|
|
|
.expect("extern crate should have been desugared to one-element path"),
|
|
|
|
);
|
2019-12-07 11:20:41 +00:00
|
|
|
PartialResolvedImport::Resolved(res)
|
2019-03-13 13:04:28 +00:00
|
|
|
} else {
|
2019-05-26 12:10:56 +00:00
|
|
|
let res = self.def_map.resolve_path_fp_with_macro(
|
|
|
|
self.db,
|
|
|
|
ResolveMode::Import,
|
|
|
|
module_id,
|
|
|
|
&import.path,
|
2019-11-30 15:29:21 +00:00
|
|
|
BuiltinShadowMode::Module,
|
2019-05-26 12:10:56 +00:00
|
|
|
);
|
2019-03-13 13:04:28 +00:00
|
|
|
|
2019-12-07 11:20:41 +00:00
|
|
|
let def = res.resolved_def;
|
2020-01-11 22:19:58 +00:00
|
|
|
if res.reached_fixedpoint == ReachedFixedPoint::No || def.is_none() {
|
2019-12-07 11:20:41 +00:00
|
|
|
return PartialResolvedImport::Unresolved;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(krate) = res.krate {
|
|
|
|
if krate != self.def_map.krate {
|
|
|
|
return PartialResolvedImport::Resolved(def);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether all namespace is resolved
|
|
|
|
if def.take_types().is_some()
|
|
|
|
&& def.take_values().is_some()
|
|
|
|
&& def.take_macros().is_some()
|
|
|
|
{
|
|
|
|
PartialResolvedImport::Resolved(def)
|
|
|
|
} else {
|
|
|
|
PartialResolvedImport::Indeterminate(def)
|
|
|
|
}
|
2019-03-13 13:04:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-07 11:20:41 +00:00
|
|
|
fn record_resolved_import(&mut self, directive: &ImportDirective) {
|
|
|
|
let module_id = directive.module_id;
|
|
|
|
let import = &directive.import;
|
|
|
|
let def = directive.status.namespaces();
|
2019-12-25 14:00:10 +00:00
|
|
|
let vis = self
|
|
|
|
.def_map
|
|
|
|
.resolve_visibility(self.db, module_id, &directive.import.visibility)
|
2019-12-26 15:00:10 +00:00
|
|
|
.unwrap_or(Visibility::Public);
|
2019-12-07 11:20:41 +00:00
|
|
|
|
2019-03-13 13:04:28 +00:00
|
|
|
if import.is_glob {
|
|
|
|
log::debug!("glob import: {:?}", import);
|
2019-09-09 12:54:02 +00:00
|
|
|
match def.take_types() {
|
2019-10-31 15:45:10 +00:00
|
|
|
Some(ModuleDefId::ModuleId(m)) => {
|
2019-03-13 13:04:28 +00:00
|
|
|
if import.is_prelude {
|
2020-05-20 10:59:20 +00:00
|
|
|
mark::hit!(std_prelude);
|
2019-03-13 13:04:28 +00:00
|
|
|
self.def_map.prelude = Some(m);
|
2019-10-31 15:45:10 +00:00
|
|
|
} else if m.krate != self.def_map.krate {
|
2020-05-20 10:59:20 +00:00
|
|
|
mark::hit!(glob_across_crates);
|
2019-03-13 13:04:28 +00:00
|
|
|
// glob import from other crate => we can just import everything once
|
2019-10-31 15:45:10 +00:00
|
|
|
let item_map = self.db.crate_def_map(m.krate);
|
2019-11-27 18:31:51 +00:00
|
|
|
let scope = &item_map[m.local_id].scope;
|
2019-09-09 12:54:02 +00:00
|
|
|
|
|
|
|
// Module scoped macros is included
|
2019-12-25 17:05:16 +00:00
|
|
|
let items = scope
|
|
|
|
.resolutions()
|
|
|
|
// only keep visible names...
|
|
|
|
.map(|(n, res)| {
|
2019-12-27 10:24:31 +00:00
|
|
|
(n, res.filter_visibility(|v| v.is_visible_from_other_crate()))
|
2019-12-25 17:05:16 +00:00
|
|
|
})
|
|
|
|
.filter(|(_, res)| !res.is_none())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2020-06-25 16:42:12 +00:00
|
|
|
self.update(module_id, &items, vis, ImportType::Glob);
|
2019-03-13 13:04:28 +00:00
|
|
|
} else {
|
|
|
|
// glob import from same crate => we do an initial
|
|
|
|
// import, and then need to propagate any further
|
|
|
|
// additions
|
2019-11-27 18:31:51 +00:00
|
|
|
let scope = &self.def_map[m.local_id].scope;
|
2019-09-09 12:54:02 +00:00
|
|
|
|
|
|
|
// Module scoped macros is included
|
2019-12-25 17:05:16 +00:00
|
|
|
let items = scope
|
|
|
|
.resolutions()
|
|
|
|
// only keep visible names...
|
|
|
|
.map(|(n, res)| {
|
|
|
|
(
|
|
|
|
n,
|
|
|
|
res.filter_visibility(|v| {
|
2019-12-27 10:24:31 +00:00
|
|
|
v.is_visible_from_def_map(&self.def_map, module_id)
|
2019-12-25 17:05:16 +00:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.filter(|(_, res)| !res.is_none())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2020-06-25 16:42:12 +00:00
|
|
|
self.update(module_id, &items, vis, ImportType::Glob);
|
2019-03-13 13:04:28 +00:00
|
|
|
// record the glob import in case we add further items
|
2019-12-07 01:50:21 +00:00
|
|
|
let glob = self.glob_imports.entry(m.local_id).or_default();
|
2019-12-25 17:05:16 +00:00
|
|
|
if !glob.iter().any(|(mid, _)| *mid == module_id) {
|
|
|
|
glob.push((module_id, vis));
|
2019-12-07 01:50:21 +00:00
|
|
|
}
|
2019-03-13 13:04:28 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-31 15:45:10 +00:00
|
|
|
Some(ModuleDefId::AdtId(AdtId::EnumId(e))) => {
|
2020-05-20 10:59:20 +00:00
|
|
|
mark::hit!(glob_enum);
|
2019-03-13 13:04:28 +00:00
|
|
|
// glob import from enum => just import all the variants
|
2020-04-11 15:52:26 +00:00
|
|
|
|
|
|
|
// XXX: urgh, so this works by accident! Here, we look at
|
|
|
|
// the enum data, and, in theory, this might require us to
|
|
|
|
// look back at the crate_def_map, creating a cycle. For
|
|
|
|
// example, `enum E { crate::some_macro!(); }`. Luckely, the
|
|
|
|
// only kind of macro that is allowed inside enum is a
|
|
|
|
// `cfg_macro`, and we don't need to run name resolution for
|
|
|
|
// it, but this is sheer luck!
|
2019-10-31 15:45:10 +00:00
|
|
|
let enum_data = self.db.enum_data(e);
|
|
|
|
let resolutions = enum_data
|
|
|
|
.variants
|
|
|
|
.iter()
|
2019-12-20 20:14:30 +00:00
|
|
|
.map(|(local_id, variant_data)| {
|
2019-11-27 20:22:20 +00:00
|
|
|
let name = variant_data.name.clone();
|
2019-10-31 15:45:10 +00:00
|
|
|
let variant = EnumVariantId { parent: e, local_id };
|
2019-12-25 14:00:10 +00:00
|
|
|
let res = PerNs::both(variant.into(), variant.into(), vis);
|
2019-12-20 20:14:30 +00:00
|
|
|
(name, res)
|
2019-03-13 13:04:28 +00:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2020-06-25 16:42:12 +00:00
|
|
|
self.update(module_id, &resolutions, vis, ImportType::Glob);
|
2019-03-13 13:04:28 +00:00
|
|
|
}
|
|
|
|
Some(d) => {
|
|
|
|
log::debug!("glob import {:?} from non-module/enum {:?}", import, d);
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
log::debug!("glob import {:?} didn't resolve as type", import);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2019-03-16 15:06:45 +00:00
|
|
|
match import.path.segments.last() {
|
|
|
|
Some(last_segment) => {
|
2020-01-31 04:14:20 +00:00
|
|
|
let name = match &import.alias {
|
2020-02-02 13:04:06 +00:00
|
|
|
Some(ImportAlias::Alias(name)) => name.clone(),
|
|
|
|
Some(ImportAlias::Underscore) => last_segment.clone(), // FIXME rust-analyzer#2736
|
|
|
|
None => last_segment.clone(),
|
2020-01-31 04:14:20 +00:00
|
|
|
};
|
2019-03-16 15:06:45 +00:00
|
|
|
log::debug!("resolved import {:?} ({:?}) to {:?}", name, import, def);
|
2019-03-13 13:04:28 +00:00
|
|
|
|
2019-03-16 15:06:45 +00:00
|
|
|
// extern crates in the crate root are special-cased to insert entries into the extern prelude: rust-lang/rust#54658
|
|
|
|
if import.is_extern_crate && module_id == self.def_map.root {
|
2019-09-09 12:54:02 +00:00
|
|
|
if let Some(def) = def.take_types() {
|
2019-03-16 15:06:45 +00:00
|
|
|
self.def_map.extern_prelude.insert(name.clone(), def);
|
|
|
|
}
|
|
|
|
}
|
2019-05-26 12:10:56 +00:00
|
|
|
|
2020-06-25 16:42:12 +00:00
|
|
|
self.update(module_id, &[(name, def)], vis, ImportType::Named);
|
2019-03-13 13:04:28 +00:00
|
|
|
}
|
2020-05-20 10:59:20 +00:00
|
|
|
None => mark::hit!(bogus_paths),
|
2019-03-13 13:04:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 01:42:44 +00:00
|
|
|
fn update(
|
|
|
|
&mut self,
|
|
|
|
module_id: LocalModuleId,
|
|
|
|
resolutions: &[(Name, PerNs)],
|
|
|
|
vis: Visibility,
|
2020-06-25 16:42:12 +00:00
|
|
|
import_type: ImportType,
|
2020-06-25 01:42:44 +00:00
|
|
|
) {
|
2020-06-25 16:42:12 +00:00
|
|
|
self.update_recursive(module_id, resolutions, vis, import_type, 0)
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 13:04:28 +00:00
|
|
|
fn update_recursive(
|
|
|
|
&mut self,
|
2019-11-23 13:49:53 +00:00
|
|
|
module_id: LocalModuleId,
|
2019-12-22 14:37:07 +00:00
|
|
|
resolutions: &[(Name, PerNs)],
|
2019-12-25 17:05:16 +00:00
|
|
|
// All resolutions are imported with this visibility; the visibilies in
|
|
|
|
// the `PerNs` values are ignored and overwritten
|
2019-12-26 15:00:10 +00:00
|
|
|
vis: Visibility,
|
2020-06-25 16:42:12 +00:00
|
|
|
import_type: ImportType,
|
2019-03-13 13:04:28 +00:00
|
|
|
depth: usize,
|
|
|
|
) {
|
|
|
|
if depth > 100 {
|
|
|
|
// prevent stack overflows (but this shouldn't be possible)
|
|
|
|
panic!("infinite recursion in glob imports!");
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
2019-12-20 15:26:49 +00:00
|
|
|
let scope = &mut self.def_map.modules[module_id].scope;
|
2019-03-13 13:04:28 +00:00
|
|
|
let mut changed = false;
|
|
|
|
for (name, res) in resolutions {
|
2020-06-26 01:34:39 +00:00
|
|
|
changed |= scope.push_res_with_import(
|
|
|
|
&mut self.from_glob_import,
|
|
|
|
(module_id, name.clone()),
|
2020-06-25 16:42:12 +00:00
|
|
|
res.with_visibility(vis),
|
|
|
|
import_type,
|
|
|
|
);
|
2019-03-13 13:04:28 +00:00
|
|
|
}
|
2019-05-26 12:10:56 +00:00
|
|
|
|
2019-03-13 13:04:28 +00:00
|
|
|
if !changed {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let glob_imports = self
|
|
|
|
.glob_imports
|
|
|
|
.get(&module_id)
|
|
|
|
.into_iter()
|
|
|
|
.flat_map(|v| v.iter())
|
|
|
|
.cloned()
|
|
|
|
.collect::<Vec<_>>();
|
2019-12-25 17:05:16 +00:00
|
|
|
for (glob_importing_module, glob_import_vis) in glob_imports {
|
|
|
|
// we know all resolutions have the same visibility (`vis`), so we
|
|
|
|
// just need to check that once
|
2019-12-27 10:24:31 +00:00
|
|
|
if !vis.is_visible_from_def_map(&self.def_map, glob_importing_module) {
|
2019-12-25 17:05:16 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-06-25 01:42:44 +00:00
|
|
|
self.update_recursive(
|
|
|
|
glob_importing_module,
|
|
|
|
resolutions,
|
|
|
|
glob_import_vis,
|
2020-06-25 16:42:12 +00:00
|
|
|
ImportType::Glob,
|
2020-06-25 01:42:44 +00:00
|
|
|
depth + 1,
|
|
|
|
);
|
2019-03-13 13:04:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_macros(&mut self) -> ReachedFixedPoint {
|
|
|
|
let mut macros = std::mem::replace(&mut self.unexpanded_macros, Vec::new());
|
2019-12-05 14:10:33 +00:00
|
|
|
let mut attribute_macros =
|
|
|
|
std::mem::replace(&mut self.unexpanded_attribute_macros, Vec::new());
|
2019-03-13 13:04:28 +00:00
|
|
|
let mut resolved = Vec::new();
|
2019-03-16 16:40:41 +00:00
|
|
|
let mut res = ReachedFixedPoint::Yes;
|
2019-12-08 12:33:42 +00:00
|
|
|
macros.retain(|directive| {
|
|
|
|
if let Some(call_id) = directive.legacy {
|
|
|
|
res = ReachedFixedPoint::No;
|
2020-03-13 12:57:44 +00:00
|
|
|
resolved.push((directive.module_id, call_id, directive.depth));
|
2019-12-08 12:33:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-11 10:08:24 +00:00
|
|
|
if let Some(call_id) =
|
|
|
|
directive.ast_id.as_call_id(self.db, self.def_map.krate, |path| {
|
|
|
|
let resolved_res = self.def_map.resolve_path_fp_with_macro(
|
|
|
|
self.db,
|
|
|
|
ResolveMode::Other,
|
|
|
|
directive.module_id,
|
|
|
|
&path,
|
|
|
|
BuiltinShadowMode::Module,
|
|
|
|
);
|
|
|
|
resolved_res.resolved_def.take_macros()
|
|
|
|
})
|
|
|
|
{
|
2020-03-13 12:57:44 +00:00
|
|
|
resolved.push((directive.module_id, call_id, directive.depth));
|
2019-12-05 14:10:33 +00:00
|
|
|
res = ReachedFixedPoint::No;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
});
|
2020-02-17 04:57:24 +00:00
|
|
|
attribute_macros.retain(|directive| {
|
2020-06-11 10:08:24 +00:00
|
|
|
if let Some(call_id) =
|
|
|
|
directive.ast_id.as_call_id(self.db, self.def_map.krate, |path| {
|
|
|
|
self.resolve_attribute_macro(&directive, &path)
|
|
|
|
})
|
2020-02-17 04:57:24 +00:00
|
|
|
{
|
2020-03-13 12:57:44 +00:00
|
|
|
resolved.push((directive.module_id, call_id, 0));
|
2019-05-26 12:10:56 +00:00
|
|
|
res = ReachedFixedPoint::No;
|
|
|
|
return false;
|
2019-03-13 13:04:28 +00:00
|
|
|
}
|
2019-05-26 12:10:56 +00:00
|
|
|
|
|
|
|
true
|
2019-03-13 13:04:28 +00:00
|
|
|
});
|
|
|
|
|
2019-05-14 15:55:24 +00:00
|
|
|
self.unexpanded_macros = macros;
|
2019-12-05 14:10:33 +00:00
|
|
|
self.unexpanded_attribute_macros = attribute_macros;
|
2019-05-14 15:55:24 +00:00
|
|
|
|
2020-03-13 12:57:44 +00:00
|
|
|
for (module_id, macro_call_id, depth) in resolved {
|
|
|
|
if depth > 1024 {
|
|
|
|
log::debug!("Max macro expansion depth reached");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
self.collect_macro_expansion(module_id, macro_call_id, depth);
|
2019-03-13 13:04:28 +00:00
|
|
|
}
|
2019-05-14 15:55:24 +00:00
|
|
|
|
2019-03-13 13:04:28 +00:00
|
|
|
res
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 09:47:59 +00:00
|
|
|
fn resolve_attribute_macro(
|
|
|
|
&self,
|
|
|
|
directive: &DeriveDirective,
|
|
|
|
path: &ModPath,
|
|
|
|
) -> Option<MacroDefId> {
|
2019-12-05 14:10:33 +00:00
|
|
|
if let Some(name) = path.as_ident() {
|
|
|
|
// FIXME this should actually be handled with the normal name
|
|
|
|
// resolution; the std lib defines built-in stubs for the derives,
|
|
|
|
// but these are new-style `macro`s, which we don't support yet
|
|
|
|
if let Some(def_id) = find_builtin_derive(name) {
|
|
|
|
return Some(def_id);
|
|
|
|
}
|
|
|
|
}
|
2020-03-18 09:47:59 +00:00
|
|
|
let resolved_res = self.def_map.resolve_path_fp_with_macro(
|
|
|
|
self.db,
|
|
|
|
ResolveMode::Other,
|
|
|
|
directive.module_id,
|
|
|
|
&path,
|
|
|
|
BuiltinShadowMode::Module,
|
|
|
|
);
|
|
|
|
|
|
|
|
resolved_res.resolved_def.take_macros()
|
2019-12-05 14:10:33 +00:00
|
|
|
}
|
|
|
|
|
2020-03-13 12:57:44 +00:00
|
|
|
fn collect_macro_expansion(
|
|
|
|
&mut self,
|
|
|
|
module_id: LocalModuleId,
|
|
|
|
macro_call_id: MacroCallId,
|
|
|
|
depth: usize,
|
|
|
|
) {
|
2020-07-14 16:31:48 +00:00
|
|
|
if depth > 100 {
|
|
|
|
mark::hit!(macro_expansion_overflow);
|
|
|
|
log::warn!("macro expansion is too deep");
|
|
|
|
return;
|
|
|
|
}
|
2019-12-08 12:33:42 +00:00
|
|
|
let file_id: HirFileId = macro_call_id.as_file();
|
2020-06-12 21:24:26 +00:00
|
|
|
let item_tree = self.db.item_tree(file_id);
|
2019-12-08 12:33:42 +00:00
|
|
|
let mod_dir = self.mod_dirs[&module_id].clone();
|
|
|
|
ModCollector {
|
|
|
|
def_collector: &mut *self,
|
2020-03-13 12:57:44 +00:00
|
|
|
macro_depth: depth,
|
2019-12-08 12:33:42 +00:00
|
|
|
file_id,
|
|
|
|
module_id,
|
2020-06-12 21:24:26 +00:00
|
|
|
item_tree: &item_tree,
|
2019-12-08 12:33:42 +00:00
|
|
|
mod_dir,
|
2019-04-20 15:05:25 +00:00
|
|
|
}
|
2020-06-12 21:24:26 +00:00
|
|
|
.collect(item_tree.top_level_items());
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn finish(self) -> CrateDefMap {
|
|
|
|
self.def_map
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 13:38:02 +00:00
|
|
|
/// Walks a single module, populating defs, imports and macros
|
2020-03-13 15:05:46 +00:00
|
|
|
struct ModCollector<'a, 'b> {
|
|
|
|
def_collector: &'a mut DefCollector<'b>,
|
2020-03-13 12:57:44 +00:00
|
|
|
macro_depth: usize,
|
2019-11-23 13:49:53 +00:00
|
|
|
module_id: LocalModuleId,
|
2019-03-13 13:38:02 +00:00
|
|
|
file_id: HirFileId,
|
2020-06-12 21:24:26 +00:00
|
|
|
item_tree: &'a ItemTree,
|
2019-10-10 11:45:05 +00:00
|
|
|
mod_dir: ModDir,
|
2019-03-13 13:38:02 +00:00
|
|
|
}
|
|
|
|
|
2020-03-13 15:05:46 +00:00
|
|
|
impl ModCollector<'_, '_> {
|
2020-06-12 21:24:26 +00:00
|
|
|
fn collect(&mut self, items: &[ModItem]) {
|
2019-10-10 11:45:05 +00:00
|
|
|
// Note: don't assert that inserted value is fresh: it's simply not true
|
|
|
|
// for macros.
|
|
|
|
self.def_collector.mod_dirs.insert(self.module_id, self.mod_dir.clone());
|
|
|
|
|
2019-09-05 08:20:36 +00:00
|
|
|
// Prelude module is always considered to be `#[macro_use]`.
|
|
|
|
if let Some(prelude_module) = self.def_collector.def_map.prelude {
|
2019-10-31 15:45:10 +00:00
|
|
|
if prelude_module.krate != self.def_collector.def_map.krate {
|
2020-05-20 10:59:20 +00:00
|
|
|
mark::hit!(prelude_is_macro_use);
|
2019-10-31 15:45:10 +00:00
|
|
|
self.def_collector.import_all_macros_exported(self.module_id, prelude_module.krate);
|
2019-09-07 18:43:41 +00:00
|
|
|
}
|
2019-09-05 08:20:36 +00:00
|
|
|
}
|
|
|
|
|
2019-09-05 10:39:56 +00:00
|
|
|
// This should be processed eagerly instead of deferred to resolving.
|
|
|
|
// `#[macro_use] extern crate` is hoisted to imports macros before collecting
|
|
|
|
// any other items.
|
|
|
|
for item in items {
|
2020-06-25 12:39:27 +00:00
|
|
|
if self.is_cfg_enabled(self.item_tree.attrs((*item).into())) {
|
2020-06-22 13:07:06 +00:00
|
|
|
if let ModItem::ExternCrate(id) = item {
|
|
|
|
let import = self.item_tree[*id].clone();
|
|
|
|
if import.is_macro_use {
|
2019-09-29 22:52:15 +00:00
|
|
|
self.def_collector.import_macros_from_extern_crate(self.module_id, &import);
|
|
|
|
}
|
2019-09-05 10:39:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 17:16:14 +00:00
|
|
|
for &item in items {
|
2020-06-25 12:39:27 +00:00
|
|
|
let attrs = self.item_tree.attrs(item.into());
|
2020-06-15 17:16:14 +00:00
|
|
|
if self.is_cfg_enabled(attrs) {
|
|
|
|
let module =
|
|
|
|
ModuleId { krate: self.def_collector.def_map.krate, local_id: self.module_id };
|
|
|
|
let container = ContainerId::ModuleId(module);
|
|
|
|
|
|
|
|
let mut def = None;
|
|
|
|
match item {
|
|
|
|
ModItem::Mod(m) => self.collect_module(&self.item_tree[m], attrs),
|
|
|
|
ModItem::Import(import_id) => {
|
2019-12-07 11:20:41 +00:00
|
|
|
self.def_collector.unresolved_imports.push(ImportDirective {
|
|
|
|
module_id: self.module_id,
|
2020-06-24 13:36:18 +00:00
|
|
|
import: Import::from_use(&self.item_tree, import_id),
|
2020-06-22 13:07:06 +00:00
|
|
|
status: PartialResolvedImport::Unresolved,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
ModItem::ExternCrate(import_id) => {
|
|
|
|
self.def_collector.unresolved_imports.push(ImportDirective {
|
|
|
|
module_id: self.module_id,
|
2020-06-24 13:36:18 +00:00
|
|
|
import: Import::from_extern_crate(&self.item_tree, import_id),
|
2019-12-07 11:20:41 +00:00
|
|
|
status: PartialResolvedImport::Unresolved,
|
|
|
|
})
|
|
|
|
}
|
2020-06-15 17:16:14 +00:00
|
|
|
ModItem::MacroCall(mac) => self.collect_macro(&self.item_tree[mac]),
|
|
|
|
ModItem::Impl(imp) => {
|
2019-11-15 16:32:56 +00:00
|
|
|
let module = ModuleId {
|
|
|
|
krate: self.def_collector.def_map.krate,
|
2019-11-27 18:31:51 +00:00
|
|
|
local_id: self.module_id,
|
2019-11-15 16:32:56 +00:00
|
|
|
};
|
2019-12-20 12:47:44 +00:00
|
|
|
let container = ContainerId::ModuleId(module);
|
2020-06-22 13:07:06 +00:00
|
|
|
let impl_id = ImplLoc { container, id: ItemTreeId::new(self.file_id, imp) }
|
|
|
|
.intern(self.def_collector.db);
|
2019-12-20 15:55:38 +00:00
|
|
|
self.def_collector.def_map.modules[self.module_id]
|
|
|
|
.scope
|
|
|
|
.define_impl(impl_id)
|
2019-11-15 16:32:56 +00:00
|
|
|
}
|
2020-06-22 13:07:06 +00:00
|
|
|
ModItem::Function(id) => {
|
|
|
|
let func = &self.item_tree[id];
|
2020-06-15 17:16:14 +00:00
|
|
|
def = Some(DefData {
|
|
|
|
id: FunctionLoc {
|
|
|
|
container: container.into(),
|
2020-06-22 13:07:06 +00:00
|
|
|
id: ItemTreeId::new(self.file_id, id),
|
2020-06-15 17:16:14 +00:00
|
|
|
}
|
|
|
|
.intern(self.def_collector.db)
|
|
|
|
.into(),
|
2020-06-22 13:07:06 +00:00
|
|
|
name: &func.name,
|
2020-06-24 13:36:18 +00:00
|
|
|
visibility: &self.item_tree[func.visibility],
|
2020-06-15 17:16:14 +00:00
|
|
|
has_constructor: false,
|
|
|
|
});
|
|
|
|
}
|
2020-06-22 13:07:06 +00:00
|
|
|
ModItem::Struct(id) => {
|
|
|
|
let it = &self.item_tree[id];
|
2020-06-15 17:16:14 +00:00
|
|
|
|
|
|
|
// FIXME: check attrs to see if this is an attribute macro invocation;
|
|
|
|
// in which case we don't add the invocation, just a single attribute
|
|
|
|
// macro invocation
|
|
|
|
self.collect_derives(attrs, it.ast_id.upcast());
|
|
|
|
|
|
|
|
def = Some(DefData {
|
2020-06-22 13:07:06 +00:00
|
|
|
id: StructLoc { container, id: ItemTreeId::new(self.file_id, id) }
|
|
|
|
.intern(self.def_collector.db)
|
|
|
|
.into(),
|
2020-06-15 17:16:14 +00:00
|
|
|
name: &it.name,
|
2020-06-24 13:36:18 +00:00
|
|
|
visibility: &self.item_tree[it.visibility],
|
2020-06-15 17:16:14 +00:00
|
|
|
has_constructor: it.kind != StructDefKind::Record,
|
|
|
|
});
|
|
|
|
}
|
2020-06-22 13:07:06 +00:00
|
|
|
ModItem::Union(id) => {
|
|
|
|
let it = &self.item_tree[id];
|
2020-06-15 17:16:14 +00:00
|
|
|
|
|
|
|
// FIXME: check attrs to see if this is an attribute macro invocation;
|
|
|
|
// in which case we don't add the invocation, just a single attribute
|
|
|
|
// macro invocation
|
|
|
|
self.collect_derives(attrs, it.ast_id.upcast());
|
|
|
|
|
|
|
|
def = Some(DefData {
|
2020-06-22 13:07:06 +00:00
|
|
|
id: UnionLoc { container, id: ItemTreeId::new(self.file_id, id) }
|
2020-06-15 17:16:14 +00:00
|
|
|
.intern(self.def_collector.db)
|
|
|
|
.into(),
|
|
|
|
name: &it.name,
|
2020-06-24 13:36:18 +00:00
|
|
|
visibility: &self.item_tree[it.visibility],
|
2020-06-15 17:16:14 +00:00
|
|
|
has_constructor: false,
|
|
|
|
});
|
|
|
|
}
|
2020-06-22 13:07:06 +00:00
|
|
|
ModItem::Enum(id) => {
|
|
|
|
let it = &self.item_tree[id];
|
2020-06-15 17:16:14 +00:00
|
|
|
|
|
|
|
// FIXME: check attrs to see if this is an attribute macro invocation;
|
|
|
|
// in which case we don't add the invocation, just a single attribute
|
|
|
|
// macro invocation
|
|
|
|
self.collect_derives(attrs, it.ast_id.upcast());
|
|
|
|
|
|
|
|
def = Some(DefData {
|
2020-06-22 13:07:06 +00:00
|
|
|
id: EnumLoc { container, id: ItemTreeId::new(self.file_id, id) }
|
2020-06-15 17:16:14 +00:00
|
|
|
.intern(self.def_collector.db)
|
|
|
|
.into(),
|
|
|
|
name: &it.name,
|
2020-06-24 13:36:18 +00:00
|
|
|
visibility: &self.item_tree[it.visibility],
|
2020-06-15 17:16:14 +00:00
|
|
|
has_constructor: false,
|
|
|
|
});
|
|
|
|
}
|
2020-06-22 13:07:06 +00:00
|
|
|
ModItem::Const(id) => {
|
|
|
|
let it = &self.item_tree[id];
|
2020-06-15 17:16:14 +00:00
|
|
|
|
|
|
|
if let Some(name) = &it.name {
|
|
|
|
def = Some(DefData {
|
|
|
|
id: ConstLoc {
|
|
|
|
container: container.into(),
|
2020-06-22 13:07:06 +00:00
|
|
|
id: ItemTreeId::new(self.file_id, id),
|
2020-06-15 17:16:14 +00:00
|
|
|
}
|
|
|
|
.intern(self.def_collector.db)
|
|
|
|
.into(),
|
|
|
|
name,
|
2020-06-24 13:36:18 +00:00
|
|
|
visibility: &self.item_tree[it.visibility],
|
2020-06-15 17:16:14 +00:00
|
|
|
has_constructor: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-06-22 13:07:06 +00:00
|
|
|
ModItem::Static(id) => {
|
|
|
|
let it = &self.item_tree[id];
|
2020-06-15 17:16:14 +00:00
|
|
|
|
|
|
|
def = Some(DefData {
|
2020-06-22 13:07:06 +00:00
|
|
|
id: StaticLoc { container, id: ItemTreeId::new(self.file_id, id) }
|
|
|
|
.intern(self.def_collector.db)
|
|
|
|
.into(),
|
2020-06-15 17:16:14 +00:00
|
|
|
name: &it.name,
|
2020-06-24 13:36:18 +00:00
|
|
|
visibility: &self.item_tree[it.visibility],
|
2020-06-15 17:16:14 +00:00
|
|
|
has_constructor: false,
|
|
|
|
});
|
|
|
|
}
|
2020-06-22 13:07:06 +00:00
|
|
|
ModItem::Trait(id) => {
|
|
|
|
let it = &self.item_tree[id];
|
2020-06-15 17:16:14 +00:00
|
|
|
|
|
|
|
def = Some(DefData {
|
2020-06-22 13:07:06 +00:00
|
|
|
id: TraitLoc { container, id: ItemTreeId::new(self.file_id, id) }
|
2020-06-15 17:16:14 +00:00
|
|
|
.intern(self.def_collector.db)
|
|
|
|
.into(),
|
|
|
|
name: &it.name,
|
2020-06-24 13:36:18 +00:00
|
|
|
visibility: &self.item_tree[it.visibility],
|
2020-06-15 17:16:14 +00:00
|
|
|
has_constructor: false,
|
|
|
|
});
|
|
|
|
}
|
2020-06-22 13:07:06 +00:00
|
|
|
ModItem::TypeAlias(id) => {
|
|
|
|
let it = &self.item_tree[id];
|
2020-06-15 17:16:14 +00:00
|
|
|
|
|
|
|
def = Some(DefData {
|
|
|
|
id: TypeAliasLoc {
|
|
|
|
container: container.into(),
|
2020-06-22 13:07:06 +00:00
|
|
|
id: ItemTreeId::new(self.file_id, id),
|
2020-06-15 17:16:14 +00:00
|
|
|
}
|
|
|
|
.intern(self.def_collector.db)
|
|
|
|
.into(),
|
|
|
|
name: &it.name,
|
2020-06-24 13:36:18 +00:00
|
|
|
visibility: &self.item_tree[it.visibility],
|
2020-06-15 17:16:14 +00:00
|
|
|
has_constructor: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(DefData { id, name, visibility, has_constructor }) = def {
|
|
|
|
self.def_collector.def_map.modules[self.module_id].scope.define_def(id);
|
|
|
|
let vis = self
|
|
|
|
.def_collector
|
|
|
|
.def_map
|
|
|
|
.resolve_visibility(self.def_collector.db, self.module_id, visibility)
|
|
|
|
.unwrap_or(Visibility::Public);
|
|
|
|
self.def_collector.update(
|
|
|
|
self.module_id,
|
|
|
|
&[(name.clone(), PerNs::from_def(id, vis, has_constructor))],
|
|
|
|
vis,
|
2020-06-25 16:42:12 +00:00
|
|
|
ImportType::Named,
|
2020-06-15 17:16:14 +00:00
|
|
|
)
|
2019-09-29 22:52:15 +00:00
|
|
|
}
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 21:24:26 +00:00
|
|
|
fn collect_module(&mut self, module: &Mod, attrs: &Attrs) {
|
2019-11-24 13:03:02 +00:00
|
|
|
let path_attr = attrs.by_key("path").string_value();
|
|
|
|
let is_macro_use = attrs.by_key("macro_use").exists();
|
2020-06-15 17:16:14 +00:00
|
|
|
match &module.kind {
|
2019-07-13 18:51:20 +00:00
|
|
|
// inline module, just recurse
|
2020-06-15 17:16:14 +00:00
|
|
|
ModKind::Inline { items } => {
|
2019-12-26 14:49:13 +00:00
|
|
|
let module_id = self.push_child_module(
|
2020-06-15 17:16:14 +00:00
|
|
|
module.name.clone(),
|
|
|
|
AstId::new(self.file_id, module.ast_id),
|
2019-12-26 14:49:13 +00:00
|
|
|
None,
|
2020-06-24 13:36:18 +00:00
|
|
|
&self.item_tree[module.visibility],
|
2019-12-26 14:49:13 +00:00
|
|
|
);
|
2019-09-06 16:55:58 +00:00
|
|
|
|
2019-03-02 20:59:04 +00:00
|
|
|
ModCollector {
|
|
|
|
def_collector: &mut *self.def_collector,
|
2020-03-13 12:57:44 +00:00
|
|
|
macro_depth: self.macro_depth,
|
2019-03-02 20:59:04 +00:00
|
|
|
module_id,
|
|
|
|
file_id: self.file_id,
|
2020-06-12 21:24:26 +00:00
|
|
|
item_tree: self.item_tree,
|
2020-06-15 17:16:14 +00:00
|
|
|
mod_dir: self.mod_dir.descend_into_definition(&module.name, path_attr),
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
2019-07-29 12:16:58 +00:00
|
|
|
.collect(&*items);
|
2019-10-10 14:48:30 +00:00
|
|
|
if is_macro_use {
|
2019-09-07 16:37:54 +00:00
|
|
|
self.import_all_legacy_macros(module_id);
|
2019-09-06 16:55:58 +00:00
|
|
|
}
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
2019-07-13 18:51:20 +00:00
|
|
|
// out of line module, resolve, parse and recurse
|
2020-06-15 17:16:14 +00:00
|
|
|
ModKind::Outline {} => {
|
|
|
|
let ast_id = AstId::new(self.file_id, module.ast_id);
|
2019-10-09 11:27:37 +00:00
|
|
|
match self.mod_dir.resolve_declaration(
|
2019-07-06 18:54:21 +00:00
|
|
|
self.def_collector.db,
|
|
|
|
self.file_id,
|
2020-06-15 17:16:14 +00:00
|
|
|
&module.name,
|
2019-11-24 13:03:02 +00:00
|
|
|
path_attr,
|
2019-07-06 18:54:21 +00:00
|
|
|
) {
|
2020-06-11 09:04:09 +00:00
|
|
|
Ok((file_id, is_mod_rs, mod_dir)) => {
|
2019-12-26 14:49:13 +00:00
|
|
|
let module_id = self.push_child_module(
|
2020-06-15 17:16:14 +00:00
|
|
|
module.name.clone(),
|
2019-12-26 14:49:13 +00:00
|
|
|
ast_id,
|
2020-06-11 09:04:09 +00:00
|
|
|
Some((file_id, is_mod_rs)),
|
2020-06-24 13:36:18 +00:00
|
|
|
&self.item_tree[module.visibility],
|
2019-12-26 14:49:13 +00:00
|
|
|
);
|
2020-06-15 17:16:14 +00:00
|
|
|
let item_tree = self.def_collector.db.item_tree(file_id.into());
|
2019-03-23 15:35:14 +00:00
|
|
|
ModCollector {
|
|
|
|
def_collector: &mut *self.def_collector,
|
2020-03-13 12:57:44 +00:00
|
|
|
macro_depth: self.macro_depth,
|
2019-03-23 15:35:14 +00:00
|
|
|
module_id,
|
|
|
|
file_id: file_id.into(),
|
2020-06-15 17:16:14 +00:00
|
|
|
item_tree: &item_tree,
|
2019-10-10 11:45:05 +00:00
|
|
|
mod_dir,
|
2019-03-23 15:35:14 +00:00
|
|
|
}
|
2020-06-15 17:16:14 +00:00
|
|
|
.collect(item_tree.top_level_items());
|
2019-10-10 14:48:30 +00:00
|
|
|
if is_macro_use {
|
2019-09-07 16:37:54 +00:00
|
|
|
self.import_all_legacy_macros(module_id);
|
2019-09-06 16:55:58 +00:00
|
|
|
}
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
2019-03-23 15:35:14 +00:00
|
|
|
Err(candidate) => self.def_collector.def_map.diagnostics.push(
|
|
|
|
DefDiagnostic::UnresolvedModule {
|
|
|
|
module: self.module_id,
|
2019-03-26 14:25:14 +00:00
|
|
|
declaration: ast_id,
|
2019-03-23 15:35:14 +00:00
|
|
|
candidate,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
};
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 13:38:02 +00:00
|
|
|
fn push_child_module(
|
|
|
|
&mut self,
|
|
|
|
name: Name,
|
2019-03-26 14:25:14 +00:00
|
|
|
declaration: AstId<ast::Module>,
|
2020-06-11 09:04:09 +00:00
|
|
|
definition: Option<(FileId, bool)>,
|
2019-12-26 14:57:14 +00:00
|
|
|
visibility: &crate::visibility::RawVisibility,
|
2019-11-23 13:49:53 +00:00
|
|
|
) -> LocalModuleId {
|
2019-12-26 14:49:13 +00:00
|
|
|
let vis = self
|
|
|
|
.def_collector
|
|
|
|
.def_map
|
|
|
|
.resolve_visibility(self.def_collector.db, self.module_id, visibility)
|
2019-12-26 15:00:10 +00:00
|
|
|
.unwrap_or(Visibility::Public);
|
2019-03-13 13:38:02 +00:00
|
|
|
let modules = &mut self.def_collector.def_map.modules;
|
|
|
|
let res = modules.alloc(ModuleData::default());
|
|
|
|
modules[res].parent = Some(self.module_id);
|
2020-06-18 14:02:45 +00:00
|
|
|
modules[res].origin = match definition {
|
|
|
|
None => ModuleOrigin::Inline { definition: declaration },
|
2020-06-11 09:04:09 +00:00
|
|
|
Some((definition, is_mod_rs)) => {
|
|
|
|
ModuleOrigin::File { declaration, definition, is_mod_rs }
|
|
|
|
}
|
2020-06-18 14:02:45 +00:00
|
|
|
};
|
2019-12-20 16:09:13 +00:00
|
|
|
for (name, mac) in modules[self.module_id].scope.collect_legacy_macros() {
|
|
|
|
modules[res].scope.define_legacy_macro(name, mac)
|
|
|
|
}
|
2019-03-14 08:53:40 +00:00
|
|
|
modules[self.module_id].children.insert(name.clone(), res);
|
2019-12-22 14:08:57 +00:00
|
|
|
let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: res };
|
|
|
|
let def: ModuleDefId = module.into();
|
2019-12-22 14:21:48 +00:00
|
|
|
self.def_collector.def_map.modules[self.module_id].scope.define_def(def);
|
2020-06-25 01:42:44 +00:00
|
|
|
self.def_collector.update(
|
|
|
|
self.module_id,
|
|
|
|
&[(name, PerNs::from_def(def, vis, false))],
|
|
|
|
vis,
|
2020-06-25 16:42:12 +00:00
|
|
|
ImportType::Named,
|
2020-06-25 01:42:44 +00:00
|
|
|
);
|
2019-03-02 20:59:04 +00:00
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2020-06-15 17:16:14 +00:00
|
|
|
fn collect_derives(&mut self, attrs: &Attrs, ast_id: FileAstId<ast::ModuleItem>) {
|
2019-12-05 14:10:33 +00:00
|
|
|
for derive_subtree in attrs.by_key("derive").tt_values() {
|
|
|
|
// for #[derive(Copy, Clone)], `derive_subtree` is the `(Copy, Clone)` subtree
|
|
|
|
for tt in &derive_subtree.token_trees {
|
|
|
|
let ident = match &tt {
|
|
|
|
tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => ident,
|
|
|
|
tt::TokenTree::Leaf(tt::Leaf::Punct(_)) => continue, // , is ok
|
|
|
|
_ => continue, // anything else would be an error (which we currently ignore)
|
|
|
|
};
|
2019-12-13 11:12:36 +00:00
|
|
|
let path = ModPath::from_tt_ident(ident);
|
2019-12-05 14:10:33 +00:00
|
|
|
|
2020-06-15 17:16:14 +00:00
|
|
|
let ast_id = AstIdWithPath::new(self.file_id, ast_id, path);
|
2020-02-17 04:57:24 +00:00
|
|
|
self.def_collector
|
|
|
|
.unexpanded_attribute_macros
|
|
|
|
.push(DeriveDirective { module_id: self.module_id, ast_id });
|
2019-12-05 14:10:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 17:16:14 +00:00
|
|
|
fn collect_macro(&mut self, mac: &MacroCall) {
|
2020-02-17 04:57:24 +00:00
|
|
|
let mut ast_id = AstIdWithPath::new(self.file_id, mac.ast_id, mac.path.clone());
|
2019-10-29 08:15:51 +00:00
|
|
|
|
2019-11-10 03:03:24 +00:00
|
|
|
// Case 0: builtin macros
|
2020-06-15 17:16:14 +00:00
|
|
|
if mac.is_builtin {
|
2019-11-10 03:03:24 +00:00
|
|
|
if let Some(name) = &mac.name {
|
|
|
|
let krate = self.def_collector.def_map.krate;
|
2020-02-17 04:57:24 +00:00
|
|
|
if let Some(macro_id) = find_builtin_macro(name, krate, ast_id.ast_id) {
|
2019-11-10 03:03:24 +00:00
|
|
|
self.def_collector.define_macro(
|
|
|
|
self.module_id,
|
|
|
|
name.clone(),
|
|
|
|
macro_id,
|
2020-06-15 17:16:14 +00:00
|
|
|
mac.is_export,
|
2019-11-10 03:03:24 +00:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 20:59:04 +00:00
|
|
|
// Case 1: macro rules, define a macro in crate-global mutable scope
|
|
|
|
if is_macro_rules(&mac.path) {
|
|
|
|
if let Some(name) = &mac.name {
|
2019-11-11 10:45:55 +00:00
|
|
|
let macro_id = MacroDefId {
|
2020-02-17 04:57:24 +00:00
|
|
|
ast_id: Some(ast_id.ast_id),
|
2019-12-05 14:10:33 +00:00
|
|
|
krate: Some(self.def_collector.def_map.krate),
|
2019-11-11 10:45:55 +00:00
|
|
|
kind: MacroDefKind::Declarative,
|
2020-06-15 17:16:14 +00:00
|
|
|
local_inner: mac.is_local_inner,
|
2019-11-11 10:45:55 +00:00
|
|
|
};
|
2020-06-15 17:16:14 +00:00
|
|
|
self.def_collector.define_macro(
|
|
|
|
self.module_id,
|
|
|
|
name.clone(),
|
|
|
|
macro_id,
|
|
|
|
mac.is_export,
|
|
|
|
);
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-08 12:33:42 +00:00
|
|
|
// Case 2: try to resolve in legacy scope and expand macro_rules
|
2020-06-11 10:08:24 +00:00
|
|
|
if let Some(macro_call_id) =
|
|
|
|
ast_id.as_call_id(self.def_collector.db, self.def_collector.def_map.krate, |path| {
|
|
|
|
path.as_ident().and_then(|name| {
|
|
|
|
self.def_collector.def_map[self.module_id].scope.get_legacy_macro(&name)
|
|
|
|
})
|
2020-02-17 04:57:24 +00:00
|
|
|
})
|
2020-06-11 10:08:24 +00:00
|
|
|
{
|
2019-12-08 12:33:42 +00:00
|
|
|
self.def_collector.unexpanded_macros.push(MacroDirective {
|
|
|
|
module_id: self.module_id,
|
|
|
|
ast_id,
|
|
|
|
legacy: Some(macro_call_id),
|
2020-03-13 12:57:44 +00:00
|
|
|
depth: self.macro_depth + 1,
|
2019-12-08 12:33:42 +00:00
|
|
|
});
|
|
|
|
|
2019-03-02 20:59:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-06 18:44:26 +00:00
|
|
|
// Case 3: resolve in module scope, expand during name resolution.
|
|
|
|
// We rewrite simple path `macro_name` to `self::macro_name` to force resolve in module scope only.
|
2020-02-17 04:57:24 +00:00
|
|
|
if ast_id.path.is_ident() {
|
|
|
|
ast_id.path.kind = PathKind::Super(0);
|
2019-09-06 18:44:26 +00:00
|
|
|
}
|
2019-12-08 12:33:42 +00:00
|
|
|
|
|
|
|
self.def_collector.unexpanded_macros.push(MacroDirective {
|
|
|
|
module_id: self.module_id,
|
|
|
|
ast_id,
|
|
|
|
legacy: None,
|
2020-03-13 12:57:44 +00:00
|
|
|
depth: self.macro_depth + 1,
|
2019-12-08 12:33:42 +00:00
|
|
|
});
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
2019-09-06 16:55:58 +00:00
|
|
|
|
2019-11-23 13:49:53 +00:00
|
|
|
fn import_all_legacy_macros(&mut self, module_id: LocalModuleId) {
|
2019-12-20 16:09:13 +00:00
|
|
|
let macros = self.def_collector.def_map[module_id].scope.collect_legacy_macros();
|
2019-09-06 16:55:58 +00:00
|
|
|
for (name, macro_) in macros {
|
2019-09-09 12:54:02 +00:00
|
|
|
self.def_collector.define_legacy_macro(self.module_id, name.clone(), macro_);
|
2019-09-06 16:55:58 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-29 22:52:15 +00:00
|
|
|
|
2019-11-22 08:27:47 +00:00
|
|
|
fn is_cfg_enabled(&self, attrs: &Attrs) -> bool {
|
2020-04-11 15:18:42 +00:00
|
|
|
attrs.is_cfg_enabled(self.def_collector.cfg_options)
|
2019-09-29 22:52:15 +00:00
|
|
|
}
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 11:12:36 +00:00
|
|
|
fn is_macro_rules(path: &ModPath) -> bool {
|
2019-12-13 21:01:06 +00:00
|
|
|
path.as_ident() == Some(&name![macro_rules])
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
2019-03-13 13:38:02 +00:00
|
|
|
|
2019-11-03 17:53:17 +00:00
|
|
|
#[cfg(test)]
|
2019-04-22 07:33:55 +00:00
|
|
|
mod tests {
|
2019-12-08 12:33:42 +00:00
|
|
|
use crate::{db::DefDatabase, test_db::TestDB};
|
2019-07-04 20:05:17 +00:00
|
|
|
use ra_arena::Arena;
|
2019-11-03 17:53:17 +00:00
|
|
|
use ra_db::{fixture::WithFixture, SourceDatabase};
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
2020-03-13 15:05:46 +00:00
|
|
|
fn do_collect_defs(db: &dyn DefDatabase, def_map: CrateDefMap) -> CrateDefMap {
|
2019-04-22 07:33:55 +00:00
|
|
|
let mut collector = DefCollector {
|
|
|
|
db,
|
|
|
|
def_map,
|
|
|
|
glob_imports: FxHashMap::default(),
|
|
|
|
unresolved_imports: Vec::new(),
|
2019-12-07 11:20:41 +00:00
|
|
|
resolved_imports: Vec::new(),
|
2019-04-22 07:33:55 +00:00
|
|
|
unexpanded_macros: Vec::new(),
|
2019-12-05 14:10:33 +00:00
|
|
|
unexpanded_attribute_macros: Vec::new(),
|
2019-10-10 11:45:05 +00:00
|
|
|
mod_dirs: FxHashMap::default(),
|
2019-09-29 22:52:15 +00:00
|
|
|
cfg_options: &CfgOptions::default(),
|
2020-03-25 12:14:22 +00:00
|
|
|
proc_macros: Default::default(),
|
2020-06-26 01:34:39 +00:00
|
|
|
from_glob_import: Default::default(),
|
2019-04-22 07:33:55 +00:00
|
|
|
};
|
|
|
|
collector.collect();
|
2019-12-08 12:33:42 +00:00
|
|
|
collector.def_map
|
2019-04-22 07:33:55 +00:00
|
|
|
}
|
|
|
|
|
2019-12-08 12:33:42 +00:00
|
|
|
fn do_resolve(code: &str) -> CrateDefMap {
|
2019-11-03 17:53:17 +00:00
|
|
|
let (db, _file_id) = TestDB::with_single_file(&code);
|
2019-11-15 10:16:16 +00:00
|
|
|
let krate = db.test_crate();
|
2019-04-22 07:33:55 +00:00
|
|
|
|
|
|
|
let def_map = {
|
2020-03-09 10:11:59 +00:00
|
|
|
let edition = db.crate_graph()[krate].edition;
|
2020-03-19 15:00:11 +00:00
|
|
|
let mut modules: Arena<ModuleData> = Arena::default();
|
2019-04-22 07:33:55 +00:00
|
|
|
let root = modules.alloc(ModuleData::default());
|
|
|
|
CrateDefMap {
|
|
|
|
krate,
|
|
|
|
edition,
|
|
|
|
extern_prelude: FxHashMap::default(),
|
|
|
|
prelude: None,
|
|
|
|
root,
|
|
|
|
modules,
|
|
|
|
diagnostics: Vec::new(),
|
|
|
|
}
|
|
|
|
};
|
2019-12-08 12:33:42 +00:00
|
|
|
do_collect_defs(&db, def_map)
|
2019-04-22 07:33:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-03-13 12:57:44 +00:00
|
|
|
fn test_macro_expand_will_stop_1() {
|
|
|
|
do_resolve(
|
|
|
|
r#"
|
|
|
|
macro_rules! foo {
|
|
|
|
($($ty:ty)*) => { foo!($($ty)*); }
|
|
|
|
}
|
|
|
|
foo!(KABOOM);
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[ignore] // this test does succeed, but takes quite a while :/
|
|
|
|
#[test]
|
|
|
|
fn test_macro_expand_will_stop_2() {
|
2019-12-08 12:33:42 +00:00
|
|
|
do_resolve(
|
2019-04-22 07:33:55 +00:00
|
|
|
r#"
|
|
|
|
macro_rules! foo {
|
2020-03-13 12:57:44 +00:00
|
|
|
($($ty:ty)*) => { foo!($($ty)* $($ty)*); }
|
2019-04-22 07:33:55 +00:00
|
|
|
}
|
2020-03-13 12:57:44 +00:00
|
|
|
foo!(KABOOM);
|
2019-04-22 07:33:55 +00:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|