@@ -83,24 +83,24 @@ sorts of identifiers in active use:
83
83
- A [ ` DefId ` ] really consists of two parts, a ` CrateNum ` (which
84
84
identifies the crate) and a ` DefIndex ` (which indexes into a list
85
85
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.
86
92
- [ ` HirId ` ] , which combines the index of a particular item with an
87
93
offset within that item.
88
94
- 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 ` ] ).
90
96
- [ ` BodyId ` ] , this is an identifier that refers to a specific
91
97
body (definition of a function or constant) in the crate. It is currently
92
98
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.
99
99
100
100
[ `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
101
102
[ `HirId` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir_id/struct.HirId.html
102
103
[ `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
104
104
105
105
We also have an internal map to go from ` DefId ` to what’s called "Def path". "Def path" is like a
106
106
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.
121
121
[ HIR map ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/hir/map/struct.Map.html
122
122
[ number of methods ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/hir/map/struct.Map.html#methods
123
123
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 ] .
131
128
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
133
131
134
132
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
136
134
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
138
136
itself. Often, you know what sort of node ` n ` is – e.g. if you know
139
137
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.
142
140
143
141
[ find ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/hir/map/struct.Map.html#method.find
144
142
[ `Node` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/enum.Node.html
0 commit comments