Skip to content

Commit 200d939

Browse files
committed
Document AST lowering implementation
1 parent 307cc88 commit 200d939

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/hir/lowering.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,24 @@ of such structures include but are not limited to
1515
* Existential `impl Trait`
1616
* Converted to a virtual `existential type` declaration
1717

18+
The implementation of AST lowering is in the [`rustc_ast_lowering`] crate.
19+
The entry point is [`lower_to_hir`], which retrieves the post-expansion AST
20+
and resolver data from [`TyCtxt`] and builds the [`hir::Crate`] for the whole crate.
21+
22+
Lowering is organized around HIR owners. [`lower_to_hir`] first indexes the
23+
crate and then [`ItemLowerer::lower_node`] lowers each crate, item, associated
24+
item, and foreign item.
25+
26+
Most of the lowering logic lives on [`LoweringContext`]. The implementation is
27+
split across multiple files in the [`rustc_ast_lowering`] crate such as `item.rs`,
28+
`expr.rs`, `pat.rs`, `path.rs`, and others, but they all share the same [`LoweringContext`]
29+
state and ID‑lowering machinery.
30+
31+
Each owner is lowered in its own [`with_hir_id_owner`] scope. This is why the
32+
`HirId` invariants below matter: `lower_node_id` maps AST `NodeId`s into the
33+
current owner, while `next_id` creates fresh HIR-only nodes introduced during
34+
desugaring.
35+
1836
Lowering needs to uphold several invariants in order to not trigger the
1937
sanity checks in [`compiler/rustc_passes/src/hir_id_validator.rs`][hir_id_validator]:
2038

@@ -33,6 +51,13 @@ sanity checks in [`compiler/rustc_passes/src/hir_id_validator.rs`][hir_id_valida
3351
which produces both a new `NodeId` as well as automatically lowering it
3452
for you so you also get the `HirId`.
3553

54+
[`rustc_ast_lowering`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast_lowering/index.html
55+
[`lower_to_hir`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast_lowering/fn.lower_to_hir.html
56+
[`TyCtxt`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyCtxt.html
57+
[`hir::Crate`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/struct.Crate.html
58+
[`ItemLowerer::lower_node`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast_lowering/item/struct.ItemLowerer.html
59+
[`LoweringContext`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast_lowering/struct.LoweringContext.html
60+
[`with_hir_id_owner`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast_lowering/struct.LoweringContext.html#method.with_hir_id_owner
3661
[hir_id_validator]: https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc_passes/src/hir_id_validator.rs
3762

3863
If you are creating new `DefId`s, since each `DefId` needs to have a

0 commit comments

Comments
 (0)