Skip to content

Commit f2e7ef1

Browse files
oli-obkmark-i-m
authored andcommitted
Add some details about DefId creation
1 parent e8a0108 commit f2e7ef1

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/lowering.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,16 @@ sanity checks in `src/librustc/hir/map/hir_id_validator.rs`:
3232
4. If you are creating new nodes that didn't exist in the `AST`, you *must*
3333
create new ids for them. This is done by calling the `next_id` method,
3434
which produces both a new `NodeId` as well as automatically lowering it
35-
for you so you also get the `HirId`.
35+
for you so you also get the `HirId`.
36+
37+
If you are creating new `DefId`s, since each `DefId` needs to have a corresponding
38+
`NodeId`, it is adviseable to add these `NodeId`s to the `AST` so you don't have
39+
to generate new ones during lowering. This has the advantage of creating a
40+
way to find the `DefId` of something via its `NodeId`. If lowering needs this
41+
`DefId` in multiple places, you can't generate a new `NodeId` in all those places
42+
because you'd also get a new `DefId` then. With a `NodeId` from the `AST` this is
43+
not an issue.
44+
45+
Having the `NodeId` also allows the `DefCollector` to generate the `DefId`s instead
46+
of lowering having to do it on the fly. Centralizing the `DefId` generation in one
47+
place makes it easier to refactor and reason about.

0 commit comments

Comments
 (0)