Skip to content

Commit 7eb9337

Browse files
committed
auto merge of #17527 : sfackler/rust/cfg-syntax, r=alexcrichton
We'll need a snapshot before we can convert the codebase over and turn on the deprecation warnings. cc #17490 This is sitting on top of #17506
2 parents e079ed7 + 9519abe commit 7eb9337

13 files changed

+102
-66
lines changed

src/librustc/driver/driver.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ pub fn phase_2_configure_and_expand(sess: &Session,
226226
// baz! should not use this definition unless foo is enabled.
227227

228228
krate = time(time_passes, "configuration 1", krate, |krate|
229-
syntax::config::strip_unconfigured_items(krate));
229+
syntax::config::strip_unconfigured_items(sess.diagnostic(), krate));
230230

231231
let mut addl_plugins = Some(addl_plugins);
232232
let Plugins { macros, registrars }
@@ -307,7 +307,7 @@ pub fn phase_2_configure_and_expand(sess: &Session,
307307

308308
// strip again, in case expansion added anything with a #[cfg].
309309
krate = time(time_passes, "configuration 2", krate, |krate|
310-
syntax::config::strip_unconfigured_items(krate));
310+
syntax::config::strip_unconfigured_items(sess.diagnostic(), krate));
311311

312312
krate = time(time_passes, "maybe building test harness", krate, |krate|
313313
syntax::test::modify_for_testing(&sess.parse_sess,

src/libsyntax/attr.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,32 @@ pub fn requests_inline(attrs: &[Attribute]) -> bool {
307307
}
308308
}
309309

310+
/// Tests if a cfg-pattern matches the cfg set
311+
pub fn cfg_matches(diagnostic: &SpanHandler, cfgs: &[P<MetaItem>], cfg: &ast::MetaItem) -> bool {
312+
match cfg.node {
313+
ast::MetaList(ref pred, ref mis) if pred.get() == "any" =>
314+
mis.iter().any(|mi| cfg_matches(diagnostic, cfgs, &**mi)),
315+
ast::MetaList(ref pred, ref mis) if pred.get() == "all" =>
316+
mis.iter().all(|mi| cfg_matches(diagnostic, cfgs, &**mi)),
317+
ast::MetaList(ref pred, ref mis) if pred.get() == "not" => {
318+
// NOTE: turn on after snapshot
319+
/*
320+
if mis.len() != 1 {
321+
diagnostic.span_warn(cfg.span, "the use of multiple cfgs in the same `not` \
322+
statement is deprecated. Change `not(a, b)` to \
323+
`not(all(a, b))`.");
324+
}
325+
*/
326+
!mis.iter().all(|mi| cfg_matches(diagnostic, cfgs, &**mi))
327+
}
328+
ast::MetaList(ref pred, _) => {
329+
diagnostic.span_err(cfg.span, format!("invalid predicate `{}`", pred).as_slice());
330+
false
331+
},
332+
ast::MetaWord(_) | ast::MetaNameValue(..) => contains(cfgs, cfg),
333+
}
334+
}
335+
310336
/// Tests if any `cfg(...)` meta items in `metas` match `cfg`. e.g.
311337
///
312338
/// test_cfg(`[foo="a", bar]`, `[cfg(foo), cfg(bar)]`) == true

src/libsyntax/config.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use attr::AttrMetaMethods;
12+
use diagnostic::SpanHandler;
1113
use fold::Folder;
1214
use {ast, fold, attr};
1315
use codemap::Spanned;
@@ -21,9 +23,9 @@ struct Context<'a> {
2123

2224
// Support conditional compilation by transforming the AST, stripping out
2325
// any items that do not belong in the current configuration
24-
pub fn strip_unconfigured_items(krate: ast::Crate) -> ast::Crate {
26+
pub fn strip_unconfigured_items(diagnostic: &SpanHandler, krate: ast::Crate) -> ast::Crate {
2527
let config = krate.config.clone();
26-
strip_items(krate, |attrs| in_cfg(config.as_slice(), attrs))
28+
strip_items(krate, |attrs| in_cfg(diagnostic, config.as_slice(), attrs))
2729
}
2830

2931
impl<'a> fold::Folder for Context<'a> {
@@ -249,7 +251,34 @@ fn impl_item_in_cfg(cx: &mut Context, impl_item: &ast::ImplItem) -> bool {
249251

250252
// Determine if an item should be translated in the current crate
251253
// configuration based on the item's attributes
252-
fn in_cfg(cfg: &[P<ast::MetaItem>], attrs: &[ast::Attribute]) -> bool {
253-
attr::test_cfg(cfg, attrs.iter())
254+
fn in_cfg(diagnostic: &SpanHandler, cfg: &[P<ast::MetaItem>], attrs: &[ast::Attribute]) -> bool {
255+
let mut in_cfg = false;
256+
let mut seen_cfg = false;
257+
for attr in attrs.iter() {
258+
let mis = match attr.node.value.node {
259+
ast::MetaList(_, ref mis) if attr.check_name("cfg") => mis,
260+
_ => continue
261+
};
262+
263+
// NOTE: turn on after snapshot
264+
/*
265+
if mis.len() != 1 {
266+
diagnostic.span_warn(attr.span, "The use of multiple cfgs in the top level of \
267+
`#[cfg(..)]` is deprecated. Change `#[cfg(a, b)]` to \
268+
`#[cfg(all(a, b))]`.");
269+
}
270+
271+
if seen_cfg {
272+
diagnostic.span_warn(attr.span, "The semantics of multiple `#[cfg(..)]` attributes on \
273+
same item are changing from the union of the cfgs to \
274+
the intersection of the cfgs. Change `#[cfg(a)] \
275+
#[cfg(b)]` to `#[cfg(any(a, b))]`.");
276+
}
277+
*/
278+
279+
seen_cfg = true;
280+
in_cfg |= mis.iter().all(|mi| attr::cfg_matches(diagnostic, cfg, &**mi));
281+
}
282+
in_cfg | !seen_cfg
254283
}
255284

src/libsyntax/ext/cfg.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use ext::build::AstBuilder;
2222
use attr;
2323
use attr::*;
2424
use parse::attr::ParserAttr;
25-
use parse::token::InternedString;
2625
use parse::token;
2726

2827

@@ -39,11 +38,17 @@ pub fn expand_cfg<'cx>(cx: &mut ExtCtxt,
3938
p.expect(&token::COMMA);
4039
}
4140

42-
// test_cfg searches for meta items looking like `cfg(foo, ...)`
43-
let in_cfg = Some(cx.meta_list(sp, InternedString::new("cfg"), cfgs));
41+
// NOTE: turn on after snapshot
42+
/*
43+
if cfgs.len() != 1 {
44+
cx.span_warn(sp, "The use of multiple cfgs at the top level of `cfg!` \
45+
is deprecated. Change `cfg!(a, b)` to \
46+
`cfg!(all(a, b))`.");
47+
}
48+
*/
49+
50+
let matches_cfg = cfgs.iter().all(|cfg| attr::cfg_matches(&cx.parse_sess.span_diagnostic,
51+
cx.cfg.as_slice(), &**cfg));
4452

45-
let matches_cfg = attr::test_cfg(cx.cfg().as_slice(),
46-
in_cfg.iter());
47-
let e = cx.expr_bool(sp, matches_cfg);
48-
MacExpr::new(e)
53+
MacExpr::new(cx.expr_bool(sp, matches_cfg))
4954
}

src/libsyntax/ext/cfg_attr.rs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,10 @@ pub fn expand(cx: &mut ExtCtxt, sp: Span, mi: &ast::MetaItem, it: P<ast::Item>)
2525
};
2626

2727
let mut out = (*it).clone();
28-
if cfg_matches(cx, &**cfg) {
28+
if attr::cfg_matches(&cx.parse_sess.span_diagnostic, cx.cfg.as_slice(), &**cfg) {
2929
out.attrs.push(cx.attribute(attr.span, attr.clone()));
3030
}
3131

3232
P(out)
3333
}
3434

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/test/compile-fail/asm-in-bad-modifier.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212

1313
fn foo(x: int) { println!("{}", x); }
1414

15-
#[cfg(target_arch = "x86")]
16-
#[cfg(target_arch = "x86_64")]
17-
#[cfg(target_arch = "arm")]
18-
15+
#[cfg(any(target_arch = "x86",
16+
target_arch = "x86_64",
17+
target_arch = "arm"))]
1918
pub fn main() {
2019
let x: int;
2120
let y: int;
@@ -27,5 +26,5 @@ pub fn main() {
2726
foo(y);
2827
}
2928

30-
#[cfg(not(target_arch = "x86"), not(target_arch = "x86_64"), not(target_arch = "arm"))]
29+
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "arm")))]
3130
pub fn main() {}

