Skip to main content

Module nameres

Module nameres 

Source
Expand description

This module implements import-resolution/macro expansion algorithm.

The result of this module is DefMap: a data structure which contains:

  • a tree of modules for the crate
  • for each module, a set of items visible in the module (directly declared or imported)

Note that DefMap contains fully macro expanded code.

Computing DefMap can be partitioned into several logically independent “phases”. The phases are mutually recursive though, there’s no strict ordering.

§Collecting RawItems

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).

§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 path and, upon success, we run macro expansion and “collect module” phase on the result

Modules§

assoc
Expansion of associated items
attr_resolution
Post-nameres attribute resolution.
collector 🔒
The core of the module-level name resolution algorithm.
diagnostics
Diagnostics emitted during DefMap construction.
mod_resolution 🔒
This module resolves mod foo; declaration to file.
path_resolution 🔒
This module implements a function to resolve a path foo::bar::baz to a def, which is used within name resolution.
proc_macro
Nameres-specific procedural macro data and helpers.

Structs§

BlockInfo 🔒
For DefMaps computed for a block expression, this stores its location in the parent map.
DefMap
Contains the results of (early) name resolution.
DefMapCrateData 🔒
Data that belongs to a crate which is shared between a crate’s def map and all its block def maps.
DefMapPair 🔒
LocalDefMap
Parts of the def map that are only needed when analyzing code in the same crate.
ModuleData
ModulesMap
A newtype wrapper around FxHashMap<ModuleId, ModuleData> that implements IndexMut.
ResolvePathResultPrefixInfo

Enums§

MacroSubNs
See sub_namespace_match().
ModuleOrigin
ModuleSource

Constants§

PREDEFINED_TOOLS 🔒

Functions§

block_def_map
crate_def_map
crate_local_def_map 🔒
enable_builtin_derive_fast_path 🔒
macro_styles_from_id 🔒
sub_namespace_match 🔒
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.