33<!-- toc -->
44
55In 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
88how this is actually done and more.
99
1010[ ast ] : ./ast-validation.md
1111
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
1515resolve 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
1717resolve a name once, since no new names can be added. If we fail to resolve a
1818name, then it is a compiler error.
1919
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)
2222scopes. Also, different types of names can fail resolution differently, and
2323failures 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
2525that module. On the other hand, in a function body scope, failure requires that a
2626name be absent from the block we are in, all outer scopes, and the global
2727scope.
@@ -53,7 +53,7 @@ expansion and name resolution communicate with each other via the
5353The input to the second phase is the syntax tree, produced by parsing input
5454files and expanding ` macros ` . This phase produces links from all the names in the
5555source 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
5757unused items.
5858
5959A 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
8585them.
8686
8787In 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.
8989
9090## Scopes and ribs
9191
@@ -105,12 +105,12 @@ example:
105105 modules.
106106* Introducing a ` let ` binding ‒ this can shadow another binding with the same
107107 name.
108- * Macro expansion border ‒ to cope with ` macro ` hygiene.
108+ * Macro expansion border ‒ to cope with macro hygiene.
109109
110110When searching for a name, the stack of [ ` ribs ` ] is traversed from the innermost
111111outwards. This helps to find the closest meaning of the name (the one not
112112shadowed 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 ),
114114the inner one can't access parameters and local bindings of the outer one,
115115even though they should be visible by ordinary scoping rules. An example:
116116
@@ -150,14 +150,14 @@ used even before encountered ‒ therefore every block needs to be first scanned
150150for items to fill in its [ ` Rib ` ] .
151151
152152Other, 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
154154the code can be processed.
155155
156156Therefore, the resolution is performed in multiple stages.
157157
158158## Speculative crate loading
159159
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
161161not found. How does it do this? It looks through every module of every crate
162162and looks for possible matches. This even includes crates that haven't yet
163163been loaded!
@@ -176,7 +176,7 @@ To tell the difference between speculative loads and loads initiated by the
176176user, [ ` rustc_resolve ` ] passes around a ` record_used ` parameter, which is ` false ` when
177177the load is speculative.
178178
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 )
180180
181181This is a result of the first pass of learning the code. It is definitely
182182incomplete 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.
190190* The overall strategy description is a bit vague.
191191* Where does the name ` Rib ` come from?
192192* Does this thing have its own tests, or is it tested only as part of some e2e
193- testing? -->
193+ testing?
0 commit comments