Skip to content

Commit e7704aa

Browse files
committed
sembr src/name-resolution.md
1 parent 33fa6be commit e7704aa

1 file changed

Lines changed: 65 additions & 42 deletions

File tree

src/name-resolution.md

Lines changed: 65 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,66 @@
11
# Name resolution
22

33
In 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

1011
In 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].
1417
Unlike 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

1821
Name resolution is complex. There are different namespaces (e.g.
1922
macros, 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
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
2227
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.
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

3136
In 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
3642
type x = u32;
3743
let x: x = 1;
3844
let 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
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, name
4350
resolution defines that type names and variable names live in separate
4451
namespaces 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, which runs
55+
during `macro` expansion, we build a tree of modules and resolve imports.
56+
Macro expansion and name resolution communicate with each other via the
4957
[`ResolverAstLoweringExt`] trait.
5058

5159
The 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
60+
files and expanding `macros`.
61+
This phase produces links from all the names in the
62+
source to relevant places where the name was introduced.
63+
It also generates
5464
helpful error messages, like typo suggestions, traits to import or lints about
5565
unused items.
5666

@@ -68,9 +78,11 @@ The name resolution lives in the [`rustc_resolve`] crate, with the bulk in
6878
## Namespaces
6979

7080
Different 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
81+
clash with variables.
82+
This usually doesn't happen, because variables start with
7283
lower-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):
84+
convention.
85+
This is legal Rust code that will compile (with warnings):
7486

7587
```rust
7688
type x = u32;
@@ -83,19 +95,23 @@ namespaces, the resolver keeps them separated and builds separate structures for
8395
them.
8496

8597
In other words, when the code talks about namespaces, it doesn't mean the module
86-
hierarchy, it's types vs. values vs. macros.
98+
hierarchy, it's types vs.
99+
values vs.
100+
macros.
87101

88102
## Scopes and ribs
89103

90-
A name is visible only in certain area in the source code. This forms a
104+
A name is visible only in certain area in the source code.
105+
This forms a
91106
hierarchical structure, but not necessarily a simple one ‒ if one scope is
92107
part of another, it doesn't mean a name visible in the outer scope is also
93108
visible in the inner scope, or that it refers to the same thing.
94109

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:
110+
To cope with that, the compiler introduces the concept of [`Rib`]s.
111+
This is an abstraction of a scope.
112+
Every time the set of visible names potentially changes,
113+
a new [`Rib`] is pushed onto a stack.
114+
The places where this can happen include for example:
99115

100116
[`Rib`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_resolve/late/struct.Rib.html
101117

@@ -106,11 +122,14 @@ example:
106122
* Macro expansion border ‒ to cope with macro hygiene.
107123

108124
When 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
125+
outwards.
126+
This helps to find the closest meaning of the name (the one not
127+
shadowed by anything else).
128+
The transition to outer [`Rib`] may also affect
111129
what names are usable ‒ if there are nested functions (not closures),
112130
the 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:
131+
even though they should be visible by ordinary scoping rules.
132+
An example:
114133

115134
[`ribs`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_resolve/late/struct.LateResolutionVisitor.html#structfield.ribs
116135

@@ -139,11 +158,13 @@ blocks), which isn't a full namespace in its own right.
139158
## Overall strategy
140159

141160
To 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
161+
top-down and every encountered name is resolved.
162+
This works for most kinds of
143163
names, because at the point of use of a name it is already introduced in the [`Rib`]
144164
hierarchy.
145165

146-
There are some exceptions to this. Items are bit tricky, because they can be
166+
There are some exceptions to this.
167+
Items are bit tricky, because they can be
147168
used even before encountered ‒ therefore every block needs to be first scanned
148169
for items to fill in its [`Rib`].
149170

@@ -156,14 +177,15 @@ Therefore, the resolution is performed in multiple stages.
156177
## Speculative crate loading
157178

158179
To 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!
180+
not found.
181+
How does it do this?
182+
It looks through every module of every crate and looks for possible matches.
183+
This even includes crates that haven't yet been loaded!
162184

163185
Eagerly loading crates to include import suggestions that haven't yet been
164186
loaded 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
187+
shouldn't be reported: [`rustc_resolve`] decided to load them, not the user.
188+
The function that does this is [`lookup_import_candidates`] and lives in
167189
[`rustc_resolve::diagnostics`].
168190

169191
[`rustc_resolve`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_resolve/index.html
@@ -176,8 +198,9 @@ the load is speculative.
176198

177199
## TODO: [#16](https://github.com/rust-lang/rustc-dev-guide/issues/16)
178200

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.
201+
This is a result of the first pass of learning the code.
202+
It is definitely incomplete and not detailed enough.
203+
It also might be inaccurate in places.
181204
Still, it probably provides useful first guidepost to what happens in there.
182205

183206
* What exactly does it link to and how is that published and consumed by

0 commit comments

Comments
 (0)