Skip to content

Commit 84283b1

Browse files
committed
Expand name resolution stub
1 parent 2585fd6 commit 84283b1

File tree

3 files changed

+138
-2
lines changed

3 files changed

+138
-2
lines changed

src/items/use-declarations.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,18 @@ r[items.use.ambiguities]
398398
r[items.use.ambiguities.intro]
399399
Some situations are an error when there is an ambiguity as to which name a `use` declaration refers. This happens when there are two name candidates that do not resolve to the same entity.
400400

401+
* except where shadowing is allowed
402+
r[names.resolution.early.imports.errors.ambiguity.globvsglob]
403+
* it is an error to name an item through ambiguous use declarations
404+
* two globs imports which both have an item matching that name where the items are different
405+
* this is still an error even if there is a third non glob binding resolution to an item with the same name
406+
* 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
407+
401408
r[items.use.ambiguities.glob]
402409
Glob imports are allowed to import conflicting names in the same namespace as long as the name is not used.
403410
For example:
404411

412+
405413
```rust
406414
mod foo {
407415
pub struct Qux;
@@ -442,6 +450,29 @@ fn main() {
442450
}
443451
```
444452

453+
r[names.resolution.early.imports.errors.ambiguity.builtin-attr]
454+
* it is an error to have a user defined attribute or derive macro with the same name as a builtin attribute (e.g. inline)
455+
* I think we may special case this one and allow certain kinds of
456+
ambiguities where the builtin-attr is shadowed by a user attribute (not
457+
sure if this actually exists or is just proposed, TODO investigate)
458+
r[names.resolution.early.imports.errors.ambiguity.derivehelper]
459+
* derive helpers used before their associated derive may not shadow other attributes or other derive helpers that are otherwise in scope after their derive
460+
r[names.resolution.early.imports.errors.ambiguity.textualvspathbasedscope]
461+
* path-based scope bindings for macros may not shadow textual scope bindings to macros
462+
* This is sort of an intersection between macros and imports, because at
463+
least in stable rust you can only get path-based macro resolutions from
464+
imports of mbe macros (and presumably from proc macro crates), but you
465+
can only get textual scope of macros from macro declarations
466+
* https://doc.rust-lang.org/nightly/reference/names/namespaces.html#r-names.namespaces.sub-namespaces.use-shadow
467+
r[names.resolution.early.imports.errors.ambiguity.globvsouter]
468+
* it is an error to shadow an outer name binding with a glob import
469+
r[names.resolution.early.imports.errors.ambiguity.globvsexpanded]
470+
* Grey Area
471+
r[names.resolution.early.imports.errors.ambiguity.moreexpandedvsouter]
472+
* it is an error for name bindings from macro expansions to shadow name bindings from outside of those expansions
473+
* I'm not sure if this is actually specifically tied to imports, I imagine
474+
this could be triggered by bindings to macro declarations just as easily
475+
445476
[`extern crate`]: extern-crates.md
446477
[`macro_rules`]: ../macros-by-example.md
447478
[`self`]: ../paths.md#self

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: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,102 @@
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.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

Comments
 (0)