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
This commit is contained in:
Corey Richardson 2014-03-18 09:20:30 -04:00 committed by Alex Crichton
parent f9e0baa19a
commit 873f7408bd
3 changed files with 46 additions and 5 deletions

View File

@ -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);
}

View File

@ -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

View File

@ -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) { }