11# Name resolution
22
33In the previous chapters, we saw how the [ * Abstract Syntax Tree* (` AST ` )] [ ast ]
4- is built with all macros expanded. We saw how doing that requires doing some
5- name resolution to resolve imports and macro names. In this chapter, we show
6- how this is actually done and more.
4+ is built with all macros expanded.
5+ We saw how doing that requires doing some
6+ name resolution to resolve imports and macro names.
7+ In this chapter, we show how this is actually done and more.
78
89[ ast ] : ./ast-validation.md
910
1011In fact, we don't do full name resolution during macro expansion -- we only
11- resolve imports and macros at that time. This is required to know what to even
12- expand. Later, after we have the whole AST, we do full name resolution to
13- resolve all names in the crate. This happens in [ ` rustc_resolve::late ` ] [ late ] .
12+ resolve imports and macros at that time.
13+ This is required to know what to even expand.
14+ Later, after we have the whole AST, we do full name resolution to
15+ resolve all names in the crate.
16+ This happens in [ ` rustc_resolve::late ` ] [ late ] .
1417Unlike during macro expansion, in this late expansion, we only need to try to
15- resolve a name once, since no new names can be added. If we fail to resolve a
16- name, then it is a compiler error.
18+ resolve a name once, since no new names can be added.
19+ If we fail to resolve a name, then it is a compiler error.
1720
1821Name resolution is complex. There are different namespaces (e.g.
1922macros, values, types, lifetimes), and names may be valid at different (nested)
20- scopes. Also, different types of names can fail resolution differently, and
21- failures can happen differently at different scopes. For example, in a module
22- scope, failure means no unexpanded macros and no unresolved glob imports in
23- that module. On the other hand, in a function body scope, failure requires that a
24- name be absent from the block we are in, all outer scopes, and the global
25- scope.
23+ scopes.
24+ Also, different types of names can fail resolution differently, and
25+ failures can happen differently at different scopes.
26+ For example, in a module scope,
27+ failure means no unexpanded macros and no unresolved glob imports in
28+ that module.
29+ On the other hand, in a function body scope, failure requires that a
30+ name be absent from the block we are in, all outer scopes, and the global scope.
2631
2732[ late ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_resolve/late/index.html
2833
2934## Basics
3035
3136In our programs we refer to variables, types, functions, etc, by giving them
32- a name. These names are not always unique. For example, take this valid Rust
33- program:
37+ a name.
38+ These names are not always unique.
39+ For example, take this valid Rust program:
3440
3541``` rust
3642type x = u32 ;
3743let x : x = 1 ;
3844let y : x = 2 ;
3945```
4046
41- How do we know on line 3 whether ` x ` is a type (` u32 ` ) or a value (1)? These
42- conflicts are resolved during name resolution. In this specific case, name
43- resolution defines that type names and variable names live in separate
47+ How do we know on line 3 whether ` x ` is a type (` u32 ` ) or a value (1)?
48+ These conflicts are resolved during name resolution.
49+ In this specific case,
50+ name resolution defines that type names and variable names live in separate
4451namespaces and therefore can co-exist.
4552
46- The name resolution in Rust is a two-phase process. In the first phase, which runs
47- during ` macro ` expansion, we build a tree of modules and resolve imports. Macro
48- expansion and name resolution communicate with each other via the
53+ The name resolution in Rust is a two-phase process.
54+ In the first phase,
55+ which runs during ` macro ` expansion,
56+ we build a tree of modules and resolve imports.
57+ Macro expansion and name resolution communicate with each other via the
4958[ ` ResolverAstLoweringExt ` ] trait.
5059
5160The input to the second phase is the syntax tree, produced by parsing input
52- files and expanding ` macros ` . This phase produces links from all the names in the
53- source to relevant places where the name was introduced. It also generates
54- helpful error messages, like typo suggestions, traits to import or lints about
61+ files and expanding ` macros ` .
62+ This phase produces links from all the names in the
63+ source to relevant places where the name was introduced.
64+ It also generates helpful error messages,
65+ like typo suggestions, traits to import or lints about
5566unused items.
5667
5768A successful run of the second phase ([ ` Resolver::resolve_crate ` ] ) creates kind
@@ -68,9 +79,11 @@ The name resolution lives in the [`rustc_resolve`] crate, with the bulk in
6879## Namespaces
6980
7081Different kind of symbols live in different namespaces ‒ e.g. types don't
71- clash with variables. This usually doesn't happen, because variables start with
82+ clash with variables.
83+ This usually doesn't happen, because variables start with
7284lower-case letter while types with upper-case one, but this is only a
73- convention. This is legal Rust code that will compile (with warnings):
85+ convention.
86+ This is legal Rust code that will compile (with warnings):
7487
7588``` rust
7689type x = u32 ;
@@ -83,19 +96,21 @@ namespaces, the resolver keeps them separated and builds separate structures for
8396them.
8497
8598In other words, when the code talks about namespaces, it doesn't mean the module
86- hierarchy, it's types vs. values vs. macros.
99+ hierarchy, it's types versus values versus macros.
87100
88101## Scopes and ribs
89102
90- A name is visible only in certain area in the source code. This forms a
91- hierarchical structure, but not necessarily a simple one ‒ if one scope is
103+ A name is visible only in certain area in the source code.
104+ This forms a hierarchical structure,
105+ but not necessarily a simple one ‒ if one scope is
92106part of another, it doesn't mean a name visible in the outer scope is also
93107visible in the inner scope, or that it refers to the same thing.
94108
95- To cope with that, the compiler introduces the concept of [ ` Rib ` ] s. This is
96- an abstraction of a scope. Every time the set of visible names potentially changes,
97- a new [ ` Rib ` ] is pushed onto a stack. The places where this can happen include for
98- example:
109+ To cope with that, the compiler introduces the concept of [ ` Rib ` ] s.
110+ This is an abstraction of a scope.
111+ Every time the set of visible names potentially changes,
112+ a new [ ` Rib ` ] is pushed onto a stack.
113+ The places where this can happen include for example:
99114
100115[ `Rib` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_resolve/late/struct.Rib.html
101116
@@ -106,11 +121,14 @@ example:
106121* Macro expansion border ‒ to cope with macro hygiene.
107122
108123When searching for a name, the stack of [ ` ribs ` ] is traversed from the innermost
109- outwards. This helps to find the closest meaning of the name (the one not
110- shadowed by anything else). The transition to outer [ ` Rib ` ] may also affect
124+ outwards.
125+ This helps to find the closest meaning of the name (the one not
126+ shadowed by anything else).
127+ The transition to outer [ ` Rib ` ] may also affect
111128what names are usable ‒ if there are nested functions (not closures),
112129the inner one can't access parameters and local bindings of the outer one,
113- even though they should be visible by ordinary scoping rules. An example:
130+ even though they should be visible by ordinary scoping rules.
131+ An example:
114132
115133[ `ribs` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_resolve/late/struct.LateResolutionVisitor.html#structfield.ribs
116134
@@ -139,11 +157,13 @@ blocks), which isn't a full namespace in its own right.
139157## Overall strategy
140158
141159To perform the name resolution of the whole crate, the syntax tree is traversed
142- top-down and every encountered name is resolved. This works for most kinds of
143- names, because at the point of use of a name it is already introduced in the [ ` Rib ` ]
160+ top-down and every encountered name is resolved.
161+ This works for most kinds of names,
162+ because at the point of use of a name it is already introduced in the [ ` Rib ` ]
144163hierarchy.
145164
146- There are some exceptions to this. Items are bit tricky, because they can be
165+ There are some exceptions to this.
166+ Items are bit tricky, because they can be
147167used even before encountered ‒ therefore every block needs to be first scanned
148168for items to fill in its [ ` Rib ` ] .
149169
@@ -156,14 +176,15 @@ Therefore, the resolution is performed in multiple stages.
156176## Speculative crate loading
157177
158178To give useful errors, rustc suggests importing paths into scope if they're
159- not found. How does it do this? It looks through every module of every crate
160- and looks for possible matches. This even includes crates that haven't yet
161- been loaded!
179+ not found.
180+ How does it do this?
181+ It looks through every module of every crate and looks for possible matches.
182+ This even includes crates that haven't yet been loaded!
162183
163184Eagerly loading crates to include import suggestions that haven't yet been
164185loaded is called _ speculative crate loading_ , because any errors it encounters
165- shouldn't be reported: [ ` rustc_resolve ` ] decided to load them, not the user. The function
166- that does this is [ ` lookup_import_candidates ` ] and lives in
186+ shouldn't be reported: [ ` rustc_resolve ` ] decided to load them, not the user.
187+ The function that does this is [ ` lookup_import_candidates ` ] and lives in
167188[ ` rustc_resolve::diagnostics ` ] .
168189
169190[ `rustc_resolve` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_resolve/index.html
@@ -176,8 +197,9 @@ the load is speculative.
176197
177198## TODO: [ #16 ] ( https://github.com/rust-lang/rustc-dev-guide/issues/16 )
178199
179- This is a result of the first pass of learning the code. It is definitely
180- incomplete and not detailed enough. It also might be inaccurate in places.
200+ This is a result of the first pass of learning the code.
201+ It is definitely incomplete and not detailed enough.
202+ It also might be inaccurate in places.
181203Still, it probably provides useful first guidepost to what happens in there.
182204
183205* What exactly does it link to and how is that published and consumed by
0 commit comments