# Apply a procedural macro to all the compiled code

**URL:** <https://internals.rust-lang.org/t/apply-a-procedural-macro-to-all-the-compiled-code/10582>\
**Category:** Uncategorized\
**Created:** [July 17, 2019, 8:02am UTC](https://internals.rust-lang.org/t/apply-a-procedural-macro-to-all-the-compiled-code/10582 "2019-07-17T08:02:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fpoli](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/fpoli/32/3784_2.png) [@fpoli](https://internals.rust-lang.org/u/fpoli)\
**Post date:** [July 17, 2019, 8:02am UTC](https://internals.rust-lang.org/t/apply-a-procedural-macro-to-all-the-compiled-code/10582/1 "2019-07-17T08:02:53Z")

</div>

Is there a way to apply a procedural macro to all the compiled code of a crate?

Adding a `#![my_proc_macro]` to all the source code files would do what I want, but I’m looking for a more automatic way. For example, a “rustc --preprocess-with my\_proc\_macro program.rs” flag, or a way to run a procedural macro from `rustc_driver::Callbacks`.

---

<div class="post-metadata">

**Author:** ![petrochenkov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/petrochenkov/32/2784_2.png) [@petrochenkov](https://internals.rust-lang.org/u/petrochenkov)\
**Post date:** [July 17, 2019, 8:42am UTC](https://internals.rust-lang.org/t/apply-a-procedural-macro-to-all-the-compiled-code/10582/2 "2019-07-17T08:42:55Z")

</div>

Macros at the crate root level are not currently supported and are hard to implement for reasons described in [https://github.com/rust-lang/rust/issues/54726](https://github.com/rust-lang/rust/issues/54726) (some form of eager expansion is required).

The workaround is to make an inner module and apply the macro to it.

---

<div class="post-metadata">

**Author:** ![fpoli](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/fpoli/32/3784_2.png) [@fpoli](https://internals.rust-lang.org/u/fpoli)\
**Post date:** [July 17, 2019, 9:20am UTC](https://internals.rust-lang.org/t/apply-a-procedural-macro-to-all-the-compiled-code/10582/3 "2019-07-17T09:20:35Z")

</div>

I see, thanks!

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [October 15, 2019, 9:26am UTC](https://internals.rust-lang.org/t/apply-a-procedural-macro-to-all-the-compiled-code/10582/4 "2019-10-15T09:26:00Z")

</div>

Closed on October 15, 2019, 9:26am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
