Skip to content

Commit 154c958

Browse files
committed
Expand name resolution stub
1 parent 2585fd6 commit 154c958

File tree

2 files changed

+121
-2
lines changed

2 files changed

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

0 commit comments

Comments
 (0)