@@ -15,8 +15,26 @@ 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+
1836Lowering needs to uphold several invariants in order to not trigger the
19- sanity checks in ` compiler/rustc_passes/src/hir_id_validator.rs ` :
37+ sanity checks in [ ` compiler/rustc_passes/src/hir_id_validator.rs ` ] [ hir_id_validator ] :
2038
21391 . A ` HirId ` must be used if created. So if you use the ` lower_node_id ` ,
2240 you * must* use the resulting ` NodeId ` or ` HirId ` (either is fine, since
@@ -33,6 +51,15 @@ sanity checks in `compiler/rustc_passes/src/hir_id_validator.rs`:
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
61+ [ hir_id_validator ] : https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc_passes/src/hir_id_validator.rs
62+
3663If you are creating new ` DefId ` s, since each ` DefId ` needs to have a
3764corresponding ` NodeId ` , it is advisable to add these ` NodeId ` s to the
3865` AST ` so you don't have to generate new ones during lowering. This has
0 commit comments