# Get compiler version/edition from proc macro?

**URL:** <https://internals.rust-lang.org/t/get-compiler-version-edition-from-proc-macro/9507>\
**Category:** compiler\
**Created:** [February 23, 2019, 8:23pm UTC](https://internals.rust-lang.org/t/get-compiler-version-edition-from-proc-macro/9507 "2019-02-23T20:23:32Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![joshlf](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/joshlf/32/3815_2.png) [@joshlf](https://internals.rust-lang.org/u/joshlf)\
**Post date:** [February 23, 2019, 8:23pm UTC](https://internals.rust-lang.org/t/get-compiler-version-edition-from-proc-macro/9507/1 "2019-02-23T20:23:32Z")

</div>

I’m writing a custom derive that uses some standard but ugly hacks to avoid namespace collisions. However, those hacks are unnecessary in the 2018 edition. Is it possible, either from the proc macro or using `#[cfg(...)]` or something like that, to detect a) the compiler version and/or, b) the edition being used to compile the current crate so that I can do the nice thing for 2018?

---

<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:** [February 23, 2019, 9:11pm UTC](https://internals.rust-lang.org/t/get-compiler-version-edition-from-proc-macro/9507/2 "2019-02-23T21:11:21Z")

</div>

If the proc macro crate itself has 2018 edition, then in most cases you should be able to simply use 2018 edition techniques in the macro, it will be usable on 2015 edition anyway due to edition hygiene.

---

<div class="post-metadata">

**Author:** ![Ixrec](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ixrec/32/6754_2.png) [@Ixrec](https://internals.rust-lang.org/u/Ixrec)\
**Post date:** [February 23, 2019, 10:57pm UTC](https://internals.rust-lang.org/t/get-compiler-version-edition-from-proc-macro/9507/3 "2019-02-23T22:57:29Z")

</div>

Indeed, “edition hygiene” should solve all cases of this that are unique to proc macros.

For other cases, see: [RFC: #[cfg(accessible(…) / version(…))]](https://github.com/rust-lang/rfcs/pull/2523)

---

<div class="post-metadata">

**Author:** ![joshlf](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/joshlf/32/3815_2.png) [@joshlf](https://internals.rust-lang.org/u/joshlf)\
**Post date:** [February 23, 2019, 11:40pm UTC](https://internals.rust-lang.org/t/get-compiler-version-edition-from-proc-macro/9507/4 "2019-02-23T23:40:12Z")

</div>

> [@Ixrec](#):
>
> Indeed, “edition hygiene” should solve all cases of this that are unique to proc macros.

Does this include code generated by a proc macro? E.g., let's say I have a proc macro defined in a crate with edition 2018, and it's consumed by a crate with edition 2015. Is the compiler smart enough to parse the code generated by the proc macro as 2018 even though the surrounding code is 2015?

---

<div class="post-metadata">

**Author:** ![Ixrec](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ixrec/32/6754_2.png) [@Ixrec](https://internals.rust-lang.org/u/Ixrec)\
**Post date:** [February 23, 2019, 11:45pm UTC](https://internals.rust-lang.org/t/get-compiler-version-edition-from-proc-macro/9507/5 "2019-02-23T23:45:56Z")

</div>

I’m almost certain this is the case for declarative macros, and I don’t know of any reason it wouldn’t be true of proc macros, but someone else would have to confirm.

---

<div class="post-metadata">

**Author:** ![joshlf](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/joshlf/32/3815_2.png) [@joshlf](https://internals.rust-lang.org/u/joshlf)\
**Post date:** [February 23, 2019, 11:49pm UTC](https://internals.rust-lang.org/t/get-compiler-version-edition-from-proc-macro/9507/6 "2019-02-23T23:49:37Z")

</div>

> [@Ixrec](#):
>
> I’m almost certain this is the case for declarative macros, and I don’t know of any reason it wouldn’t be true of proc macros, but someone else would have to confirm.

OK, cool! cc @dtolnay who's been helping me with this, and would likely know for sure.

---

<div class="post-metadata">

**Author:** ![dtolnay](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dtolnay/32/1447_2.png) [@dtolnay](https://internals.rust-lang.org/u/dtolnay)\
**Post date:** [February 24, 2019, 4:12am UTC](https://internals.rust-lang.org/t/get-compiler-version-edition-from-proc-macro/9507/7 "2019-02-24T04:12:40Z")

</div>

I have relied on edition hygiene in proc macros in the case of 2015 macros invoked from 2018 crates, since serde\_derive emits code containing `try` as an identifier. I haven’t used it for paths / extern crate related code in 2018 macros invoked from 2015 crates. Hopefully that works! Try it out.

---

<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:** [February 24, 2019, 6:22am UTC](https://internals.rust-lang.org/t/get-compiler-version-edition-from-proc-macro/9507/8 "2019-02-24T06:22:35Z")

</div>

If it doesn’t work, please report, it generally means a bug.  
(Or a rare case of edition being set globally rather than per-span, like for NLL.)

---

<div class="post-metadata">

**Author:** ![joshlf](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/joshlf/32/3815_2.png) [@joshlf](https://internals.rust-lang.org/u/joshlf)\
**Post date:** [February 24, 2019, 7:57am UTC](https://internals.rust-lang.org/t/get-compiler-version-edition-from-proc-macro/9507/9 "2019-02-24T07:57:40Z")

</div>

Cool, thanks folks! Seems to work so far, albeit not with a huge amount of testing.

---

<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:** [May 25, 2019, 8:08am UTC](https://internals.rust-lang.org/t/get-compiler-version-edition-from-proc-macro/9507/10 "2019-05-25T08:08:35Z")

</div>

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