How would I go about printing AST after expansion?

⚓ Rust    📅 2025-04-28    👤 surdeus    👁️ 4      

surdeus

Warning

This post was published 115 days ago. The information described in this article may have changed.

Relevant Docs
This is how to print the AST using rustc_driver::CallBacks

fn after_crate_root_parsing(
    &mut self,
    _compiler: &Compiler,
    krate: &mut rustc_ast::Crate,
) -> Compilation {
    for item in &krate.items {
        println!("{}", item_to_string(&item));
    }

}

If it compiles this

fn main() {
let message = "Hello, World!";
println!("{message}");
}

It prints this

fn main() { let message = "Hello, World!"; println!("{message}"); }

The issue is that I'd like the println! macro to be expanded, so the solution is to attempt the same thing in the after_expansion function. However, it doesn't have a type rustc_ast::Crate in the params, so as far as I can tell, I'm not able to. Can somebody tell me if there is a way to extract expanded AST?

1 post - 1 participant

Read full topic

🏷️ rust_feed