src/test/compile-fail/asm-misplaced-option.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
#![allow(dead_code)]
1616

17-
#[cfg(target_arch = "x86")]
18-
#[cfg(target_arch = "x86_64")]
17+
#[cfg(any(target_arch = "x86",
18+
target_arch = "x86_64"))]
1919
pub fn main() {
2020
// assignment not dead
2121
let mut x: int = 0;

src/test/compile-fail/asm-out-assign-imm.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
fn foo(x: int) { println!("{}", x); }
1414

15-
#[cfg(target_arch = "x86")]
16-
#[cfg(target_arch = "x86_64")]
17-
#[cfg(target_arch = "arm")]
15+
#[cfg(any(target_arch = "x86",
16+
target_arch = "x86_64",
17+
target_arch = "arm"))]
1818
pub fn main() {
1919
let x: int;
2020
x = 1; //~ NOTE prior assignment occurs here
@@ -25,5 +25,5 @@ pub fn main() {
2525
foo(x);
2626
}
2727

28-
#[cfg(not(target_arch = "x86"), not(target_arch = "x86_64"), not(target_arch = "arm"))]
28+
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "arm")))]
2929
pub fn main() {}

src/test/compile-fail/asm-out-no-modifier.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
fn foo(x: int) { println!("{}", x); }
1414

