I was looking into the compiler API for rustc_drivers and I saw that there exists a MutVisitor trait one can implement to mutate different language items/expressions when working with the AST. This trait also exits for the MIR. However, it does not seem to exist for the HIR (only Visitor exists). My first question is why is this the case. I looked at previous posts and they all mention that the HIR is immutable but I don't understand why it needs to be that way. Finally, assuming there is a principled reason the HIR is immutable. Would it be possible to copy the HIR perform some modifications on the copy and pass it down for further compilation? If this is not possible it would also be good to understand why. Thank you very much in advance.
Cross post: Mutation/Creation of HIR - help - The Rust Programming Language Forum
The HIR is arena-allocated and immutable, with interior shared references. Mutating one thing requires cloning everything.
Could you expand a bit in this answer I am new to working with the compiler.
My understanding is that arena allocated means that the whole HIR is in the same region of memory sharing the same lifetime. My guess as to why the HIR is NOT clonable in the arena is because the compiler uses pointers to for type comparisons (so if we clone same types would have different identifiers). Given that it has interior shared references I understand now also why it is not mutable.
I do not understand what you meant by "Mutating one thing requires cloning everything."
I wanted to create a copy of the HIR (maybe to a new arena) that I could modify or maybe just build my own based on the original copy and compile that instead. Is this just not possible?
Thank you!
I just meant that because it has interior refs, to mutate one need you need to recreate the whole body from scratch.