Is there an alternative solution for writing code that interacts with the compiler? I would say that proc macros are close, but they cannot interact with the compiler without modifying existing source code. I am looking for a way to plug into the compiler and inject changes at compile time.
So writing plugins which access the full rustc API are not supported? Are compiler drivers equally powerful, or are they limited regarding which components of the compiler they can access?
I see. Does rustc not have any built-in capability to modify partially compiled programs?
In rustc-driver-example.rs, the callback function fn after_crate_root_parsing accepts a mutable rustc_ast::Crate. If I want to inject code changes at compile time, do you suppose it would be possible to add arbitrary changes to the crate with a callback like this? Unless this is a mutable copy of the parsed crate root rather than the genuine copy... I will have to look into it more, but what makes you say this?
I would generally expect that to not be a well-supported functionality — it might be possible intentionally or by accident, or it might not, depending on the change you wish to make. Discussions I’ve read about modifying programs — in particular, adding extra trait implementations — suggest that it is difficult to do late (though I imagine it might be simple enough if you do it immediately after parsing).
That was just a quick estimate of the difficulty of your task. It could be wrong. As I said, I have never written a rustc driver, so I don’t know how much they actually can do without modifying any rustc crates.