Caching proc_macro expansion to speed up checking?

In a crate I'm using a few proc macros that take a long time to generate the code.

These proc macros cause very long cargo check times and bad IDE experience.

I'm wondering if there is a way to tell rustc to cache the macro outputs and reuse when the macro inputs to do not change.

Is this possible?

No, rustc doesn't currently support caching macro inputs. It doesn't know when it is safe to do so. We may need to wait for wasm proc macros before that can be safely implemented.

Thanks.

I wish we had a way to mark a proc macro as "pure" -- i.e. given same inputs it produces same outputs.

Such macros could then be cached by rustc.

1 Like

Could the macro itself do the caching?

The idea of WASM proc-macros is partially this - since WASM doesn't have access to the system API, the only thing it can do is either shuffle around input tokens or create constant new ones, therefore being functionally pure.

Interesting idea, I think this should be doable. I should give this a try.