Skip to content

Commit

Permalink
Support #[cfg] on methods
Browse files Browse the repository at this point in the history
  • Loading branch information
brson committed Nov 1, 2012
1 parent 768247f commit 3edccc3
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/libsyntax/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export noop_fold_pat;
export noop_fold_mod;
export noop_fold_ty;
export noop_fold_block;
export noop_fold_item_underscore;
export wrap;
export fold_ty_param;
export fold_ty_params;
Expand Down
28 changes: 28 additions & 0 deletions src/rustc/front/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ fn strip_items(crate: @ast::crate, in_cfg: in_cfg_pred)
@{fold_mod: |a,b| fold_mod(ctxt, a, b),
fold_block: fold::wrap(|a,b| fold_block(ctxt, a, b) ),
fold_foreign_mod: |a,b| fold_foreign_mod(ctxt, a, b),
fold_item_underscore: |a,b| fold_item_underscore(ctxt, a, b),
.. *fold::default_ast_fold()};

let fold = fold::make_fold(precursor);
Expand Down Expand Up @@ -79,6 +80,22 @@ fn fold_foreign_mod(cx: ctxt, nm: ast::foreign_mod,
};
}

fn fold_item_underscore(cx: ctxt, item: ast::item_, fld: fold::ast_fold) -> ast::item_ {
let item = match item {
ast::item_impl(a, b, c, Some(methods)) => {
let methods = methods.filter(|m| method_in_cfg(cx, *m) );
ast::item_impl(a, b, c, Some(methods))
}
ast::item_trait(a, b, ref methods) => {
let methods = methods.filter(|m| trait_method_in_cfg(cx, m) );
ast::item_trait(a, b, methods)
}
_ => item
};

fold::noop_fold_item_underscore(item, fld)
}

fn filter_stmt(cx: ctxt, &&stmt: @ast::stmt) ->
Option<@ast::stmt> {
match stmt.node {
Expand Down Expand Up @@ -118,6 +135,17 @@ fn view_item_in_cfg(cx: ctxt, item: @ast::view_item) -> bool {
return cx.in_cfg(item.attrs);
}

fn method_in_cfg(cx: ctxt, meth: @ast::method) -> bool {
return cx.in_cfg(meth.attrs);
}

fn trait_method_in_cfg(cx: ctxt, meth: &ast::trait_method) -> bool {
match *meth {
ast::required(ref meth) => cx.in_cfg(meth.attrs),
ast::provided(@ref meth) => cx.in_cfg(meth.attrs)
}
}

// Determine if an item should be translated in the current crate
// configuration based on the item's attributes
fn in_cfg(cfg: ast::crate_cfg, attrs: ~[ast::attribute]) -> bool {
Expand Down
36 changes: 35 additions & 1 deletion src/test/run-pass/conditional-compile.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Crate use statements
#[cfg(bogus)]
use flippity;

#[cfg(bogus)]
const b: bool = false;

Expand Down Expand Up @@ -115,4 +119,34 @@ mod test_use_statements {
#[cfg(bogus)]
use flippity_foo;
}
}
}

mod test_methods {
struct Foo {
bar: uint
}

impl Foo: Fooable {
#[cfg(bogus)]
static fn what() { }

static fn what() { }

#[cfg(bogus)]
fn the() { }

fn the() { }
}

trait Fooable {
#[cfg(bogus)]
static fn what();

static fn what();

#[cfg(bogus)]
fn the();

fn the();
}
}

0 comments on commit 3edccc3

Please sign in to comment.