about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCorey Richardson <corey@octayn.net>2014-03-18 09:20:30 -0400
committerAlex Crichton <alex@alexcrichton.com>2014-03-18 13:47:50 -0700
commit873f7408bdffdb05b23f77aa343abd05f2e3126c (patch)
tree4fe3d6d69bf02851c1fb9aa188fa2b504150c444
parentf9e0baa19a33c2b98f502d9604dabacd8f634965 (diff)
downloadrust-873f7408bdffdb05b23f77aa343abd05f2e3126c.tar.gz
rust-873f7408bdffdb05b23f77aa343abd05f2e3126c.zip
rustc: test: don't silently ignore bad benches
This is adequate because when a function has a type that isn't caught here,
that is, it has a single argument, but it *isn't* `&mut BenchHarness`, it
errors later on with:

     error: mismatched types: expected `fn(&mut test::BenchHarness)` but found
     `fn(int)` (expected &-ptr but found int)

which I consider acceptable.

Closes #12997
-rw-r--r--src/librustc/front/test.rs15
-rw-r--r--src/test/compile-fail/issue-12997-1.rs19
-rw-r--r--src/test/compile-fail/issue-12997-2.rs17
3 files changed, 46 insertions, 5 deletions
diff --git a/src/librustc/front/test.rs b/src/librustc/front/test.rs
index cf5373fd17d..6d8029b1638 100644
--- a/src/librustc/front/test.rs
+++ b/src/librustc/front/test.rs
@@ -95,10 +95,9 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> {
         debug!("current path: {}",
                ast_util::path_name_i(self.cx.path.get().as_slice()));
 
-        if is_test_fn(&self.cx, i) || is_bench_fn(i) {
+        if is_test_fn(&self.cx, i) || is_bench_fn(&self.cx, i) {
             match i.node {
-                ast::ItemFn(_, purity, _, _, _)
-                    if purity == ast::UnsafeFn => {
+                ast::ItemFn(_, ast::UnsafeFn, _, _, _) => {
                     let sess = self.cx.sess;
                     sess.span_fatal(i.span,
                                     "unsafe functions cannot be used for \
@@ -109,7 +108,7 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> {
                     let test = Test {
                         span: i.span,
                         path: self.cx.path.get(),
-                        bench: is_bench_fn(i),
+                        bench: is_bench_fn(&self.cx, i),
                         ignore: is_ignored(&self.cx, i),
                         should_fail: should_fail(i)
                     };
@@ -233,7 +232,7 @@ fn is_test_fn(cx: &TestCtxt, i: @ast::Item) -> bool {
     return has_test_attr && has_test_signature(i);
 }
 
-fn is_bench_fn(i: @ast::Item) -> bool {
+fn is_bench_fn(cx: &TestCtxt, i: @ast::Item) -> bool {
     let has_bench_attr = attr::contains_name(i.attrs.as_slice(), "bench");
 
     fn has_test_signature(i: @ast::Item) -> bool {
@@ -254,6 +253,12 @@ fn is_bench_fn(i: @ast::Item) -> bool {
         }
     }
 
+    if has_bench_attr && !has_test_signature(i) {
+        let sess = cx.sess;
+        sess.span_err(i.span, "functions used as benches must have signature \
+                      `fn(&mut BenchHarness) -> ()`");
+    }
+
     return has_bench_attr && has_test_signature(i);
 }
 
diff --git a/src/test/compile-fail/issue-12997-1.rs b/src/test/compile-fail/issue-12997-1.rs
new file mode 100644
index 00000000000..193cbcb25b7
--- /dev/null
+++ b/src/test/compile-fail/issue-12997-1.rs
@@ -0,0 +1,19 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// compile-flags: --test
+
+//! Test that makes sure wrongly-typed bench functions aren't ignored
+
+#[bench]
+fn foo() { } //~ ERROR functions used as benches
+
+#[bench]
+fn bar(x: int, y: int) { } //~ ERROR functions used as benches
diff --git a/src/test/compile-fail/issue-12997-2.rs b/src/test/compile-fail/issue-12997-2.rs
new file mode 100644
index 00000000000..f520ce0eabb
--- /dev/null
+++ b/src/test/compile-fail/issue-12997-2.rs
@@ -0,0 +1,17 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// compile-flags: --test
+
+//! Test that makes sure wrongly-typed bench functions are rejected
+
+// error-pattern:expected &-ptr but found int
+#[bench]
+fn bar(x: int) { }