How to find out the compile conditions of an item in a crate automatically?

Given the source code of a crate and the path of an item, how to find out the compile conditions of the item?

For example:

#[cfg(target_os = "linux")]
pub mod foo {
    #[cfg(target_env = "gnu")]
    pub const FOO: u32 = 1;

    #[cfg(target_env = "musl")]
    pub const FOO: u32 = 2;
}

#[cfg(target_os = "windows")]
pub mod foo {
    pub const FOO: u32 = 3;
}

Given the path the_crate::foo::FOO, I want to get the output:

#[cfg(any(
    all(target_os = "linux", any(target_env = "gnu", target_env = "musl",)),
    target_os = "windows",
))]
use the_crate::foo::FOO;

How to generate the output automatically?

I do not think there is any automated way to get that.

In the future, you may be able to write:

#[cfg(accessible(::the_crate::foo::FOO))]
use ::the_crate::foo::FOO;

provided the_crate is an external crate.

See the associated RFC:

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.