Skip to content

Commit 3ffcf65

Browse files
committed
rewrite bullet points as prose
1 parent d4ce0af commit 3ffcf65

File tree

2 files changed

+107
-37
lines changed

2 files changed

+107
-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: 98 additions & 35 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".
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.
73+
74+
r[names.resolution.expansion.imports]
75+
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.
79+
1680
* `Type::assoc_item`, `<Type>::assoc_item`, `<Enum>::Variant` and `EnumTyAlias::Variant` are resolved during type checking
1781
* `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
2682

27-
r[names.resolution.early.intro]
83+
```rust
84+
mod my_mod {
85+
pub const Const: () = ();
2886

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.
87+
pub enum MyEnum {
88+
MyVariant
89+
}
4090

41-
TODO Document some time traveling ambiguitie examples, place in relevant ambiguity section
91+
impl MyEnum {
92+
pub const Const: () = ();
93+
}
4294

43-
r[names.resolution.early.imports]
95+
pub type TypeAlias = MyEnum;
96+
}
4497

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.
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+
```
47109

48-
TODO example of unsupported type dependent import
49-
50-
r[names.resolution.early.imports.shadowing]
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]
119+
120+
TODO shadowing and ambiguity may or may not represent the same section or one may be a subsection of the other
59121

60-
* shadowing and ambiguity may or may not represent the same section or one may be a subsection of the other
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
@@ -67,7 +130,7 @@ r[names.resolution.early.imports.errors.ambiguity]
67130
* ~~Glob vs Expanded~~ pretty certain we don't want to mention this one
68131
* More Expanded vs Outer
69132

70-
r[names.resolution.early.macros]
133+
r[names.resolution.expansion.macros]
71134

72135
* .visitation-order
73136
* derive helpers
@@ -89,7 +152,7 @@ r[names.resolution.early.macros]
89152
* 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
90153
* https://doc.rust-lang.org/nightly/reference/names/namespaces.html#r-names.namespaces.sub-namespaces
91154

92-
r[names.resolution.early.macros.errors.reserved-names]
155+
r[names.resolution.expansion.macros.errors.reserved-names]
93156

94157
the names cfg and cfg_attr are reserved in the macro attribute sub-namespace
95158

0 commit comments

Comments
 (0)