# Add API for evaluating #\[cfg(...)\] conditions from proc macros

**URL:** <https://internals.rust-lang.org/t/add-api-for-evaluating-cfg-conditions-from-proc-macros/12459>\
**Category:** libs\
**Created:** [May 29, 2020, 6:35pm UTC](https://internals.rust-lang.org/t/add-api-for-evaluating-cfg-conditions-from-proc-macros/12459 "2020-05-29T18:35:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![programmerjake](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/programmerjake/32/5893_2.png) [@programmerjake](https://internals.rust-lang.org/u/programmerjake)\
**Post date:** [May 29, 2020, 6:35pm UTC](https://internals.rust-lang.org/t/add-api-for-evaluating-cfg-conditions-from-proc-macros/12459/1 "2020-05-29T18:35:44Z")

</div>

I think this should be part of the proc\_macros API since that's something the compiler is best equipped to deal with.

Motivation: [https://github.com/PyO3/pyo3/issues/780#issuecomment-636122320](https://github.com/PyO3/pyo3/issues/780#issuecomment-636122320)

---

<div class="post-metadata">

**Author:** ![birkenfeld](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/birkenfeld/32/1150_2.png) [@birkenfeld](https://internals.rust-lang.org/u/birkenfeld)\
**Post date:** [May 29, 2020, 7:51pm UTC](https://internals.rust-lang.org/t/add-api-for-evaluating-cfg-conditions-from-proc-macros/12459/2 "2020-05-29T19:51:17Z")

</div>

TL;DR from the issue:

PyO3 allows annotating an inherent `impl` block with an attribute `#[pyclass]`, which exports all contained methods as Python methods. Some of those methods can be annotated with additional attributes, such as `#[staticmethod]`, which signals special kinds of methods to the proc macro.

A problem arises here with conditional compilation: if the outer attribute is behind a `#[cfg_attr]`, the inner attribute must be as well. But then, the attribute proc macro gets passed the full `#[cfg_attr(condition, staticmethod)]` and needs to evaluate itself whether to parse it - the condition could be different from the one that activated the outer attribute.

This is in contrast to derive macros, where the structure passed to the macro already has such conditions resolved.

---

<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:** [August 27, 2020, 7:51pm UTC](https://internals.rust-lang.org/t/add-api-for-evaluating-cfg-conditions-from-proc-macros/12459/3 "2020-08-27T19:51:20Z")

</div>

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