Skip to content

Commit b7d099d

Browse files
Update HIR chapter to reflect the removal of NodeIds
1 parent a10ba12 commit b7d099d

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

src/hir.md

+18-20
Original file line numberDiff line numberDiff line change
@@ -83,24 +83,24 @@ sorts of identifiers in active use:
8383
- A [`DefId`] really consists of two parts, a `CrateNum` (which
8484
identifies the crate) and a `DefIndex` (which indexes into a list
8585
of items that is maintained per crate).
86+
- [`LocalDefId`], which is basically a `DefId` but for the local crate.
87+
- They are the prefered way to encode that only a local `DefId` is
88+
expected, because it prevents [`DefId`] from upstream crates from
89+
being passed instead and causing bugs at compile time.
90+
- They can still be transformed back into `DefId`s as needed by using
91+
the [`LocalDefId::to_def_id`] method.
8692
- [`HirId`], which combines the index of a particular item with an
8793
offset within that item.
8894
- the key point of a [`HirId`] is that it is *relative* to some item
89-
(which is named via a [`DefId`]).
95+
(which is named via a [`LocalDefId`]).
9096
- [`BodyId`], this is an identifier that refers to a specific
9197
body (definition of a function or constant) in the crate. It is currently
9298
effectively a "newtype'd" [`HirId`].
93-
- [`NodeId`], which is an absolute id that identifies a single node in the HIR
94-
tree.
95-
- While these are still in common use, **they are being slowly phased out**.
96-
- Since they are absolute within the crate, adding a new node anywhere in the
97-
tree causes the [`NodeId`]s of all subsequent code in the crate to change.
98-
This is terrible for incremental compilation, as you can perhaps imagine.
9999

100100
[`DefId`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def_id/struct.DefId.html
101+
[`LocalDefId`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def_id/struct.LocalDefId.html
101102
[`HirId`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir_id/struct.HirId.html
102103
[`BodyId`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/struct.BodyId.html
103-
[`NodeId`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast/node_id/struct.NodeId.html
104104

105105
We also have an internal map to go from `DefId` to what’s called "Def path". "Def path" is like a
106106
module path but a bit more rich. For example, it may be `crate::foo::MyStruct` that identifies
@@ -121,24 +121,22 @@ with an HIR node.
121121
[HIR map]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/hir/map/struct.Map.html
122122
[number of methods]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/hir/map/struct.Map.html#methods
123123

124-
For example, if you have a [`DefId`], and you would like to convert it
125-
to a [`NodeId`], you can use
126-
[`tcx.hir.as_local_node_id(def_id)`][as_local_node_id]. This returns
127-
an `Option<NodeId>` – this will be `None` if the def-id refers to
128-
something outside of the current crate (since then it has no HIR
129-
node), but otherwise returns `Some(n)` where `n` is the node-id of the
130-
definition.
124+
For example, if you have a [`LocalDefId`], and you would like to convert it
125+
to a [`HirId`], you can use [`tcx.hir.as_local_hir_id(def_id)`][as_local_hir_id].
126+
The inverse is just as simple, if you want to convert a [`HirId`] to a [`LocalDefId`],
127+
you can use [`tcx.hir.local_def_id(hir_id)`][local_def_id].
131128

132-
[as_local_node_id]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/hir/map/struct.Map.html#method.as_local_node_id
129+
[as_local_hir_id]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/hir/map/struct.Map.html#method.as_local_hir_id
130+
[local_def_id]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/hir/map/struct.Map.html#method.local_def_id
133131

134132
Similarly, you can use [`tcx.hir.find(n)`][find] to lookup the node for a
135-
[`NodeId`]. This returns a `Option<Node<'tcx>>`, where [`Node`] is an enum
133+
[`HirId`]. This returns a `Option<Node<'tcx>>`, where [`Node`] is an enum
136134
defined in the map; by matching on this you can find out what sort of
137-
node the node-id referred to and also get a pointer to the data
135+
node the [`HirId`] referred to and also get a pointer to the data
138136
itself. Often, you know what sort of node `n` is – e.g. if you know
139137
that `n` must be some HIR expression, you can do
140-
[`tcx.hir.expect_expr(n)`][expect_expr], which will extract and return the
141-
[`&hir::Expr`][Expr], panicking if `n` is not in fact an expression.
138+
[`tcx.hir.expect_expr(hir_id)`][expect_expr], which will extract and return the
139+
[`&hir::Expr`][Expr], panicking if `hir_id` is not in fact an expression.
142140

143141
[find]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/hir/map/struct.Map.html#method.find
144142
[`Node`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/enum.Node.html

0 commit comments

Comments
 (0)