Request to make `rustc_interface::passes::configure_and_expand` public

Hello,

I am wondering if it is possible to make rustc_interface::passes::configure_and_expand public to call from a custom compiler driver written using the rustc_interface crate.

The first two callbacks rustc_driver offers are after crate root parsing, and after name resolution + macro expansion. I have not found a callback or clean way to do some analysis after parsing all submodules in a crate and completing macro expansion before name resolution.

The main way I can see this being possible is by allowing people who use rustc_interface to call configure_and_expand to get a fully parsed Crate inside their custom driver to do some work with.

Is something like this already possible, or is it infeasible to access a fully parsed crate with a callback or some function in rustc_interface?

Thanks --

Ben

configure_and_expand already finishes name resolution. Macro expansion and early name resolution are intertwined. You can't make arbitrary changes to the AST after early name resolution. And if you are not modifying the AST, doing full name resolution is harmless. If you want the AST after macro expansion and name resolution, but before lowering to HIR you can use tcx.resolver_for_lowering() in the after_expansion callback.

My goal is to modify the entire crate after parsing but before name resolution. Is it possible to modify the full AST with submodules at any point? Perhaps after

sess.time("expand_crate", || ecx.monotonic_expander().expand_crate(krate));?

I assume by the time resolver.resolve_crate(&krate); happens it is too late?

The reason that name resolution and macro expansion are intertwined is that

  • Macros can expand to mod and use.
  • mod and use can affect the name resolution of macro calls.

Therefore, in the general case, there is not such a thing as “the entire crate before name resolution” because name resolution is required to figure out what “the entire crate” is.

1 Like

I see. What is the appropriate time to use the rustc_ast::mut_visit::MutVisitor, since it seems like the AST becomes immutable as soon as parsing is completed? Is this trait only designed to be used for the crate root? The comments say it is used for macro expansion, so is there a way for me to add modifications during macro expansion as the full AST is built?