Skip to content

Commit 73e3dbf

Browse files
committed
driver: perform stripping before and after macro expansion.
This allows macros to both be conditionally defined, and expand to items with #[cfg]'s.
1 parent 4ec05e0 commit 73e3dbf

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

src/librustc/driver/driver.rs

Lines changed: 12 additions & 1 deletion
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

Lines changed: 35 additions & 0 deletions
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+
}
Lines changed: 35 additions & 0 deletions
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)