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

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

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.

3 Likes