15-
#[cfg(target_arch = "x86")]
16-
#[cfg(target_arch = "x86_64")]
17-
#[cfg(target_arch = "arm")]
15+
#[cfg(any(target_arch = "x86",
16+
target_arch = "x86_64",
17+
target_arch = "arm"))]
1818
pub fn main() {
1919
let x: int;
2020
unsafe {
@@ -23,5 +23,5 @@ pub fn main() {
2323
foo(x);
2424
}
2525

26-
#[cfg(not(target_arch = "x86"), not(target_arch = "x86_64"), not(target_arch = "arm"))]
26+
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "arm")))]
2727
pub fn main() {}

src/test/compile-fail/asm-out-read-uninit.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
fn foo(x: int) { println!("{}", x); }
1414

15-
#[cfg(target_arch = "x86")]
16-
#[cfg(target_arch = "x86_64")]
17-
#[cfg(target_arch = "arm")]
15+
#[cfg(any(target_arch = "x86",
16+
target_arch = "x86_64",
17+
target_arch = "arm"))]
1818
pub fn main() {
1919
let x: int;
2020
unsafe {
@@ -23,5 +23,5 @@ pub fn main() {
2323
foo(x);
2424
}
2525

26-
#[cfg(not(target_arch = "x86"), not(target_arch = "x86_64"), not(target_arch = "arm"))]
26+
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "arm")))]
2727
pub fn main() {}

src/test/compile-fail/test-cfg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
// compile-flags: --cfg foo
1212

13-
#[cfg(foo, bar)] // foo AND bar
13+
#[cfg(all(foo, bar))] // foo AND bar
1414
fn foo() {}
1515

1616
fn main() {

src/test/pretty/raw-str-nonexpr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#![feature(asm)]
1414

15-
#[cfg = r#"just parse this"#]
15+
#[cfg(foo = r#"just parse this"#)]
1616
extern crate r##"blah"## as blah;
1717

1818
fn main() { unsafe { asm!(r###"blah"###); } }

src/test/run-pass/syntax-extension-cfg.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,27 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags: --cfg foo --cfg bar(baz) --cfg qux="foo"
11+
// compile-flags: --cfg foo --cfg qux="foo"
1212

1313
pub fn main() {
1414
// check
1515
if ! cfg!(foo) { fail!() }
1616
if cfg!(not(foo)) { fail!() }
1717

18-
if ! cfg!(bar(baz)) { fail!() }
19-
if cfg!(not(bar(baz))) { fail!() }
20-
2118
if ! cfg!(qux="foo") { fail!() }
2219
if cfg!(not(qux="foo")) { fail!() }
2320

24-
if ! cfg!(foo, bar(baz), qux="foo") { fail!() }
25-
if cfg!(not(foo, bar(baz), qux="foo")) { fail!() }
21+
if ! cfg!(foo, qux="foo") { fail!() }
22+
if cfg!(not(foo, qux="foo")) { fail!() }
23+
if cfg!(all(not(foo, qux="foo"))) { fail!() }
2624

2725
if cfg!(not_a_cfg) { fail!() }
28-
if cfg!(not_a_cfg, foo, bar(baz), qux="foo") { fail!() }
26+
if cfg!(not_a_cfg, foo, qux="foo") { fail!() }
27+
if cfg!(all(not_a_cfg, foo, qux="foo")) { fail!() }
28+
if ! cfg!(any(not_a_cfg, foo)) { fail!() }
2929

3030
if ! cfg!(not(not_a_cfg)) { fail!() }
31-
if ! cfg!(not(not_a_cfg), foo, bar(baz), qux="foo") { fail!() }
31+
if ! cfg!(not(not_a_cfg), foo, qux="foo") { fail!() }
3232

3333
if cfg!(trailing_comma, ) { fail!() }
3434
}

0 commit comments

Comments
 (0)