I have been reading reading about the rustc_driver module in the compiler. I also found out I could write a tool based off of that without modifying the compiler directly. I am wondering whether it's possible to modify the AST after the 'queries.analysis()' part of the driver. I realize I would have to rebuild the HIR and repeat the type-checking, but is it at all possible? I have so far failed to get hold of a mutable ast::Crate at that point of the compilation process. I also didn't see a MutVisitor for the HIR, so I was wondering if there's any way to modify HIR if the AST fails. My current plan is to "hack" the ast lowering phase if everything fails. I'm wondering whether that's really all I can do. I don't want to use an MIR pass because what I need to do requires type-checking after then modifications. Thanks in advance.
Please note when you cross post.
The HIR is immutable. For modifying the AST you can take the result of the
expansion query in the
after_expansion callback, modify it and then put back the new result I think. It is not possible to do this after analysis due to a cyclic dependency. Analysis depends on HIR depends on AST which would depend on analysis again if you want to modify the AST depending on the analysis results. What are you trying to do?
To modify the AST, you can retrieve the expanded AST using the
expansion_result query, modify it using the AST APIs, and then put the modified AST back using the
You will need to rebuild the HIR and repeat the type-checking after modifying the AST, as you mentioned. You can use the
typeck_tables queries to access the HIR and type-checking information, respectively.
There is no way to rebuild the HIR other than destroying the
TyCtxt and recreating it again, at which point you are almost running an entirely new compiler session.
Hey, I am trying to achieve a similar thing and found this post. However, going through the documentation and source code I can not find any mentions of the queries you referred to. Is this already deprecated? I am also trying to get a mutable reference to the ast (after expansion), or some way of setting the ast that will be used by the compiler for the lowering, etc.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.