2019-09-05 17:27:10 +00:00
|
|
|
use ra_db::FileId;
|
|
|
|
use ra_syntax::ast;
|
2019-07-04 20:05:17 +00:00
|
|
|
use rustc_hash::FxHashMap;
|
|
|
|
use test_utils::tested_by;
|
2019-03-02 20:59:04 +00:00
|
|
|
|
|
|
|
use crate::{
|
2019-09-08 06:53:49 +00:00
|
|
|
db::DefDatabase,
|
2019-07-04 20:05:17 +00:00
|
|
|
either::Either,
|
|
|
|
ids::{AstItemDef, LocationCtx, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind},
|
2019-07-07 21:29:38 +00:00
|
|
|
name::MACRO_RULES,
|
2019-03-23 17:41:59 +00:00
|
|
|
nameres::{
|
2019-09-05 17:27:10 +00:00
|
|
|
diagnostics::DefDiagnostic,
|
|
|
|
mod_resolution::{resolve_submodule, ParentModule},
|
|
|
|
raw, CrateDefMap, CrateModuleId, ItemOrMacro, ModuleData, ModuleDef, PerNs,
|
|
|
|
ReachedFixedPoint, Resolution, ResolveMode,
|
2019-03-23 17:41:59 +00:00
|
|
|
},
|
2019-09-08 06:53:49 +00:00
|
|
|
AstId, Const, Enum, Function, HirFileId, MacroDef, Module, Name, Path, Static, Struct, Trait,
|
|
|
|
TypeAlias, Union,
|
2019-03-02 20:59:04 +00:00
|
|
|
};
|
|
|
|
|
2019-06-26 18:50:42 +00:00
|
|
|
pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap {
|
2019-03-13 13:04:28 +00:00
|
|
|
// populate external prelude
|
2019-03-13 13:38:02 +00:00
|
|
|
for dep in def_map.krate.dependencies(db) {
|
2019-03-13 13:04:28 +00:00
|
|
|
log::debug!("crate dep {:?} -> {:?}", dep.name, dep.krate);
|
|
|
|
if let Some(module) = dep.krate.root_module(db) {
|
|
|
|
def_map.extern_prelude.insert(dep.name.clone(), module.into());
|
|
|
|
}
|
|
|
|
// look for the prelude
|
|
|
|
if def_map.prelude.is_none() {
|
2019-03-14 09:54:03 +00:00
|
|
|
let map = db.crate_def_map(dep.krate);
|
|
|
|
if map.prelude.is_some() {
|
|
|
|
def_map.prelude = map.prelude;
|
2019-03-13 13:04:28 +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(),
|
|
|
|
unexpanded_macros: Vec::new(),
|
2019-04-22 09:15:22 +00:00
|
|
|
macro_stack_monitor: MacroStackMonitor::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-04-22 07:33:55 +00:00
|
|
|
#[derive(Default)]
|
2019-04-22 09:15:22 +00:00
|
|
|
struct MacroStackMonitor {
|
2019-04-22 07:33:55 +00:00
|
|
|
counts: FxHashMap<MacroDefId, u32>,
|
2019-04-22 09:15:22 +00:00
|
|
|
|
|
|
|
/// Mainly use for test
|
|
|
|
validator: Option<Box<dyn Fn(u32) -> bool>>,
|
2019-04-22 07:33:55 +00:00
|
|
|
}
|
|
|
|
|
2019-04-22 09:15:22 +00:00
|
|
|
impl MacroStackMonitor {
|
2019-04-22 07:33:55 +00:00
|
|
|
fn increase(&mut self, macro_def_id: MacroDefId) {
|
|
|
|
*self.counts.entry(macro_def_id).or_default() += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn decrease(&mut self, macro_def_id: MacroDefId) {
|
|
|
|
*self.counts.entry(macro_def_id).or_default() -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_poison(&self, macro_def_id: MacroDefId) -> bool {
|
2019-04-22 09:15:22 +00:00
|
|
|
let cur = *self.counts.get(¯o_def_id).unwrap_or(&0);
|
|
|
|
|
|
|
|
if let Some(validator) = &self.validator {
|
|
|
|
validator(cur)
|
|
|
|
} else {
|
|
|
|
cur > 100
|
|
|
|
}
|
2019-04-22 07:33:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 20:59:04 +00:00
|
|
|
/// Walks the tree of module recursively
|
2019-04-22 09:15:22 +00:00
|
|
|
struct DefCollector<DB> {
|
2019-03-02 20:59:04 +00:00
|
|
|
db: DB,
|
|
|
|
def_map: CrateDefMap,
|
2019-03-16 15:57:53 +00:00
|
|
|
glob_imports: FxHashMap<CrateModuleId, Vec<(CrateModuleId, raw::ImportId)>>,
|
|
|
|
unresolved_imports: Vec<(CrateModuleId, raw::ImportId, raw::ImportData)>,
|
2019-03-26 15:03:17 +00:00
|
|
|
unexpanded_macros: Vec<(CrateModuleId, AstId<ast::MacroCall>, Path)>,
|
2019-04-20 15:05:25 +00:00
|
|
|
|
|
|
|
/// Some macro use `$tt:tt which mean we have to handle the macro perfectly
|
2019-07-13 18:26:04 +00:00
|
|
|
/// To prevent stack overflow, we add a deep counter here for prevent that.
|
2019-04-22 09:15:22 +00:00
|
|
|
macro_stack_monitor: MacroStackMonitor,
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2019-04-22 09:15:22 +00:00
|
|
|
impl<'a, DB> DefCollector<&'a DB>
|
2019-03-02 20:59:04 +00:00
|
|
|
where
|
2019-06-26 18:50:42 +00:00
|
|
|
DB: DefDatabase,
|
2019-03-02 20:59:04 +00:00
|
|
|
{
|
|
|
|
fn collect(&mut self) {
|
|
|
|
let crate_graph = self.db.crate_graph();
|
2019-03-13 13:38:02 +00:00
|
|
|
let file_id = crate_graph.crate_root(self.def_map.krate.crate_id());
|
2019-03-26 10:09:39 +00:00
|
|
|
let raw_items = self.db.raw_items(file_id.into());
|
2019-03-02 20:59:04 +00:00
|
|
|
let module_id = self.def_map.root;
|
2019-03-13 13:38:02 +00:00
|
|
|
self.def_map.modules[module_id].definition = Some(file_id);
|
2019-03-02 20:59:04 +00:00
|
|
|
ModCollector {
|
|
|
|
def_collector: &mut *self,
|
|
|
|
module_id,
|
|
|
|
file_id: file_id.into(),
|
|
|
|
raw_items: &raw_items,
|
2019-07-29 12:16:58 +00:00
|
|
|
parent_module: None,
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
2019-07-29 12:16:58 +00:00
|
|
|
.collect(raw_items.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-03-02 20:59:04 +00:00
|
|
|
match (self.resolve_imports(), self.resolve_macros()) {
|
|
|
|
(ReachedFixedPoint::Yes, ReachedFixedPoint::Yes) => break,
|
|
|
|
_ => i += 1,
|
|
|
|
}
|
|
|
|
if i == 1000 {
|
|
|
|
log::error!("diverging name resolution");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
for (module_id, import, import_data) in unresolved_imports {
|
2019-06-10 22:06:11 +00:00
|
|
|
self.record_resolved_import(module_id, Either::A(PerNs::none()), import, &import_data)
|
2019-03-14 08:53:40 +00:00
|
|
|
}
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-08 17:42:02 +00:00
|
|
|
fn define_macro(
|
|
|
|
&mut self,
|
|
|
|
module_id: CrateModuleId,
|
|
|
|
name: Name,
|
|
|
|
macro_id: MacroDefId,
|
|
|
|
export: bool,
|
|
|
|
) {
|
2019-06-10 22:06:11 +00:00
|
|
|
let def = Either::B(MacroDef { id: macro_id });
|
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-06-08 17:42:02 +00:00
|
|
|
self.update(self.def_map.root, None, &[(name.clone(), def.clone())]);
|
2019-09-02 06:36:20 +00:00
|
|
|
|
|
|
|
// Exported macros are collected in crate level ready for
|
|
|
|
// glob import with `#[macro_use]`.
|
|
|
|
self.def_map.exported_macros.insert(name.clone(), macro_id);
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
2019-06-08 17:42:02 +00:00
|
|
|
self.update(module_id, None, &[(name.clone(), def)]);
|
2019-09-06 16:55:58 +00:00
|
|
|
self.define_textual_macro(module_id, name.clone(), macro_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Define a macro in current textual scope.
|
|
|
|
///
|
|
|
|
/// We use a map `textual_macros` to store all textual macros visable per module.
|
|
|
|
/// It will clone all macros from parent textual scope, whose definition is prior to
|
|
|
|
/// the definition of current module.
|
|
|
|
/// And also, `macro_use` on a module will import all textual macros visable inside to
|
|
|
|
/// current textual scope, with possible shadowing.
|
|
|
|
///
|
|
|
|
/// In a single module, the order of definition/usage of textual scoped macros matters.
|
|
|
|
/// But we ignore it here to make it easy to implement.
|
|
|
|
fn define_textual_macro(&mut self, module_id: CrateModuleId, name: Name, macro_id: MacroDefId) {
|
|
|
|
// Always shadowing
|
|
|
|
self.def_map.modules[module_id]
|
|
|
|
.scope
|
|
|
|
.textual_macros
|
|
|
|
.insert(name, MacroDef { id: macro_id });
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2019-09-05 03:35:13 +00:00
|
|
|
/// Import macros from `#[macro_use] extern crate`.
|
|
|
|
///
|
|
|
|
/// They are non-scoped, and will only be inserted into mutable `global_macro_scope`.
|
2019-09-06 16:55:58 +00:00
|
|
|
fn import_macros_from_extern_crate(
|
|
|
|
&mut self,
|
|
|
|
current_module_id: CrateModuleId,
|
|
|
|
import: &raw::ImportData,
|
|
|
|
) {
|
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"),
|
|
|
|
);
|
|
|
|
|
|
|
|
if let Some(ModuleDef::Module(m)) = res.take_types() {
|
|
|
|
tested_by!(macro_rules_from_other_crates_are_visible_with_macro_use);
|
2019-09-06 16:55:58 +00:00
|
|
|
self.import_all_macros_exported(current_module_id, m);
|
2019-09-05 08:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-05 03:35:13 +00:00
|
|
|
|
2019-09-06 16:55:58 +00:00
|
|
|
fn import_all_macros_exported(&mut self, current_module_id: CrateModuleId, module: Module) {
|
2019-09-05 08:20:36 +00:00
|
|
|
let item_map = self.db.crate_def_map(module.krate);
|
|
|
|
for (name, ¯o_id) in &item_map.exported_macros {
|
2019-09-06 16:55:58 +00:00
|
|
|
self.define_textual_macro(current_module_id, name.clone(), macro_id);
|
2019-09-05 03:35:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 20:59:04 +00:00
|
|
|
fn resolve_imports(&mut self) -> ReachedFixedPoint {
|
2019-03-13 13:04:28 +00:00
|
|
|
let mut imports = std::mem::replace(&mut self.unresolved_imports, Vec::new());
|
|
|
|
let mut resolved = Vec::new();
|
|
|
|
imports.retain(|(module_id, import, import_data)| {
|
|
|
|
let (def, fp) = self.resolve_import(*module_id, import_data);
|
|
|
|
if fp == ReachedFixedPoint::Yes {
|
|
|
|
resolved.push((*module_id, def, *import, import_data.clone()))
|
|
|
|
}
|
|
|
|
fp == ReachedFixedPoint::No
|
|
|
|
});
|
|
|
|
self.unresolved_imports = imports;
|
2019-03-02 20:59:04 +00:00
|
|
|
// Resolves imports, filling-in module scopes
|
2019-03-13 13:04:28 +00:00
|
|
|
let result =
|
|
|
|
if resolved.is_empty() { ReachedFixedPoint::Yes } else { ReachedFixedPoint::No };
|
|
|
|
for (module_id, def, import, import_data) in resolved {
|
|
|
|
self.record_resolved_import(module_id, def, import, &import_data)
|
|
|
|
}
|
|
|
|
result
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 13:04:28 +00:00
|
|
|
fn resolve_import(
|
2019-03-19 15:35:03 +00:00
|
|
|
&self,
|
2019-03-16 15:57:53 +00:00
|
|
|
module_id: CrateModuleId,
|
2019-03-13 13:04:28 +00:00
|
|
|
import: &raw::ImportData,
|
2019-05-26 12:10:56 +00:00
|
|
|
) -> (ItemOrMacro, ReachedFixedPoint) {
|
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-06-10 22:06:11 +00:00
|
|
|
(Either::A(res), ReachedFixedPoint::Yes)
|
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-03-13 13:04:28 +00:00
|
|
|
|
|
|
|
(res.resolved_def, res.reached_fixedpoint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn record_resolved_import(
|
|
|
|
&mut self,
|
2019-03-16 15:57:53 +00:00
|
|
|
module_id: CrateModuleId,
|
2019-05-26 12:10:56 +00:00
|
|
|
def: ItemOrMacro,
|
2019-03-13 13:04:28 +00:00
|
|
|
import_id: raw::ImportId,
|
|
|
|
import: &raw::ImportData,
|
|
|
|
) {
|
|
|
|
if import.is_glob {
|
|
|
|
log::debug!("glob import: {:?}", import);
|
2019-06-10 22:06:11 +00:00
|
|
|
match def.a().and_then(|item| item.take_types()) {
|
2019-03-13 13:04:28 +00:00
|
|
|
Some(ModuleDef::Module(m)) => {
|
|
|
|
if import.is_prelude {
|
|
|
|
tested_by!(std_prelude);
|
|
|
|
self.def_map.prelude = Some(m);
|
2019-03-13 13:38:02 +00:00
|
|
|
} else if m.krate != self.def_map.krate {
|
2019-03-13 13:04:28 +00:00
|
|
|
tested_by!(glob_across_crates);
|
|
|
|
// glob import from other crate => we can just import everything once
|
2019-03-14 09:54:03 +00:00
|
|
|
let item_map = self.db.crate_def_map(m.krate);
|
|
|
|
let scope = &item_map[m.module_id].scope;
|
2019-03-13 13:04:28 +00:00
|
|
|
let items = scope
|
|
|
|
.items
|
|
|
|
.iter()
|
2019-06-10 22:06:11 +00:00
|
|
|
.map(|(name, res)| (name.clone(), Either::A(res.clone())));
|
2019-07-04 17:26:44 +00:00
|
|
|
let macros =
|
|
|
|
scope.macros.iter().map(|(name, res)| (name.clone(), Either::B(*res)));
|
2019-05-26 12:10:56 +00:00
|
|
|
|
|
|
|
let all = items.chain(macros).collect::<Vec<_>>();
|
|
|
|
self.update(module_id, Some(import_id), &all);
|
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-03-13 13:38:02 +00:00
|
|
|
let scope = &self.def_map[m.module_id].scope;
|
2019-03-13 13:04:28 +00:00
|
|
|
let items = scope
|
|
|
|
.items
|
|
|
|
.iter()
|
2019-06-10 22:06:11 +00:00
|
|
|
.map(|(name, res)| (name.clone(), Either::A(res.clone())));
|
2019-07-04 17:26:44 +00:00
|
|
|
let macros =
|
|
|
|
scope.macros.iter().map(|(name, res)| (name.clone(), Either::B(*res)));
|
2019-05-26 12:10:56 +00:00
|
|
|
|
|
|
|
let all = items.chain(macros).collect::<Vec<_>>();
|
|
|
|
|
|
|
|
self.update(module_id, Some(import_id), &all);
|
2019-03-13 13:04:28 +00:00
|
|
|
// record the glob import in case we add further items
|
|
|
|
self.glob_imports
|
|
|
|
.entry(m.module_id)
|
|
|
|
.or_default()
|
|
|
|
.push((module_id, import_id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(ModuleDef::Enum(e)) => {
|
|
|
|
tested_by!(glob_enum);
|
|
|
|
// glob import from enum => just import all the variants
|
|
|
|
let variants = e.variants(self.db);
|
|
|
|
let resolutions = variants
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|variant| {
|
|
|
|
let res = Resolution {
|
|
|
|
def: PerNs::both(variant.into(), variant.into()),
|
|
|
|
import: Some(import_id),
|
|
|
|
};
|
|
|
|
let name = variant.name(self.db)?;
|
2019-06-10 22:06:11 +00:00
|
|
|
Some((name, Either::A(res)))
|
2019-03-13 13:04:28 +00:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
self.update(module_id, Some(import_id), &resolutions);
|
|
|
|
}
|
|
|
|
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) => {
|
|
|
|
let name = import.alias.clone().unwrap_or_else(|| last_segment.name.clone());
|
|
|
|
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-06-10 22:06:11 +00:00
|
|
|
if let Some(def) = def.a().and_then(|item| item.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
|
|
|
|
|
|
|
let resolution = match def {
|
2019-06-10 22:06:11 +00:00
|
|
|
Either::A(item) => {
|
|
|
|
Either::A(Resolution { def: item, import: Some(import_id) })
|
2019-05-26 12:10:56 +00:00
|
|
|
}
|
2019-06-10 22:06:11 +00:00
|
|
|
Either::B(macro_) => Either::B(macro_),
|
2019-05-26 12:10:56 +00:00
|
|
|
};
|
|
|
|
|
2019-03-16 15:06:45 +00:00
|
|
|
self.update(module_id, Some(import_id), &[(name, resolution)]);
|
2019-03-13 13:04:28 +00:00
|
|
|
}
|
2019-03-16 15:06:45 +00:00
|
|
|
None => tested_by!(bogus_paths),
|
2019-03-13 13:04:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update(
|
|
|
|
&mut self,
|
2019-03-16 15:57:53 +00:00
|
|
|
module_id: CrateModuleId,
|
2019-03-13 13:04:28 +00:00
|
|
|
import: Option<raw::ImportId>,
|
2019-05-26 12:10:56 +00:00
|
|
|
resolutions: &[(Name, Either<Resolution, MacroDef>)],
|
2019-03-13 13:04:28 +00:00
|
|
|
) {
|
|
|
|
self.update_recursive(module_id, import, resolutions, 0)
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 13:04:28 +00:00
|
|
|
fn update_recursive(
|
|
|
|
&mut self,
|
2019-03-16 15:57:53 +00:00
|
|
|
module_id: CrateModuleId,
|
2019-03-13 13:04:28 +00:00
|
|
|
import: Option<raw::ImportId>,
|
2019-05-26 12:10:56 +00:00
|
|
|
resolutions: &[(Name, Either<Resolution, MacroDef>)],
|
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-03-13 13:04:28 +00:00
|
|
|
let module_items = &mut self.def_map.modules[module_id].scope;
|
|
|
|
let mut changed = false;
|
|
|
|
for (name, res) in resolutions {
|
2019-05-26 12:10:56 +00:00
|
|
|
match res {
|
|
|
|
// item
|
2019-06-10 22:06:11 +00:00
|
|
|
Either::A(res) => {
|
2019-05-26 12:10:56 +00:00
|
|
|
let existing = module_items.items.entry(name.clone()).or_default();
|
|
|
|
|
|
|
|
if existing.def.types.is_none() && res.def.types.is_some() {
|
|
|
|
existing.def.types = res.def.types;
|
|
|
|
existing.import = import.or(res.import);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
if existing.def.values.is_none() && res.def.values.is_some() {
|
|
|
|
existing.def.values = res.def.values;
|
|
|
|
existing.import = import.or(res.import);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if existing.def.is_none()
|
|
|
|
&& res.def.is_none()
|
|
|
|
&& existing.import.is_none()
|
|
|
|
&& res.import.is_some()
|
|
|
|
{
|
|
|
|
existing.import = res.import;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// macro
|
2019-06-10 22:06:11 +00:00
|
|
|
Either::B(res) => {
|
2019-05-26 12:10:56 +00:00
|
|
|
// Always shadowing
|
|
|
|
module_items.macros.insert(name.clone(), *res);
|
|
|
|
}
|
2019-03-14 10:14:54 +00:00
|
|
|
}
|
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<_>>();
|
|
|
|
for (glob_importing_module, glob_import) in glob_imports {
|
|
|
|
// We pass the glob import so that the tracked import in those modules is that glob import
|
|
|
|
self.update_recursive(glob_importing_module, Some(glob_import), resolutions, depth + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_macros(&mut self) -> ReachedFixedPoint {
|
|
|
|
let mut macros = std::mem::replace(&mut self.unexpanded_macros, Vec::new());
|
|
|
|
let mut resolved = Vec::new();
|
2019-03-16 16:40:41 +00:00
|
|
|
let mut res = ReachedFixedPoint::Yes;
|
2019-03-26 15:03:17 +00:00
|
|
|
macros.retain(|(module_id, ast_id, path)| {
|
2019-05-26 12:10:56 +00:00
|
|
|
let resolved_res = self.def_map.resolve_path_fp_with_macro(
|
|
|
|
self.db,
|
|
|
|
ResolveMode::Other,
|
|
|
|
*module_id,
|
|
|
|
path,
|
|
|
|
);
|
|
|
|
|
2019-06-10 22:06:11 +00:00
|
|
|
if let Some(def) = resolved_res.resolved_def.b() {
|
2019-05-26 12:10:56 +00:00
|
|
|
let call_id = MacroCallLoc { def: def.id, ast_id: *ast_id }.id(self.db);
|
|
|
|
resolved.push((*module_id, call_id, def.id));
|
|
|
|
res = ReachedFixedPoint::No;
|
|
|
|
return false;
|
2019-03-13 13:04:28 +00:00
|
|
|
}
|
2019-05-26 12:10:56 +00:00
|
|
|
|
|
|
|
if resolved_res.reached_fixedpoint != ReachedFixedPoint::Yes {
|
|
|
|
let crate_name = &path.segments[0].name;
|
|
|
|
|
|
|
|
// FIXME:
|
|
|
|
// $crate are not handled in resolver right now
|
|
|
|
if crate_name.to_string() == "$crate" {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME:
|
|
|
|
// Currently `#[cfg(test)]` are ignored and cargo-metadata do not insert
|
|
|
|
// dev-dependencies of dependencies. For example,
|
|
|
|
// if we depend on parking lot, and parking lot has a dev-dependency on lazy_static.
|
|
|
|
// Then `lazy_static` wil not included in `CrateGraph`
|
|
|
|
// We can fix that by proper handling `cfg(test)`.
|
|
|
|
//
|
|
|
|
// So right now we set the fixpoint to No only if its crate is in CrateGraph
|
|
|
|
// See issue #1282 for details
|
|
|
|
let krate =
|
|
|
|
match self.def_map.resolve_name_in_extern_prelude(crate_name).take_types() {
|
|
|
|
Some(ModuleDef::Module(m)) => m.krate(self.db),
|
|
|
|
_ => return true,
|
|
|
|
};
|
|
|
|
if krate.is_none() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = resolved_res.reached_fixedpoint;
|
2019-03-16 16:40:41 +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-04-22 07:33:55 +00:00
|
|
|
for (module_id, macro_call_id, macro_def_id) in resolved {
|
|
|
|
self.collect_macro_expansion(module_id, macro_call_id, macro_def_id);
|
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
|
|
|
}
|
|
|
|
|
2019-04-22 07:33:55 +00:00
|
|
|
fn collect_macro_expansion(
|
|
|
|
&mut self,
|
|
|
|
module_id: CrateModuleId,
|
|
|
|
macro_call_id: MacroCallId,
|
|
|
|
macro_def_id: MacroDefId,
|
|
|
|
) {
|
|
|
|
if self.def_map.poison_macros.contains(¯o_def_id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.macro_stack_monitor.increase(macro_def_id);
|
2019-04-20 15:05:25 +00:00
|
|
|
|
2019-04-22 07:33:55 +00:00
|
|
|
if !self.macro_stack_monitor.is_poison(macro_def_id) {
|
2019-05-13 22:12:07 +00:00
|
|
|
let file_id: HirFileId = macro_call_id.as_file(MacroFileKind::Items);
|
2019-04-20 15:05:25 +00:00
|
|
|
let raw_items = self.db.raw_items(file_id);
|
2019-07-29 12:16:58 +00:00
|
|
|
ModCollector {
|
|
|
|
def_collector: &mut *self,
|
|
|
|
file_id,
|
|
|
|
module_id,
|
|
|
|
raw_items: &raw_items,
|
|
|
|
parent_module: None,
|
|
|
|
}
|
|
|
|
.collect(raw_items.items());
|
2019-04-20 15:05:25 +00:00
|
|
|
} else {
|
2019-06-26 18:50:42 +00:00
|
|
|
log::error!("Too deep macro expansion: {:?}", macro_call_id);
|
2019-04-22 07:33:55 +00:00
|
|
|
self.def_map.poison_macros.insert(macro_def_id);
|
2019-04-20 15:05:25 +00:00
|
|
|
}
|
|
|
|
|
2019-04-22 07:33:55 +00:00
|
|
|
self.macro_stack_monitor.decrease(macro_def_id);
|
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
|
|
|
|
struct ModCollector<'a, D> {
|
|
|
|
def_collector: D,
|
2019-03-16 15:57:53 +00:00
|
|
|
module_id: CrateModuleId,
|
2019-03-13 13:38:02 +00:00
|
|
|
file_id: HirFileId,
|
|
|
|
raw_items: &'a raw::RawItems,
|
2019-08-03 16:44:59 +00:00
|
|
|
parent_module: Option<ParentModule<'a>>,
|
2019-03-13 13:38:02 +00:00
|
|
|
}
|
|
|
|
|
2019-04-22 09:15:22 +00:00
|
|
|
impl<DB> ModCollector<'_, &'_ mut DefCollector<&'_ DB>>
|
2019-03-02 20:59:04 +00:00
|
|
|
where
|
2019-06-26 18:50:42 +00:00
|
|
|
DB: DefDatabase,
|
2019-03-02 20:59:04 +00:00
|
|
|
{
|
2019-07-29 12:16:58 +00:00
|
|
|
fn collect(&mut self, items: &[raw::RawItem]) {
|
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-09-07 18:43:41 +00:00
|
|
|
if prelude_module.krate != self.def_collector.def_map.krate {
|
|
|
|
tested_by!(prelude_is_macro_use);
|
2019-09-06 16:55:58 +00:00
|
|
|
self.def_collector.import_all_macros_exported(self.module_id, prelude_module);
|
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 {
|
|
|
|
if let raw::RawItem::Import(import_id) = *item {
|
|
|
|
let import = self.raw_items[import_id].clone();
|
|
|
|
if import.is_extern_crate && import.is_macro_use {
|
2019-09-06 16:55:58 +00:00
|
|
|
self.def_collector.import_macros_from_extern_crate(self.module_id, &import);
|
2019-09-05 10:39:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 20:59:04 +00:00
|
|
|
for item in items {
|
|
|
|
match *item {
|
2019-07-29 12:16:58 +00:00
|
|
|
raw::RawItem::Module(m) => self.collect_module(&self.raw_items[m]),
|
2019-09-05 10:39:56 +00:00
|
|
|
raw::RawItem::Import(import_id) => self.def_collector.unresolved_imports.push((
|
|
|
|
self.module_id,
|
|
|
|
import_id,
|
|
|
|
self.raw_items[import_id].clone(),
|
|
|
|
)),
|
2019-03-02 20:59:04 +00:00
|
|
|
raw::RawItem::Def(def) => self.define_def(&self.raw_items[def]),
|
|
|
|
raw::RawItem::Macro(mac) => self.collect_macro(&self.raw_items[mac]),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 12:16:58 +00:00
|
|
|
fn collect_module(&mut self, module: &raw::ModuleData) {
|
2019-03-02 20:59:04 +00:00
|
|
|
match module {
|
2019-07-13 18:51:20 +00:00
|
|
|
// inline module, just recurse
|
2019-09-06 16:55:58 +00:00
|
|
|
raw::ModuleData::Definition { name, items, ast_id, attr_path, is_macro_use } => {
|
2019-03-26 14:25:14 +00:00
|
|
|
let module_id =
|
|
|
|
self.push_child_module(name.clone(), ast_id.with_file_id(self.file_id), None);
|
2019-08-03 16:44:59 +00:00
|
|
|
let parent_module = ParentModule { name, attr_path: attr_path.as_ref() };
|
2019-09-06 16:55:58 +00:00
|
|
|
|
2019-03-02 20:59:04 +00:00
|
|
|
ModCollector {
|
|
|
|
def_collector: &mut *self.def_collector,
|
|
|
|
module_id,
|
|
|
|
file_id: self.file_id,
|
|
|
|
raw_items: self.raw_items,
|
2019-08-03 16:44:59 +00:00
|
|
|
parent_module: Some(parent_module),
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
2019-07-29 12:16:58 +00:00
|
|
|
.collect(&*items);
|
2019-09-06 16:55:58 +00:00
|
|
|
if *is_macro_use {
|
|
|
|
self.import_all_textual_macros(module_id);
|
|
|
|
}
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
2019-07-13 18:51:20 +00:00
|
|
|
// out of line module, resolve, parse and recurse
|
2019-09-06 16:55:58 +00:00
|
|
|
raw::ModuleData::Declaration { name, ast_id, attr_path, is_macro_use } => {
|
2019-03-26 14:25:14 +00:00
|
|
|
let ast_id = ast_id.with_file_id(self.file_id);
|
2019-03-02 20:59:04 +00:00
|
|
|
let is_root = self.def_collector.def_map.modules[self.module_id].parent.is_none();
|
2019-07-06 18:54:21 +00:00
|
|
|
match resolve_submodule(
|
|
|
|
self.def_collector.db,
|
|
|
|
self.file_id,
|
|
|
|
name,
|
|
|
|
is_root,
|
|
|
|
attr_path.as_ref(),
|
2019-09-05 17:27:10 +00:00
|
|
|
self.parent_module,
|
2019-07-06 18:54:21 +00:00
|
|
|
) {
|
2019-03-23 15:35:14 +00:00
|
|
|
Ok(file_id) => {
|
2019-03-26 14:25:14 +00:00
|
|
|
let module_id = self.push_child_module(name.clone(), ast_id, Some(file_id));
|
2019-03-26 10:09:39 +00:00
|
|
|
let raw_items = self.def_collector.db.raw_items(file_id.into());
|
2019-03-23 15:35:14 +00:00
|
|
|
ModCollector {
|
|
|
|
def_collector: &mut *self.def_collector,
|
|
|
|
module_id,
|
|
|
|
file_id: file_id.into(),
|
|
|
|
raw_items: &raw_items,
|
2019-07-29 12:16:58 +00:00
|
|
|
parent_module: None,
|
2019-03-23 15:35:14 +00:00
|
|
|
}
|
2019-09-06 16:55:58 +00:00
|
|
|
.collect(raw_items.items());
|
|
|
|
if *is_macro_use {
|
|
|
|
self.import_all_textual_macros(module_id);
|
|
|
|
}
|
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>,
|
2019-03-13 13:38:02 +00:00
|
|
|
definition: Option<FileId>,
|
2019-03-16 15:57:53 +00:00
|
|
|
) -> CrateModuleId {
|
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);
|
|
|
|
modules[res].declaration = Some(declaration);
|
|
|
|
modules[res].definition = definition;
|
2019-09-06 16:55:58 +00:00
|
|
|
modules[res].scope.textual_macros = modules[self.module_id].scope.textual_macros.clone();
|
2019-03-14 08:53:40 +00:00
|
|
|
modules[self.module_id].children.insert(name.clone(), res);
|
|
|
|
let resolution = Resolution {
|
|
|
|
def: PerNs::types(
|
|
|
|
Module { krate: self.def_collector.def_map.krate, module_id: res }.into(),
|
|
|
|
),
|
|
|
|
import: None,
|
|
|
|
};
|
2019-06-10 22:06:11 +00:00
|
|
|
self.def_collector.update(self.module_id, None, &[(name, Either::A(resolution))]);
|
2019-03-02 20:59:04 +00:00
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
fn define_def(&mut self, def: &raw::DefData) {
|
2019-03-13 13:38:02 +00:00
|
|
|
let module = Module { krate: self.def_collector.def_map.krate, module_id: self.module_id };
|
2019-06-03 14:21:08 +00:00
|
|
|
let ctx = LocationCtx::new(self.def_collector.db, module, self.file_id);
|
2019-03-26 15:27:22 +00:00
|
|
|
|
|
|
|
macro_rules! def {
|
|
|
|
($kind:ident, $ast_id:ident) => {
|
|
|
|
$kind { id: AstItemDef::from_ast_id(ctx, $ast_id) }.into()
|
2019-03-02 20:59:04 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
let name = def.name.clone();
|
|
|
|
let def: PerNs<ModuleDef> = match def.kind {
|
2019-03-26 15:27:22 +00:00
|
|
|
raw::DefKind::Function(ast_id) => PerNs::values(def!(Function, ast_id)),
|
|
|
|
raw::DefKind::Struct(ast_id) => {
|
|
|
|
let s = def!(Struct, ast_id);
|
2019-03-02 20:59:04 +00:00
|
|
|
PerNs::both(s, s)
|
|
|
|
}
|
2019-05-23 17:18:47 +00:00
|
|
|
raw::DefKind::Union(ast_id) => {
|
|
|
|
let s = def!(Union, ast_id);
|
|
|
|
PerNs::both(s, s)
|
|
|
|
}
|
2019-03-26 15:27:22 +00:00
|
|
|
raw::DefKind::Enum(ast_id) => PerNs::types(def!(Enum, ast_id)),
|
|
|
|
raw::DefKind::Const(ast_id) => PerNs::values(def!(Const, ast_id)),
|
|
|
|
raw::DefKind::Static(ast_id) => PerNs::values(def!(Static, ast_id)),
|
|
|
|
raw::DefKind::Trait(ast_id) => PerNs::types(def!(Trait, ast_id)),
|
|
|
|
raw::DefKind::TypeAlias(ast_id) => PerNs::types(def!(TypeAlias, ast_id)),
|
2019-03-02 20:59:04 +00:00
|
|
|
};
|
|
|
|
let resolution = Resolution { def, import: None };
|
2019-06-10 22:06:11 +00:00
|
|
|
self.def_collector.update(self.module_id, None, &[(name, Either::A(resolution))])
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn collect_macro(&mut self, mac: &raw::MacroData) {
|
|
|
|
// 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-03-26 15:03:17 +00:00
|
|
|
let macro_id = MacroDefId(mac.ast_id.with_file_id(self.file_id));
|
2019-06-08 17:42:02 +00:00
|
|
|
self.def_collector.define_macro(self.module_id, name.clone(), macro_id, mac.export)
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-26 15:03:17 +00:00
|
|
|
let ast_id = mac.ast_id.with_file_id(self.file_id);
|
2019-03-02 20:59:04 +00:00
|
|
|
|
2019-09-06 16:55:58 +00:00
|
|
|
// Case 2: try to resolve in textual scope and expand macro_rules, triggering
|
2019-03-02 20:59:04 +00:00
|
|
|
// recursive item collection.
|
2019-09-06 16:55:58 +00:00
|
|
|
if let Some(macro_def) = mac.path.as_ident().and_then(|name| {
|
|
|
|
self.def_collector.def_map[self.module_id].scope.get_textual_macro(&name)
|
|
|
|
}) {
|
|
|
|
let def = macro_def.id;
|
2019-04-29 12:03:51 +00:00
|
|
|
let macro_call_id = MacroCallLoc { def, ast_id }.id(self.def_collector.db);
|
2019-03-26 10:09:39 +00:00
|
|
|
|
2019-04-29 12:03:51 +00:00
|
|
|
self.def_collector.collect_macro_expansion(self.module_id, macro_call_id, def);
|
2019-03-02 20:59:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case 3: path to a macro from another crate, expand during name resolution
|
2019-03-26 15:03:17 +00:00
|
|
|
self.def_collector.unexpanded_macros.push((self.module_id, ast_id, mac.path.clone()))
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
2019-09-06 16:55:58 +00:00
|
|
|
|
|
|
|
fn import_all_textual_macros(&mut self, module_id: CrateModuleId) {
|
|
|
|
let macros = self.def_collector.def_map[module_id].scope.textual_macros.clone();
|
|
|
|
for (name, macro_) in macros {
|
|
|
|
self.def_collector.define_textual_macro(self.module_id, name.clone(), macro_.id);
|
|
|
|
}
|
|
|
|
}
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_macro_rules(path: &Path) -> bool {
|
2019-07-07 21:29:38 +00:00
|
|
|
path.as_ident() == Some(&MACRO_RULES)
|
2019-03-02 20:59:04 +00:00
|
|
|
}
|
2019-03-13 13:38:02 +00:00
|
|
|
|
2019-04-22 07:33:55 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use ra_db::SourceDatabase;
|
|
|
|
|
|
|
|
use super::*;
|
2019-09-08 06:53:49 +00:00
|
|
|
use crate::{db::DefDatabase, mock::MockDatabase, Crate};
|
2019-07-04 20:05:17 +00:00
|
|
|
use ra_arena::Arena;
|
2019-04-22 07:33:55 +00:00
|
|
|
use rustc_hash::FxHashSet;
|
|
|
|
|
|
|
|
fn do_collect_defs(
|
2019-06-26 18:50:42 +00:00
|
|
|
db: &impl DefDatabase,
|
2019-04-22 07:33:55 +00:00
|
|
|
def_map: CrateDefMap,
|
2019-04-22 09:15:22 +00:00
|
|
|
monitor: MacroStackMonitor,
|
2019-04-22 07:33:55 +00:00
|
|
|
) -> CrateDefMap {
|
|
|
|
let mut collector = DefCollector {
|
|
|
|
db,
|
|
|
|
def_map,
|
|
|
|
glob_imports: FxHashMap::default(),
|
|
|
|
unresolved_imports: Vec::new(),
|
|
|
|
unexpanded_macros: Vec::new(),
|
|
|
|
macro_stack_monitor: monitor,
|
|
|
|
};
|
|
|
|
collector.collect();
|
|
|
|
collector.finish()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn do_limited_resolve(code: &str, limit: u32, poison_limit: u32) -> CrateDefMap {
|
|
|
|
let (db, _source_root, _) = MockDatabase::with_single_file(&code);
|
|
|
|
let crate_id = db.crate_graph().iter().next().unwrap();
|
|
|
|
let krate = Crate { crate_id };
|
|
|
|
|
|
|
|
let def_map = {
|
|
|
|
let edition = krate.edition(&db);
|
|
|
|
let mut modules: Arena<CrateModuleId, ModuleData> = Arena::default();
|
|
|
|
let root = modules.alloc(ModuleData::default());
|
|
|
|
CrateDefMap {
|
|
|
|
krate,
|
|
|
|
edition,
|
|
|
|
extern_prelude: FxHashMap::default(),
|
|
|
|
prelude: None,
|
|
|
|
root,
|
|
|
|
modules,
|
|
|
|
poison_macros: FxHashSet::default(),
|
2019-09-02 06:36:20 +00:00
|
|
|
exported_macros: FxHashMap::default(),
|
2019-04-22 07:33:55 +00:00
|
|
|
diagnostics: Vec::new(),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-22 09:15:22 +00:00
|
|
|
let mut monitor = MacroStackMonitor::default();
|
|
|
|
monitor.validator = Some(Box::new(move |count| {
|
|
|
|
assert!(count < limit);
|
|
|
|
count >= poison_limit
|
|
|
|
}));
|
|
|
|
|
|
|
|
do_collect_defs(&db, def_map, monitor)
|
2019-04-22 07:33:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_macro_expand_limit_width() {
|
|
|
|
do_limited_resolve(
|
|
|
|
r#"
|
|
|
|
macro_rules! foo {
|
|
|
|
($($ty:ty)*) => { foo!($($ty)*, $($ty)*); }
|
|
|
|
}
|
|
|
|
foo!(KABOOM);
|
|
|
|
"#,
|
|
|
|
16,
|
|
|
|
1000,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_macro_expand_poisoned() {
|
|
|
|
let def = do_limited_resolve(
|
|
|
|
r#"
|
|
|
|
macro_rules! foo {
|
|
|
|
($ty:ty) => { foo!($ty); }
|
|
|
|
}
|
|
|
|
foo!(KABOOM);
|
|
|
|
"#,
|
|
|
|
100,
|
|
|
|
16,
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(def.poison_macros.len(), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_macro_expand_normal() {
|
|
|
|
let def = do_limited_resolve(
|
|
|
|
r#"
|
|
|
|
macro_rules! foo {
|
|
|
|
($ident:ident) => { struct $ident {} }
|
|
|
|
}
|
|
|
|
foo!(Bar);
|
|
|
|
"#,
|
|
|
|
16,
|
|
|
|
16,
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(def.poison_macros.len(), 0);
|
|
|
|
}
|
|
|
|
}
|