rust-lang/rust — pull request #16230 by sfackler (2014-08-04 00:59:24+00:00)
Title: Add a cfg_attr syntax extension

This extends cfg-gating to attributes.

```rust
#[cfg_attr(, )]
```
will expand to
```rust
#[]
```
if the `` matches the current cfg environment, and nothing
if it does not. The grammar for the cfg pattern has a simple
recursive structure:

* `value` and `key = "value"` are cfg patterns,
* `not()` is a cfg pattern and matches if ``
does not.
* `all(, ...)` is a cfg pattern and matches if all of the
``s do.
* `any(, ...)` is a cfg pattern and matches if any of the
``s do.

Examples:

```rust
// only derive Show for assert_eq! in tests
#[cfg_attr(test, deriving(Show))]
struct Foo { ... }

// only derive Show for assert_eq! in tests and debug builds
#[cfg_attr(any(test, not(ndebug)), deriving(Show))]
struct Foo { ... }

// ignore a test in certain cases
#[test]
#[cfg_attr(all(not(target_os = "linux"), target_endian = "big"), ignore)]
fn test_broken_thing() { ... }

// Avoid duplication when fixing staging issues in rustc
#[cfg_attr(not(stage0), lang="iter")]
pub trait Iterator { ... }
```

I also deprecated the `#[ignore(cfg(...))]` syntax in favor of `#[cfg_attr(..., ignore)]`.

I think this cfg pattern syntax is a good system to move `#[cfg(...)]` to as well, but that should probably go through an RFC first.