Skip to content

Commit bdcf85f

Browse files
committed
rewrite bullet points as prose
1 parent 79a3a0d commit bdcf85f

File tree

1 file changed

+109
-39
lines changed

1 file changed

+109
-39
lines changed

src/names/name-resolution.md

Lines changed: 109 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,54 +10,117 @@ without conflict. Each name is valid within a [scope], or a region of source
1010
text where that name may be referenced. Access to certain names may be
1111
restricted based on their [visibility].
1212

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-
* `Type::assoc_item`, `<Type>::assoc_item`, `<Enum>::Variant` and `EnumTyAlias::Variant` are resolved during type checking
17-
* `Trait::assoc_item`, `<Type as Trait>::assoc_item` and `Enum::Variant` are resolved during late resolution
18-
* This stage of resolution is known as type-relative resolution.
19-
* in reality this is never talked about so I doubt it has a name yet.
20-
* All other names are resolved during AST lowering.
21-
* This stage of resolution is known as "Late Resolution".
22-
* Note, late resolution occurs before type dependent resolution.
13+
Name resolution is split into three stages throughout the compilation process.
14+
The first stage, Expansion-time resolution, resolves all [use declarations] and
15+
[macro invocations]. The second stage, Primary resolution, resolves all names
16+
that have not yet been resolved that do not depend on type information to
17+
resolve. The last stage, Type-relative resolution, resolves the remaining names
18+
once type information is available.
19+
20+
> Note
21+
>
22+
> * Expansion-time resolution is also known as "Early Resolution"
23+
> * Primary resolution is also known as "Late Resolution"
24+
25+
r[names.resolution.expansion]
26+
## Expansion-time name resolution
27+
28+
r[names.resolution.expansion.intro]
29+
30+
Expansion-time name resolution is the stage of name resolution necessary to
31+
complete macro expansion and fully generate a crate's AST. This stage requires
32+
the resolution of macro invocations and use declarations. Resolving use
33+
declarations is required to resolve [path-based scope] macro invocations.
34+
Resolving macro invocations is required in order to expand them.
35+
36+
The expansion process is iterative, alternately resolving imports, resolving
37+
and expanding macro invocations, then repeating until there are no further
38+
macros invocations to resolve. Once this process is completed all the imports
39+
are resolved again to ensure that the macro expansion process did not introduce
40+
any new ambiguious imports.
41+
42+
TODO: do we want to talk about this? feels like an implementation detail but
43+
also really helps to understand certain kinds of ambiguity errors that users
44+
can run into.
45+
46+
> Note
47+
>
48+
> This causes so called time traveling ambiguities, such as when a glob import introduces an item that is ambiguous with its own base path.
49+
>
50+
```rust
51+
macro_rules! m {
52+
() => { mod bar {} }
53+
}
54+
55+
mod bar {
56+
pub(crate) use m;
57+
}
58+
59+
fn f() {
60+
// * initially speculatively resolve bar to the module in the crate root
61+
// * expansion of m introduces a second bar module inside the body of f
62+
// * expansion-time resolution finalizes resolutions by re-resolving all
63+
// imports and macro invocations, sees the introduced ambiguity
64+
// and reports it as an error
65+
bar::m!(); // ERROR `bar` is ambiguous
66+
}
67+
```
68+
69+
TODO I would like to be able to link to a path-based scope section that
70+
discusses the various kinds of macros that can be invoked via path-based scope.
71+
Right now the section I know of off of the top of my head lives in the macros
72+
by example chapter.
2373

24-
r[names.resolution.early]
25-
## Early name resolution
74+
r[names.resolution.expansion.imports]
2675

27-
r[names.resolution.early.intro]
76+
All use declarations are fully resolved during this stage of resolution.
77+
Type-relative paths cannot be resolved at this stage of compilation and will
78+
produce an error.
2879

29-
* early name resolution is the part of name resolution that happens during macro expansion
30-
* early name resolution includes the resolution of imports and macros
31-
* early name resolution is the minimum amount of resolution required to resolve macro invocations so they can be expanded.
32-
* resolving imports is necessary to resolve macro invocations
33-
* specifically for path-based scope macro resolutions, this can occur
34-
either because of a `#[macro_export]`, a proc macro definition, or a
35-
reexport of a macro in 2018 or later code.
36-
* resolving macro invocations and tying them to macro declarations is necessary so they can be expanded
37-
* this process is iterative and repeats until there are no remaining unexpanded macro invocations (fixed point algorithm)
38-
* Post expansion these resolutions are checked again to ensure no new ambiguities were introduced by the expansion process
39-
* This causes so called time traveling ambiguities, such as when a glob import introduces an item that is ambiguous with its own base path.
80+
* `Type::assoc_item`, `<Type>::assoc_item`, `<Enum>::Variant` and `EnumTyAlias::Variant` are resolved during type checking
81+
* `Trait::assoc_item`, `<Type as Trait>::assoc_item` and `Enum::Variant` are resolved during late resolution
4082

