22
33The AST lowering step converts AST to [ HIR] ( ../hir.md ) .
44This means many structures are removed if they are irrelevant
5- for type analysis or similar syntax agnostic analyses. Examples
6- of such structures include but are not limited to
5+ for type analysis or similar syntax agnostic analyses.
6+ Examples of such structures include but are not limited to
77
88* Parenthesis
99 * Removed without replacement, the tree structure makes order explicit
@@ -19,35 +19,40 @@ The implementation of AST lowering is in the [`rustc_ast_lowering`] crate.
1919The entry point is [ ` lower_to_hir ` ] , which retrieves the post-expansion AST
2020and resolver data from [ ` TyCtxt ` ] and builds the [ ` hir::Crate ` ] for the whole crate.
2121
22- Lowering is organized around HIR owners. [ ` lower_to_hir ` ] first indexes the
22+ Lowering is organized around HIR owners.
23+ [ ` lower_to_hir ` ] first indexes the
2324crate and then [ ` ItemLowerer::lower_node ` ] lowers each crate, item, associated
2425item, and foreign item.
2526
26- Most of the lowering logic lives on [ ` LoweringContext ` ] . The implementation is
27+ Most of the lowering logic lives on [ ` LoweringContext ` ] .
28+ The implementation is
2729split across multiple files in the [ ` rustc_ast_lowering ` ] crate such as ` item.rs ` ,
2830` expr.rs ` , ` pat.rs ` , ` path.rs ` , and others, but they all share the same [ ` LoweringContext ` ]
2931state and ID‑lowering machinery.
3032
31- Each owner is lowered in its own [ ` with_hir_id_owner ` ] scope. This is why the
33+ Each owner is lowered in its own [ ` with_hir_id_owner ` ] scope.
34+ This is why the
3235` HirId ` invariants below matter: ` lower_node_id ` maps AST ` NodeId ` s into the
3336current owner, while ` next_id ` creates fresh HIR-only nodes introduced during
3437desugaring.
3538
3639Lowering needs to uphold several invariants in order to not trigger the
3740sanity checks in [ ` compiler/rustc_passes/src/hir_id_validator.rs ` ] [ hir_id_validator ] :
3841
39- 1 . A ` HirId ` must be used if created. So if you use the ` lower_node_id ` ,
42+ 1 . A ` HirId ` must be used if created.
43+ So if you use the ` lower_node_id ` ,
4044 you * must* use the resulting ` NodeId ` or ` HirId ` (either is fine, since
4145 any ` NodeId ` s in the ` HIR ` are checked for existing ` HirId ` s)
42462 . Lowering a ` HirId ` must be done in the scope of the * owning* item.
4347 This means you need to use ` with_hir_id_owner ` if you are creating parts
44- of an item other than the one being currently lowered. This happens for
45- example during the lowering of existential ` impl Trait `
48+ of an item other than the one being currently lowered.
49+ This happens for example during the lowering of existential ` impl Trait `
46503 . A ` NodeId ` that will be placed into a HIR structure must be lowered,
47- even if its ` HirId ` is unused. Calling
48- ` let _ = self.lower_node_id(node_id); ` is perfectly legitimate.
51+ even if its ` HirId ` is unused.
52+ Calling ` let _ = self.lower_node_id(node_id); ` is perfectly legitimate.
49534 . If you are creating new nodes that didn't exist in the ` AST ` , you * must*
50- create new ids for them. This is done by calling the ` next_id ` method,
54+ create new ids for them.
55+ This is done by calling the ` next_id ` method,
5156 which produces both a new ` NodeId ` as well as automatically lowering it
5257 for you so you also get the ` HirId ` .
5358
@@ -62,12 +67,16 @@ sanity checks in [`compiler/rustc_passes/src/hir_id_validator.rs`][hir_id_valida
6267
6368If you are creating new ` DefId ` s, since each ` DefId ` needs to have a
6469corresponding ` NodeId ` , it is advisable to add these ` NodeId ` s to the
65- ` AST ` so you don't have to generate new ones during lowering. This has
70+ ` AST ` so you don't have to generate new ones during lowering.
71+ This has
6672the advantage of creating a way to find the ` DefId ` of something via its
67- ` NodeId ` . If lowering needs this ` DefId ` in multiple places, you can't
73+ ` NodeId ` .
74+ If lowering needs this ` DefId ` in multiple places, you can't
6875generate a new ` NodeId ` in all those places because you'd also get a new
69- ` DefId ` then. With a ` NodeId ` from the ` AST ` this is not an issue.
76+ ` DefId ` then.
77+ With a ` NodeId ` from the ` AST ` this is not an issue.
7078
7179Having the ` NodeId ` also allows the ` DefCollector ` to generate the ` DefId ` s
72- instead of lowering having to do it on the fly. Centralizing the ` DefId `
80+ instead of lowering having to do it on the fly.
81+ Centralizing the ` DefId `
7382generation in one place makes it easier to refactor and reason about.
0 commit comments