]> git.proxmox.com Git - rustc.git/blame - src/doc/rustc-dev-guide/src/identifiers.md
New upstream version 1.70.0+dfsg1
[rustc.git] / src / doc / rustc-dev-guide / src / identifiers.md
CommitLineData
9ffffee4 1# Identifiers in the compiler
6a06907d
XL
2
3If you have read the few previous chapters, you now know that `rustc` uses
4many different intermediate representations to perform different kinds of analyses.
5However, like in every data structure, you need a way to traverse the structure
6and refer to other elements. In this chapter, you will find information on the
7different identifiers `rustc` uses for each intermediate representation.
8
9## In the AST
10
11A [`NodeId`] is an identifier number that uniquely identifies an AST node within
12a crate. Every node in the AST has its own [`NodeId`], including top-level items
13such as structs, but also individual statements and expressions.
14
15However, because they are absolute within a crate, adding or removing a single
16node in the AST causes all the subsequent [`NodeId`]s to change. This renders
17[`NodeId`]s pretty much useless for incremental compilation, where you want as
18few things as possible to change.
19
20[`NodeId`]s are used in all the `rustc` bits that operate directly on the AST,
21like macro expansion and name resolution.
22
23[`NodeId`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast/node_id/struct.NodeId.html
24
25## In the HIR
26
27The HIR uses a bunch of different identifiers that coexist and serve different purposes.
28
29- A [`DefId`], as the name suggests, identifies a particular definition, or top-level
30 item, in a given crate. It is composed of two parts: a [`CrateNum`] which identifies
31 the crate the definition comes from, and a [`DefIndex`] which identifies the definition
32 within the crate. Unlike [`HirId`]s, there isn't a [`DefId`] for every expression, which
33 makes them more stable across compilations.
34
35- A [`LocalDefId`] is basically a [`DefId`] that is known to come from the current crate.
36 This allows us to drop the [`CrateNum`] part, and use the type system to ensure that
37 only local definitions are passed to functions that expect a local definition.
38
39- A [`HirId`] uniquely identifies a node in the HIR of the current crate. It is composed
40 of two parts: an `owner` and a `local_id` that is unique within the `owner`. This
41 combination makes for more stable values which are helpful for incremental compilation.
42 Unlike [`DefId`]s, a [`HirId`] can refer to [fine-grained entities][Node] like expressions,
43 but stays local to the current crate.
44
45- A [`BodyId`] identifies a HIR [`Body`] in the current crate. It is currently only
46 a wrapper around a [`HirId`]. For more info about HIR bodies, please refer to the
47 [HIR chapter][hir-bodies].
48
49These identifiers can be converted into one another through the [HIR map][map].
50See the [HIR chapter][hir-map] for more detailed information.
51
52[`DefId`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def_id/struct.DefId.html
53[`LocalDefId`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def_id/struct.LocalDefId.html
54[`HirId`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir_id/struct.HirId.html
55[`BodyId`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/struct.BodyId.html
17df50a5 56[`CrateNum`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def_id/struct.CrateNum.html
6a06907d
XL
57[`DefIndex`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def_id/struct.DefIndex.html
58[`Body`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/struct.Body.html
59[Node]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/enum.Node.html
60[hir-map]: ./hir.md#the-hir-map
61[hir-bodies]: ./hir.md#hir-bodies
62[map]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/hir/map/struct.Map.html
63
64## In the MIR
65
66- [`BasicBlock`] identifies a *basic block*. It points to an instance of
67 [`BasicBlockData`], which can be retrieved by indexing into
353b0b11 68 [`Body.basic_blocks`].
6a06907d
XL
69
70- [`Local`] identifies a local variable in a function. Its associated data is in
71 [`LocalDecl`], which can be retrieved by indexing into [`Body.local_decls`].
72
73- [`Field`] identifies a struct's, union's, or enum variant's field. It is used
74 as a "projection" in [`Place`].
75
76- [`SourceScope`] identifies a name scope in the original source code. Used for
77 diagnostics and for debuginfo in debuggers. It points to an instance of
78 [`SourceScopeData`], which can be retrieved by indexing into
79 [`Body.source_scopes`].
80
81- [`Promoted`] identifies a promoted constant within another item (related to
82 const evaluation). Note: it is unique only locally within the item, so it
83 should be associated with a `DefId`.
136023e0 84 [`GlobalId`] will give you a more specific identifier.
6a06907d
XL
85
86- [`GlobalId`] identifies a global variable: a `const`, a `static`, a `const fn`
87 where all arguments are [zero-sized types], or a promoted constant.
88
89- [`Location`] represents the location in the MIR of a statement or terminator.
90 It identifies the block (using [`BasicBlock`]) and the index of the statement
91 or terminator in the block.
92
93[`BasicBlock`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/struct.BasicBlock.html
94[`BasicBlockData`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/struct.BasicBlockData.html
353b0b11 95[`Body.basic_blocks`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/struct.Body.html#structfield.basic_blocks
6a06907d
XL
96[`Local`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/struct.Local.html
97[`LocalDecl`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/struct.LocalDecl.html
98[`Body.local_decls`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/struct.Body.html#structfield.local_decls
99[`Field`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/struct.Field.html
100[`Place`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/struct.Place.html
101[`SourceScope`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/struct.SourceScope.html
102[`SourceScopeData`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/struct.SourceScopeData.html
103[`Body.source_scopes`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/struct.Body.html#structfield.source_scopes
104[`Promoted`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/struct.Promoted.html
105[`GlobalId`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/interpret/struct.GlobalId.html
106[`Location`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/struct.Location.html
107[zero-sized types]: https://doc.rust-lang.org/nomicon/exotic-sizes.html#zero-sized-types-zsts