Skip to content

Commit 1a399f5

Browse files
alexregmark-i-m
authored andcommitted
Improved grammar of HIR section.
1 parent c32587a commit 1a399f5

File tree

1 file changed

+43
-46
lines changed

1 file changed

+43
-46
lines changed

src/hir.md

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
# The HIR
22

3-
The HIR – "High-level IR" – is the primary IR used in most of
4-
rustc. It is a desugared version of the "abstract syntax tree" (AST)
5-
that is generated after parsing, macro expansion, and name resolution
6-
have completed. Many parts of HIR resemble Rust surface syntax quite
7-
closely, with the exception that some of Rust's expression forms have
8-
been desugared away (as an example, `for` loops are converted into a
9-
`loop` and do not appear in the HIR).
3+
The HIR – "High-level IR" – is the primary IR used in most of rustc.
4+
It is a desugared version of the "abstract syntax tree" (AST) that is generated
5+
after parsing, macro expansion, and name resolution have completed. Many parts
6+
of HIR resemble Rust surface syntax quite closely, with the exception that some
7+
of Rust's expression forms have been desugared away (as an example, `for` loops
8+
are converted into a `loop` and do not appear in the HIR).
109

1110
This chapter covers the main concepts of the HIR.
1211

@@ -21,34 +20,34 @@ serve to organize the content of the crate for easier access.
2120

2221
For example, the contents of individual items (e.g., modules,
2322
functions, traits, impls, etc) in the HIR are not immediately
24-
accessible in the parents. So, for example, if had a module item `foo`
25-
containing a function `bar()`:
23+
accessible in the parents. So, for example, if there is a module item
24+
`foo` containing a function `bar()`:
2625

2726
```
2827
mod foo {
2928
fn bar() { }
3029
}
3130
```
3231

33-
Then in the HIR the representation of module `foo` (the `Mod`
34-
stuct) would have only the **`ItemId`** `I` of `bar()`. To get the
32+
then in the HIR the representation of module `foo` (the `Mod`
33+
stuct) would only have the **`ItemId`** `I` of `bar()`. To get the
3534
details of the function `bar()`, we would lookup `I` in the
3635
`items` map.
3736

3837
One nice result from this representation is that one can iterate
3938
over all items in the crate by iterating over the key-value pairs
40-
in these maps (without the need to trawl through the IR in total).
39+
in these maps (without the need to trawl through the whole HIR).
4140
There are similar maps for things like trait items and impl items,
4241
as well as "bodies" (explained below).
4342

44-
The other reason to setup the representation this way is for better
43+
The other reason to set up the representation this way is for better
4544
integration with incremental compilation. This way, if you gain access
46-
to a `&hir::Item` (e.g. for the mod `foo`), you do not immediately
45+
to an `&hir::Item` (e.g. for the mod `foo`), you do not immediately
4746
gain access to the contents of the function `bar()`. Instead, you only
4847
gain access to the **id** for `bar()`, and you must invoke some
49-
function to lookup the contents of `bar()` given its id; this gives us
50-
a chance to observe that you accessed the data for `bar()` and record
51-
the dependency.
48+
function to lookup the contents of `bar()` given its id; this gives the
49+
compiler a chance to observe that you accessed the data for `bar()`,
50+
and then record the dependency.
5251

5352
### Identifiers in the HIR
5453

@@ -57,37 +56,35 @@ carry around references into the HIR, but rather to carry around
5756
*identifier numbers* (or just "ids"). Right now, you will find four
5857
sorts of identifiers in active use:
5958

