Adding Macros at Compile Time

⚓ Rust    📅 2025-06-21    👤 surdeus    👁️ 5      

surdeus

Warning

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

Hi, I have an attribute procedural macro I want to add to every function in a Rust file for compilation without modifying source code. This might be analogous to defining macros at compile time in C using the -D option. It allows you to make use of macros without adding #define statements.

I know how to use the syn crate to modify the syntax tree of Rust code to add my macro to every function in the syntax tree using the syn::visit_mut module. However, I do not want to use the quote macro to return new code as one might do with a syntax tree from the syn crate. Is there a way to invoke rustc on the new syntax tree I want to use directly instead?

Alternatively, are there any options in rustc or cargo to insert procedural macros for each function or struct without modifying the source code after compilation?

Thanks for your comments.

2 posts - 2 participants

Read full topic

🏷️ rust_feed