Skip to content

Commit 2e2c9fd

Browse files
committed
Expand name resolution stub
1 parent 2585fd6 commit 2e2c9fd

File tree

2 files changed

+119
-2
lines changed

2 files changed

+119
-2
lines changed

src/macros-by-example.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,13 @@ fn foo() {
326326
// m!(); // Error: m is not in scope.
327327
```
328328

329+
* textual scope name bindings for macros may shadow path-based scope bindings
330+
to macros
331+
* (the later may be unresolved, in which case it doesn't matter if it's a
332+
macro, e.g. you could have an invalid import with the same name as a
333+
valid macro resolution via textual scope and the textual scope macro will
334+
shadow the invalid import) TODO: do we care to document this?
335+
329336
r[macro.decl.scope.macro_use]
330337
### The `macro_use` attribute
331338

src/names/name-resolution.md

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,114 @@
1+
r[names.resolution]
12
# Name resolution
23

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+
* [use glob shadowing]
45+
* [macro textual scope shadowing]
46+
47+
r[names.resolution.early.imports.errors]
48+
r[names.resolution.early.imports.errors.reserved-names]
49+
50+
the names cfg and cfg_attr are reserved in the macro attribute sub-namespace
51+
52+
* https://doc.rust-lang.org/nightly/reference/names/namespaces.html#r-names.namespaces.sub-namespaces
53+
54+
r[names.resolution.early.imports.errors.ambiguity]
55+
56+
* How does this relate to? https://doc.rust-lang.org/nightly/reference/items/use-declarations.html#ambiguities
57+
* Note that shadowing is an exception
58+
* Fill out the documentation for shadowing and ambiguity alongside these items, link to them in the name resolution chapter
59+
60+
* shadowing and ambiguity may or may not represent the same section or one may be a subsection of the other
61+
62+
r[names.resolution.early.imports.errors.ambiguity.builtin-attr]
63+
* it is an error to have a user defined attribute or derive macro with the same name as a builtin attribute (e.g. inline)
64+
r[names.resolution.early.imports.errors.ambiguity.derivehelper]
65+
* derive helpers used before their associated derive may not shadow other attributes or other derive helpers that are otherwise in scope after their derive
66+
r[names.resolution.early.imports.errors.ambiguity.textualvspathbasedscope]
67+
* path-based scope bindings for macros may not shadow textual scope bindings to macros
68+
* https://doc.rust-lang.org/nightly/reference/names/namespaces.html#r-names.namespaces.sub-namespaces.use-shadow
69+
r[names.resolution.early.imports.errors.ambiguity.globvsouter]
70+
* it is an error to shadow an outer name binding with a glob import
71+
r[names.resolution.early.imports.errors.ambiguity.globvsglob]
72+
* it is an error to name an item through ambiguous use declarations
73+
* two globs imports which both have an item matching that name where the items are different
74+
* this is still an error even if there is a third non glob binding resolution to an item with the same name
75+
* it is not an error to have two glob imports which include items which would be ambiguous so long as you do not name one of those items through the ambiguous glob imports
76+
r[names.resolution.early.imports.errors.ambiguity.globvsexpanded]
77+
* Grey Area
78+
r[names.resolution.early.imports.errors.ambiguity.moreexpandedvsouter]
79+
* it is an error for name bindings from macro expansions to shadow name bindings from outside of those expansions
80+
81+
r[names.resolution.early.macros]
82+
83+
* .visitation-order
84+
* derive helpers
85+
* not visited when resolving derive macros in the parent scope (starting scope)
86+
* derive helpers compat
87+
* always visited
88+
* macro rules bindings (textual scope macros)
89+
* always visited
90+
* modules (path-based scope macros)
91+
* always visited
92+
* macrouseprelude
93+
* not visited in 2018 and later when `#[no_implicit_prelude]` is present
94+
* stdlibprelude
95+
* always visited for macro resolutions
96+
* builtinattrs
97+
* always visited
98+
* .subnamespaces
99+
* 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
100+
* https://doc.rust-lang.org/nightly/reference/names/namespaces.html#r-names.namespaces.sub-namespaces
101+
102+
r[names.resolution.late]
103+
104+
r[names.resolution.type-dependent]
105+
106+
[use glob shadowing]: ../items/use-declarations.md#items.use.glob.shadowing
107+
[Macros]: ../macros.md
108+
[use declarations]: ../items/use-declarations.md
109+
[macro textual scope shadowing]: ../macros-by-example.md#macro.decl.scope.textual.shadow
110+
[`let` bindings]: ../statements.md#let-statements
111+
[item definitions]: ../items.md
112+
[namespaces]: ../names/namespaces.md
113+
[scope]: ../names/scopes.md
114+
[visibility]: ../visibility-and-privacy.md

0 commit comments

Comments
 (0)