Skip to content

Commit 5354317

Browse files
author
Keegan McAllister
committed
Process cfg_attr right before stripping cfg
Fixes #22070. Fixes #19372.
1 parent 0110f5e commit 5354317

File tree

8 files changed

+113
-38
lines changed

8 files changed

+113
-38
lines changed

src/libsyntax/config.rs

+48-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use attr::AttrMetaMethods;
1212
use diagnostic::SpanHandler;
1313
use fold::Folder;
1414
use {ast, fold, attr};
15-
use codemap::Spanned;
15+
use codemap::{Spanned, respan};
1616
use ptr::P;
1717

1818
use util::small_vector::SmallVector;
@@ -26,6 +26,7 @@ struct Context<F> where F: FnMut(&[ast::Attribute]) -> bool {
2626
// Support conditional compilation by transforming the AST, stripping out
2727
// any items that do not belong in the current configuration
2828
pub fn strip_unconfigured_items(diagnostic: &SpanHandler, krate: ast::Crate) -> ast::Crate {
29+
let krate = process_cfg_attr(diagnostic, krate);
2930
let config = krate.config.clone();
3031
strip_items(krate, |attrs| in_cfg(diagnostic, &config, attrs))
3132
}
@@ -281,3 +282,49 @@ fn in_cfg(diagnostic: &SpanHandler, cfg: &[P<ast::MetaItem>], attrs: &[ast::Attr
281282
attr::cfg_matches(diagnostic, cfg, &*mis[0])
282283
})
283284
}
285+
286+
struct CfgAttrFolder<'a> {
287+
diag: &'a SpanHandler,
288+
config: ast::CrateConfig,
289+
}
290+
291+
// Process `#[cfg_attr]`.
292+
fn process_cfg_attr(diagnostic: &SpanHandler, krate: ast::Crate) -> ast::Crate {
293+
let mut fld = CfgAttrFolder {
294+
diag: diagnostic,
295+
config: krate.config.clone(),
296+
};
297+
fld.fold_crate(krate)
298+
}
299+
300+
impl<'a> fold::Folder for CfgAttrFolder<'a> {
301+
fn fold_attribute(&mut self, attr: ast::Attribute) -> Option<ast::Attribute> {
302+
if !attr.check_name("cfg_attr") {
303+
return fold::noop_fold_attribute(attr, self);
304+
}
305+
306+
let (cfg, mi) = match attr.meta_item_list() {
307+
Some([ref cfg, ref mi]) => (cfg, mi),
308+
_ => {
309+
self.diag.span_err(attr.span, "expected `#[cfg_attr(<cfg pattern>, <attr>)]`");
310+
return None;
311+
}
312+
};
313+
314+
if attr::cfg_matches(self.diag, &self.config[], &cfg) {
315+
Some(respan(mi.span, ast::Attribute_ {
316+
id: attr::mk_attr_id(),
317+
style: attr.node.style,
318+
value: mi.clone(),
319+
is_sugared_doc: false,
320+
}))
321+
} else {
322+
None
323+
}
324+
}
325+
326+
// Need the ability to run pre-expansion.
327+
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
328+
fold::noop_fold_mac(mac, self)
329+
}
330+
}

src/libsyntax/ext/base.rs

-2
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,6 @@ fn initial_syntax_expander_table(ecfg: &expand::ExpansionConfig) -> SyntaxEnv {
528528
syntax_expanders.insert(intern("cfg"),
529529
builtin_normal_expander(
530530
ext::cfg::expand_cfg));
531-
syntax_expanders.insert(intern("cfg_attr"),
532-
Modifier(box ext::cfg_attr::expand));
533531
syntax_expanders.insert(intern("trace_macros"),
534532
builtin_normal_expander(
535533
ext::trace_macros::expand_trace_macros));

src/libsyntax/ext/cfg_attr.rs

-34
This file was deleted.

src/libsyntax/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ pub mod ext {
9696
pub mod base;
9797
pub mod build;
9898
pub mod cfg;
99-
pub mod cfg_attr;
10099
pub mod concat;
101100
pub mod concat_idents;
102101
pub mod deriving;
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2015 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+
// error-pattern: main function not found
12+
// compile-flags: --cfg foo
13+
14+
// main is conditionally compiled, but the conditional compilation
15+
// is conditional too!
16+
17+
#[cfg_attr(foo, cfg(bar))]
18+
fn main() { }
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2015 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 broken
12+
13+
// https://github.com/rust-lang/rust/issues/21833#issuecomment-72353044
14+
15+
#![cfg_attr(broken, no_std)] //~ ERROR no_std is experimental
16+
17+
fn main() { }

src/test/run-pass/cfg-attr-cfg.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2015 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+
// main is conditionally compiled, but the conditional compilation
12+
// is conditional too!
13+
14+
#[cfg_attr(foo, cfg(bar))]
15+
fn main() { }

src/test/run-pass/cfg-attr-crate.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2015 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+
// https://github.com/rust-lang/rust/issues/21833#issuecomment-72353044
12+
13+
#![cfg_attr(not_used, no_std)]
14+
15+
fn main() { }

0 commit comments

Comments
 (0)