Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustc: test: don't silently ignore bad benches #13000

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/librustc/front/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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)
};
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}

Expand Down
19 changes: 19 additions & 0 deletions src/test/compile-fail/issue-12997-1.rs
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions src/test/compile-fail/issue-12997-2.rs
Original file line number Diff line number Diff line change
@@ -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) { }