2019-11-03 14:49:44 -06:00
|
|
|
//! This module implements import-resolution/macro expansion algorithm.
|
|
|
|
//!
|
2021-09-05 05:16:02 -05:00
|
|
|
//! The result of this module is `DefMap`: a data structure which contains:
|
2019-11-03 14:49:44 -06:00
|
|
|
//!
|
|
|
|
//! * a tree of modules for the crate
|
|
|
|
//! * for each module, a set of items visible in the module (directly declared
|
|
|
|
//! or imported)
|
|
|
|
//!
|
2021-09-05 05:16:02 -05:00
|
|
|
//! Note that `DefMap` contains fully macro expanded code.
|
2019-11-03 14:49:44 -06:00
|
|
|
//!
|
2021-09-05 05:16:02 -05:00
|
|
|
//! Computing `DefMap` can be partitioned into several logically
|
2019-11-03 14:49:44 -06:00
|
|
|
//! independent "phases". The phases are mutually recursive though, there's no
|
|
|
|
//! strict ordering.
|
|
|
|
//!
|
|
|
|
//! ## Collecting RawItems
|
|
|
|
//!
|
2019-11-08 15:23:19 -06:00
|
|
|
//! This happens in the `raw` module, which parses a single source file into a
|
|
|
|
//! set of top-level items. Nested imports are desugared to flat imports in this
|
|
|
|
//! phase. Macro calls are represented as a triple of (Path, Option<Name>,
|
|
|
|
//! TokenTree).
|
2019-11-03 14:49:44 -06:00
|
|
|
//!
|
|
|
|
//! ## Collecting Modules
|
|
|
|
//!
|
|
|
|
//! This happens in the `collector` module. In this phase, we recursively walk
|
|
|
|
//! tree of modules, collect raw items from submodules, populate module scopes
|
|
|
|
//! with defined items (so, we assign item ids in this phase) and record the set
|
|
|
|
//! of unresolved imports and macros.
|
|
|
|
//!
|
|
|
|
//! While we walk tree of modules, we also record macro_rules definitions and
|
|
|
|
//! expand calls to macro_rules defined macros.
|
|
|
|
//!
|
|
|
|
//! ## Resolving Imports
|
|
|
|
//!
|
|
|
|
//! We maintain a list of currently unresolved imports. On every iteration, we
|
|
|
|
//! try to resolve some imports from this list. If the import is resolved, we
|
|
|
|
//! record it, by adding an item to current module scope and, if necessary, by
|
|
|
|
//! recursively populating glob imports.
|
|
|
|
//!
|
|
|
|
//! ## Resolving Macros
|
|
|
|
//!
|
|
|
|
//! macro_rules from the same crate use a global mutable namespace. We expand
|
|
|
|
//! them immediately, when we collect modules.
|
|
|
|
//!
|
|
|
|
//! Macros from other crates (including proc-macros) can be used with
|
|
|
|
//! `foo::bar!` syntax. We handle them similarly to imports. There's a list of
|
|
|
|
//! unexpanded macros. On every iteration, we try to resolve each macro call
|
2019-11-08 15:23:19 -06:00
|
|
|
//! path and, upon success, we run macro expansion and "collect module" phase on
|
|
|
|
//! the result
|
2019-10-30 09:40:13 -05:00
|
|
|
|
2022-01-06 05:30:16 -06:00
|
|
|
pub mod attr_resolution;
|
2022-07-26 04:53:50 -05:00
|
|
|
pub mod proc_macro;
|
2022-07-24 09:04:20 -05:00
|
|
|
pub mod diagnostics;
|
2022-07-26 04:53:50 -05:00
|
|
|
mod collector;
|
2019-11-04 12:42:25 -06:00
|
|
|
mod mod_resolution;
|
2019-11-08 15:17:17 -06:00
|
|
|
mod path_resolution;
|
2019-10-31 10:45:10 -05:00
|
|
|
|
2019-11-03 14:35:48 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2022-07-26 04:53:50 -05:00
|
|
|
use std::{cmp::Ord, ops::Deref, sync::Arc};
|
2019-10-31 10:45:10 -05:00
|
|
|
|
2020-08-13 09:25:38 -05:00
|
|
|
use base_db::{CrateId, Edition, FileId};
|
2022-07-26 04:53:50 -05:00
|
|
|
use hir_expand::{name::Name, InFile, MacroCallId, MacroDefId};
|
2022-07-24 09:04:20 -05:00
|
|
|
use itertools::Itertools;
|
2021-01-14 09:47:42 -06:00
|
|
|
use la_arena::Arena;
|
2021-01-21 10:04:50 -06:00
|
|
|
use profile::Count;
|
2022-09-06 13:20:49 -05:00
|
|
|
use rustc_hash::{FxHashMap, FxHashSet};
|
2020-03-28 05:08:19 -05:00
|
|
|
use stdx::format_to;
|
2022-01-06 05:30:16 -06:00
|
|
|
use syntax::{ast, SmolStr};
|
2019-10-31 10:45:10 -05:00
|
|
|
|
|
|
|
use crate::{
|
2019-11-23 05:44:43 -06:00
|
|
|
db::DefDatabase,
|
2019-12-20 08:45:12 -06:00
|
|
|
item_scope::{BuiltinShadowMode, ItemScope},
|
2022-07-21 09:05:52 -05:00
|
|
|
item_tree::{ItemTreeId, Mod, TreeId},
|
2019-11-23 07:53:16 -06:00
|
|
|
nameres::{diagnostics::DefDiagnostic, path_resolution::ResolveMode},
|
2019-12-13 05:12:36 -06:00
|
|
|
path::ModPath,
|
2019-11-23 07:53:16 -06:00
|
|
|
per_ns::PerNs,
|
2021-07-12 13:13:43 -05:00
|
|
|
visibility::Visibility,
|
2022-07-26 04:53:50 -05:00
|
|
|
AstId, BlockId, BlockLoc, FunctionId, LocalModuleId, MacroId, ModuleId, ProcMacroId,
|
2019-10-31 10:45:10 -05:00
|
|
|
};
|
|
|
|
|
2021-02-03 10:48:41 -06:00
|
|
|
/// Contains the results of (early) name resolution.
|
|
|
|
///
|
|
|
|
/// A `DefMap` stores the module tree and the definitions that are in scope in every module after
|
|
|
|
/// item-level macros have been expanded.
|
|
|
|
///
|
|
|
|
/// Every crate has a primary `DefMap` whose root is the crate's main file (`main.rs`/`lib.rs`),
|
|
|
|
/// computed by the `crate_def_map` query. Additionally, every block expression introduces the
|
|
|
|
/// opportunity to write arbitrary item and module hierarchies, and thus gets its own `DefMap` that
|
|
|
|
/// is computed by the `block_def_map` query.
|
2019-10-31 10:45:10 -05:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2021-01-18 13:18:05 -06:00
|
|
|
pub struct DefMap {
|
2021-01-21 10:04:50 -06:00
|
|
|
_c: Count<Self>,
|
2021-01-25 12:02:05 -06:00
|
|
|
block: Option<BlockInfo>,
|
2021-01-20 08:41:18 -06:00
|
|
|
root: LocalModuleId,
|
|
|
|
modules: Arena<ModuleData>,
|
2021-01-20 11:17:48 -06:00
|
|
|
krate: CrateId,
|
2019-10-31 10:45:10 -05:00
|
|
|
/// The prelude module for this crate. This either comes from an import
|
|
|
|
/// marked with the `prelude_import` attribute, or (in the normal case) from
|
|
|
|
/// a dependency (`std` or `core`).
|
2022-09-06 13:20:49 -05:00
|
|
|
/// The prelude is empty for non-block DefMaps (unless `#[prelude_import]` was used,
|
|
|
|
/// but that attribute is nightly and when used in a block, it affects resolution globally
|
|
|
|
/// so we aren't handling this correctly anyways).
|
2021-01-20 11:17:48 -06:00
|
|
|
prelude: Option<ModuleId>,
|
2022-09-06 13:20:49 -05:00
|
|
|
/// The extern prelude is only populated for non-block DefMaps
|
2022-03-22 09:54:46 -05:00
|
|
|
extern_prelude: FxHashMap<Name, ModuleId>,
|
2019-10-31 10:45:10 -05:00
|
|
|
|
2022-03-08 17:01:19 -06:00
|
|
|
/// Side table for resolving derive helpers.
|
|
|
|
exported_derives: FxHashMap<MacroDefId, Box<[Name]>>,
|
2022-03-09 04:26:06 -06:00
|
|
|
fn_proc_macro_mapping: FxHashMap<FunctionId, ProcMacroId>,
|
2022-06-15 11:04:39 -05:00
|
|
|
/// The error that occurred when failing to load the proc-macro dll.
|
|
|
|
proc_macro_loading_error: Option<Box<str>>,
|
2022-07-26 04:53:50 -05:00
|
|
|
/// Tracks which custom derives are in scope for an item, to allow resolution of derive helper
|
|
|
|
/// attributes.
|
|
|
|
derive_helpers_in_scope: FxHashMap<AstId<ast::Item>, Vec<(Name, MacroId, MacroCallId)>>,
|
2021-03-18 13:56:37 -05:00
|
|
|
|
2022-01-06 05:30:16 -06:00
|
|
|
/// Custom attributes registered with `#![register_attr]`.
|
|
|
|
registered_attrs: Vec<SmolStr>,
|
|
|
|
/// Custom tool modules registered with `#![register_tool]`.
|
|
|
|
registered_tools: Vec<SmolStr>,
|
2022-09-06 13:20:49 -05:00
|
|
|
/// Unstable features of Rust enabled with `#![feature(A, B)]`.
|
|
|
|
unstable_features: FxHashSet<SmolStr>,
|
2022-01-06 05:30:16 -06:00
|
|
|
|
2019-11-24 09:05:12 -06:00
|
|
|
edition: Edition,
|
2022-01-27 16:23:09 -06:00
|
|
|
recursion_limit: Option<u32>,
|
2019-10-31 10:45:10 -05:00
|
|
|
diagnostics: Vec<DefDiagnostic>,
|
|
|
|
}
|
|
|
|
|
2021-02-03 10:48:41 -06:00
|
|
|
/// For `DefMap`s computed for a block expression, this stores its location in the parent map.
|
2021-02-04 06:44:54 -06:00
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
2021-01-25 12:02:05 -06:00
|
|
|
struct BlockInfo {
|
2021-02-04 06:44:54 -06:00
|
|
|
/// The `BlockId` this `DefMap` was created from.
|
2021-01-25 12:02:05 -06:00
|
|
|
block: BlockId,
|
2021-02-04 06:44:54 -06:00
|
|
|
/// The containing module.
|
|
|
|
parent: ModuleId,
|
2021-01-25 12:02:05 -06:00
|
|
|
}
|
|
|
|
|
2021-01-18 13:18:05 -06:00
|
|
|
impl std::ops::Index<LocalModuleId> for DefMap {
|
2019-10-31 10:45:10 -05:00
|
|
|
type Output = ModuleData;
|
2019-11-23 07:49:53 -06:00
|
|
|
fn index(&self, id: LocalModuleId) -> &ModuleData {
|
2019-10-31 10:45:10 -05:00
|
|
|
&self.modules[id]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 13:58:29 -06:00
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
|
|
|
|
pub enum ModuleOrigin {
|
2019-12-05 07:33:29 -06:00
|
|
|
CrateRoot {
|
|
|
|
definition: FileId,
|
|
|
|
},
|
2019-12-03 13:58:29 -06:00
|
|
|
/// Note that non-inline modules, by definition, live inside non-macro file.
|
2019-12-05 07:19:27 -06:00
|
|
|
File {
|
2020-06-11 04:04:09 -05:00
|
|
|
is_mod_rs: bool,
|
2019-12-05 07:19:27 -06:00
|
|
|
declaration: AstId<ast::Module>,
|
2022-07-21 09:05:52 -05:00
|
|
|
declaration_tree_id: ItemTreeId<Mod>,
|
2019-12-05 07:19:27 -06:00
|
|
|
definition: FileId,
|
|
|
|
},
|
|
|
|
Inline {
|
2022-07-21 09:05:52 -05:00
|
|
|
definition_tree_id: ItemTreeId<Mod>,
|
2019-12-05 07:19:27 -06:00
|
|
|
definition: AstId<ast::Module>,
|
|
|
|
},
|
2021-01-20 13:05:48 -06:00
|
|
|
/// Pseudo-module introduced by a block scope (contains only inner items).
|
|
|
|
BlockExpr {
|
|
|
|
block: AstId<ast::BlockExpr>,
|
|
|
|
},
|
2019-12-03 13:58:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ModuleOrigin {
|
2021-11-27 05:25:05 -06:00
|
|
|
pub fn declaration(&self) -> Option<AstId<ast::Module>> {
|
2019-12-03 13:58:29 -06:00
|
|
|
match self {
|
2019-12-05 07:19:27 -06:00
|
|
|
ModuleOrigin::File { declaration: module, .. }
|
|
|
|
| ModuleOrigin::Inline { definition: module, .. } => Some(*module),
|
2021-01-20 13:05:48 -06:00
|
|
|
ModuleOrigin::CrateRoot { .. } | ModuleOrigin::BlockExpr { .. } => None,
|
2019-12-03 13:58:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 07:37:39 -06:00
|
|
|
pub fn file_id(&self) -> Option<FileId> {
|
2019-12-03 13:58:29 -06:00
|
|
|
match self {
|
2019-12-05 07:33:29 -06:00
|
|
|
ModuleOrigin::File { definition, .. } | ModuleOrigin::CrateRoot { definition } => {
|
|
|
|
Some(*definition)
|
2019-12-05 07:19:27 -06:00
|
|
|
}
|
2019-12-03 13:58:29 -06:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 07:42:57 -06:00
|
|
|
pub fn is_inline(&self) -> bool {
|
|
|
|
match self {
|
2021-01-20 13:05:48 -06:00
|
|
|
ModuleOrigin::Inline { .. } | ModuleOrigin::BlockExpr { .. } => true,
|
2020-01-15 07:42:57 -06:00
|
|
|
ModuleOrigin::CrateRoot { .. } | ModuleOrigin::File { .. } => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 13:58:29 -06:00
|
|
|
/// Returns a node which defines this module.
|
|
|
|
/// That is, a file or a `mod foo {}` with items.
|
2020-03-13 10:05:46 -05:00
|
|
|
fn definition_source(&self, db: &dyn DefDatabase) -> InFile<ModuleSource> {
|
2019-12-03 13:58:29 -06:00
|
|
|
match self {
|
2019-12-05 07:33:29 -06:00
|
|
|
ModuleOrigin::File { definition, .. } | ModuleOrigin::CrateRoot { definition } => {
|
|
|
|
let file_id = *definition;
|
2019-12-03 13:58:29 -06:00
|
|
|
let sf = db.parse(file_id).tree();
|
2020-02-18 06:53:02 -06:00
|
|
|
InFile::new(file_id.into(), ModuleSource::SourceFile(sf))
|
2019-12-03 13:58:29 -06:00
|
|
|
}
|
2022-07-21 09:05:52 -05:00
|
|
|
ModuleOrigin::Inline { definition, .. } => InFile::new(
|
2020-03-13 10:05:46 -05:00
|
|
|
definition.file_id,
|
|
|
|
ModuleSource::Module(definition.to_node(db.upcast())),
|
|
|
|
),
|
2021-01-20 13:05:48 -06:00
|
|
|
ModuleOrigin::BlockExpr { block } => {
|
|
|
|
InFile::new(block.file_id, ModuleSource::BlockExpr(block.to_node(db.upcast())))
|
|
|
|
}
|
2019-12-03 13:58:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-12 12:00:17 -05:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2019-10-31 10:45:10 -05:00
|
|
|
pub struct ModuleData {
|
2021-07-12 13:13:43 -05:00
|
|
|
/// Where does this module come from?
|
|
|
|
pub origin: ModuleOrigin,
|
|
|
|
/// Declared visibility of this module.
|
|
|
|
pub visibility: Visibility,
|
|
|
|
|
2019-11-23 07:49:53 -06:00
|
|
|
pub parent: Option<LocalModuleId>,
|
|
|
|
pub children: FxHashMap<Name, LocalModuleId>,
|
2019-12-20 08:45:12 -06:00
|
|
|
pub scope: ItemScope,
|
2019-10-31 10:45:10 -05:00
|
|
|
}
|
|
|
|
|
2021-01-18 13:18:05 -06:00
|
|
|
impl DefMap {
|
|
|
|
pub(crate) fn crate_def_map_query(db: &dyn DefDatabase, krate: CrateId) -> Arc<DefMap> {
|
2020-08-12 09:32:36 -05:00
|
|
|
let _p = profile::span("crate_def_map_query").detail(|| {
|
2020-10-20 08:38:11 -05:00
|
|
|
db.crate_graph()[krate].display_name.as_deref().unwrap_or_default().to_string()
|
2020-03-16 04:47:52 -05:00
|
|
|
});
|
2021-07-12 12:14:58 -05:00
|
|
|
|
|
|
|
let crate_graph = db.crate_graph();
|
|
|
|
|
|
|
|
let edition = crate_graph[krate].edition;
|
|
|
|
let origin = ModuleOrigin::CrateRoot { definition: crate_graph[krate].root_file_id };
|
2022-07-14 16:22:54 -05:00
|
|
|
let def_map = DefMap::empty(krate, edition, ModuleData::new(origin, Visibility::Public));
|
2021-11-25 17:17:20 -06:00
|
|
|
let def_map = collector::collect_defs(
|
|
|
|
db,
|
|
|
|
def_map,
|
|
|
|
TreeId::new(crate_graph[krate].root_file_id.into(), None),
|
|
|
|
);
|
2021-07-12 12:14:58 -05:00
|
|
|
|
2021-01-21 08:22:17 -06:00
|
|
|
Arc::new(def_map)
|
|
|
|
}
|
|
|
|
|
2021-02-01 06:32:43 -06:00
|
|
|
pub(crate) fn block_def_map_query(
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
block_id: BlockId,
|
|
|
|
) -> Option<Arc<DefMap>> {
|
2021-01-25 12:02:05 -06:00
|
|
|
let block: BlockLoc = db.lookup_intern_block(block_id);
|
2021-01-21 08:22:17 -06:00
|
|
|
|
2021-11-25 17:17:20 -06:00
|
|
|
let tree_id = TreeId::new(block.ast_id.file_id, Some(block_id));
|
|
|
|
let item_tree = tree_id.item_tree(db);
|
|
|
|
if item_tree.top_level_items().is_empty() {
|
2021-02-01 06:32:43 -06:00
|
|
|
return None;
|
|
|
|
}
|
2021-01-21 08:22:17 -06:00
|
|
|
|
2021-02-04 06:44:54 -06:00
|
|
|
let parent_map = block.module.def_map(db);
|
2022-07-14 16:22:54 -05:00
|
|
|
let krate = block.module.krate;
|
|
|
|
let local_id = LocalModuleId::from_raw(la_arena::RawIdx::from(0));
|
|
|
|
// NB: we use `None` as block here, which would be wrong for implicit
|
|
|
|
// modules declared by blocks with items. At the moment, we don't use
|
|
|
|
// this visibility for anything outside IDE, so that's probably OK.
|
|
|
|
let visibility = Visibility::Module(ModuleId { krate, local_id, block: None });
|
|
|
|
let module_data =
|
|
|
|
ModuleData::new(ModuleOrigin::BlockExpr { block: block.ast_id }, visibility);
|
|
|
|
|
|
|
|
let mut def_map = DefMap::empty(krate, parent_map.edition, module_data);
|
|
|
|
def_map.block = Some(BlockInfo { block: block_id, parent: block.module });
|
2021-01-21 08:22:17 -06:00
|
|
|
|
2021-11-25 17:17:20 -06:00
|
|
|
let def_map = collector::collect_defs(db, def_map, tree_id);
|
2021-02-01 06:32:43 -06:00
|
|
|
Some(Arc::new(def_map))
|
2019-10-31 10:45:10 -05:00
|
|
|
}
|
|
|
|
|
2022-07-14 16:22:54 -05:00
|
|
|
fn empty(krate: CrateId, edition: Edition, module_data: ModuleData) -> DefMap {
|
2021-01-21 08:22:17 -06:00
|
|
|
let mut modules: Arena<ModuleData> = Arena::default();
|
2022-07-14 16:22:54 -05:00
|
|
|
let root = modules.alloc(module_data);
|
2021-07-12 13:13:43 -05:00
|
|
|
|
2021-01-21 08:22:17 -06:00
|
|
|
DefMap {
|
2021-01-21 10:04:50 -06:00
|
|
|
_c: Count::new(),
|
2021-01-25 12:02:05 -06:00
|
|
|
block: None,
|
2021-01-21 08:22:17 -06:00
|
|
|
krate,
|
|
|
|
edition,
|
2022-01-27 16:23:09 -06:00
|
|
|
recursion_limit: None,
|
2021-01-21 08:22:17 -06:00
|
|
|
extern_prelude: FxHashMap::default(),
|
2022-03-08 17:01:19 -06:00
|
|
|
exported_derives: FxHashMap::default(),
|
2022-03-08 18:13:38 -06:00
|
|
|
fn_proc_macro_mapping: FxHashMap::default(),
|
2022-06-15 11:04:39 -05:00
|
|
|
proc_macro_loading_error: None,
|
2022-07-26 04:53:50 -05:00
|
|
|
derive_helpers_in_scope: FxHashMap::default(),
|
2021-01-21 08:22:17 -06:00
|
|
|
prelude: None,
|
|
|
|
root,
|
|
|
|
modules,
|
2022-01-06 05:30:16 -06:00
|
|
|
registered_attrs: Vec::new(),
|
|
|
|
registered_tools: Vec::new(),
|
2022-09-06 13:20:49 -05:00
|
|
|
unstable_features: FxHashSet::default(),
|
2021-01-21 08:22:17 -06:00
|
|
|
diagnostics: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 07:49:53 -06:00
|
|
|
pub fn modules_for_file(&self, file_id: FileId) -> impl Iterator<Item = LocalModuleId> + '_ {
|
2019-11-15 01:26:31 -06:00
|
|
|
self.modules
|
|
|
|
.iter()
|
2019-12-03 13:58:29 -06:00
|
|
|
.filter(move |(_id, data)| data.origin.file_id() == Some(file_id))
|
2019-11-15 01:26:31 -06:00
|
|
|
.map(|(id, _data)| id)
|
|
|
|
}
|
2019-11-24 04:34:27 -06:00
|
|
|
|
2021-01-20 08:41:18 -06:00
|
|
|
pub fn modules(&self) -> impl Iterator<Item = (LocalModuleId, &ModuleData)> + '_ {
|
|
|
|
self.modules.iter()
|
|
|
|
}
|
2022-07-26 04:53:50 -05:00
|
|
|
|
|
|
|
pub fn derive_helpers_in_scope(
|
|
|
|
&self,
|
|
|
|
id: AstId<ast::Adt>,
|
|
|
|
) -> Option<&[(Name, MacroId, MacroCallId)]> {
|
|
|
|
self.derive_helpers_in_scope.get(&id.map(|it| it.upcast())).map(Deref::deref)
|
|
|
|
}
|
|
|
|
|
2022-01-06 07:56:50 -06:00
|
|
|
pub fn registered_tools(&self) -> &[SmolStr] {
|
|
|
|
&self.registered_tools
|
|
|
|
}
|
2022-07-26 04:53:50 -05:00
|
|
|
|
2022-01-06 07:56:50 -06:00
|
|
|
pub fn registered_attrs(&self) -> &[SmolStr] {
|
|
|
|
&self.registered_attrs
|
|
|
|
}
|
2022-07-26 04:53:50 -05:00
|
|
|
|
2022-09-06 13:20:49 -05:00
|
|
|
pub fn is_unstable_feature_enabled(&self, feature: &str) -> bool {
|
|
|
|
self.unstable_features.contains(feature)
|
|
|
|
}
|
|
|
|
|
2021-01-20 08:41:18 -06:00
|
|
|
pub fn root(&self) -> LocalModuleId {
|
|
|
|
self.root
|
|
|
|
}
|
|
|
|
|
2022-03-09 04:26:06 -06:00
|
|
|
pub fn fn_as_proc_macro(&self, id: FunctionId) -> Option<ProcMacroId> {
|
2022-03-08 18:13:38 -06:00
|
|
|
self.fn_proc_macro_mapping.get(&id).copied()
|
|
|
|
}
|
2022-07-26 04:53:50 -05:00
|
|
|
|
2022-06-15 11:04:39 -05:00
|
|
|
pub fn proc_macro_loading_error(&self) -> Option<&str> {
|
|
|
|
self.proc_macro_loading_error.as_deref()
|
|
|
|
}
|
2022-03-08 18:13:38 -06:00
|
|
|
|
2021-01-20 11:17:48 -06:00
|
|
|
pub(crate) fn krate(&self) -> CrateId {
|
|
|
|
self.krate
|
|
|
|
}
|
|
|
|
|
2021-02-02 05:25:13 -06:00
|
|
|
pub(crate) fn block_id(&self) -> Option<BlockId> {
|
|
|
|
self.block.as_ref().map(|block| block.block)
|
|
|
|
}
|
|
|
|
|
2021-01-20 11:17:48 -06:00
|
|
|
pub(crate) fn prelude(&self) -> Option<ModuleId> {
|
|
|
|
self.prelude
|
|
|
|
}
|
|
|
|
|
2022-03-22 09:54:46 -05:00
|
|
|
pub(crate) fn extern_prelude(&self) -> impl Iterator<Item = (&Name, &ModuleId)> + '_ {
|
2021-01-20 11:17:48 -06:00
|
|
|
self.extern_prelude.iter()
|
|
|
|
}
|
|
|
|
|
2021-01-25 08:21:33 -06:00
|
|
|
pub fn module_id(&self, local_id: LocalModuleId) -> ModuleId {
|
2021-01-25 12:02:05 -06:00
|
|
|
let block = self.block.as_ref().map(|b| b.block);
|
|
|
|
ModuleId { krate: self.krate, local_id, block }
|
2021-01-25 08:21:33 -06:00
|
|
|
}
|
|
|
|
|
2021-02-04 06:44:54 -06:00
|
|
|
pub(crate) fn crate_root(&self, db: &dyn DefDatabase) -> ModuleId {
|
|
|
|
self.with_ancestor_maps(db, self.root, &mut |def_map, _module| {
|
2022-07-25 07:18:28 -05:00
|
|
|
if def_map.block.is_none() {
|
|
|
|
Some(def_map.module_id(def_map.root))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2021-02-04 06:44:54 -06:00
|
|
|
})
|
|
|
|
.expect("DefMap chain without root")
|
2021-02-02 11:02:12 -06:00
|
|
|
}
|
|
|
|
|
2019-11-24 04:34:27 -06:00
|
|
|
pub(crate) fn resolve_path(
|
|
|
|
&self,
|
2020-03-13 10:05:46 -05:00
|
|
|
db: &dyn DefDatabase,
|
2019-11-24 04:34:27 -06:00
|
|
|
original_module: LocalModuleId,
|
2019-12-13 05:12:36 -06:00
|
|
|
path: &ModPath,
|
2019-11-30 09:29:21 -06:00
|
|
|
shadow: BuiltinShadowMode,
|
2019-11-24 04:34:27 -06:00
|
|
|
) -> (PerNs, Option<usize>) {
|
2019-11-30 09:29:21 -06:00
|
|
|
let res =
|
|
|
|
self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path, shadow);
|
2019-11-24 04:34:27 -06:00
|
|
|
(res.resolved_def, res.segment_index)
|
|
|
|
}
|
2020-01-28 09:29:31 -06:00
|
|
|
|
2021-03-22 12:47:19 -05:00
|
|
|
pub(crate) fn resolve_path_locally(
|
|
|
|
&self,
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
original_module: LocalModuleId,
|
|
|
|
path: &ModPath,
|
|
|
|
shadow: BuiltinShadowMode,
|
|
|
|
) -> (PerNs, Option<usize>) {
|
|
|
|
let res = self.resolve_path_fp_with_macro_single(
|
|
|
|
db,
|
|
|
|
ResolveMode::Other,
|
|
|
|
original_module,
|
|
|
|
path,
|
|
|
|
shadow,
|
|
|
|
);
|
|
|
|
(res.resolved_def, res.segment_index)
|
|
|
|
}
|
|
|
|
|
2021-02-04 06:44:54 -06:00
|
|
|
/// Ascends the `DefMap` hierarchy and calls `f` with every `DefMap` and containing module.
|
|
|
|
///
|
|
|
|
/// If `f` returns `Some(val)`, iteration is stopped and `Some(val)` is returned. If `f` returns
|
|
|
|
/// `None`, iteration continues.
|
2021-02-09 10:23:25 -06:00
|
|
|
pub fn with_ancestor_maps<T>(
|
2021-01-27 12:16:29 -06:00
|
|
|
&self,
|
2021-02-04 06:44:54 -06:00
|
|
|
db: &dyn DefDatabase,
|
2021-01-27 12:16:29 -06:00
|
|
|
local_mod: LocalModuleId,
|
2021-02-04 06:44:54 -06:00
|
|
|
f: &mut dyn FnMut(&DefMap, LocalModuleId) -> Option<T>,
|
|
|
|
) -> Option<T> {
|
|
|
|
if let Some(it) = f(self, local_mod) {
|
|
|
|
return Some(it);
|
|
|
|
}
|
|
|
|
let mut block = self.block;
|
|
|
|
while let Some(block_info) = block {
|
|
|
|
let parent = block_info.parent.def_map(db);
|
|
|
|
if let Some(it) = f(&parent, block_info.parent.local_id) {
|
|
|
|
return Some(it);
|
|
|
|
}
|
|
|
|
block = parent.block;
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
2021-01-27 12:16:29 -06:00
|
|
|
}
|
2021-02-23 10:56:16 -06:00
|
|
|
|
|
|
|
/// If this `DefMap` is for a block expression, returns the module containing the block (which
|
|
|
|
/// might again be a block, or a module inside a block).
|
|
|
|
pub fn parent(&self) -> Option<ModuleId> {
|
|
|
|
Some(self.block?.parent)
|
|
|
|
}
|
2021-01-27 12:16:29 -06:00
|
|
|
|
2021-03-01 12:36:34 -06:00
|
|
|
/// Returns the module containing `local_mod`, either the parent `mod`, or the module containing
|
|
|
|
/// the block, if `self` corresponds to a block expression.
|
|
|
|
pub fn containing_module(&self, local_mod: LocalModuleId) -> Option<ModuleId> {
|
|
|
|
match &self[local_mod].parent {
|
|
|
|
Some(parent) => Some(self.module_id(*parent)),
|
2021-06-07 06:59:01 -05:00
|
|
|
None => self.block.as_ref().map(|block| block.parent),
|
2021-03-01 12:36:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-28 09:29:31 -06:00
|
|
|
// FIXME: this can use some more human-readable format (ideally, an IR
|
|
|
|
// even), as this should be a great debugging aid.
|
2021-02-04 06:44:54 -06:00
|
|
|
pub fn dump(&self, db: &dyn DefDatabase) -> String {
|
2020-01-28 09:29:31 -06:00
|
|
|
let mut buf = String::new();
|
2021-02-04 06:44:54 -06:00
|
|
|
let mut arc;
|
2021-01-21 08:22:17 -06:00
|
|
|
let mut current_map = self;
|
2021-01-25 12:02:05 -06:00
|
|
|
while let Some(block) = ¤t_map.block {
|
2021-01-21 08:22:17 -06:00
|
|
|
go(&mut buf, current_map, "block scope", current_map.root);
|
2021-02-03 11:23:59 -06:00
|
|
|
buf.push('\n');
|
2021-02-04 06:44:54 -06:00
|
|
|
arc = block.parent.def_map(db);
|
|
|
|
current_map = &*arc;
|
2021-01-21 08:22:17 -06:00
|
|
|
}
|
|
|
|
go(&mut buf, current_map, "crate", current_map.root);
|
2020-07-20 10:44:44 -05:00
|
|
|
return buf;
|
2020-01-28 09:29:31 -06:00
|
|
|
|
2021-01-18 13:18:05 -06:00
|
|
|
fn go(buf: &mut String, map: &DefMap, path: &str, module: LocalModuleId) {
|
2020-07-20 10:44:44 -05:00
|
|
|
format_to!(buf, "{}\n", path);
|
2020-01-28 09:29:31 -06:00
|
|
|
|
2021-02-03 12:05:11 -06:00
|
|
|
map.modules[module].scope.dump(buf);
|
2020-01-28 09:29:31 -06:00
|
|
|
|
2022-07-24 09:04:20 -05:00
|
|
|
for (name, child) in
|
|
|
|
map.modules[module].children.iter().sorted_by(|a, b| Ord::cmp(&a.0, &b.0))
|
|
|
|
{
|
2022-12-23 12:42:58 -06:00
|
|
|
let path = format!("{path}::{name}");
|
2020-07-20 10:44:44 -05:00
|
|
|
buf.push('\n');
|
2020-01-28 09:29:31 -06:00
|
|
|
go(buf, map, &path, *child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-03 16:45:27 -05:00
|
|
|
|
2021-04-21 10:57:45 -05:00
|
|
|
pub fn dump_block_scopes(&self, db: &dyn DefDatabase) -> String {
|
|
|
|
let mut buf = String::new();
|
|
|
|
let mut arc;
|
|
|
|
let mut current_map = self;
|
|
|
|
while let Some(block) = ¤t_map.block {
|
|
|
|
format_to!(buf, "{:?} in {:?}\n", block.block, block.parent);
|
|
|
|
arc = block.parent.def_map(db);
|
|
|
|
current_map = &*arc;
|
|
|
|
}
|
|
|
|
|
|
|
|
format_to!(buf, "crate scope\n");
|
|
|
|
buf
|
|
|
|
}
|
|
|
|
|
2021-04-03 16:45:27 -05:00
|
|
|
fn shrink_to_fit(&mut self) {
|
2021-04-03 19:56:11 -05:00
|
|
|
// Exhaustive match to require handling new fields.
|
|
|
|
let Self {
|
|
|
|
_c: _,
|
2022-03-09 04:26:06 -06:00
|
|
|
exported_derives,
|
2021-04-03 19:56:11 -05:00
|
|
|
extern_prelude,
|
|
|
|
diagnostics,
|
|
|
|
modules,
|
2022-01-06 05:30:16 -06:00
|
|
|
registered_attrs,
|
|
|
|
registered_tools,
|
2022-03-08 18:13:38 -06:00
|
|
|
fn_proc_macro_mapping,
|
2022-07-26 04:53:50 -05:00
|
|
|
derive_helpers_in_scope,
|
2022-09-06 13:20:49 -05:00
|
|
|
unstable_features,
|
2022-06-15 11:04:39 -05:00
|
|
|
proc_macro_loading_error: _,
|
2021-04-03 19:56:11 -05:00
|
|
|
block: _,
|
|
|
|
edition: _,
|
2022-01-27 16:23:09 -06:00
|
|
|
recursion_limit: _,
|
2021-04-03 19:56:11 -05:00
|
|
|
krate: _,
|
|
|
|
prelude: _,
|
|
|
|
root: _,
|
|
|
|
} = self;
|
|
|
|
|
|
|
|
extern_prelude.shrink_to_fit();
|
2022-03-09 04:26:06 -06:00
|
|
|
exported_derives.shrink_to_fit();
|
2021-04-03 19:56:11 -05:00
|
|
|
diagnostics.shrink_to_fit();
|
|
|
|
modules.shrink_to_fit();
|
2022-01-06 05:30:16 -06:00
|
|
|
registered_attrs.shrink_to_fit();
|
|
|
|
registered_tools.shrink_to_fit();
|
2022-03-08 18:13:38 -06:00
|
|
|
fn_proc_macro_mapping.shrink_to_fit();
|
2022-07-26 04:53:50 -05:00
|
|
|
derive_helpers_in_scope.shrink_to_fit();
|
2022-09-06 13:20:49 -05:00
|
|
|
unstable_features.shrink_to_fit();
|
2021-04-03 19:56:11 -05:00
|
|
|
for (_, module) in modules.iter_mut() {
|
2021-04-03 16:45:27 -05:00
|
|
|
module.children.shrink_to_fit();
|
|
|
|
module.scope.shrink_to_fit();
|
|
|
|
}
|
|
|
|
}
|
internal: move diagnostics to hir
The idea here is to eventually get rid of `dyn Diagnostic` and
`DiagnosticSink` infrastructure altogether, and just have a `enum
hir::Diagnostic` instead.
The problem with `dyn Diagnostic` is that it is defined in the lowest
level of the stack (hir_expand), but is used by the highest level (ide).
As a first step, we free hir_expand and hir_def from `dyn Diagnostic`
and kick the can up to `hir_ty`, as an intermediate state. The plan is
then to move DiagnosticSink similarly to the hir crate, and, as final
third step, remove its usage from the ide.
One currently unsolved problem is testing. You can notice that the test
which checks precise diagnostic ranges, unresolved_import_in_use_tree,
was moved to the ide layer. Logically, only IDE should have the infra to
render a specific range.
At the same time, the range is determined with the data produced in
hir_def and hir crates, so this layering is rather unfortunate. Working
on hir_def shouldn't require compiling `ide` for testing.
2021-05-23 15:31:59 -05:00
|
|
|
|
|
|
|
/// Get a reference to the def map's diagnostics.
|
|
|
|
pub fn diagnostics(&self) -> &[DefDiagnostic] {
|
|
|
|
self.diagnostics.as_slice()
|
|
|
|
}
|
2022-01-27 16:23:09 -06:00
|
|
|
|
|
|
|
pub fn recursion_limit(&self) -> Option<u32> {
|
|
|
|
self.recursion_limit
|
|
|
|
}
|
2019-10-31 10:45:10 -05:00
|
|
|
}
|
|
|
|
|
2019-11-23 02:14:10 -06:00
|
|
|
impl ModuleData {
|
2021-07-12 13:13:43 -05:00
|
|
|
pub(crate) fn new(origin: ModuleOrigin, visibility: Visibility) -> Self {
|
2021-07-12 12:02:56 -05:00
|
|
|
ModuleData {
|
2021-07-12 13:13:43 -05:00
|
|
|
origin,
|
|
|
|
visibility,
|
2021-07-12 12:02:56 -05:00
|
|
|
parent: None,
|
|
|
|
children: FxHashMap::default(),
|
|
|
|
scope: ItemScope::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 02:14:10 -06:00
|
|
|
/// Returns a node which defines this module. That is, a file or a `mod foo {}` with items.
|
2020-03-13 10:05:46 -05:00
|
|
|
pub fn definition_source(&self, db: &dyn DefDatabase) -> InFile<ModuleSource> {
|
2019-12-03 13:58:29 -06:00
|
|
|
self.origin.definition_source(db)
|
2019-11-23 02:14:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
|
2019-12-03 13:58:29 -06:00
|
|
|
/// `None` for the crate root or block.
|
2020-03-13 10:05:46 -05:00
|
|
|
pub fn declaration_source(&self, db: &dyn DefDatabase) -> Option<InFile<ast::Module>> {
|
2019-12-03 13:58:29 -06:00
|
|
|
let decl = self.origin.declaration()?;
|
2020-03-13 10:05:46 -05:00
|
|
|
let value = decl.to_node(db.upcast());
|
2019-11-28 07:00:03 -06:00
|
|
|
Some(InFile { file_id: decl.file_id, value })
|
2019-11-23 02:14:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 14:58:38 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2019-12-03 14:24:02 -06:00
|
|
|
pub enum ModuleSource {
|
|
|
|
SourceFile(ast::SourceFile),
|
|
|
|
Module(ast::Module),
|
2021-01-20 13:05:48 -06:00
|
|
|
BlockExpr(ast::BlockExpr),
|
2019-12-03 14:24:02 -06:00
|
|
|
}
|