pub struct DefMap {
krate: Crate,
block: Option<BlockInfo>,
pub root: ModuleId,
pub modules: ModulesMap,
prelude: Option<(ModuleId, Option<UseId>)>,
macro_use_prelude: FxHashMap<Name, (MacroId, Option<ExternCrateId>)>,
derive_helpers_in_scope: FxHashMap<AstId<Item>, Vec<(Name, MacroId, Either<MacroCallId, BuiltinDeriveImplId>)>>,
pub macro_def_to_macro_id: FxHashMap<ErasedAstId, MacroId>,
diagnostics: Vec<DefDiagnostic>,
data: Arc<DefMapCrateData>,
}Expand description
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.
Fields§
§krate: CrateThe crate this DefMap belongs to.
block: Option<BlockInfo>When this is a block def map, this will hold the block id of the block and module that contains this block.
root: ModuleId§modules: ModulesMapThe modules and their data declared in this crate.
prelude: Option<(ModuleId, Option<UseId>)>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).
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).
macro_use_prelude: FxHashMap<Name, (MacroId, Option<ExternCrateId>)>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.
ExternCrateId being None implies it being imported from the general prelude import.
derive_helpers_in_scope: FxHashMap<AstId<Item>, Vec<(Name, MacroId, Either<MacroCallId, BuiltinDeriveImplId>)>>Tracks which custom derives are in scope for an item, to allow resolution of derive helper attributes.
macro_def_to_macro_id: FxHashMap<ErasedAstId, MacroId>A mapping from [hir_expand::MacroDefId] to crate::MacroId.
diagnostics: Vec<DefDiagnostic>The diagnostics that need to be emitted for this crate.
data: Arc<DefMapCrateData>The crate data that is shared between a crate’s def map and all its block def maps.
Implementations§
Source§impl DefMap
impl DefMap
Sourcepub(crate) fn resolve_attr_macro(
&self,
local_def_map: &LocalDefMap,
db: &dyn SourceDatabase,
original_module: ModuleId,
ast_id: AstIdWithPath<Item>,
attr: &Attr,
attr_id: AttrId,
macro_depth: u32,
) -> Result<ResolvedAttr, UnresolvedMacro>
pub(crate) fn resolve_attr_macro( &self, local_def_map: &LocalDefMap, db: &dyn SourceDatabase, original_module: ModuleId, ast_id: AstIdWithPath<Item>, attr: &Attr, attr_id: AttrId, macro_depth: u32, ) -> Result<ResolvedAttr, UnresolvedMacro>
This cannot be used to resolve items that allow derives.
pub(crate) fn is_builtin_or_registered_attr(&self, path: &ModPath) -> bool
Source§impl DefMap
impl DefMap
pub(crate) fn resolve_visibility( &self, local_def_map: &LocalDefMap, db: &dyn SourceDatabase, original_module: ModuleId, visibility: &RawVisibility, within_impl: bool, ) -> Option<Visibility>
pub(super) fn resolve_path_fp_with_macro( &self, local_def_map: &LocalDefMap, db: &dyn SourceDatabase, mode: ResolveMode, original_module: ModuleId, path: &ModPath, shadow: BuiltinShadowMode, expected_macro_subns: Option<MacroSubNs>, ) -> ResolvePathResult
pub(super) fn resolve_path_fp_with_macro_single( &self, local_def_map: &LocalDefMap, db: &dyn SourceDatabase, mode: ResolveMode, original_module: ModuleId, path: &ModPath, shadow: BuiltinShadowMode, expected_macro_subns: Option<MacroSubNs>, ) -> ResolvePathResult
Sourcepub(super) fn resolve_path_fp_in_all_preludes(
&self,
local_def_map: &LocalDefMap,
db: &dyn SourceDatabase,
mode: ResolveMode,
original_module: ModuleId,
path: &ModPath,
shadow: BuiltinShadowMode,
) -> ResolvePathResult
pub(super) fn resolve_path_fp_in_all_preludes( &self, local_def_map: &LocalDefMap, db: &dyn SourceDatabase, mode: ResolveMode, original_module: ModuleId, path: &ModPath, shadow: BuiltinShadowMode, ) -> ResolvePathResult
Resolves a path only in the preludes, without accounting for item scopes.
Sourcefn resolve_path_abs<'a>(
&self,
local_def_map: &LocalDefMap,
segments: &mut impl Iterator<Item = (usize, &'a Name)>,
path: &ModPath,
) -> Either<PerNs, ReachedFixedPoint> ⓘ
fn resolve_path_abs<'a>( &self, local_def_map: &LocalDefMap, segments: &mut impl Iterator<Item = (usize, &'a Name)>, path: &ModPath, ) -> Either<PerNs, ReachedFixedPoint> ⓘ
2018-style absolute path – only extern prelude
fn resolve_remaining_segments<'a>( &self, db: &dyn SourceDatabase, mode: ResolveMode, segments: impl Iterator<Item = (usize, &'a Name)>, curr_per_ns: PerNs, path: &ModPath, shadow: BuiltinShadowMode, original_module: ModuleId, ) -> ResolvePathResult
fn resolve_name_in_module( &self, local_def_map: &LocalDefMap, db: &dyn SourceDatabase, module: ModuleId, name: &Name, shadow: BuiltinShadowMode, expected_macro_subns: Option<MacroSubNs>, ) -> PerNs
fn resolve_name_in_all_preludes( &self, local_def_map: &LocalDefMap, db: &dyn SourceDatabase, name: &Name, ) -> PerNs
fn resolve_name_in_extern_prelude( &self, local_def_map: &LocalDefMap, name: &Name, ) -> PerNs
fn resolve_in_macro_use_prelude(&self, name: &Name) -> PerNs
fn resolve_name_in_crate_root_or_extern_prelude( &self, local_def_map: &LocalDefMap, db: &dyn SourceDatabase, module: ModuleId, name: &Name, ) -> PerNs
fn resolve_in_prelude(&self, db: &dyn SourceDatabase, name: &Name) -> PerNs
Source§impl DefMap
impl DefMap
pub fn edition(&self) -> Edition
fn empty( db: &dyn SourceDatabase, krate: Crate, crate_data: Arc<DefMapCrateData>, module_data: ModuleData, block: Option<BlockInfo>, ) -> DefMap
fn shrink_to_fit(&mut self)
Source§impl DefMap
impl DefMap
Sourcepub fn modules_for_file<'a>(
&'a self,
db: &'a dyn SourceDatabase,
file_id: FileId,
) -> impl Iterator<Item = ModuleId> + 'a
pub fn modules_for_file<'a>( &'a self, db: &'a dyn SourceDatabase, file_id: FileId, ) -> impl Iterator<Item = ModuleId> + 'a
Returns all modules in the crate that are associated with the given file.
pub fn modules(&self) -> impl Iterator<Item = (ModuleId, &ModuleData)> + '_
Sourcepub fn inline_modules_for_macro_file(
&self,
file_id: MacroCallId,
) -> impl Iterator<Item = ModuleId> + '_
pub fn inline_modules_for_macro_file( &self, file_id: MacroCallId, ) -> impl Iterator<Item = ModuleId> + '_
Returns all inline modules (mod name { … }) in the crate that are associated with the given macro expansion.
pub fn derive_helpers_in_scope( &self, id: AstId<Adt>, ) -> Option<&[(Name, MacroId, Either<MacroCallId, BuiltinDeriveImplId>)]>
pub fn registered_tools(&self) -> &[Symbol]
pub fn features(&self) -> &UnstableFeatures
pub fn is_rustc_coherence_is_core(&self) -> bool
pub fn is_no_std(&self) -> bool
pub fn is_no_core(&self) -> bool
pub fn fn_as_proc_macro(&self, id: FunctionId) -> Option<ProcMacroId>
pub fn proc_macro_as_fn(&self, id: ProcMacroId) -> Option<FunctionId>
pub fn krate(&self) -> Crate
pub fn crate_root(&self, db: &dyn SourceDatabase) -> ModuleId
Sourcepub fn root_module_id(&self) -> ModuleId
pub fn root_module_id(&self) -> ModuleId
This is the same as Self::crate_root for crate def maps, but for block def maps, it
returns the root block module.
Sourcepub fn parent(&self) -> Option<ModuleId>
pub fn parent(&self) -> Option<ModuleId>
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).
Sourcepub fn containing_module(&self, local_mod: ModuleIdLt<'_>) -> Option<ModuleId>
pub fn containing_module(&self, local_mod: ModuleIdLt<'_>) -> Option<ModuleId>
Returns the module containing local_mod, either the parent mod, or the module (or block) containing
the block, if self corresponds to a block expression.
Sourcepub fn diagnostics(&self) -> &[DefDiagnostic]
pub fn diagnostics(&self) -> &[DefDiagnostic]
Get a reference to the def map’s diagnostics.
pub fn recursion_limit(&self) -> u32
pub fn dump(&self, db: &dyn SourceDatabase) -> String
Source§impl DefMap
impl DefMap
pub(crate) fn block_id(&self) -> Option<BlockId>
pub(crate) fn prelude(&self) -> Option<(ModuleId, Option<UseId>)>
pub(crate) fn macro_use_prelude( &self, ) -> &FxHashMap<Name, (MacroId, Option<ExternCrateId>)>
pub(crate) fn resolve_path( &self, local_def_map: &LocalDefMap, db: &dyn SourceDatabase, original_module: ModuleId, path: &ModPath, shadow: BuiltinShadowMode, expected_macro_subns: Option<MacroSubNs>, ) -> (PerNs, Option<usize>)
Sourcepub(crate) fn resolve_path_locally(
&self,
local_def_map: &LocalDefMap,
db: &dyn SourceDatabase,
original_module: ModuleId,
path: &ModPath,
shadow: BuiltinShadowMode,
) -> (PerNs, Option<usize>, ResolvePathResultPrefixInfo)
pub(crate) fn resolve_path_locally( &self, local_def_map: &LocalDefMap, db: &dyn SourceDatabase, original_module: ModuleId, path: &ModPath, shadow: BuiltinShadowMode, ) -> (PerNs, Option<usize>, ResolvePathResultPrefixInfo)
The first Option<usize> points at the Enum segment in case of Enum::Variant, the second
points at the unresolved segments.
Sourcepub(crate) fn with_ancestor_maps<'db, T>(
&self,
db: &'db dyn SourceDatabase,
local_mod: ModuleIdLt<'db>,
f: &mut dyn FnMut(&DefMap, ModuleIdLt<'db>) -> Option<T>,
) -> Option<T>
pub(crate) fn with_ancestor_maps<'db, T>( &self, db: &'db dyn SourceDatabase, local_mod: ModuleIdLt<'db>, f: &mut dyn FnMut(&DefMap, ModuleIdLt<'db>) -> Option<T>, ) -> Option<T>
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.
Trait Implementations§
impl Eq for DefMap
Source§impl Index<ModuleIdLt<'_>> for DefMap
impl Index<ModuleIdLt<'_>> for DefMap
Source§type Output = ModuleData
type Output = ModuleData
Source§fn index(&self, id: ModuleIdLt<'_>) -> &ModuleData
fn index(&self, id: ModuleIdLt<'_>) -> &ModuleData
container[index]) operation. Read moreSource§impl IndexMut<ModuleIdLt<'static>> for DefMap
impl IndexMut<ModuleIdLt<'static>> for DefMap
impl StructuralPartialEq for DefMap
Auto Trait Implementations§
impl Freeze for DefMap
impl RefUnwindSafe for DefMap
impl Send for DefMap
impl Sync for DefMap
impl Unpin for DefMap
impl UnsafeUnpin for DefMap
impl UnwindSafe for DefMap
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more