You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/items/use-declarations.md
+9-2Lines changed: 9 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -396,9 +396,11 @@ r[items.use.ambiguities]
396
396
> This section is incomplete.
397
397
398
398
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.
Copy file name to clipboardExpand all lines: src/names/name-resolution.md
+98-35Lines changed: 98 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,54 +10,117 @@ without conflict. Each name is valid within a [scope], or a region of source
10
10
text where that name may be referenced. Access to certain names may be
11
11
restricted based on their [visibility].
12
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".
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
+
() => { modbar {} }
53
+
}
54
+
55
+
modbar {
56
+
pub(crate) use m;
57
+
}
58
+
59
+
fnf() {
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
+
16
80
*`Type::assoc_item`, `<Type>::assoc_item`, `<Enum>::Variant` and `EnumTyAlias::Variant` are resolved during type checking
17
81
*`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
26
82
27
-
r[names.resolution.early.intro]
83
+
```rust
84
+
modmy_mod {
85
+
pubconstConst: () = ();
28
86
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
+
pubenumMyEnum {
88
+
MyVariant
89
+
}
40
90
41
-
TODO Document some time traveling ambiguitie examples, place in relevant ambiguity section
91
+
implMyEnum {
92
+
pubconstConst: () = ();
93
+
}
42
94
43
-
r[names.resolution.early.imports]
95
+
pubtypeTypeAlias=MyEnum;
96
+
}
44
97
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
+
fnfoo() {
99
+
usemy_mod::MyEnum; // OK
100
+
usemy_mod::MyEnum::MyVariant; // OK
101
+
usemy_mod::TypeAlias; // OK
102
+
usemy_mod::TypeAlias::MyVariant; // Doesn't work
103
+
usemy_mod::MyEnum::Const; // Doesn't work
104
+
usemy_mod::Const; // OK
105
+
let_=my_mod::TypeAlias::MyVariant; // OK
106
+
let_=my_mod::MyEnum::Const; // OK
107
+
}
108
+
```
47
109
48
-
TODO example of unsupported type dependent import
49
-
50
-
r[names.resolution.early.imports.shadowing]
110
+
r[names.resolution.expansion.imports.shadowing]
51
111
52
112
The following is a list of situations where shadowing of use declarations is permitted:
* 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
0 commit comments