Skip to content

Commit 43d7d7c

Browse files
committed
auto merge of #17506 : sfackler/rust/cfg-attr, r=alexcrichton
cc #17490 Reopening of #16230
2 parents d64b410 + dcdbdc1 commit 43d7d7c

File tree

20 files changed

+143
-20
lines changed

20 files changed

+143
-20
lines changed

src/doc/guide-testing.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ is not used.
7272
Tests that should not be run can be annotated with the `ignore`
7373
attribute. The existence of these tests will be noted in the test
7474
runner output, but the test will not be run. Tests can also be ignored
75-
by configuration so, for example, to ignore a test on windows you can
76-
write `#[ignore(cfg(target_os = "win32"))]`.
75+
by configuration using the `cfg_attr` attribute so, for example, to ignore a
76+
test on windows you can write `#[cfg_attr(windows, ignore)]`.
7777

7878
Tests that are intended to fail can be annotated with the
7979
`should_fail` attribute. The test will be run, and if it causes its

src/libnative/io/file_unix.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ mod tests {
506506
use std::os;
507507
use std::rt::rtio::{RtioFileStream, SeekSet};
508508

509-
#[ignore(cfg(target_os = "freebsd"))] // hmm, maybe pipes have a tiny buffer
509+
#[cfg_attr(target_os = "freebsd", ignore)] // hmm, maybe pipes have a tiny buffer
510510
#[test]
511511
fn test_file_desc() {
512512
// Run this test with some pipes so we don't have to mess around with

src/libnum/complex.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ mod test {
219219
}
220220

221221
#[test]
222-
#[ignore(cfg(target_arch = "x86"))]
222+
#[cfg_attr(target_arch = "x86", ignore)]
223223
// FIXME #7158: (maybe?) currently failing on x86.
224224
fn test_norm() {
225225
fn test(c: Complex64, ns: f64) {

src/librustuv/net.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,7 @@ mod test {
10851085
}
10861086

10871087
#[test]
1088-
#[ignore(cfg(windows))] // FIXME(#10102) server never sees second packet
1088+
#[cfg_attr(windows, ignore)] // FIXME(#10102) server never sees second packet
10891089
fn test_udp_twice() {
10901090
let server_addr = ::next_test_ip4();
10911091
let client_addr = ::next_test_ip4();

src/libserialize/json.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3352,7 +3352,7 @@ mod tests {
33523352
}
33533353
}
33543354
#[test]
3355-
#[ignore(cfg(target_word_size = "32"))] // FIXME(#14064)
3355+
#[cfg_attr(target_word_size = "32", ignore)] // FIXME(#14064)
33563356
fn test_streaming_parser() {
33573357
assert_stream_equal(
33583358
r#"{ "foo":"bar", "array" : [0, 1, 2, 3, 4, 5], "idents":[null,true,false]}"#,
@@ -3388,7 +3388,7 @@ mod tests {
33883388
}
33893389

33903390
#[test]
3391-
#[ignore(cfg(target_word_size = "32"))] // FIXME(#14064)
3391+
#[cfg_attr(target_word_size = "32", ignore)] // FIXME(#14064)
33923392
fn test_read_object_streaming() {
33933393
assert_eq!(last_event("{ "), Error(SyntaxError(EOFWhileParsingObject, 1, 3)));
33943394
assert_eq!(last_event("{1"), Error(SyntaxError(KeyMustBeAString, 1, 2)));
@@ -3461,7 +3461,7 @@ mod tests {
34613461
);
34623462
}
34633463
#[test]
3464-
#[ignore(cfg(target_word_size = "32"))] // FIXME(#14064)
3464+
#[cfg_attr(target_word_size = "32", ignore)] // FIXME(#14064)
34653465
fn test_read_list_streaming() {
34663466
assert_stream_equal(
34673467
"[]",

src/libstd/dynamic_lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ mod test {
162162
use mem;
163163

164164
#[test]
165-
#[ignore(cfg(windows))] // FIXME #8818
166-
#[ignore(cfg(target_os="android"))] // FIXME(#10379)
165+
#[cfg_attr(any(windows, target_os = "android"), ignore)] // FIXME #8818, #10379
167166
fn test_loading_cosine() {
168167
// The math library does not need to be loaded since it is already
169168
// statically linked in

src/libstd/io/net/pipe.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ mod tests {
320320
}, proc(_client) {
321321
// drop the client
322322
})
323-
} #[ignore(cfg(windows))]) // FIXME(#12516)
323+
} #[cfg_attr(windows, ignore)]) // FIXME(#12516)
324324

325325
iotest!(fn write_begone() {
326326
smalltest(proc(mut server) {

src/libstd/io/net/tcp.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ mod test {
533533
Ok(..) => fail!(),
534534
Err(e) => assert_eq!(e.kind, PermissionDenied),
535535
}
536-
} #[ignore(cfg(windows))] #[ignore(cfg(target_os = "android"))])
536+
} #[cfg_attr(any(windows, target_os = "android"), ignore)])
537537

538538
iotest!(fn connect_error() {
539539
match TcpStream::connect("0.0.0.0", 1) {

src/libstd/io/net/udp.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ mod test {
273273
Ok(..) => fail!(),
274274
Err(e) => assert_eq!(e.kind, PermissionDenied),
275275
}
276-
} #[ignore(cfg(windows))] #[ignore(cfg(target_os = "android"))])
276+
} #[cfg_attr(any(windows, target_os = "android"), ignore)])
277277

278278
iotest!(fn socket_smoke_test_ip4() {
279279
let server_ip = next_test_ip4();

src/libstd/num/f32.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ mod tests {
766766
assert_eq!((-0f32).frexp(), (-0f32, 0));
767767
}
768768

769-
#[test] #[ignore(cfg(windows))] // FIXME #8755
769+
#[test] #[cfg_attr(windows, ignore)] // FIXME #8755
770770
fn test_frexp_nowin() {
771771
let inf: f32 = Float::infinity();
772772
let neg_inf: f32 = Float::neg_infinity();

src/libstd/num/f64.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ mod tests {
768768
assert_eq!((-0f64).frexp(), (-0f64, 0));
769769
}
770770

771-
#[test] #[ignore(cfg(windows))] // FIXME #8755
771+
#[test] #[cfg_attr(windows, ignore)] // FIXME #8755
772772
fn test_frexp_nowin() {
773773
let inf: f64 = Float::infinity();
774774
let neg_inf: f64 = Float::neg_infinity();

src/libsync/deque.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ mod tests {
600600
}
601601

602602
#[test]
603-
#[ignore(cfg(windows))] // apparently windows scheduling is weird?
603+
#[cfg_attr(windows, ignore)] // apparently windows scheduling is weird?
604604
fn no_starvation() {
605605
static AMT: int = 10000;
606606
static NTHREADS: int = 4;

src/libsyntax/ext/base.rs

+2
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,8 @@ fn initial_syntax_expander_table() -> SyntaxEnv {
439439
syntax_expanders.insert(intern("cfg"),
440440
builtin_normal_expander(
441441
ext::cfg::expand_cfg));
442+
syntax_expanders.insert(intern("cfg_attr"),
443+
Modifier(box ext::cfg_attr::expand));
442444
syntax_expanders.insert(intern("trace_macros"),
443445
builtin_normal_expander(
444446
ext::trace_macros::expand_trace_macros));

src/libsyntax/ext/cfg_attr.rs

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use ast;
12+
use attr;
13+
use codemap::Span;
14+
use ext::base::ExtCtxt;
15+
use ext::build::AstBuilder;
16+
use ptr::P;
17+
18+
pub fn expand(cx: &mut ExtCtxt, sp: Span, mi: &ast::MetaItem, it: P<ast::Item>) -> P<ast::Item> {
19+
let (cfg, attr) = match mi.node {
20+
ast::MetaList(_, ref mis) if mis.len() == 2 => (&mis[0], &mis[1]),
21+
_ => {
22+
cx.span_err(sp, "expected `#[cfg_attr(<cfg pattern>, <attr>)]`");
23+
return it;
24+
}
25+
};
26+
27+
let mut out = (*it).clone();
28+
if cfg_matches(cx, &**cfg) {
29+
out.attrs.push(cx.attribute(attr.span, attr.clone()));
30+
}
31+
32+
P(out)
33+
}
34+
35+
fn cfg_matches(cx: &mut ExtCtxt, cfg: &ast::MetaItem) -> bool {
36+
match cfg.node {
37+
ast::MetaList(ref pred, ref mis) if pred.get() == "any" =>
38+
mis.iter().any(|mi| cfg_matches(cx, &**mi)),
39+
ast::MetaList(ref pred, ref mis) if pred.get() == "all" =>
40+
mis.iter().all(|mi| cfg_matches(cx, &**mi)),
41+
ast::MetaList(ref pred, ref mis) if pred.get() == "not" => {
42+
if mis.len() != 1 {
43+
cx.span_err(cfg.span, format!("expected 1 value, got {}",
44+
mis.len()).as_slice());
45+
return false;
46+
}
47+
!cfg_matches(cx, &*mis[0])
48+
}
49+
ast::MetaList(ref pred, _) => {
50+
cx.span_err(cfg.span,
51+
format!("invalid predicate `{}`", pred).as_slice());
52+
false
53+
},
54+
ast::MetaWord(_) | ast::MetaNameValue(..) =>
55+
attr::contains(cx.cfg.as_slice(), cfg),
56+
}
57+
}

src/libsyntax/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ pub mod ext {
8383
pub mod build;
8484
pub mod bytes;
8585
pub mod cfg;
86+
pub mod cfg_attr;
8687
pub mod concat;
8788
pub mod concat_idents;
8889
pub mod deriving;

src/libsyntax/test.rs

+6
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,12 @@ fn is_ignored(cx: &TestCtxt, i: &ast::Item) -> bool {
339339
// check ignore(cfg(foo, bar))
340340
attr.check_name("ignore") && match attr.meta_item_list() {
341341
Some(ref cfgs) => {
342+
if cfgs.iter().any(|cfg| cfg.check_name("cfg")) {
343+
cx.span_diagnostic.span_warn(attr.span,
344+
"The use of cfg filters in #[ignore] is \
345+
deprecated. Use #[cfg_attr(<cfg pattern>, \
346+
ignore)] instead.");
347+
}
342348
attr::test_cfg(cx.config.as_slice(), cfgs.iter())
343349
}
344350
None => true

src/libtest/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,7 @@ Test Attributes:
356356
#[ignore] - When applied to a function which is already attributed as a
357357
test, then the test runner will ignore these tests during
358358
normal test runs. Running with --ignored will run these
359-
tests. This may also be written as #[ignore(cfg(...))] to
360-
ignore the test on certain configurations.",
359+
tests.",
361360
usage = getopts::usage(message.as_slice(),
362361
optgroups().as_slice()));
363362
}

src/libtime/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1564,7 +1564,7 @@ mod tests {
15641564
}
15651565

15661566
#[test]
1567-
#[ignore(cfg(target_os = "android"))] // FIXME #10958
1567+
#[cfg_attr(target_os = "android", ignore)] // FIXME #10958
15681568
fn run_tests() {
15691569
// The tests race on tzset. So instead of having many independent
15701570
// tests, we will just call the functions now.

src/test/run-pass/cfg_attr.rs

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags:--cfg set1 --cfg set2
12+
#![allow(dead_code)]
13+
use std::fmt::Show;
14+
15+
struct NotShowable;
16+
17+
#[cfg_attr(set1, deriving(Show))]
18+
struct Set1;
19+
20+
#[cfg_attr(notset, deriving(Show))]
21+
struct Notset(NotShowable);
22+
23+
#[cfg_attr(not(notset), deriving(Show))]
24+
struct NotNotset;
25+
26+
#[cfg_attr(not(set1), deriving(Show))]
27+
struct NotSet1(NotShowable);
28+
29+
#[cfg_attr(all(set1, set2), deriving(Show))]
30+
struct AllSet1Set2;
31+
32+
#[cfg_attr(all(set1, notset), deriving(Show))]
33+
struct AllSet1Notset(NotShowable);
34+
35+
#[cfg_attr(any(set1, notset), deriving(Show))]
36+
struct AnySet1Notset;
37+
38+
#[cfg_attr(any(notset, notset2), deriving(Show))]
39+
struct AnyNotsetNotset2(NotShowable);
40+
41+
#[cfg_attr(all(not(notset), any(set1, notset)), deriving(Show))]
42+
struct Complex;
43+
44+
#[cfg_attr(any(notset, not(any(set1, notset))), deriving(Show))]
45+
struct ComplexNot(NotShowable);
46+
47+
#[cfg_attr(any(target_endian = "little", target_endian = "big"), deriving(Show))]
48+
struct KeyValue;
49+
50+
fn is_show<T: Show>() {}
51+
52+
fn main() {
53+
is_show::<Set1>();
54+
is_show::<NotNotset>();
55+
is_show::<AllSet1Set2>();
56+
is_show::<AnySet1Notset>();
57+
is_show::<Complex>();
58+
is_show::<KeyValue>();
59+
}

src/test/run-pass/tcp-connect-timeouts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ iotest!(fn eventual_timeout() {
8080
}
8181
}
8282
fail!("never timed out!");
83-
} #[ignore(cfg(target_os = "freebsd"))])
83+
} #[cfg_attr(target_os = "freebsd", ignore)])
8484

8585
iotest!(fn timeout_success() {
8686
let addr = next_test_ip4();

0 commit comments

Comments
 (0)