2019-11-03 20:49:44 +00:00
|
|
|
//! This module implements import-resolution/macro expansion algorithm.
|
|
|
|
//!
|
2021-09-05 10:16:02 +00:00
|
|
|
//! The result of this module is `DefMap`: a data structure which contains:
|
2019-11-03 20:49:44 +00: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 10:16:02 +00:00
|
|
|
//! Note that `DefMap` contains fully macro expanded code.
|
2019-11-03 20:49:44 +00:00
|
|
|
//!
|
2021-09-05 10:16:02 +00:00
|
|
|
//! Computing `DefMap` can be partitioned into several logically
|
2019-11-03 20:49:44 +00:00
|
|
|
//! independent "phases". The phases are mutually recursive though, there's no
|
|
|
|
//! strict ordering.
|
|
|
|
//!
|
|
|
|
//! ## Collecting RawItems
|
|
|
|
//!
|
2019-11-08 21:23:19 +00: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 20:49:44 +00: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 21:23:19 +00:00
|
|
|
//! path and, upon success, we run macro expansion and "collect module" phase on
|
|
|
|
//! the result
|
2019-10-30 14:40:13 +00:00
|
|
|
|
2022-01-06 11:30:16 +00:00
|
|
|
pub mod attr_resolution;
|
2022-07-26 09:53:50 +00:00
|
|
|
mod collector;
|
2024-01-26 19:08:10 +00:00
|
|
|
pub mod diagnostics;
|
2019-11-04 18:42:25 +00:00
|
|
|
mod mod_resolution;
|
2019-11-08 21:17:17 +00:00
|
|
|
mod path_resolution;
|
2024-01-26 19:08:10 +00:00
|
|
|
pub mod proc_macro;
|
2019-10-31 15:45:10 +00:00
|
|
|
|
2019-11-03 20:35:48 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2023-05-02 14:12:22 +00:00
|
|
|
use std::{cmp::Ord, ops::Deref};
|
2019-10-31 15:45:10 +00:00
|
|
|
|
2023-12-18 11:09:54 +00:00
|
|
|
use base_db::{CrateId, Edition, FileId};
|
|
|
|
use hir_expand::{
|
|
|
|
ast_id_map::FileAstId, name::Name, proc_macro::ProcMacroKind, HirFileId, InFile, MacroCallId,
|
|
|
|
MacroDefId,
|
|
|
|
};
|
2022-07-24 14:04:20 +00:00
|
|
|
use itertools::Itertools;
|
2021-01-14 15:47:42 +00:00
|
|
|
use la_arena::Arena;
|
2021-01-21 16:04:50 +00:00
|
|
|
use profile::Count;
|
2022-09-06 18:20:49 +00:00
|
|
|
use rustc_hash::{FxHashMap, FxHashSet};
|
2020-03-28 10:08:19 +00:00
|
|
|
use stdx::format_to;
|
2022-01-06 11:30:16 +00:00
|
|
|
use syntax::{ast, SmolStr};
|
2023-05-02 14:12:22 +00:00
|
|
|
use triomphe::Arc;
|
2019-10-31 15:45:10 +00:00
|
|
|
|
|
|
|
use crate::{
|
2019-11-23 11:44:43 +00:00
|
|
|
db::DefDatabase,
|
2019-12-20 14:45:12 +00:00
|
|
|
item_scope::{BuiltinShadowMode, ItemScope},
|
2022-07-21 14:05:52 +00:00
|
|
|
item_tree::{ItemTreeId, Mod, TreeId},
|
2019-11-23 13:53:16 +00:00
|
|
|
nameres::{diagnostics::DefDiagnostic, path_resolution::ResolveMode},
|
2019-12-13 11:12:36 +00:00
|
|
|
path::ModPath,
|
2019-11-23 13:53:16 +00:00
|
|
|
per_ns::PerNs,
|
2024-01-11 10:36:10 +00:00
|
|
|
visibility::{Visibility, VisibilityExplicity},
|
2024-01-15 09:58:05 +00:00
|
|
|
AstId, BlockId, BlockLoc, CrateRootModuleId, EnumId, EnumVariantId, ExternCrateId, FunctionId,
|
|
|
|
LocalModuleId, Lookup, MacroExpander, MacroId, ModuleId, ProcMacroId, UseId,
|
2019-10-31 15:45:10 +00:00
|
|
|
};
|
|
|
|
|
2021-02-03 16:48:41 +00: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 15:45:10 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2021-01-18 19:18:05 +00:00
|
|
|
pub struct DefMap {
|
2021-01-21 16:04:50 +00:00
|
|
|
_c: Count<Self>,
|
2023-09-02 06:18:10 +00:00
|
|
|
/// When this is a block def map, this will hold the block id of the block and module that
|
2023-06-13 10:59:52 +00:00
|
|
|
/// contains this block.
|
2021-01-25 18:02:05 +00:00
|
|
|
block: Option<BlockInfo>,
|
2023-06-13 10:59:52 +00:00
|
|
|
/// The modules and their data declared in this crate.
|
2023-12-21 08:18:06 +00:00
|
|
|
pub modules: Arena<ModuleData>,
|
2021-01-20 17:17:48 +00:00
|
|
|
krate: CrateId,
|
2019-10-31 15:45:10 +00: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 18:20:49 +00: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).
|
2023-08-09 13:20:42 +00:00
|
|
|
prelude: Option<(ModuleId, Option<UseId>)>,
|
2023-05-11 06:41:24 +00:00
|
|
|
/// `macro_use` prelude that contains macros from `#[macro_use]`'d external crates. Note that
|
|
|
|
/// this contains all kinds of macro, not just `macro_rules!` macro.
|
2023-08-16 09:18:11 +00:00
|
|
|
/// ExternCrateId being None implies it being imported from the general prelude import.
|
2023-08-09 13:20:42 +00:00
|
|
|
macro_use_prelude: FxHashMap<Name, (MacroId, Option<ExternCrateId>)>,
|
2024-01-15 09:58:05 +00:00
|
|
|
pub(crate) enum_definitions: FxHashMap<EnumId, Box<[EnumVariantId]>>,
|
2019-10-31 15:45:10 +00:00
|
|
|
|
2023-06-01 13:04:38 +00: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)>>,
|
|
|
|
|
2023-06-13 10:59:52 +00:00
|
|
|
/// The diagnostics that need to be emitted for this crate.
|
2023-06-01 13:04:38 +00:00
|
|
|
diagnostics: Vec<DefDiagnostic>,
|
|
|
|
|
2023-06-13 10:59:52 +00:00
|
|
|
/// The crate data that is shared between a crate's def map and all its block def maps.
|
2023-06-01 13:50:19 +00:00
|
|
|
data: Arc<DefMapCrateData>,
|
2023-06-01 13:04:38 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 13:50:19 +00:00
|
|
|
/// Data that belongs to a crate which is shared between a crate's def map and all its block def maps.
|
2023-06-01 13:27:05 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
2023-06-01 13:50:19 +00:00
|
|
|
struct DefMapCrateData {
|
2023-06-13 10:59:52 +00:00
|
|
|
/// The extern prelude which contains all root modules of external crates that are in scope.
|
2023-08-09 13:20:42 +00:00
|
|
|
extern_prelude: FxHashMap<Name, (CrateRootModuleId, Option<ExternCrateId>)>,
|
2023-06-01 13:49:05 +00:00
|
|
|
|
2022-03-08 23:01:19 +00:00
|
|
|
/// Side table for resolving derive helpers.
|
|
|
|
exported_derives: FxHashMap<MacroDefId, Box<[Name]>>,
|
2022-03-09 10:26:06 +00:00
|
|
|
fn_proc_macro_mapping: FxHashMap<FunctionId, ProcMacroId>,
|
2022-06-15 16:04:39 +00:00
|
|
|
/// The error that occurred when failing to load the proc-macro dll.
|
|
|
|
proc_macro_loading_error: Option<Box<str>>,
|
2021-03-18 18:56:37 +00:00
|
|
|
|
2022-01-06 11:30:16 +00: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 18:20:49 +00:00
|
|
|
/// Unstable features of Rust enabled with `#![feature(A, B)]`.
|
|
|
|
unstable_features: FxHashSet<SmolStr>,
|
2023-01-20 22:09:35 +00:00
|
|
|
/// #[rustc_coherence_is_core]
|
|
|
|
rustc_coherence_is_core: bool,
|
2023-05-26 20:41:32 +00:00
|
|
|
no_core: bool,
|
|
|
|
no_std: bool,
|
2022-01-06 11:30:16 +00:00
|
|
|
|
2019-11-24 15:05:12 +00:00
|
|
|
edition: Edition,
|
2022-01-27 22:23:09 +00:00
|
|
|
recursion_limit: Option<u32>,
|
2019-10-31 15:45:10 +00:00
|
|
|
}
|
2023-06-01 13:49:05 +00:00
|
|
|
|
2023-06-01 13:50:19 +00:00
|
|
|
impl DefMapCrateData {
|
2023-06-01 13:27:05 +00:00
|
|
|
fn shrink_to_fit(&mut self) {
|
|
|
|
let Self {
|
2023-06-01 13:49:05 +00:00
|
|
|
extern_prelude,
|
2023-06-01 13:27:05 +00:00
|
|
|
exported_derives,
|
|
|
|
fn_proc_macro_mapping,
|
|
|
|
registered_attrs,
|
|
|
|
registered_tools,
|
|
|
|
unstable_features,
|
|
|
|
proc_macro_loading_error: _,
|
|
|
|
rustc_coherence_is_core: _,
|
|
|
|
no_core: _,
|
|
|
|
no_std: _,
|
|
|
|
edition: _,
|
|
|
|
recursion_limit: _,
|
|
|
|
} = self;
|
2023-06-01 13:49:05 +00:00
|
|
|
extern_prelude.shrink_to_fit();
|
2023-06-01 13:27:05 +00:00
|
|
|
exported_derives.shrink_to_fit();
|
|
|
|
fn_proc_macro_mapping.shrink_to_fit();
|
|
|
|
registered_attrs.shrink_to_fit();
|
|
|
|
registered_tools.shrink_to_fit();
|
|
|
|
unstable_features.shrink_to_fit();
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 15:45:10 +00:00
|
|
|
|
2021-02-03 16:48:41 +00:00
|
|
|
/// For `DefMap`s computed for a block expression, this stores its location in the parent map.
|
2021-02-04 12:44:54 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
2021-01-25 18:02:05 +00:00
|
|
|
struct BlockInfo {
|
2021-02-04 12:44:54 +00:00
|
|
|
/// The `BlockId` this `DefMap` was created from.
|
2021-01-25 18:02:05 +00:00
|
|
|
block: BlockId,
|
2021-02-04 12:44:54 +00:00
|
|
|
/// The containing module.
|
2023-06-01 12:46:36 +00:00
|
|
|
parent: BlockRelativeModuleId,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
|
|
|
struct BlockRelativeModuleId {
|
|
|
|
block: Option<BlockId>,
|
|
|
|
local_id: LocalModuleId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BlockRelativeModuleId {
|
|
|
|
fn def_map(self, db: &dyn DefDatabase, krate: CrateId) -> Arc<DefMap> {
|
2023-06-01 13:49:05 +00:00
|
|
|
self.into_module(krate).def_map(db)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn into_module(self, krate: CrateId) -> ModuleId {
|
|
|
|
ModuleId { krate, block: self.block, local_id: self.local_id }
|
2023-06-01 12:46:36 +00:00
|
|
|
}
|
2023-06-27 06:22:05 +00:00
|
|
|
|
|
|
|
fn is_block_module(self) -> bool {
|
|
|
|
self.block.is_some() && self.local_id == DefMap::ROOT
|
|
|
|
}
|
2021-01-25 18:02:05 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 19:18:05 +00:00
|
|
|
impl std::ops::Index<LocalModuleId> for DefMap {
|
2019-10-31 15:45:10 +00:00
|
|
|
type Output = ModuleData;
|
2019-11-23 13:49:53 +00:00
|
|
|
fn index(&self, id: LocalModuleId) -> &ModuleData {
|
2019-10-31 15:45:10 +00:00
|
|
|
&self.modules[id]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 19:58:29 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
|
|
|
|
pub enum ModuleOrigin {
|
2019-12-05 13:33:29 +00:00
|
|
|
CrateRoot {
|
|
|
|
definition: FileId,
|
|
|
|
},
|
2019-12-03 19:58:29 +00:00
|
|
|
/// Note that non-inline modules, by definition, live inside non-macro file.
|
2019-12-05 13:19:27 +00:00
|
|
|
File {
|
2020-06-11 09:04:09 +00:00
|
|
|
is_mod_rs: bool,
|
2023-08-10 17:04:46 +00:00
|
|
|
declaration: FileAstId<ast::Module>,
|
2022-07-21 14:05:52 +00:00
|
|
|
declaration_tree_id: ItemTreeId<Mod>,
|
2019-12-05 13:19:27 +00:00
|
|
|
definition: FileId,
|
|
|
|
},
|
|
|
|
Inline {
|
2022-07-21 14:05:52 +00:00
|
|
|
definition_tree_id: ItemTreeId<Mod>,
|
2023-08-10 17:04:46 +00:00
|
|
|
definition: FileAstId<ast::Module>,
|
2019-12-05 13:19:27 +00:00
|
|
|
},
|
2021-01-20 19:05:48 +00:00
|
|
|
/// Pseudo-module introduced by a block scope (contains only inner items).
|
|
|
|
BlockExpr {
|
2023-08-10 16:52:27 +00:00
|
|
|
id: BlockId,
|
2021-01-20 19:05:48 +00:00
|
|
|
block: AstId<ast::BlockExpr>,
|
|
|
|
},
|
2019-12-03 19:58:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ModuleOrigin {
|
2021-11-27 11:25:05 +00:00
|
|
|
pub fn declaration(&self) -> Option<AstId<ast::Module>> {
|
2019-12-03 19:58:29 +00:00
|
|
|
match self {
|
2023-08-10 17:04:46 +00:00
|
|
|
&ModuleOrigin::File { declaration, declaration_tree_id, .. } => {
|
|
|
|
Some(AstId::new(declaration_tree_id.file_id(), declaration))
|
|
|
|
}
|
|
|
|
&ModuleOrigin::Inline { definition, definition_tree_id } => {
|
|
|
|
Some(AstId::new(definition_tree_id.file_id(), definition))
|
|
|
|
}
|
2021-01-20 19:05:48 +00:00
|
|
|
ModuleOrigin::CrateRoot { .. } | ModuleOrigin::BlockExpr { .. } => None,
|
2019-12-03 19:58:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 13:37:39 +00:00
|
|
|
pub fn file_id(&self) -> Option<FileId> {
|
2019-12-03 19:58:29 +00:00
|
|
|
match self {
|
2019-12-05 13:33:29 +00:00
|
|
|
ModuleOrigin::File { definition, .. } | ModuleOrigin::CrateRoot { definition } => {
|
|
|
|
Some(*definition)
|
2019-12-05 13:19:27 +00:00
|
|
|
}
|
2019-12-03 19:58:29 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 13:42:57 +00:00
|
|
|
pub fn is_inline(&self) -> bool {
|
|
|
|
match self {
|
2021-01-20 19:05:48 +00:00
|
|
|
ModuleOrigin::Inline { .. } | ModuleOrigin::BlockExpr { .. } => true,
|
2020-01-15 13:42:57 +00:00
|
|
|
ModuleOrigin::CrateRoot { .. } | ModuleOrigin::File { .. } => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 19:58:29 +00:00
|
|
|
/// Returns a node which defines this module.
|
|
|
|
/// That is, a file or a `mod foo {}` with items.
|
2020-03-13 15:05:46 +00:00
|
|
|
fn definition_source(&self, db: &dyn DefDatabase) -> InFile<ModuleSource> {
|
2019-12-03 19:58:29 +00:00
|
|
|
match self {
|
2023-08-10 17:04:46 +00:00
|
|
|
&ModuleOrigin::File { definition, .. } | &ModuleOrigin::CrateRoot { definition } => {
|
|
|
|
let sf = db.parse(definition).tree();
|
|
|
|
InFile::new(definition.into(), ModuleSource::SourceFile(sf))
|
2019-12-03 19:58:29 +00:00
|
|
|
}
|
2023-08-10 17:04:46 +00:00
|
|
|
&ModuleOrigin::Inline { definition, definition_tree_id } => InFile::new(
|
|
|
|
definition_tree_id.file_id(),
|
|
|
|
ModuleSource::Module(
|
|
|
|
AstId::new(definition_tree_id.file_id(), definition).to_node(db.upcast()),
|
|
|
|
),
|
2020-03-13 15:05:46 +00:00
|
|
|
),
|
2023-08-10 16:52:27 +00:00
|
|
|
ModuleOrigin::BlockExpr { block, .. } => {
|
2021-01-20 19:05:48 +00:00
|
|
|
InFile::new(block.file_id, ModuleSource::BlockExpr(block.to_node(db.upcast())))
|
|
|
|
}
|
2019-12-03 19:58:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-12 17:00:17 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2019-10-31 15:45:10 +00:00
|
|
|
pub struct ModuleData {
|
2021-07-12 18:13:43 +00:00
|
|
|
/// Where does this module come from?
|
|
|
|
pub origin: ModuleOrigin,
|
|
|
|
/// Declared visibility of this module.
|
|
|
|
pub visibility: Visibility,
|
2023-06-27 06:22:05 +00:00
|
|
|
/// Parent module in the same `DefMap`.
|
|
|
|
///
|
|
|
|
/// [`None`] for block modules because they are always its `DefMap`'s root.
|
2019-11-23 13:49:53 +00:00
|
|
|
pub parent: Option<LocalModuleId>,
|
|
|
|
pub children: FxHashMap<Name, LocalModuleId>,
|
2019-12-20 14:45:12 +00:00
|
|
|
pub scope: ItemScope,
|
2019-10-31 15:45:10 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 19:18:05 +00:00
|
|
|
impl DefMap {
|
2023-06-13 10:59:52 +00:00
|
|
|
/// The module id of a crate or block root.
|
2023-06-01 12:46:36 +00:00
|
|
|
pub const ROOT: LocalModuleId = LocalModuleId::from_raw(la_arena::RawIdx::from_u32(0));
|
|
|
|
|
2021-01-18 19:18:05 +00:00
|
|
|
pub(crate) fn crate_def_map_query(db: &dyn DefDatabase, krate: CrateId) -> Arc<DefMap> {
|
2020-08-12 14:32:36 +00:00
|
|
|
let _p = profile::span("crate_def_map_query").detail(|| {
|
2020-10-20 13:38:11 +00:00
|
|
|
db.crate_graph()[krate].display_name.as_deref().unwrap_or_default().to_string()
|
2020-03-16 09:47:52 +00:00
|
|
|
});
|
2021-07-12 17:14:58 +00: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 21:22:54 +00:00
|
|
|
let def_map = DefMap::empty(krate, edition, ModuleData::new(origin, Visibility::Public));
|
2021-11-25 23:17:20 +00:00
|
|
|
let def_map = collector::collect_defs(
|
|
|
|
db,
|
|
|
|
def_map,
|
|
|
|
TreeId::new(crate_graph[krate].root_file_id.into(), None),
|
|
|
|
);
|
2021-07-12 17:14:58 +00:00
|
|
|
|
2021-01-21 14:22:17 +00:00
|
|
|
Arc::new(def_map)
|
|
|
|
}
|
|
|
|
|
2023-04-14 10:15:48 +00:00
|
|
|
pub(crate) fn block_def_map_query(db: &dyn DefDatabase, block_id: BlockId) -> Arc<DefMap> {
|
2023-08-10 18:10:19 +00:00
|
|
|
let block: BlockLoc = block_id.lookup(db);
|
2021-01-21 14:22:17 +00:00
|
|
|
|
2021-02-04 12:44:54 +00:00
|
|
|
let parent_map = block.module.def_map(db);
|
2022-07-14 21:22:54 +00: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.
|
2024-01-11 10:36:10 +00:00
|
|
|
let visibility = Visibility::Module(
|
|
|
|
ModuleId { krate, local_id, block: None },
|
|
|
|
VisibilityExplicity::Implicit,
|
|
|
|
);
|
2023-08-10 16:52:27 +00:00
|
|
|
let module_data = ModuleData::new(
|
|
|
|
ModuleOrigin::BlockExpr { block: block.ast_id, id: block_id },
|
|
|
|
visibility,
|
|
|
|
);
|
2022-07-14 21:22:54 +00:00
|
|
|
|
2023-06-01 13:04:38 +00:00
|
|
|
let mut def_map = DefMap::empty(krate, parent_map.data.edition, module_data);
|
2023-06-01 13:27:05 +00:00
|
|
|
def_map.data = parent_map.data.clone();
|
2023-06-01 12:46:36 +00:00
|
|
|
def_map.block = Some(BlockInfo {
|
|
|
|
block: block_id,
|
|
|
|
parent: BlockRelativeModuleId {
|
|
|
|
block: block.module.block,
|
|
|
|
local_id: block.module.local_id,
|
|
|
|
},
|
|
|
|
});
|
2021-01-21 14:22:17 +00:00
|
|
|
|
2023-08-10 18:10:19 +00:00
|
|
|
let def_map =
|
|
|
|
collector::collect_defs(db, def_map, TreeId::new(block.ast_id.file_id, Some(block_id)));
|
2023-04-14 10:15:48 +00:00
|
|
|
Arc::new(def_map)
|
2019-10-31 15:45:10 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 21:22:54 +00:00
|
|
|
fn empty(krate: CrateId, edition: Edition, module_data: ModuleData) -> DefMap {
|
2021-01-21 14:22:17 +00:00
|
|
|
let mut modules: Arena<ModuleData> = Arena::default();
|
2022-07-14 21:22:54 +00:00
|
|
|
let root = modules.alloc(module_data);
|
2023-06-01 12:46:36 +00:00
|
|
|
assert_eq!(root, Self::ROOT);
|
2021-07-12 18:13:43 +00:00
|
|
|
|
2021-01-21 14:22:17 +00:00
|
|
|
DefMap {
|
2021-01-21 16:04:50 +00:00
|
|
|
_c: Count::new(),
|
2021-01-25 18:02:05 +00:00
|
|
|
block: None,
|
2023-06-01 13:49:05 +00:00
|
|
|
modules,
|
2021-01-21 14:22:17 +00:00
|
|
|
krate,
|
2023-06-01 13:49:05 +00:00
|
|
|
prelude: None,
|
2023-05-11 06:41:24 +00:00
|
|
|
macro_use_prelude: FxHashMap::default(),
|
2022-07-26 09:53:50 +00:00
|
|
|
derive_helpers_in_scope: FxHashMap::default(),
|
2021-01-21 14:22:17 +00:00
|
|
|
diagnostics: Vec::new(),
|
2024-01-15 09:58:05 +00:00
|
|
|
enum_definitions: FxHashMap::default(),
|
2023-06-01 13:50:19 +00:00
|
|
|
data: Arc::new(DefMapCrateData {
|
2023-06-01 13:49:05 +00:00
|
|
|
extern_prelude: FxHashMap::default(),
|
2023-06-01 13:04:38 +00:00
|
|
|
exported_derives: FxHashMap::default(),
|
|
|
|
fn_proc_macro_mapping: FxHashMap::default(),
|
|
|
|
proc_macro_loading_error: None,
|
|
|
|
registered_attrs: Vec::new(),
|
|
|
|
registered_tools: Vec::new(),
|
|
|
|
unstable_features: FxHashSet::default(),
|
|
|
|
rustc_coherence_is_core: false,
|
|
|
|
no_core: false,
|
|
|
|
no_std: false,
|
|
|
|
edition,
|
2023-06-01 13:49:05 +00:00
|
|
|
recursion_limit: None,
|
2023-06-01 13:27:05 +00:00
|
|
|
}),
|
2021-01-21 14:22:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 13:49:53 +00:00
|
|
|
pub fn modules_for_file(&self, file_id: FileId) -> impl Iterator<Item = LocalModuleId> + '_ {
|
2019-11-15 07:26:31 +00:00
|
|
|
self.modules
|
|
|
|
.iter()
|
2019-12-03 19:58:29 +00:00
|
|
|
.filter(move |(_id, data)| data.origin.file_id() == Some(file_id))
|
2019-11-15 07:26:31 +00:00
|
|
|
.map(|(id, _data)| id)
|
|
|
|
}
|
2019-11-24 10:34:27 +00:00
|
|
|
|
2021-01-20 14:41:18 +00:00
|
|
|
pub fn modules(&self) -> impl Iterator<Item = (LocalModuleId, &ModuleData)> + '_ {
|
|
|
|
self.modules.iter()
|
|
|
|
}
|
2022-07-26 09:53:50 +00: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 13:56:50 +00:00
|
|
|
pub fn registered_tools(&self) -> &[SmolStr] {
|
2023-06-01 13:04:38 +00:00
|
|
|
&self.data.registered_tools
|
2022-01-06 13:56:50 +00:00
|
|
|
}
|
2022-07-26 09:53:50 +00:00
|
|
|
|
2022-01-06 13:56:50 +00:00
|
|
|
pub fn registered_attrs(&self) -> &[SmolStr] {
|
2023-06-01 13:04:38 +00:00
|
|
|
&self.data.registered_attrs
|
2022-01-06 13:56:50 +00:00
|
|
|
}
|
2022-07-26 09:53:50 +00:00
|
|
|
|
2022-09-06 18:20:49 +00:00
|
|
|
pub fn is_unstable_feature_enabled(&self, feature: &str) -> bool {
|
2023-06-01 13:04:38 +00:00
|
|
|
self.data.unstable_features.contains(feature)
|
2022-09-06 18:20:49 +00:00
|
|
|
}
|
|
|
|
|
2023-01-20 22:09:35 +00:00
|
|
|
pub fn is_rustc_coherence_is_core(&self) -> bool {
|
2023-06-01 13:04:38 +00:00
|
|
|
self.data.rustc_coherence_is_core
|
2023-01-20 22:09:35 +00:00
|
|
|
}
|
|
|
|
|
2023-05-26 20:41:32 +00:00
|
|
|
pub fn is_no_std(&self) -> bool {
|
2023-06-01 13:04:38 +00:00
|
|
|
self.data.no_std || self.data.no_core
|
2023-05-26 20:41:32 +00:00
|
|
|
}
|
|
|
|
|
2022-03-09 10:26:06 +00:00
|
|
|
pub fn fn_as_proc_macro(&self, id: FunctionId) -> Option<ProcMacroId> {
|
2023-06-01 13:04:38 +00:00
|
|
|
self.data.fn_proc_macro_mapping.get(&id).copied()
|
2022-03-09 00:13:38 +00:00
|
|
|
}
|
2022-07-26 09:53:50 +00:00
|
|
|
|
2022-06-15 16:04:39 +00:00
|
|
|
pub fn proc_macro_loading_error(&self) -> Option<&str> {
|
2023-06-01 13:04:38 +00:00
|
|
|
self.data.proc_macro_loading_error.as_deref()
|
2022-06-15 16:04:39 +00:00
|
|
|
}
|
2022-03-09 00:13:38 +00:00
|
|
|
|
2023-01-20 22:09:35 +00:00
|
|
|
pub fn krate(&self) -> CrateId {
|
2021-01-20 17:17:48 +00:00
|
|
|
self.krate
|
|
|
|
}
|
|
|
|
|
2021-02-02 11:25:13 +00:00
|
|
|
pub(crate) fn block_id(&self) -> Option<BlockId> {
|
2023-02-19 10:02:51 +00:00
|
|
|
self.block.map(|block| block.block)
|
2021-02-02 11:25:13 +00:00
|
|
|
}
|
|
|
|
|
2023-08-09 13:20:42 +00:00
|
|
|
pub(crate) fn prelude(&self) -> Option<(ModuleId, Option<UseId>)> {
|
2021-01-20 17:17:48 +00:00
|
|
|
self.prelude
|
|
|
|
}
|
|
|
|
|
2023-08-09 13:20:42 +00:00
|
|
|
pub(crate) fn extern_prelude(
|
|
|
|
&self,
|
|
|
|
) -> impl Iterator<Item = (&Name, (CrateRootModuleId, Option<ExternCrateId>))> + '_ {
|
|
|
|
self.data.extern_prelude.iter().map(|(name, &def)| (name, def))
|
2023-05-17 08:45:44 +00:00
|
|
|
}
|
|
|
|
|
2023-08-09 13:20:42 +00:00
|
|
|
pub(crate) fn macro_use_prelude(
|
|
|
|
&self,
|
|
|
|
) -> impl Iterator<Item = (&Name, (MacroId, Option<ExternCrateId>))> + '_ {
|
2023-06-13 10:59:52 +00:00
|
|
|
self.macro_use_prelude.iter().map(|(name, &def)| (name, def))
|
2021-01-20 17:17:48 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 14:21:33 +00:00
|
|
|
pub fn module_id(&self, local_id: LocalModuleId) -> ModuleId {
|
2023-02-19 10:02:51 +00:00
|
|
|
let block = self.block.map(|b| b.block);
|
2021-01-25 18:02:05 +00:00
|
|
|
ModuleId { krate: self.krate, local_id, block }
|
2021-01-25 14:21:33 +00:00
|
|
|
}
|
|
|
|
|
2023-06-13 10:59:52 +00:00
|
|
|
pub fn crate_root(&self) -> CrateRootModuleId {
|
|
|
|
CrateRootModuleId { krate: self.krate }
|
2021-02-02 17:02:12 +00:00
|
|
|
}
|
|
|
|
|
2019-11-24 10:34:27 +00:00
|
|
|
pub(crate) fn resolve_path(
|
|
|
|
&self,
|
2020-03-13 15:05:46 +00:00
|
|
|
db: &dyn DefDatabase,
|
2019-11-24 10:34:27 +00:00
|
|
|
original_module: LocalModuleId,
|
2019-12-13 11:12:36 +00:00
|
|
|
path: &ModPath,
|
2019-11-30 15:29:21 +00:00
|
|
|
shadow: BuiltinShadowMode,
|
2023-05-11 06:52:13 +00:00
|
|
|
expected_macro_subns: Option<MacroSubNs>,
|
2019-11-24 10:34:27 +00:00
|
|
|
) -> (PerNs, Option<usize>) {
|
2023-05-11 06:52:13 +00:00
|
|
|
let res = self.resolve_path_fp_with_macro(
|
|
|
|
db,
|
|
|
|
ResolveMode::Other,
|
|
|
|
original_module,
|
|
|
|
path,
|
|
|
|
shadow,
|
|
|
|
expected_macro_subns,
|
|
|
|
);
|
2019-11-24 10:34:27 +00:00
|
|
|
(res.resolved_def, res.segment_index)
|
|
|
|
}
|
2020-01-28 15:29:31 +00:00
|
|
|
|
2021-03-22 17:47:19 +00: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,
|
2023-05-11 06:52:13 +00:00
|
|
|
None, // Currently this function isn't used for macro resolution.
|
2021-03-22 17:47:19 +00:00
|
|
|
);
|
|
|
|
(res.resolved_def, res.segment_index)
|
|
|
|
}
|
|
|
|
|
2021-02-04 12:44:54 +00: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.
|
2023-06-13 10:59:52 +00:00
|
|
|
pub(crate) fn with_ancestor_maps<T>(
|
2021-01-27 18:16:29 +00:00
|
|
|
&self,
|
2021-02-04 12:44:54 +00:00
|
|
|
db: &dyn DefDatabase,
|
2021-01-27 18:16:29 +00:00
|
|
|
local_mod: LocalModuleId,
|
2021-02-04 12:44:54 +00: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 {
|
2023-06-01 12:46:36 +00:00
|
|
|
let parent = block_info.parent.def_map(db, self.krate);
|
2021-02-04 12:44:54 +00:00
|
|
|
if let Some(it) = f(&parent, block_info.parent.local_id) {
|
|
|
|
return Some(it);
|
|
|
|
}
|
|
|
|
block = parent.block;
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
2021-01-27 18:16:29 +00:00
|
|
|
}
|
2021-02-23 16:56:16 +00: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> {
|
2023-06-01 12:46:36 +00:00
|
|
|
let BlockRelativeModuleId { block, local_id } = self.block?.parent;
|
|
|
|
Some(ModuleId { krate: self.krate, block, local_id })
|
2021-02-23 16:56:16 +00:00
|
|
|
}
|
2021-01-27 18:16:29 +00:00
|
|
|
|
2023-03-14 12:42:08 +00:00
|
|
|
/// Returns the module containing `local_mod`, either the parent `mod`, or the module (or block) containing
|
2021-03-01 18:36:34 +00:00
|
|
|
/// the block, if `self` corresponds to a block expression.
|
|
|
|
pub fn containing_module(&self, local_mod: LocalModuleId) -> Option<ModuleId> {
|
2023-02-19 10:02:51 +00:00
|
|
|
match self[local_mod].parent {
|
|
|
|
Some(parent) => Some(self.module_id(parent)),
|
2023-06-01 12:46:36 +00:00
|
|
|
None => {
|
|
|
|
self.block.map(
|
|
|
|
|BlockInfo { parent: BlockRelativeModuleId { block, local_id }, .. }| {
|
|
|
|
ModuleId { krate: self.krate, block, local_id }
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2021-03-01 18:36:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-28 15:29:31 +00: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 12:44:54 +00:00
|
|
|
pub fn dump(&self, db: &dyn DefDatabase) -> String {
|
2020-01-28 15:29:31 +00:00
|
|
|
let mut buf = String::new();
|
2021-02-04 12:44:54 +00:00
|
|
|
let mut arc;
|
2021-01-21 14:22:17 +00:00
|
|
|
let mut current_map = self;
|
2023-02-19 10:02:51 +00:00
|
|
|
while let Some(block) = current_map.block {
|
2023-06-01 12:46:36 +00:00
|
|
|
go(&mut buf, db, current_map, "block scope", Self::ROOT);
|
2021-02-03 17:23:59 +00:00
|
|
|
buf.push('\n');
|
2023-06-01 12:46:36 +00:00
|
|
|
arc = block.parent.def_map(db, self.krate);
|
2023-02-19 10:02:51 +00:00
|
|
|
current_map = &arc;
|
2021-01-21 14:22:17 +00:00
|
|
|
}
|
2023-06-01 12:46:36 +00:00
|
|
|
go(&mut buf, db, current_map, "crate", Self::ROOT);
|
2020-07-20 15:44:44 +00:00
|
|
|
return buf;
|
2020-01-28 15:29:31 +00:00
|
|
|
|
2023-05-24 16:04:29 +00:00
|
|
|
fn go(
|
|
|
|
buf: &mut String,
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
map: &DefMap,
|
|
|
|
path: &str,
|
|
|
|
module: LocalModuleId,
|
|
|
|
) {
|
2020-07-20 15:44:44 +00:00
|
|
|
format_to!(buf, "{}\n", path);
|
2020-01-28 15:29:31 +00:00
|
|
|
|
2023-05-24 16:04:29 +00:00
|
|
|
map.modules[module].scope.dump(db.upcast(), buf);
|
2020-01-28 15:29:31 +00:00
|
|
|
|
2022-07-24 14:04:20 +00:00
|
|
|
for (name, child) in
|
|
|
|
map.modules[module].children.iter().sorted_by(|a, b| Ord::cmp(&a.0, &b.0))
|
|
|
|
{
|
2023-05-24 16:04:29 +00:00
|
|
|
let path = format!("{path}::{}", name.display(db.upcast()));
|
2020-07-20 15:44:44 +00:00
|
|
|
buf.push('\n');
|
2023-05-24 16:04:29 +00:00
|
|
|
go(buf, db, map, &path, *child);
|
2020-01-28 15:29:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-03 21:45:27 +00:00
|
|
|
|
2021-04-21 15:57:45 +00:00
|
|
|
pub fn dump_block_scopes(&self, db: &dyn DefDatabase) -> String {
|
|
|
|
let mut buf = String::new();
|
|
|
|
let mut arc;
|
|
|
|
let mut current_map = self;
|
2023-02-19 10:02:51 +00:00
|
|
|
while let Some(block) = current_map.block {
|
2021-04-21 15:57:45 +00:00
|
|
|
format_to!(buf, "{:?} in {:?}\n", block.block, block.parent);
|
2023-06-01 12:46:36 +00:00
|
|
|
arc = block.parent.def_map(db, self.krate);
|
2023-02-19 10:02:51 +00:00
|
|
|
current_map = &arc;
|
2021-04-21 15:57:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
format_to!(buf, "crate scope\n");
|
|
|
|
buf
|
|
|
|
}
|
|
|
|
|
2021-04-03 21:45:27 +00:00
|
|
|
fn shrink_to_fit(&mut self) {
|
2021-04-04 00:56:11 +00:00
|
|
|
// Exhaustive match to require handling new fields.
|
|
|
|
let Self {
|
|
|
|
_c: _,
|
2023-05-11 06:41:24 +00:00
|
|
|
macro_use_prelude,
|
2021-04-04 00:56:11 +00:00
|
|
|
diagnostics,
|
|
|
|
modules,
|
2022-07-26 09:53:50 +00:00
|
|
|
derive_helpers_in_scope,
|
2021-04-04 00:56:11 +00:00
|
|
|
block: _,
|
|
|
|
krate: _,
|
|
|
|
prelude: _,
|
2023-06-01 13:27:05 +00:00
|
|
|
data: _,
|
2024-01-15 09:58:05 +00:00
|
|
|
enum_definitions,
|
2021-04-04 00:56:11 +00:00
|
|
|
} = self;
|
|
|
|
|
2023-05-11 06:41:24 +00:00
|
|
|
macro_use_prelude.shrink_to_fit();
|
2021-04-04 00:56:11 +00:00
|
|
|
diagnostics.shrink_to_fit();
|
|
|
|
modules.shrink_to_fit();
|
2022-07-26 09:53:50 +00:00
|
|
|
derive_helpers_in_scope.shrink_to_fit();
|
2024-01-15 09:58:05 +00:00
|
|
|
enum_definitions.shrink_to_fit();
|
2021-04-04 00:56:11 +00:00
|
|
|
for (_, module) in modules.iter_mut() {
|
2021-04-03 21:45:27 +00: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 20:31:59 +00:00
|
|
|
|
|
|
|
/// Get a reference to the def map's diagnostics.
|
|
|
|
pub fn diagnostics(&self) -> &[DefDiagnostic] {
|
|
|
|
self.diagnostics.as_slice()
|
|
|
|
}
|
2022-01-27 22:23:09 +00:00
|
|
|
|
2023-12-22 11:46:22 +00:00
|
|
|
pub fn recursion_limit(&self) -> u32 {
|
|
|
|
// 128 is the default in rustc
|
|
|
|
self.data.recursion_limit.unwrap_or(128)
|
2022-01-27 22:23:09 +00:00
|
|
|
}
|
2019-10-31 15:45:10 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 08:14:10 +00:00
|
|
|
impl ModuleData {
|
2021-07-12 18:13:43 +00:00
|
|
|
pub(crate) fn new(origin: ModuleOrigin, visibility: Visibility) -> Self {
|
2021-07-12 17:02:56 +00:00
|
|
|
ModuleData {
|
2021-07-12 18:13:43 +00:00
|
|
|
origin,
|
|
|
|
visibility,
|
2021-07-12 17:02:56 +00:00
|
|
|
parent: None,
|
|
|
|
children: FxHashMap::default(),
|
|
|
|
scope: ItemScope::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 08:14:10 +00:00
|
|
|
/// Returns a node which defines this module. That is, a file or a `mod foo {}` with items.
|
2020-03-13 15:05:46 +00:00
|
|
|
pub fn definition_source(&self, db: &dyn DefDatabase) -> InFile<ModuleSource> {
|
2019-12-03 19:58:29 +00:00
|
|
|
self.origin.definition_source(db)
|
2019-11-23 08:14:10 +00:00
|
|
|
}
|
|
|
|
|
2023-06-17 08:58:52 +00:00
|
|
|
/// Same as [`definition_source`] but only returns the file id to prevent parsing the ASt.
|
|
|
|
pub fn definition_source_file_id(&self) -> HirFileId {
|
|
|
|
match self.origin {
|
|
|
|
ModuleOrigin::File { definition, .. } | ModuleOrigin::CrateRoot { definition } => {
|
|
|
|
definition.into()
|
|
|
|
}
|
2023-08-10 17:04:46 +00:00
|
|
|
ModuleOrigin::Inline { definition_tree_id, .. } => definition_tree_id.file_id(),
|
2023-08-10 16:52:27 +00:00
|
|
|
ModuleOrigin::BlockExpr { block, .. } => block.file_id,
|
2023-06-17 08:58:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 08:14:10 +00:00
|
|
|
/// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
|
2019-12-03 19:58:29 +00:00
|
|
|
/// `None` for the crate root or block.
|
2020-03-13 15:05:46 +00:00
|
|
|
pub fn declaration_source(&self, db: &dyn DefDatabase) -> Option<InFile<ast::Module>> {
|
2019-12-03 19:58:29 +00:00
|
|
|
let decl = self.origin.declaration()?;
|
2020-03-13 15:05:46 +00:00
|
|
|
let value = decl.to_node(db.upcast());
|
2019-11-28 13:00:03 +00:00
|
|
|
Some(InFile { file_id: decl.file_id, value })
|
2019-11-23 08:14:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 20:58:38 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2019-12-03 20:24:02 +00:00
|
|
|
pub enum ModuleSource {
|
|
|
|
SourceFile(ast::SourceFile),
|
|
|
|
Module(ast::Module),
|
2021-01-20 19:05:48 +00:00
|
|
|
BlockExpr(ast::BlockExpr),
|
2019-12-03 20:24:02 +00:00
|
|
|
}
|
2023-05-11 06:52:13 +00:00
|
|
|
|
|
|
|
/// See `sub_namespace_match()`.
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub enum MacroSubNs {
|
|
|
|
/// Function-like macros, suffixed with `!`.
|
|
|
|
Bang,
|
|
|
|
/// Macros inside attributes, i.e. attribute macros and derive macros.
|
|
|
|
Attr,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MacroSubNs {
|
|
|
|
fn from_id(db: &dyn DefDatabase, macro_id: MacroId) -> Self {
|
|
|
|
let expander = match macro_id {
|
|
|
|
MacroId::Macro2Id(it) => it.lookup(db).expander,
|
|
|
|
MacroId::MacroRulesId(it) => it.lookup(db).expander,
|
|
|
|
MacroId::ProcMacroId(it) => {
|
|
|
|
return match it.lookup(db).kind {
|
|
|
|
ProcMacroKind::CustomDerive | ProcMacroKind::Attr => Self::Attr,
|
|
|
|
ProcMacroKind::FuncLike => Self::Bang,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Eager macros aren't *guaranteed* to be bang macros, but they *are* all bang macros currently.
|
|
|
|
match expander {
|
|
|
|
MacroExpander::Declarative
|
|
|
|
| MacroExpander::BuiltIn(_)
|
|
|
|
| MacroExpander::BuiltInEager(_) => Self::Bang,
|
|
|
|
MacroExpander::BuiltInAttr(_) | MacroExpander::BuiltInDerive(_) => Self::Attr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Quoted from [rustc]:
|
|
|
|
/// Macro namespace is separated into two sub-namespaces, one for bang macros and
|
|
|
|
/// one for attribute-like macros (attributes, derives).
|
|
|
|
/// We ignore resolutions from one sub-namespace when searching names in scope for another.
|
|
|
|
///
|
|
|
|
/// [rustc]: https://github.com/rust-lang/rust/blob/1.69.0/compiler/rustc_resolve/src/macros.rs#L75
|
|
|
|
fn sub_namespace_match(candidate: Option<MacroSubNs>, expected: Option<MacroSubNs>) -> bool {
|
|
|
|
match (candidate, expected) {
|
|
|
|
(Some(candidate), Some(expected)) => candidate == expected,
|
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
}
|