Skip to content

Commit d670d76

Browse files
committed
auto merge of #17963 : sfackler/rust/cfg-error, r=alexcrichton
All deprecation warnings have been converted to errors. This includes the warning for multiple cfgs on one item. We'll leave that as an error for some period of time to ensure that all uses are updated before the behavior changes from "or" to "and".
2 parents 7dbd434 + aa3b126 commit d670d76

22 files changed

+81
-172
lines changed

src/doc/guide-ffi.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ conventions. Rust provides a way to tell the compiler which convention to use:
475475
~~~~
476476
extern crate libc;
477477

478-
#[cfg(target_os = "win32", target_arch = "x86")]
478+
#[cfg(all(target_os = "win32", target_arch = "x86"))]
479479
#[link(name = "kernel32")]
480480
#[allow(non_snake_case)]
481481
extern "stdcall" {

src/doc/guide-unsafe.md

+7-9
Original file line numberDiff line numberDiff line change
@@ -313,17 +313,15 @@ literal string (i.e `""`)
313313
```
314314
#![feature(asm)]
315315
316-
#[cfg(target_arch = "x86")]
317-
#[cfg(target_arch = "x86_64")]
316+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
318317
fn foo() {
319318
unsafe {
320319
asm!("NOP");
321320
}
322321
}
323322
324323
// other platforms
325-
#[cfg(not(target_arch = "x86"),
326-
not(target_arch = "x86_64"))]
324+
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
327325
fn foo() { /* ... */ }
328326
329327
fn main() {
@@ -340,7 +338,7 @@ but you must add the right number of `:` if you skip them:
340338

341339
```
342340
# #![feature(asm)]
343-
# #[cfg(target_arch = "x86")] #[cfg(target_arch = "x86_64")]
341+
# #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
344342
# fn main() { unsafe {
345343
asm!("xor %eax, %eax"
346344
:
@@ -354,7 +352,7 @@ Whitespace also doesn't matter:
354352

355353
```
356354
# #![feature(asm)]
357-
# #[cfg(target_arch = "x86")] #[cfg(target_arch = "x86_64")]
355+
# #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
358356
# fn main() { unsafe {
359357
asm!("xor %eax, %eax" ::: "eax");
360358
# } }
@@ -368,7 +366,7 @@ expressions must be mutable lvalues:
368366

369367
```
370368
# #![feature(asm)]
371-
# #[cfg(target_arch = "x86")] #[cfg(target_arch = "x86_64")]
369+
# #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
372370
fn add(a: int, b: int) -> int {
373371
let mut c = 0;
374372
unsafe {
@@ -379,7 +377,7 @@ fn add(a: int, b: int) -> int {
379377
}
380378
c
381379
}
382-
# #[cfg(not(target_arch = "x86"), not(target_arch = "x86_64"))]
380+
# #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
383381
# fn add(a: int, b: int) -> int { a + b }
384382
385383
fn main() {
@@ -396,7 +394,7 @@ stay valid.
396394

397395
```
398396
# #![feature(asm)]
399-
# #[cfg(target_arch = "x86")] #[cfg(target_arch = "x86_64")]
397+
# #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
400398
# fn main() { unsafe {
401399
// Put the value 0x200 in eax
402400
asm!("mov $$0x200, %eax" : /* no outputs */ : /* no inputs */ : "eax");

src/doc/reference.md

+10-8
Original file line numberDiff line numberDiff line change
@@ -2049,26 +2049,28 @@ fn macos_only() {
20492049
}
20502050
20512051
// This function is only included when either foo or bar is defined
2052-
#[cfg(foo)]
2053-
#[cfg(bar)]
2052+
#[cfg(any(foo, bar))]
20542053
fn needs_foo_or_bar() {
20552054
// ...
20562055
}
20572056
20582057
// This function is only included when compiling for a unixish OS with a 32-bit
20592058
// architecture
2060-
#[cfg(unix, target_word_size = "32")]
2059+
#[cfg(all(unix, target_word_size = "32"))]
20612060
fn on_32bit_unix() {
20622061
// ...
20632062
}
2063+
2064+
// This function is only included when foo is not defined
2065+
#[cfg(not(foo))]
2066+
fn needs_not_foo() {
2067+
// ...
2068+
}
20642069
```
20652070

20662071
This illustrates some conditional compilation can be achieved using the
2067-
`#[cfg(...)]` attribute. Note that `#[cfg(foo, bar)]` is a condition that needs
2068-
both `foo` and `bar` to be defined while `#[cfg(foo)] #[cfg(bar)]` only needs
2069-
one of `foo` and `bar` to be defined (this resembles in the disjunctive normal
2070-
form). Additionally, one can reverse a condition by enclosing it in a
2071-
`not(...)`, like e. g. `#[cfg(not(target_os = "win32"))]`.
2072+
`#[cfg(...)]` attribute. `any`, `all` and `not` can be used to assemble
2073+
arbitrarily complex configurations through nesting.
20722074

20732075
The following configurations must be defined by the implementation:
20742076

src/libsyntax/attr.rs

+3-54
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,10 @@ pub fn cfg_matches(diagnostic: &SpanHandler, cfgs: &[P<MetaItem>], cfg: &ast::Me
316316
mis.iter().all(|mi| cfg_matches(diagnostic, cfgs, &**mi)),
317317
ast::MetaList(ref pred, ref mis) if pred.get() == "not" => {
318318
if mis.len() != 1 {
319-
diagnostic.span_warn(cfg.span, "the use of multiple cfgs in the same `not` \
320-
statement is deprecated. Change `not(a, b)` to \
321-
`not(all(a, b))`.");
319+
diagnostic.span_err(cfg.span, "expected 1 cfg-pattern");
320+
return false;
322321
}
323-
!mis.iter().all(|mi| cfg_matches(diagnostic, cfgs, &**mi))
322+
!cfg_matches(diagnostic, cfgs, &*mis[0])
324323
}
325324
ast::MetaList(ref pred, _) => {
326325
diagnostic.span_err(cfg.span, format!("invalid predicate `{}`", pred).as_slice());
@@ -330,56 +329,6 @@ pub fn cfg_matches(diagnostic: &SpanHandler, cfgs: &[P<MetaItem>], cfg: &ast::Me
330329
}
331330
}
332331

333-
/// Tests if any `cfg(...)` meta items in `metas` match `cfg`. e.g.
334-
///
335-
/// test_cfg(`[foo="a", bar]`, `[cfg(foo), cfg(bar)]`) == true
336-
/// test_cfg(`[foo="a", bar]`, `[cfg(not(bar))]`) == false
337-
/// test_cfg(`[foo="a", bar]`, `[cfg(bar, foo="a")]`) == true
338-
/// test_cfg(`[foo="a", bar]`, `[cfg(bar, foo="b")]`) == false
339-
pub fn test_cfg<'a, AM: AttrMetaMethods, It: Iterator<&'a AM>>
340-
(cfg: &[P<MetaItem>], mut metas: It) -> bool {
341-
// having no #[cfg(...)] attributes counts as matching.
342-
let mut no_cfgs = true;
343-
344-
// this would be much nicer as a chain of iterator adaptors, but
345-
// this doesn't work.
346-
let some_cfg_matches = metas.fold(false, |matches, mi| {
347-
debug!("testing name: {}", mi.name());
348-
let this_matches = if mi.check_name("cfg") { // it is a #[cfg()] attribute
349-
debug!("is cfg");
350-
no_cfgs = false;
351-
// only #[cfg(...)] ones are understood.
352-
match mi.meta_item_list() {
353-
Some(cfg_meta) => {
354-
debug!("is cfg(...)");
355-
cfg_meta.iter().all(|cfg_mi| {
356-
debug!("cfg({}[...])", cfg_mi.name());
357-
match cfg_mi.node {
358-
ast::MetaList(ref s, ref not_cfgs)
359-
if s.equiv(&("not")) => {
360-
debug!("not!");
361-
// inside #[cfg(not(...))], so these need to all
362-
// not match.
363-
!not_cfgs.iter().all(|mi| {
364-
debug!("cfg(not({}[...]))", mi.name());
365-
contains(cfg, &**mi)
366-
})
367-
}
368-
_ => contains(cfg, &**cfg_mi)
369-
}
370-
})
371-
}
372-
None => false
373-
}
374-
} else {
375-
false
376-
};
377-
matches || this_matches
378-
});
379-
debug!("test_cfg (no_cfgs={}, some_cfg_matches={})", no_cfgs, some_cfg_matches);
380-
no_cfgs || some_cfg_matches
381-
}
382-
383332
/// Represents the #[deprecated="foo"] and friends attributes.
384333
#[deriving(Encodable,Decodable,Clone,Show)]
385334
pub struct Stability {

src/libsyntax/config.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -259,20 +259,20 @@ fn in_cfg(diagnostic: &SpanHandler, cfg: &[P<ast::MetaItem>], attrs: &[ast::Attr
259259
};
260260

261261
if mis.len() != 1 {
262-
diagnostic.span_warn(attr.span, "The use of multiple cfgs in the top level of \
263-
`#[cfg(..)]` is deprecated. Change `#[cfg(a, b)]` to \
264-
`#[cfg(all(a, b))]`.");
262+
diagnostic.span_err(attr.span, "expected 1 cfg-pattern");
263+
return false;
265264
}
266265

267266
if seen_cfg {
268-
diagnostic.span_warn(attr.span, "The semantics of multiple `#[cfg(..)]` attributes on \
269-
same item are changing from the union of the cfgs to \
270-
the intersection of the cfgs. Change `#[cfg(a)] \
271-
#[cfg(b)]` to `#[cfg(any(a, b))]`.");
267+
diagnostic.span_err(attr.span, "The semantics of multiple `#[cfg(..)]` attributes on \
268+
same item are changing from the union of the cfgs to \
269+
the intersection of the cfgs. Change `#[cfg(a)] \
270+
#[cfg(b)]` to `#[cfg(any(a, b))]`.");
271+
return false;
272272
}
273273

274274
seen_cfg = true;
275-
in_cfg |= mis.iter().all(|mi| attr::cfg_matches(diagnostic, cfg, &**mi));
275+
in_cfg |= attr::cfg_matches(diagnostic, cfg, &*mis[0]);
276276
}
277277
in_cfg | !seen_cfg
278278
}

src/libsyntax/ext/cfg.rs

+8-20
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
/**
12-
The compiler code necessary to support the cfg! extension, which
13-
expands to a literal `true` or `false` based on whether the given cfgs
14-
match the current compilation environment.
15-
*/
11+
/// The compiler code necessary to support the cfg! extension, which expands to
12+
/// a literal `true` or `false` based on whether the given cfg matches the
13+
/// current compilation environment.
1614
1715
use ast;
1816
use codemap::Span;
@@ -24,28 +22,18 @@ use attr::*;
2422
use parse::attr::ParserAttr;
2523
use parse::token;
2624

27-
2825
pub fn expand_cfg<'cx>(cx: &mut ExtCtxt,
2926
sp: Span,
3027
tts: &[ast::TokenTree])
3128
-> Box<base::MacResult+'static> {
3229
let mut p = cx.new_parser_from_tts(tts);
33-
let mut cfgs = Vec::new();
34-
// parse `cfg!(meta_item, meta_item(x,y), meta_item="foo", ...)`
35-
while p.token != token::EOF {
36-
cfgs.push(p.parse_meta_item());
37-
if p.eat(&token::EOF) { break } // trailing comma is optional,.
38-
p.expect(&token::COMMA);
39-
}
30+
let cfg = p.parse_meta_item();
4031

41-
if cfgs.len() != 1 {
42-
cx.span_warn(sp, "The use of multiple cfgs at the top level of `cfg!` \
43-
is deprecated. Change `cfg!(a, b)` to \
44-
`cfg!(all(a, b))`.");
32+
if !p.eat(&token::EOF) {
33+
cx.span_err(sp, "expected 1 cfg-pattern");
34+
return DummyResult::expr(sp);
4535
}
4636

47-
let matches_cfg = cfgs.iter().all(|cfg| attr::cfg_matches(&cx.parse_sess.span_diagnostic,
48-
cx.cfg.as_slice(), &**cfg));
49-
37+
let matches_cfg = attr::cfg_matches(&cx.parse_sess.span_diagnostic, cx.cfg.as_slice(), &*cfg);
5038
MacExpr::new(cx.expr_bool(sp, matches_cfg))
5139
}

src/libsyntax/test.rs

+3-17
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> {
126126
span: i.span,
127127
path: self.cx.path.clone(),
128128
bench: is_bench_fn(&self.cx, &*i),
129-
ignore: is_ignored(&self.cx, &*i),
129+
ignore: is_ignored(&*i),
130130
should_fail: should_fail(&*i)
131131
};
132132
self.cx.testfns.push(test);
@@ -343,22 +343,8 @@ fn is_bench_fn(cx: &TestCtxt, i: &ast::Item) -> bool {
343343
return has_bench_attr && has_test_signature(i);
344344
}
345345

346-
fn is_ignored(cx: &TestCtxt, i: &ast::Item) -> bool {
347-
i.attrs.iter().any(|attr| {
348-
// check ignore(cfg(foo, bar))
349-
attr.check_name("ignore") && match attr.meta_item_list() {
350-
Some(ref cfgs) => {
351-
if cfgs.iter().any(|cfg| cfg.check_name("cfg")) {
352-
cx.span_diagnostic.span_warn(attr.span,
353-
"The use of cfg filters in #[ignore] is \
354-
deprecated. Use #[cfg_attr(<cfg pattern>, \
355-
ignore)] instead.");
356-
}
357-
attr::test_cfg(cx.config.as_slice(), cfgs.iter())
358-
}
359-
None => true
360-
}
361-
})
346+
fn is_ignored(i: &ast::Item) -> bool {
347+
i.attrs.iter().any(|attr| attr.check_name("ignore"))
362348
}
363349

364350
fn should_fail(i: &ast::Item) -> bool {

src/test/auxiliary/extern_calling_convention.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ pub extern "win64" fn foo(a: int, b: int, c: int, d: int) {
2626
}
2727

2828
#[inline(never)]
29-
#[cfg(target_arch = "x86")]
30-
#[cfg(target_arch = "arm")]
29+
#[cfg(any(target_arch = "x86", target_arch = "arm"))]
3130
pub extern fn foo(a: int, b: int, c: int, d: int) {
3231
assert!(a == 1);
3332
assert!(b == 2);

src/test/run-make/test-harness/test-ignore-cfg.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
// except according to those terms.
1010

1111
#[test]
12-
#[ignore(cfg(ignorecfg))]
12+
#[cfg_attr(ignorecfg, ignore)]
1313
fn shouldignore() {
1414
}
1515

1616
#[test]
17-
#[ignore(cfg(noignorecfg))]
17+
#[cfg_attr(noignorecfg, ignore)]
1818
fn shouldnotignore() {
1919
}

src/test/run-pass/asm-in-out-operand.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
#![feature(asm)]
1212

13-
#[cfg(target_arch = "x86")]
14-
#[cfg(target_arch = "x86_64")]
13+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
1514
unsafe fn next_power_of_2(n: u32) -> u32 {
1615
let mut tmp = n;
1716
asm!("dec $0" : "+rm"(tmp) :: "cc");
@@ -28,8 +27,7 @@ unsafe fn next_power_of_2(n: u32) -> u32 {
2827
return tmp;
2928
}
3029

31-
#[cfg(target_arch = "x86")]
32-
#[cfg(target_arch = "x86_64")]
30+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
3331
pub fn main() {
3432
unsafe {
3533
assert_eq!(64, next_power_of_2(37));
@@ -62,5 +60,5 @@ pub fn main() {
6260
assert_eq!(x, 60);
6361
}
6462

65-
#[cfg(not(target_arch = "x86"), not(target_arch = "x86_64"))]
63+
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
6664
pub fn main() {}

src/test/run-pass/asm-out-assign.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
#![feature(asm)]
1212

13-
#[cfg(target_arch = "x86")]
14-
#[cfg(target_arch = "x86_64")]
13+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
1514
pub fn main() {
1615
let x: int;
1716
unsafe {
@@ -30,5 +29,5 @@ pub fn main() {
3029
assert_eq!(x, 13);
3130
}
3231

33-
#[cfg(not(target_arch = "x86"), not(target_arch = "x86_64"))]
32+
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
3433
pub fn main() {}

src/test/run-pass/bitwise.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
// except according to those terms.
1010

1111

12-
#[cfg(target_arch = "x86")]
13-
#[cfg(target_arch = "arm")]
12+
#[cfg(any(target_arch = "x86", target_arch = "arm"))]
1413
fn target() {
1514
assert_eq!(-1000 as uint >> 3u, 536870787u);
1615
}

0 commit comments

Comments
 (0)