]> git.lizzy.rs Git - rust.git/commitdiff
rustc: Configure out #[test] functions when not testing
authorBrian Anderson <banderson@mozilla.com>
Fri, 6 Jan 2012 01:30:00 +0000 (17:30 -0800)
committerBrian Anderson <banderson@mozilla.com>
Fri, 6 Jan 2012 01:31:57 +0000 (17:31 -0800)
src/comp/driver/driver.rs
src/comp/front/config.rs
src/comp/front/test.rs
src/test/compile-fail/elided-test.rs [new file with mode: 0644]

index 74aafca0a150c1820f8413801b1ee291f8473281..efad512221efa85184a927c2788789f3e317eff6 100644 (file)
@@ -146,11 +146,9 @@ fn compile_input(sess: session::session, cfg: ast::crate_cfg, input: str,
     crate =
         time(time_passes, "configuration",
              bind front::config::strip_unconfigured_items(crate));
-    if sess.get_opts().test {
-        crate =
-            time(time_passes, "building test harness",
-                 bind front::test::modify_for_testing(sess, crate));
-    }
+    crate =
+        time(time_passes, "maybe building test harness",
+             bind front::test::modify_for_testing(sess, crate));
     crate =
         time(time_passes, "expansion",
              bind syntax::ext::expand::expand_crate(sess, crate));
index 13f6f07849e2af18142525961091da3a10eb4257..c7de20bb1b3fcd83661c567bb99dec85ffd7db95 100644 (file)
@@ -4,16 +4,31 @@
 
 export strip_unconfigured_items;
 export metas_in_cfg;
+export strip_items;
+
+type in_cfg_pred = fn@([ast::attribute]) -> bool;
+
+type ctxt = @{
+    in_cfg: in_cfg_pred
+};
 
 // Support conditional compilation by transforming the AST, stripping out
 // any items that do not belong in the current configuration
 fn strip_unconfigured_items(crate: @ast::crate) -> @ast::crate {
-    let cfg = crate.node.config;
+    strip_items(crate) {|attrs|
+        in_cfg(crate.node.config, attrs)
+    }
+}
+
+fn strip_items(crate: @ast::crate, in_cfg: in_cfg_pred)
+    -> @ast::crate {
+
+    let ctxt = @{in_cfg: in_cfg};
 
     let precursor =
-        {fold_mod: bind fold_mod(cfg, _, _),
-         fold_block: bind fold_block(cfg, _, _),
-         fold_native_mod: bind fold_native_mod(cfg, _, _)
+        {fold_mod: bind fold_mod(ctxt, _, _),
+         fold_block: bind fold_block(ctxt, _, _),
+         fold_native_mod: bind fold_native_mod(ctxt, _, _)
             with *fold::default_ast_fold()};
 
     let fold = fold::make_fold(precursor);
@@ -21,41 +36,41 @@ fn strip_unconfigured_items(crate: @ast::crate) -> @ast::crate {
     ret res;
 }
 
-fn filter_item(cfg: ast::crate_cfg, &&item: @ast::item) ->
+fn filter_item(cx: ctxt, &&item: @ast::item) ->
    option::t<@ast::item> {
-    if item_in_cfg(cfg, item) { option::some(item) } else { option::none }
+    if item_in_cfg(cx, item) { option::some(item) } else { option::none }
 }
 
-fn fold_mod(cfg: ast::crate_cfg, m: ast::_mod, fld: fold::ast_fold) ->
+fn fold_mod(cx: ctxt, m: ast::_mod, fld: fold::ast_fold) ->
    ast::_mod {
-    let filter = bind filter_item(cfg, _);
+    let filter = bind filter_item(cx, _);
     let filtered_items = vec::filter_map(m.items, filter);
     ret {view_items: vec::map(m.view_items, fld.fold_view_item),
          items: vec::map(filtered_items, fld.fold_item)};
 }
 
-fn filter_native_item(cfg: ast::crate_cfg, &&item: @ast::native_item) ->
+fn filter_native_item(cx: ctxt, &&item: @ast::native_item) ->
    option::t<@ast::native_item> {
-    if native_item_in_cfg(cfg, item) {
+    if native_item_in_cfg(cx, item) {
         option::some(item)
     } else { option::none }
 }
 
-fn fold_native_mod(cfg: ast::crate_cfg, nm: ast::native_mod,
+fn fold_native_mod(cx: ctxt, nm: ast::native_mod,
                    fld: fold::ast_fold) -> ast::native_mod {
-    let filter = bind filter_native_item(cfg, _);
+    let filter = bind filter_native_item(cx, _);
     let filtered_items = vec::filter_map(nm.items, filter);
     ret {view_items: vec::map(nm.view_items, fld.fold_view_item),
          items: filtered_items};
 }
 
-fn filter_stmt(cfg: ast::crate_cfg, &&stmt: @ast::stmt) ->
+fn filter_stmt(cx: ctxt, &&stmt: @ast::stmt) ->
    option::t<@ast::stmt> {
     alt stmt.node {
       ast::stmt_decl(decl, _) {
         alt decl.node {
           ast::decl_item(item) {
-            if item_in_cfg(cfg, item) {
+            if item_in_cfg(cx, item) {
                 option::some(stmt)
             } else { option::none }
           }
@@ -66,9 +81,9 @@ fn filter_stmt(cfg: ast::crate_cfg, &&stmt: @ast::stmt) ->
     }
 }
 
-fn fold_block(cfg: ast::crate_cfg, b: ast::blk_, fld: fold::ast_fold) ->
+fn fold_block(cx: ctxt, b: ast::blk_, fld: fold::ast_fold) ->
    ast::blk_ {
-    let filter = bind filter_stmt(cfg, _);
+    let filter = bind filter_stmt(cx, _);
     let filtered_stmts = vec::filter_map(b.stmts, filter);
     ret {view_items: b.view_items,
          stmts: vec::map(filtered_stmts, fld.fold_stmt),
@@ -77,12 +92,12 @@ fn fold_block(cfg: ast::crate_cfg, b: ast::blk_, fld: fold::ast_fold) ->
          rules: b.rules};
 }
 
-fn item_in_cfg(cfg: ast::crate_cfg, item: @ast::item) -> bool {
-    ret in_cfg(cfg, item.attrs);
+fn item_in_cfg(cx: ctxt, item: @ast::item) -> bool {
+    ret cx.in_cfg(item.attrs);
 }
 
-fn native_item_in_cfg(cfg: ast::crate_cfg, item: @ast::native_item) -> bool {
-    ret in_cfg(cfg, item.attrs);
+fn native_item_in_cfg(cx: ctxt, item: @ast::native_item) -> bool {
+    ret cx.in_cfg(item.attrs);
 }
 
 // Determine if an item should be translated in the current crate
index 570c18b857c9607b134c7ab2128775e6e7d43503..46293c80310c4827c76e2c0372a1bbd7ae1ccca0 100644 (file)
 fn modify_for_testing(sess: session::session,
                       crate: @ast::crate) -> @ast::crate {
 
+    if sess.get_opts().test {
+        generate_test_harness(sess, crate)
+    } else {
+        strip_test_functions(crate)
+    }
+}
+
+fn generate_test_harness(sess: session::session,
+                         crate: @ast::crate) -> @ast::crate {
     let cx: test_ctxt =
         @{sess: sess,
           crate: crate,
@@ -43,6 +52,14 @@ fn modify_for_testing(sess: session::session,
     ret res;
 }
 
+fn strip_test_functions(crate: @ast::crate) -> @ast::crate {
+    // When not compiling with --test we should not compile the
+    // #[test] functions
+    config::strip_items(crate) {|attrs|
+        !attr::contains_name(attr::attr_metas(attrs), "test")
+    }
+}
+
 fn fold_mod(_cx: test_ctxt, m: ast::_mod, fld: fold::ast_fold) -> ast::_mod {
 
     // Remove any defined main function from the AST so it doesn't clash with
diff --git a/src/test/compile-fail/elided-test.rs b/src/test/compile-fail/elided-test.rs
new file mode 100644 (file)
index 0000000..551eaf0
--- /dev/null
@@ -0,0 +1,7 @@
+// error-pattern: main function not found
+
+// Since we're not compiling a test runner this function should be elided
+// and the build will fail because main doesn't exist
+#[test]
+fn main() {
+}
\ No newline at end of file