]> git.lizzy.rs Git - rust.git/commit
Add a cfg_attr syntax extension
authorSteven Fackler <sfackler@gmail.com>
Sun, 3 Aug 2014 21:25:30 +0000 (14:25 -0700)
committerSteven Fackler <sfackler@gmail.com>
Wed, 24 Sep 2014 06:47:45 +0000 (23:47 -0700)
commite520bb1b2f1667f17c3503af71273921c4fc9989
tree2b9b68d41ef081045271bdcfeea6fafbb6927f4d
parentc8bafe0466e6abb7342fc72fdf276d70ae83205b
Add a cfg_attr syntax extension

This extends cfg-gating to attributes.

```rust
 #[cfg_attr(<cfg pattern>, <attr>)]
```
will expand to
```rust
 #[<attr>]
```
if the `<cfg pattern>` 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(<cfg pattern>)` is a cfg pattern and matches if `<cfg pattern>`
    does not.
 * `all(<cfg pattern>, ...)` is a cfg pattern and matches if all of the
    `<cfg pattern>`s do.
 * `any(<cfg pattern>, ...)` is a cfg pattern and matches if any of the
    `<cfg pattern>`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<T> { ... }
```
src/libsyntax/ext/base.rs
src/libsyntax/ext/cfg_attr.rs [new file with mode: 0644]
src/libsyntax/lib.rs
src/test/run-pass/cfg_attr.rs [new file with mode: 0644]