60-
- `DefId`, which primarily names "definitions" or top-level items.
61-
- You can think of a `DefId` as being shorthand for a very explicit
62-
and complete path, like `std::collections::HashMap`. However,
63-
these paths are able to name things that are not nameable in
64-
normal Rust (e.g., impls), and they also include extra information
65-
about the crate (such as its version number, as two versions of
66-
the same crate can co-exist).
67-
- A `DefId` really consists of two parts, a `CrateNum` (which
68-
identifies the crate) and a `DefIndex` (which indixes into a list
69-
of items that is maintained per crate).
70-
- `HirId`, which combines the index of a particular item with an
71-
offset within that item.
72-
- the key point of a `HirId` is that it is *relative* to some item (which is named
73-
via a `DefId`).
74-
- `BodyId`, this is an absolute identifier that refers to a specific
75-
body (definition of a function or constant) in the crate. It is currently
76-
effectively a "newtype'd" `NodeId`.
77-
- `NodeId`, which is an absolute id that identifies a single node in the HIR tree.
59+
- `DefId` – primarily names "definitions" or top-level items.
60+
- You can think of a `DefId` as shorthand for a very explicit and complete
61+
path, like `std::collections::HashMap`. However, these paths are able to
62+
name things that are not nameable in normal Rust (e.g. impls), and they also
63+
include extra information about the crate (such as its version number, since
64+
two versions of the same crate can co-exist).
65+
- A `DefId` really consists of two parts, a `CrateNum` (which identifies the
66+
crate) and a `DefIndex` (which indexes into a list of items that is
67+
maintained per crate).
68+
- `HirId` – combines the index of a particular item with an offset within
69+
that item.
70+
- The key point of an `HirId` is that it is *relative* to some item (which is
71+
named via a `DefId`).
72+
- `BodyId` – an absolute identifier that refers to a specific body (definition
73+
of a function or constant) in the crate. It is currently effectively a
74+
"newtype'd" `NodeId`.
75+
- `NodeId` – an absolute ID that identifies a single node in the HIR tree.
7876
- While these are still in common use, **they are being slowly phased out**.
79-
- Since they are absolute within the crate, adding a new node
80-
anywhere in the tree causes the node-ids of all subsequent code in
81-
the crate to change. This is terrible for incremental compilation,
82-
as you can perhaps imagine.
77+
- Since they are absolute within the crate, adding a new node anywhere in the
78+
tree causes the `NodeId`s of all subsequent code in the crate to change.
79+
This is terrible for incremental compilation, as you can perhaps imagine.
8380

84-
### HIR Map
81+
### The HIR Map
8582

8683
Most of the time when you are working with the HIR, you will do so via
8784
the **HIR Map**, accessible in the tcx via `tcx.hir` (and defined in
8885
the `hir::map` module). The HIR map contains a number of methods to
89-
convert between ids of various kinds and to lookup data associated
90-
with a HIR node.
86+
convert between IDs of various kinds and to lookup data associated
87+
with an HIR node.
9188

9289
For example, if you have a `DefId`, and you would like to convert it
9390
to a `NodeId`, you can use `tcx.hir.as_local_node_id(def_id)`. This
@@ -100,7 +97,7 @@ Similarly, you can use `tcx.hir.find(n)` to lookup the node for a
10097
`NodeId`. This returns a `Option<Node<'tcx>>`, where `Node` is an enum
10198
defined in the map; by matching on this you can find out what sort of
10299
node the node-id referred to and also get a pointer to the data
103-
itself. Often, you know what sort of node `n` is – e.g., if you know
100+
itself. Often, you know what sort of node `n` is – e.g. if you know
104101
that `n` must be some HIR expression, you can do
105102
`tcx.hir.expect_expr(n)`, which will extract and return the
106103
`&hir::Expr`, panicking if `n` is not in fact an expression.
@@ -113,7 +110,7 @@ calls like `tcx.hir.get_parent_node(n)`.
113110
A **body** represents some kind of executable code, such as the body
114111
of a function/closure or the definition of a constant. Bodies are
115112
associated with an **owner**, which is typically some kind of item
116-
(e.g., a `fn()` or `const`), but could also be a closure expression
117-
(e.g., `|x, y| x + y`). You can use the HIR map to find the body
118-
associated with a given def-id (`maybe_body_owned_by()`) or to find
113+
(e.g. an `fn()` or `const`), but could also be a closure expression
114+
(e.g. `|x, y| x + y`). You can use the HIR map to find the body
115+
associated with a given `DefId` (`maybe_body_owned_by()`) or to find
119116
the owner of a body (`body_owner_def_id()`).

0 commit comments

Comments
 (0)