about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorNiko Matsakis <niko@alum.mit.edu>2011-10-11 17:50:54 -0700
committerBrian Anderson <banderson@mozilla.com>2011-10-12 16:33:07 -0700
commit9f9deff7af4b3f430b0577d48cace3d2b18efc57 (patch)
tree3214ad284f13e5e2656f94e07c9a372e40a1136c /src
parentb1a9d71218bf1f94960496ccb38e92658e89b246 (diff)
downloadrust-9f9deff7af4b3f430b0577d48cace3d2b18efc57.tar.gz
rust-9f9deff7af4b3f430b0577d48cace3d2b18efc57.zip
make a good error msg if you try to use an unsafe fn for a test
Diffstat (limited to 'src')
-rw-r--r--src/comp/driver/rustc.rs2
-rw-r--r--src/comp/front/test.rs27
2 files changed, 21 insertions, 8 deletions
diff --git a/src/comp/driver/rustc.rs b/src/comp/driver/rustc.rs
index 84409978bda..46f8da829a6 100644
--- a/src/comp/driver/rustc.rs
+++ b/src/comp/driver/rustc.rs
@@ -110,7 +110,7 @@ fn compile_input(sess: session::session, cfg: ast::crate_cfg, input: str,
     if sess.get_opts().test {
         crate =
             time(time_passes, "building test harness",
-                 bind front::test::modify_for_testing(crate));
+                 bind front::test::modify_for_testing(sess, crate));
     }
     crate =
         time(time_passes, "expansion",
diff --git a/src/comp/front/test.rs b/src/comp/front/test.rs
index 3e9e7916b33..ce1c0f02088 100644
--- a/src/comp/front/test.rs
+++ b/src/comp/front/test.rs
@@ -7,6 +7,7 @@ import syntax::ast_util::*;
 import syntax::fold;
 import syntax::print::pprust;
 import syntax::codemap::span;
+import driver::session;
 import front::attr;
 
 export modify_for_testing;
@@ -16,13 +17,15 @@ type node_id_gen = fn() -> ast::node_id;
 type test = {span: span, path: [ast::ident], ignore: bool};
 
 type test_ctxt =
-    @{next_node_id: node_id_gen,
+    @{sess: session::session,
+      next_node_id: node_id_gen,
       mutable path: [ast::ident],
       mutable testfns: [test]};
 
 // Traverse the crate, collecting all the test functions, eliding any
 // existing main functions, and synthesizing a main test harness
-fn modify_for_testing(crate: @ast::crate) -> @ast::crate {
+fn modify_for_testing(sess: session::session,
+                      crate: @ast::crate) -> @ast::crate {
 
     // FIXME: This hackasaurus assumes that 200000 is a safe number to start
     // generating node_ids at (which is totally not the case). pauls is going
@@ -37,7 +40,8 @@ fn modify_for_testing(crate: @ast::crate) -> @ast::crate {
               }(next_node_id);
 
     let cx: test_ctxt =
-        @{next_node_id: next_node_id_fn,
+        @{sess: sess,
+          next_node_id: next_node_id_fn,
           mutable path: [],
           mutable testfns: []};
 
@@ -90,10 +94,19 @@ fn fold_item(cx: test_ctxt, &&i: @ast::item, fld: fold::ast_fold) ->
     log #fmt["current path: %s", ast_util::path_name_i(cx.path)];
 
     if is_test_fn(i) {
-        log "this is a test function";
-        let test = {span: i.span, path: cx.path, ignore: is_ignored(i)};
-        cx.testfns += [test];
-        log #fmt["have %u test functions", vec::len(cx.testfns)];
+        alt i.node {
+          ast::item_fn(f, _) when f.decl.purity == ast::unsafe_fn {
+            cx.sess.span_fatal(
+                i.span,
+                "unsafe functions cannot be used for tests");
+          }
+          _ {
+            log "this is a test function";
+            let test = {span: i.span, path: cx.path, ignore: is_ignored(i)};
+            cx.testfns += [test];
+            log #fmt["have %u test functions", vec::len(cx.testfns)];
+          }
+        }
     }
 
     let res = fold::noop_fold_item(i, fld);