Skip to content

Commit 4e5b480

Browse files
committed
auto merge of #7297 : huonw/rust/strip-expand-strip, r=cmr
This allows macros to both be conditionally defined, and expand to items with #[cfg]'s. This seems to have a performance improvement, e.g. for `std`: ``` # Before time: 1.660 s expansion time: 0.125 s configuration # After time: 0.080 s configuration 1 time: 1.127 s expansion time: 0.132 s configuration 2 ``` And for `extra`: ``` # Before time: 0.593 s expansion time: 0.062 s configuration # After time: 0.047 s configuration 1 time: 0.147 s expansion time: 0.058 s configuration 2 ``` (This seems a little peculiar, but it is possibly because the expansion AST traversal is very slow, so removing as much as possible as early as possible has big benefits.)
2 parents a30ab76 + 73e3dbf commit 4e5b480

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

src/librustc/driver/driver.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,22 @@ pub fn compile_rest(sess: Session,
188188
*sess.building_library = session::building_library(
189189
sess.opts.crate_type, crate_opt.unwrap(), sess.opts.test);
190190

191+
// strip before expansion to allow macros to depend on
192+
// configuration variables e.g/ in
193+
//
194+
// #[macro_escape] #[cfg(foo)]
195+
// mod bar { macro_rules! baz!(() => {{}}) }
196+
//
197+
// baz! should not use this definition unless foo is enabled.
198+
crate_opt = Some(time(time_passes, ~"configuration 1", ||
199+
front::config::strip_unconfigured_items(crate_opt.unwrap())));
200+
191201
crate_opt = Some(time(time_passes, ~"expansion", ||
192202
syntax::ext::expand::expand_crate(sess.parse_sess, copy cfg,
193203
crate_opt.unwrap())));
194204

195-
crate_opt = Some(time(time_passes, ~"configuration", ||
205+
// strip again, in case expansion added anything with a #[cfg].
206+
crate_opt = Some(time(time_passes, ~"configuration 2", ||
196207
front::config::strip_unconfigured_items(crate_opt.unwrap())));
197208

198209
crate_opt = Some(time(time_passes, ~"maybe building test harness", ||

src/test/run-pass/cfg-macros-foo.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2013 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+
// xfail-fast compile-flags directive doesn't work for check-fast
12+
// compile-flags: --cfg foo
13+
14+
// check that cfg correctly chooses between the macro impls (see also
15+
// cfg-macros-notfoo.rs)
16+
17+
#[cfg(foo)]
18+
#[macro_escape]
19+
mod foo {
20+
macro_rules! bar {
21+
() => { true }
22+
}
23+
}
24+
25+
#[cfg(not(foo))]
26+
#[macro_escape]
27+
mod foo {
28+
macro_rules! bar {
29+
() => { false }
30+
}
31+
}
32+
33+
fn main() {
34+
assert!(bar!())
35+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2013 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+
// xfail-fast compile-flags directive doesn't work for check-fast
12+
// compile-flags:
13+
14+
// check that cfg correctly chooses between the macro impls (see also
15+
// cfg-macros-foo.rs)
16+
17+
#[cfg(foo)]
18+
#[macro_escape]
19+
mod foo {
20+
macro_rules! bar {
21+
() => { true }
22+
}
23+
}
24+
25+
#[cfg(not(foo))]
26+
#[macro_escape]
27+
mod foo {
28+
macro_rules! bar {
29+
() => { false }
30+
}
31+
}
32+
33+
fn main() {
34+
assert!(!bar!())
35+
}

0 commit comments

Comments
 (0)