3
3
<!-- toc -->
4
4
5
5
In the previous chapters, we saw how the [ * Abstract Syntax Tree* (` AST ` )] [ ast ]
6
- is built with all ` macros ` expanded. We saw how doing that requires doing some
7
- name resolution to resolve imports and ` macro ` names. In this chapter, we show
6
+ is built with all macros expanded. We saw how doing that requires doing some
7
+ name resolution to resolve imports and macro names. In this chapter, we show
8
8
how this is actually done and more.
9
9
10
10
[ ast ] : ./ast-validation.md
11
11
12
- In fact, we don't do full name resolution during ` macro ` expansion -- we only
13
- resolve imports and ` macros ` at that time. This is required to know what to even
14
- expand. Later, after we have the whole ` AST ` , we do full name resolution to
12
+ In fact, we don't do full name resolution during macro expansion -- we only
13
+ resolve imports and macros at that time. This is required to know what to even
14
+ expand. Later, after we have the whole AST, we do full name resolution to
15
15
resolve all names in the crate. This happens in [ ` rustc_resolve::late ` ] [ late ] .
16
- Unlike during ` macro ` expansion, in this late expansion, we only need to try to
16
+ Unlike during macro expansion, in this late expansion, we only need to try to
17
17
resolve a name once, since no new names can be added. If we fail to resolve a
18
18
name, then it is a compiler error.
19
19
20
- Name resolution can be complex. There are different namespaces (e.g.
21
- ` macros ` , values, types, lifetimes), and names may be valid at different (nested)
20
+ Name resolution is complex. There are different namespaces (e.g.
21
+ macros, values, types, lifetimes), and names may be valid at different (nested)
22
22
scopes. Also, different types of names can fail resolution differently, and
23
23
failures can happen differently at different scopes. For example, in a module
24
- scope, failure means no unexpanded ` macros ` and no unresolved glob imports in
24
+ scope, failure means no unexpanded macros and no unresolved glob imports in
25
25
that module. On the other hand, in a function body scope, failure requires that a
26
26
name be absent from the block we are in, all outer scopes, and the global
27
27
scope.
@@ -53,7 +53,7 @@ expansion and name resolution communicate with each other via the
53
53
The input to the second phase is the syntax tree, produced by parsing input
54
54
files and expanding ` macros ` . This phase produces links from all the names in the
55
55
source to relevant places where the name was introduced. It also generates
56
- helpful error messages, like typo suggestions, ` trait ` s to import or lints about
56
+ helpful error messages, like typo suggestions, traits to import or lints about
57
57
unused items.
58
58
59
59
A successful run of the second phase ([ ` Resolver::resolve_crate ` ] ) creates kind
@@ -85,7 +85,7 @@ namespaces, the resolver keeps them separated and builds separate structures for
85
85
them.
86
86
87
87
In other words, when the code talks about namespaces, it doesn't mean the module
88
- hierarchy, it's types vs. values vs. ` macros ` .
88
+ hierarchy, it's types vs. values vs. macros.
89
89
90
90
## Scopes and ribs
91
91
@@ -105,12 +105,12 @@ example:
105
105
modules.
106
106
* Introducing a ` let ` binding ‒ this can shadow another binding with the same
107
107
name.
108
- * Macro expansion border ‒ to cope with ` macro ` hygiene.
108
+ * Macro expansion border ‒ to cope with macro hygiene.
109
109
110
110
When searching for a name, the stack of [ ` ribs ` ] is traversed from the innermost
111
111
outwards. This helps to find the closest meaning of the name (the one not
112
112
shadowed by anything else). The transition to outer [ ` Rib ` ] may also affect
113
- what names are usable ‒ if there are nested functions (not ` closure ` s ),
113
+ what names are usable ‒ if there are nested functions (not closures ),
114
114
the inner one can't access parameters and local bindings of the outer one,
115
115
even though they should be visible by ordinary scoping rules. An example:
116
116
@@ -150,14 +150,14 @@ used even before encountered ‒ therefore every block needs to be first scanned
150
150
for items to fill in its [ ` Rib ` ] .
151
151
152
152
Other, even more problematic ones, are imports which need recursive fixed-point
153
- resolution and ` macros ` , that need to be resolved and expanded before the rest of
153
+ resolution and macros, that need to be resolved and expanded before the rest of
154
154
the code can be processed.
155
155
156
156
Therefore, the resolution is performed in multiple stages.
157
157
158
158
## Speculative crate loading
159
159
160
- To give useful errors, ` rustc ` suggests importing paths into scope if they're
160
+ To give useful errors, rustc suggests importing paths into scope if they're
161
161
not found. How does it do this? It looks through every module of every crate
162
162
and looks for possible matches. This even includes crates that haven't yet
163
163
been loaded!
@@ -176,7 +176,7 @@ To tell the difference between speculative loads and loads initiated by the
176
176
user, [ ` rustc_resolve ` ] passes around a ` record_used ` parameter, which is ` false ` when
177
177
the load is speculative.
178
178
179
- <!-- ## TODO: [#16](https://github.com/rust-lang/rustc-dev-guide/issues/16)
179
+ ## TODO: [ #16 ] ( https://github.com/rust-lang/rustc-dev-guide/issues/16 )
180
180
181
181
This is a result of the first pass of learning the code. It is definitely
182
182
incomplete and not detailed enough. It also might be inaccurate in places.
@@ -190,4 +190,4 @@ Still, it probably provides useful first guidepost to what happens in there.
190
190
* The overall strategy description is a bit vague.
191
191
* Where does the name ` Rib ` come from?
192
192
* Does this thing have its own tests, or is it tested only as part of some e2e
193
- testing? -->
193
+ testing?
0 commit comments