# Editions and generated code

**URL:** <https://internals.rust-lang.org/t/editions-and-generated-code/9054>\
**Category:** language design\
**Created:** [December 18, 2018, 4:22pm UTC](https://internals.rust-lang.org/t/editions-and-generated-code/9054 "2018-12-18T16:22:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [December 18, 2018, 4:22pm UTC](https://internals.rust-lang.org/t/editions-and-generated-code/9054/1 "2018-12-18T16:22:04Z")

</div>

Generated code (the kind that you generate in `build.rs`) is in an interesting spot in terms of edition changes. Obviously, if the generated code uses features like async blocks, it’ll have to require the new edition, as those are (unfortunately) impossible to express in the 2015 edition.

But for smaller quality-of-life changes in behavior, such as the path changes, generated code is in the unenviable spot of wanting to generate code that works in either edition. (For current stable compilers, the same applies to procedural macro-generated code with `Span::call_site`, IIRC.)

Would it make sense to add a `#[edition = "2018"]` attribute such that modules can specify a different edition from the crate default? This would allow generated modules at least to start with `#![edition = "2018"]` and not worry about the edition of the crate they’re generating code for.

This would also help large crates where `rustfix` doesn’t quite cover everything to do a slower more piecewise conversion. Or is this too much risk? Either way we should probably record somewhere best practices for code generating into an unknown edition, and keep that use case in mind for future edition changes.

---

<div class="post-metadata">

**Author:** ![steveklabnik](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steveklabnik/32/4524_2.png) [@steveklabnik](https://internals.rust-lang.org/u/steveklabnik)\
**Post date:** [December 18, 2018, 5:34pm UTC](https://internals.rust-lang.org/t/editions-and-generated-code/9054/2 "2018-12-18T17:34:58Z")

</div>

There should be a way to generate code that works in 2015, and works in 2018, where the 2018 version produces warnings. So if you generate the code with #![allow(warnings)], that should work.

---

<div class="post-metadata">

**Author:** ![mark-i-m](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mark-i-m/32/3167_2.png) [@mark-i-m](https://internals.rust-lang.org/u/mark-i-m)\
**Post date:** [December 18, 2018, 6:14pm UTC](https://internals.rust-lang.org/t/editions-and-generated-code/9054/3 "2018-12-18T18:14:03Z")

</div>

So my understanding is that internally the compiler already tracks editions on a per-span basis. This is why you can call a macro from edition 2018 in edition 2015 and vice versa. I would expect that proc-macros and custom-derives work the same.

---

<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:** [March 25, 2019, 8:31am UTC](https://internals.rust-lang.org/t/editions-and-generated-code/9054/4 "2019-03-25T08:31:19Z")

</div>

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