Skip to content

Commit 3edccc3

Browse files
committed
Support #[cfg] on methods
1 parent 768247f commit 3edccc3

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

src/libsyntax/fold.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export noop_fold_pat;
1212
export noop_fold_mod;
1313
export noop_fold_ty;
1414
export noop_fold_block;
15+
export noop_fold_item_underscore;
1516
export wrap;
1617
export fold_ty_param;
1718
export fold_ty_params;

src/rustc/front/config.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ fn strip_items(crate: @ast::crate, in_cfg: in_cfg_pred)
2727
@{fold_mod: |a,b| fold_mod(ctxt, a, b),
2828
fold_block: fold::wrap(|a,b| fold_block(ctxt, a, b) ),
2929
fold_foreign_mod: |a,b| fold_foreign_mod(ctxt, a, b),
30+
fold_item_underscore: |a,b| fold_item_underscore(ctxt, a, b),
3031
.. *fold::default_ast_fold()};
3132

3233
let fold = fold::make_fold(precursor);
@@ -79,6 +80,22 @@ fn fold_foreign_mod(cx: ctxt, nm: ast::foreign_mod,
7980
};
8081
}
8182

83+
fn fold_item_underscore(cx: ctxt, item: ast::item_, fld: fold::ast_fold) -> ast::item_ {
84+
let item = match item {
85+
ast::item_impl(a, b, c, Some(methods)) => {
86+
let methods = methods.filter(|m| method_in_cfg(cx, *m) );
87+
ast::item_impl(a, b, c, Some(methods))
88+
}
89+
ast::item_trait(a, b, ref methods) => {
90+
let methods = methods.filter(|m| trait_method_in_cfg(cx, m) );
91+
ast::item_trait(a, b, methods)
92+
}
93+
_ => item
94+
};
95+
96+
fold::noop_fold_item_underscore(item, fld)
97+
}
98+
8299
fn filter_stmt(cx: ctxt, &&stmt: @ast::stmt) ->
83100
Option<@ast::stmt> {
84101
match stmt.node {
@@ -118,6 +135,17 @@ fn view_item_in_cfg(cx: ctxt, item: @ast::view_item) -> bool {
118135
return cx.in_cfg(item.attrs);
119136
}
120137

138+
fn method_in_cfg(cx: ctxt, meth: @ast::method) -> bool {
139+
return cx.in_cfg(meth.attrs);
140+
}
141+
142+
fn trait_method_in_cfg(cx: ctxt, meth: &ast::trait_method) -> bool {
143+
match *meth {
144+
ast::required(ref meth) => cx.in_cfg(meth.attrs),
145+
ast::provided(@ref meth) => cx.in_cfg(meth.attrs)
146+
}
147+
}
148+
121149
// Determine if an item should be translated in the current crate
122150
// configuration based on the item's attributes
123151
fn in_cfg(cfg: ast::crate_cfg, attrs: ~[ast::attribute]) -> bool {

src/test/run-pass/conditional-compile.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Crate use statements
2+
#[cfg(bogus)]
3+
use flippity;
4+
15
#[cfg(bogus)]
26
const b: bool = false;
37

@@ -115,4 +119,34 @@ mod test_use_statements {
115119
#[cfg(bogus)]
116120
use flippity_foo;
117121
}
118-
}
122+
}
123+
124+
mod test_methods {
125+
struct Foo {
126+
bar: uint
127+
}
128+
129+
impl Foo: Fooable {
130+
#[cfg(bogus)]
131+
static fn what() { }
132+
133+
static fn what() { }
134+
135+
#[cfg(bogus)]
136+
fn the() { }
137+
138+
fn the() { }
139+
}
140+
141+
trait Fooable {
142+
#[cfg(bogus)]
143+
static fn what();
144+
145+
static fn what();
146+
147+
#[cfg(bogus)]
148+
fn the();
149+
150+
fn the();
151+
}
152+
}

0 commit comments

Comments
 (0)