diff --git a/src/librustc/front/test.rs b/src/librustc/front/test.rs index cf5373fd17de7..6d8029b1638c4 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 0000000000000..193cbcb25b74d --- /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 or the MIT license +// , 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 0000000000000..f520ce0eabba6 --- /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 or the MIT license +// , 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) { }