For the context, what I am working on right now is to prototype a codegen backend of some tree representation of a downstream language, so I figured it might be helpful to generate from HIR instead of MIR (because structures like if
are lost). Now in my codegen function, I am having an Instance
, and I want to obtain the corresponding HIR representation.
I have read https://rustc-dev-guide.rust-lang.org/hir.html but I am still puzzled by the relations between various id's. I thought that different id's should essentially correspond each other, but it doesn't seem to be the case. For example, as_local_node_id
gives an Option
. It doesn't quite ring a bell to me because according to the doc, NodeId
is some absolute identifier of a tree so if I am given a DefId
, in what case the NodeId
doesn't exist? What's the recommended way to obtain an HIR tree from a DefId
?
Currently, in order to obtain an HirId
, I am doing:
let hirid = tcx.hir().node_id_to_hir_id(tcx.hir().as_local_node_id(instance.def_id()).unwrap());
let hir = tcx.hir().find(hirid);
Does that make sense?