|
| 1 | +r[names.resolution] |
1 | 2 | # Name resolution |
2 | 3 |
|
3 | | -> [!NOTE] |
4 | | -> This is a placeholder for future expansion. |
| 4 | +r[names.resolution.intro] |
| 5 | + |
| 6 | +_Name resolution_ is the process of tying paths and other identifiers to the |
| 7 | +declarations of those entities. Names are segregated into different |
| 8 | +[namespaces], allowing entities in different namespaces to share the same name |
| 9 | +without conflict. Each name is valid within a [scope], or a region of source |
| 10 | +text where that name may be referenced. Access to certain names may be |
| 11 | +restricted based on their [visibility]. |
| 12 | + |
| 13 | +* Names are resolved at three different stages of compilation. |
| 14 | +* [Macros] and [use declarations] are resolved during macro expansion. |
| 15 | + * This stage of resolution is known as "Early Resolution". |
| 16 | +* Associated consts and functions, methods, and enum variants are resolved during type checking. |
| 17 | + * This stage of resolution is known as type dependent resolution. |
| 18 | + * in reality this is never talked about so I doubt it has a name yet. |
| 19 | +* All other names are resolved during AST lowering. |
| 20 | + * This stage of resolution is known as "Late Resolution". |
| 21 | + * Note, late resolution occurs before type dependent resolution. |
| 22 | + |
| 23 | +r[names.resolution.early] |
| 24 | +## Early name resolution |
| 25 | + |
| 26 | +r[names.resolution.early.intro] |
| 27 | + |
| 28 | +* early name resolution is the part of name resolution that happens during macro expansion |
| 29 | +* early name resolution includes the resolution of imports and macros |
| 30 | +* early name resolution is the minimum amount of resolution required to resolve macro invocations so they can be expanded. |
| 31 | +* resolving imports is necessary to resolve macro invocations (as of 2018 edition i think, pretty sure this applies specifically to path-based scope for macros) |
| 32 | +* resolving macro invocations and tying them to macro declarations is necessary so they can be expanded |
| 33 | +* this process is iterative and repeats until there are no remaining unexpanded macro invocations (fixed point algorithm) |
| 34 | +* Post expansion these resolutions are checked again to ensure no new ambiguities were introduced by the expansion process |
| 35 | + * This causes so called time traveling ambiguities, such as when a glob import introduces an item that is ambiguous with its own base path. |
| 36 | + |
| 37 | +r[names.resolution.early.imports] |
| 38 | + |
| 39 | +* All imports are fully resolved at this point. |
| 40 | + * imports of names that cannot be fully resolved during macro expansion, such as those depending on type information, are not supported and will produce an error. |
| 41 | + |
| 42 | +r[names.resolution.early.imports.shadowing] |
| 43 | + |
| 44 | +The following is a list of situations where shadowing of use declarations is permitted: |
| 45 | + |
| 46 | +* [use glob shadowing] |
| 47 | +* [macro textual scope shadowing] |
| 48 | + |
| 49 | +r[names.resolution.early.imports.errors] |
| 50 | +r[names.resolution.early.imports.errors.ambiguity] |
| 51 | + |
| 52 | +* shadowing and ambiguity may or may not represent the same section or one may be a subsection of the other |
| 53 | + |
| 54 | +* Builtin Attributes |
| 55 | +* Derive Helpers |
| 56 | +* Textual Vs Path-based Scope |
| 57 | +* Glob vs Outer |
| 58 | +* Glob vs Glob |
| 59 | +* ~~Glob vs Expanded~~ pretty certain we don't want to mention this one |
| 60 | +* More Expanded vs Outer |
| 61 | + |
| 62 | +r[names.resolution.early.macros] |
| 63 | + |
| 64 | +* .visitation-order |
| 65 | + * derive helpers |
| 66 | + * not visited when resolving derive macros in the parent scope (starting scope) |
| 67 | + * derive helpers compat |
| 68 | + * always visited |
| 69 | + * macro rules bindings (textual scope macros) |
| 70 | + * always visited |
| 71 | + * modules (path-based scope macros) |
| 72 | + * always visited |
| 73 | + * macrouseprelude |
| 74 | + * not visited in 2018 and later when `#[no_implicit_prelude]` is present |
| 75 | + * stdlibprelude |
| 76 | + * always visited for macro resolutions |
| 77 | + * builtinattrs |
| 78 | + * always visited |
| 79 | +* .subnamespaces |
| 80 | + * macros are split into two subnamespaces, one for bang macros, and the other for attributes and derives. Resolution candidates from the incorrect subnamespace are ignored |
| 81 | + * https://doc.rust-lang.org/nightly/reference/names/namespaces.html#r-names.namespaces.sub-namespaces |
| 82 | + |
| 83 | +r[names.resolution.early.macros.errors.reserved-names] |
| 84 | + |
| 85 | +the names cfg and cfg_attr are reserved in the macro attribute sub-namespace |
| 86 | + |
| 87 | +* https://doc.rust-lang.org/nightly/reference/names/namespaces.html#r-names.namespaces.sub-namespaces |
| 88 | + |
| 89 | + |
| 90 | +r[names.resolution.late] |
| 91 | + |
| 92 | +r[names.resolution.type-dependent] |
| 93 | + |
| 94 | +[use glob shadowing]: ../items/use-declarations.md#items.use.glob.shadowing |
| 95 | +[Macros]: ../macros.md |
| 96 | +[use declarations]: ../items/use-declarations.md |
| 97 | +[macro textual scope shadowing]: ../macros-by-example.md#macro.decl.scope.textual.shadow |
| 98 | +[`let` bindings]: ../statements.md#let-statements |
| 99 | +[item definitions]: ../items.md |
| 100 | +[namespaces]: ../names/namespaces.md |
| 101 | +[scope]: ../names/scopes.md |
| 102 | +[visibility]: ../visibility-and-privacy.md |
0 commit comments