Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

driver: perform stripping before and after macro expansion. #7297

Merged
merged 1 commit into from
Jun 26, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/librustc/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,22 @@ pub fn compile_rest(sess: Session,
*sess.building_library = session::building_library(
sess.opts.crate_type, crate_opt.unwrap(), sess.opts.test);

// strip before expansion to allow macros to depend on
// configuration variables e.g/ in
//
// #[macro_escape] #[cfg(foo)]
// mod bar { macro_rules! baz!(() => {{}}) }
//
// baz! should not use this definition unless foo is enabled.
crate_opt = Some(time(time_passes, ~"configuration 1", ||
front::config::strip_unconfigured_items(crate_opt.unwrap())));

crate_opt = Some(time(time_passes, ~"expansion", ||
syntax::ext::expand::expand_crate(sess.parse_sess, copy cfg,
crate_opt.unwrap())));

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

crate_opt = Some(time(time_passes, ~"maybe building test harness", ||
Expand Down
35 changes: 35 additions & 0 deletions src/test/run-pass/cfg-macros-foo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// xfail-fast compile-flags directive doesn't work for check-fast
// compile-flags: --cfg foo

// check that cfg correctly chooses between the macro impls (see also
// cfg-macros-notfoo.rs)

#[cfg(foo)]
#[macro_escape]
mod foo {
macro_rules! bar {
() => { true }
}
}

#[cfg(not(foo))]
#[macro_escape]
mod foo {
macro_rules! bar {
() => { false }
}
}

fn main() {
assert!(bar!())
}
35 changes: 35 additions & 0 deletions src/test/run-pass/cfg-macros-notfoo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// xfail-fast compile-flags directive doesn't work for check-fast
// compile-flags:

// check that cfg correctly chooses between the macro impls (see also
// cfg-macros-foo.rs)

#[cfg(foo)]
#[macro_escape]
mod foo {
macro_rules! bar {
() => { true }
}
}

#[cfg(not(foo))]
#[macro_escape]
mod foo {
macro_rules! bar {
() => { false }
}
}

fn main() {
assert!(!bar!())
}