41-
TODO Document some time traveling ambiguitie examples, place in relevant ambiguity section
83+
```rust
84+
mod my_mod {
85+
pub const Const: () = ();
4286

43-
r[names.resolution.early.imports]
87+
pub enum MyEnum {
88+
MyVariant
89+
}
4490

45-
* All imports are fully resolved at this point.
46-
* 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.
91+
impl MyEnum {
92+
pub const Const: () = ();
93+
}
4794

48-
TODO example of unsupported type dependent import
95+
pub type TypeAlias = MyEnum;
96+
}
4997

50-
r[names.resolution.early.imports.shadowing]
98+
fn foo() {
99+
use my_mod::MyEnum; // OK
100+
use my_mod::MyEnum::MyVariant; // OK
101+
use my_mod::TypeAlias; // OK
102+
use my_mod::TypeAlias::MyVariant; // Doesn't work
103+
use my_mod::MyEnum::Const; // Doesn't work
104+
use my_mod::Const; // OK
105+
let _ = my_mod::TypeAlias::MyVariant; // OK
106+
let _ = my_mod::MyEnum::Const; // OK
107+
}
108+
```
109+
110+
r[names.resolution.expansion.imports.shadowing]
51111

52112
The following is a list of situations where shadowing of use declarations is permitted:
53113

54114
* [use glob shadowing]
55115
* [macro textual scope shadowing]
56116

57-
r[names.resolution.early.imports.errors]
58-
r[names.resolution.early.imports.errors.ambiguity]
117+
r[names.resolution.expansion.imports.errors]
118+
r[names.resolution.expansion.imports.errors.ambiguity]
59119

60-
* shadowing and ambiguity may or may not represent the same section or one may be a subsection of the other
120+
TODO shadowing and ambiguity may or may not represent the same section or one may be a subsection of the other
121+
122+
The following is a list of situations where shadowing of use declarations is
123+
_NOT_ permitted, otherwise known as ambiguity errors:
61124

62125
* Builtin Attributes
63126
* Derive Helpers
@@ -74,19 +137,26 @@ r[items.use.ambiguities]
74137
> This section is incomplete.
75138
76139
r[items.use.ambiguities.intro]
77-
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.
140+
Some situations are an error when there is an ambiguity as to which name a
141+
`use` declaration refers. This happens when there are two name candidates that
142+
do not resolve to the same entity where neither import is
143+
[permitted](names.resolution.expansion.imports.shadowing) to shadow the other.
78144

79-
* except where shadowing is allowed
80145
r[names.resolution.early.imports.errors.ambiguity.globvsglob]
81146
* it is an error to name an item through ambiguous use declarations
82-
* two globs imports which both have an item matching that name where the items are different
83-
* this is still an error even if there is a third non glob binding resolution to an item with the same name
147+
* two globs imports which both have an item matching that name where the items are different
148+
* this is still an error even if there is a third non glob binding resolution to an item with the same name
84149
* 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
85150

86151
r[items.use.ambiguities.glob]
87152
Glob imports are allowed to import conflicting names in the same namespace as long as the name is not used.
88153
For example:
89154

155+
TODO: move this section? It's documenting a situation that _isnt_ an ambiguity
156+
error. I've been working off of a pattern I think I saw in a few other
157+
locations, where we have specific error sections that document all of the
158+
reference relevant error cases associated with an some part of the language.
159+
90160
```rust
91161
mod foo {
92162
pub struct Qux;
@@ -267,7 +337,7 @@ pub fn foo() {
267337
r[names.resolution.early.imports.errors.ambiguity.globvsexpanded]
268338
* Grey Area
269339

270-
r[names.resolution.early.macros]
340+
r[names.resolution.expansion.macros]
271341

272342
* .visitation-order
273343
* derive helpers
@@ -289,7 +359,7 @@ r[names.resolution.early.macros]
289359
* 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
290360
* https://doc.rust-lang.org/nightly/reference/names/namespaces.html#r-names.namespaces.sub-namespaces
291361

292-
r[names.resolution.early.macros.errors.reserved-names]
362+
r[names.resolution.expansion.macros.errors.reserved-names]
293363

294364
the names cfg and cfg_attr are reserved in the macro attribute sub-namespace
295365

0 commit comments

Comments
 (0)