From 3edccc311ef4f29c6bf8676f7a414f8b0ef9360d Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Thu, 1 Nov 2012 15:48:37 -0700 Subject: [PATCH] Support #[cfg] on methods --- src/libsyntax/fold.rs | 1 + src/rustc/front/config.rs | 28 ++++++++++++++++++ src/test/run-pass/conditional-compile.rs | 36 +++++++++++++++++++++++- 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index 5fe3c8feea6..8efe4c9944c 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -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; diff --git a/src/rustc/front/config.rs b/src/rustc/front/config.rs index 4577b54fb5c..bd4b4712393 100644 --- a/src/rustc/front/config.rs +++ b/src/rustc/front/config.rs @@ -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); @@ -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 { @@ -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 { diff --git a/src/test/run-pass/conditional-compile.rs b/src/test/run-pass/conditional-compile.rs index bc97114b581..cc2f0fec96f 100644 --- a/src/test/run-pass/conditional-compile.rs +++ b/src/test/run-pass/conditional-compile.rs @@ -1,3 +1,7 @@ +// Crate use statements +#[cfg(bogus)] +use flippity; + #[cfg(bogus)] const b: bool = false; @@ -115,4 +119,34 @@ mod test_use_statements { #[cfg(bogus)] use flippity_foo; } -} \ No newline at end of file +} + +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(); + } +}