-
Notifications
You must be signed in to change notification settings - Fork 547
Add a chapter on all the identifiers used through rustc
#872
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d23b66b
Add a chapter on all the identifiers used through `rustc`
LeSeulArtichaut 9c7c592
Apply suggestions from code review
LeSeulArtichaut c56ac82
Make the HIR chapter point to the new chapter on IDs
LeSeulArtichaut c58eec8
Fix typo
LeSeulArtichaut File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Identifiers in the Compiler | ||
|
||
If you have read the few previous chapters, you now know that the `rustc` uses | ||
many different intermediate representations to perform different kinds of analysis. | ||
LeSeulArtichaut marked this conversation as resolved.
Show resolved
Hide resolved
|
||
However, like in every data structure, you need a way to traverse the structure | ||
and refer to other elements. In this chapter, you will find information on the | ||
different identifiers `rustc` uses for each intermediate representation. | ||
|
||
## In the AST | ||
|
||
A [`NodeId`] is an identifier number that uniquely identifies an AST node within | ||
a crate. Every node in the AST has its own [`NodeId`], including top-level items | ||
such as structs, but also individual statements and expressions. | ||
|
||
However, because they are absolute within in a crate, adding or removing a single | ||
node in the AST causes all the subsequent [`NodeId`]s to change. This renders | ||
[`NodeId`]s pretty much useless for incremental compilation, where you want as | ||
few things as possible to change. | ||
|
||
[`NodeId`]s are used in all the `rustc` bits that operate directly on the AST, | ||
like macro expansion and name resolution. | ||
|
||
[`NodeId`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast/node_id/struct.NodeId.html | ||
|
||
## In the HIR | ||
|
||
The HIR uses a bunch of different identifiers that coexist and serve different purposes. | ||
|
||
- A [`DefId`], as the name suggests, identifies a particular definition, or top-level | ||
item, in a given grate. It is composed of two parts: a [`CrateNum`] which identifies | ||
LeSeulArtichaut marked this conversation as resolved.
Show resolved
Hide resolved
|
||
the crate the definition comes from, and a [`DefIndex`] which identifies the definition | ||
within the crate. Unlike [`NodeId`]s, there isn't a [`DefId`] for every expression, which | ||
makes them more stable across compilations. | ||
- A [`LocalDefId`] is basically a [`DefId`] that is known to come from the current crate. | ||
This allows us to drop the [`CrateNum`] part, and use the type system to ensure that | ||
only local definitions are passed to functions that expect a local definition. | ||
- A [`HirId`] uniquely identifies a node in the HIR of the current crate. It is composed | ||
of two parts: an `owner` and a `local_id` that is unique within the `owner`. This | ||
combination makes for more stable values which are helpful for incremental compilation. | ||
Unlike [`DefId`]s, a [`HirId`] can refer to [fine-grained entities][Node] like expressions, | ||
but stays local to the current crate. | ||
- A [`BodyId`] identifies a HIR [`Body`] in the current crate. It is currenty only | ||
a wrapper around a [`HirId`]. For more info about HIR bodies, please refer to the | ||
[HIR chapter][hir-bodies]. | ||
|
||
These identifiers can be converted into one another through the [HIR map][map]. | ||
See the [HIR chapter][hir-map] for more detailed information. | ||
|
||
[`DefId`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def_id/struct.DefId.html | ||
[`LocalDefId`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def_id/struct.LocalDefId.html | ||
[`HirId`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir_id/struct.HirId.html | ||
[`BodyId`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/struct.BodyId.html | ||
[`CrateNum`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def_id/enum.CrateNum.html | ||
[`DefIndex`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def_id/struct.DefIndex.html | ||
[`Body`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/struct.Body.html | ||
[Node]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/enum.Node.html | ||
[hir-map]: ./hir.md#the-hir-map | ||
[hir-bodies]: ./hir.md#hir-bodies | ||
[map]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/hir/map/struct.Map.html | ||
|
||
## In the MIR | ||
|
||
**TODO** |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.