Skip to content

Commit 5397d08

Browse files
committed
rewrite bullet points as prose
1 parent b93b3a9 commit 5397d08

File tree

2 files changed

+110
-37
lines changed

2 files changed

+110
-37
lines changed

src/items/use-declarations.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,11 @@ r[items.use.ambiguities]
396396
> This section is incomplete.
397397
398398
r[items.use.ambiguities.intro]
399-
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.
399+
Some situations are an error when there is an ambiguity as to which name a
400+
`use` declaration refers. This happens when there are two name candidates that
401+
do not resolve to the same entity where neither import is
402+
[permitted](names.resolution.expansion.imports.shadowing) to shadow the other.
400403

401-
* except where shadowing is allowed
402404
r[names.resolution.early.imports.errors.ambiguity.globvsglob]
403405
* it is an error to name an item through ambiguous use declarations
404406
* two globs imports which both have an item matching that name where the items are different
@@ -409,6 +411,11 @@ r[items.use.ambiguities.glob]
409411
Glob imports are allowed to import conflicting names in the same namespace as long as the name is not used.
410412
For example:
411413

414+
TODO: move this section? It's documenting a situation that _isnt_ an ambiguity
415+
error. I've been working off of a pattern I think I saw in a few other
416+
locations, where we have specific error sections that document all of the
417+
reference relevant error cases associated with an some part of the language.
418+
412419
```rust
413420
mod foo {
414421
pub struct Qux;

src/names/name-resolution.md

Lines changed: 101 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,54 +10,120 @@ 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".
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 foo {
56+
pub mod bar {
57+
pub(crate) use m;
58+
}
59+
}
60+
61+
fn f() {
62+
use foo::*;
63+
// * initially speculatively resolve bar from foo via glob import
64+
// * expansion of m introduces a second bar module
65+
// * expansion-time resolution finalizes resolutions by re-resolving all
66+
// imports and macro invocations, sees the introduced ambiguity
67+
// and reports it as an error
68+
bar::m!(); // ERROR `bar` is ambiguous
69+
}
70+
```
71+
72+
TODO I would like to be able to link to a path-based scope section that
73+
discusses the various kinds of macros that can be invoked via path-based scope.
74+
Right now the section I know of off of the top of my head lives in the macros
75+
by example chapter.
76+
77+
r[names.resolution.expansion.imports]
78+
79+
All use declarations are fully resolved during this stage of resolution.
80+
Type-relative paths cannot be resolved at this stage of compilation and will
81+
produce an error.
82+
1683
* `Type::assoc_item`, `<Type>::assoc_item`, `<Enum>::Variant` and `EnumTyAlias::Variant` are resolved during type checking
1784
* `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.
23-
24-
r[names.resolution.early]
25-
## Early name resolution
2685

27-
r[names.resolution.early.intro]
86+
```rust
87+
mod my_mod {
88+
pub const Const: () = ();
2889

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.
90+
pub enum MyEnum {
91+
MyVariant
92+
}
4093

41-
TODO Document some time traveling ambiguitie examples, place in relevant ambiguity section
94+
impl MyEnum {
95+
pub const Const: () = ();
96+
}
4297

43-
r[names.resolution.early.imports]
98+
pub type TypeAlias = MyEnum;
99+
}
44100

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.
101+
fn foo() {
102+
use my_mod::MyEnum; // OK
103+
use my_mod::MyEnum::MyVariant; // OK
104+
use my_mod::TypeAlias; // OK
105+
use my_mod::TypeAlias::MyVariant; // Doesn't work
106+
use my_mod::MyEnum::Const; // Doesn't work
107+
use my_mod::Const; // OK
108+
let _ = my_mod::TypeAlias::MyVariant; // OK
109+
let _ = my_mod::MyEnum::Const; // OK
110+
}
111+
```
47112

48-
TODO example of unsupported type dependent import
49-
50-
r[names.resolution.early.imports.shadowing]
113+
r[names.resolution.expansion.imports.shadowing]
51114

52115
The following is a list of situations where shadowing of use declarations is permitted:
53116

54117
* [use glob shadowing]
55118
* [macro textual scope shadowing]
56119

57-
r[names.resolution.early.imports.errors]
58-
r[names.resolution.early.imports.errors.ambiguity]
120+
r[names.resolution.expansion.imports.errors]
121+
r[names.resolution.expansion.imports.errors.ambiguity]
122+
123+
TODO shadowing and ambiguity may or may not represent the same section or one may be a subsection of the other
59124

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

62128
* Builtin Attributes
63129
* Derive Helpers
@@ -67,7 +133,7 @@ r[names.resolution.early.imports.errors.ambiguity]
67133
* ~~Glob vs Expanded~~ pretty certain we don't want to mention this one
68134
* More Expanded vs Outer
69135

70-
r[names.resolution.early.macros]
136+
r[names.resolution.expansion.macros]
71137

72138
* .visitation-order
73139
* derive helpers
@@ -89,7 +155,7 @@ r[names.resolution.early.macros]
89155
* 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
90156
* https://doc.rust-lang.org/nightly/reference/names/namespaces.html#r-names.namespaces.sub-namespaces
91157

92-
r[names.resolution.early.macros.errors.reserved-names]
158+
r[names.resolution.expansion.macros.errors.reserved-names]
93159

94160
the names cfg and cfg_attr are reserved in the macro attribute sub-namespace
95161

0 commit comments

Comments
 (0)