Interaction between independent proc macros via file I/O

⚓ Rust    📅 2026-02-22    👤 surdeus    👁️ 2      

surdeus

This topic has nothing to do with any projects and is purely out of curiosity, but is it reliable to use independent proc macros via file I/O in a single crate?

Suppose I have two macro crates containing a proc macro, respectively, and I want to use those dependently within a single crate. For example, imagine that the first macro, say macro_a, performs complex operations and returns i32 while the second one, say macro_b, expects a token of i32 and performs another operation. In this scenario, since macros are expanded lazily in the current macro system, we can not write something like

macro_b!(macro_a!());

to get the deaired result.

In order to achive this type of thing, one may make a new orchestrator macro which understands what both macros do. However, it may be sometimes a pain to unify different logics into a single piece.

Then, I wondered if it is a practical approach to write the result of macro_a to a file and make macro_b read the content later to reuse it.

I asked a similar question to some open AIs, but they told that this approach is not reliable because macro expansion order may change, and the expansion procedures may be parallelized as well in the future.
However, it seems that proc macros are expanded sequentially from top to bottom in the current system.

I would appreciate any responses!

2 posts - 2 participants

Read full topic

🏷️ Rust_feed