# Idea: Pre-expanded crates and \`expansion-dependencies\`

**URL:** <https://internals.rust-lang.org/t/idea-pre-expanded-crates-and-expansion-dependencies/17595>\
**Category:** language design\
**Created:** [October 20, 2022, 5:04am UTC](https://internals.rust-lang.org/t/idea-pre-expanded-crates-and-expansion-dependencies/17595 "2022-10-20T05:04:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![crlf0710](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/crlf0710/32/1987_2.png) [@crlf0710](https://internals.rust-lang.org/u/crlf0710)\
**Post date:** [October 20, 2022, 5:04am UTC](https://internals.rust-lang.org/t/idea-pre-expanded-crates-and-expansion-dependencies/17595/1 "2022-10-20T05:04:15Z")

</div>

Currently procedure macro is used as a dependency of the downstream crate, So when the downstream crate is compiled, the macros got expanded on the fly during compilation.

I'm thinking maybe it's possible to add a new kind of dependency in `Cargo.toml`, maybe call it `[expansion-dependencies]`, that acts just as normal dependencies. But instead, before uploading to [crates.io](http://crates.io), the proc macros defined in these crates got expanded and inlining the expansion results into the original source code. Only the expanded source code is uploaded to `crates.io`, and the dependency got automatically removed.

The benefits is much shorter compilation time.

The cons is the implementation complexity. Also when inlining, span info is removed, so the generated code has to use the current crate edition.

---

<div class="post-metadata">

**Author:** ![Nemo157](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nemo157/32/11585_2.png) [@Nemo157](https://internals.rust-lang.org/u/Nemo157)\
**Post date:** [October 20, 2022, 6:44am UTC](https://internals.rust-lang.org/t/idea-pre-expanded-crates-and-expansion-dependencies/17595/2 "2022-10-20T06:44:37Z")

</div>

It’s very common for proc-macros to rely on an exact version of their runtime crate by using non-public semver exempt details from them. Pre-expanding would then extend this exact version requirement to the published crate, likely causing many unresolveable dependency trees.

---

<div class="post-metadata">

**Author:** ![djc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/djc/32/1592_2.png) [@djc](https://internals.rust-lang.org/u/djc)\
**Post date:** [October 20, 2022, 7:38am UTC](https://internals.rust-lang.org/t/idea-pre-expanded-crates-and-expansion-dependencies/17595/3 "2022-10-20T07:38:28Z")

</div>

See recent discussion here:

> [@Pre-expanding proc macros](https://internals.rust-lang.org/t/pre-compiling-proc-macros/17550/13):
>
> That would require a special case in every build system to check if rustc supports wasm proc macros on the current target and if not switch to compiling native dylibs. It would also mean that we can't remove the -Zdual-proc-macro hack that is necessary for cross compiling rustc, but breaks cross compiling tools linked against rustc itself.

---

<div class="post-metadata">

**Author:** ![crlf0710](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/crlf0710/32/1987_2.png) [@crlf0710](https://internals.rust-lang.org/u/crlf0710)\
**Post date:** [October 20, 2022, 11:17am UTC](https://internals.rust-lang.org/t/idea-pre-expanded-crates-and-expansion-dependencies/17595/4 "2022-10-20T11:17:10Z")

</div>

@Nemo157 Right, but then a proc macro crate can declare itself as able to be pre-expanded or not. For crates like `thiserror` and `displaydoc`, i think they're perfectly fittable for such pre-expansion.

@djc Thanks for the pointer! Indeed the motivation is exactly the same, however that topic has a slightly wrong title, which leads to people start talking about the implementation of pre-compiled proc macro crates themselves. It also defaulted to all proc\_macros, and as @Nemo157 pointed out above, it's not possible in many cases.

---

<div class="post-metadata">

**Author:** ![crlf0710](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/crlf0710/32/1987_2.png) [@crlf0710](https://internals.rust-lang.org/u/crlf0710)\
**Post date:** [October 20, 2022, 11:23am UTC](https://internals.rust-lang.org/t/idea-pre-expanded-crates-and-expansion-dependencies/17595/5 "2022-10-20T11:23:51Z")

</div>

Let's continue the discussion in that topic.

---

<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:** [January 18, 2023, 11:24am UTC](https://internals.rust-lang.org/t/idea-pre-expanded-crates-and-expansion-dependencies/17595/6 "2023-01-18T11:24:31Z")

</div>

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