Skip to content

Commit 873f740

Browse files
emberianalexcrichton
authored andcommitted
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
1 parent f9e0baa commit 873f740

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

src/librustc/front/test.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,9 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> {
9595
debug!("current path: {}",
9696
ast_util::path_name_i(self.cx.path.get().as_slice()));
9797

98-
if is_test_fn(&self.cx, i) || is_bench_fn(i) {
98+
if is_test_fn(&self.cx, i) || is_bench_fn(&self.cx, i) {
9999
match i.node {
100-
ast::ItemFn(_, purity, _, _, _)
101-
if purity == ast::UnsafeFn => {
100+
ast::ItemFn(_, ast::UnsafeFn, _, _, _) => {
102101
let sess = self.cx.sess;
103102
sess.span_fatal(i.span,
104103
"unsafe functions cannot be used for \
@@ -109,7 +108,7 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> {
109108
let test = Test {
110109
span: i.span,
111110
path: self.cx.path.get(),
112-
bench: is_bench_fn(i),
111+
bench: is_bench_fn(&self.cx, i),
113112
ignore: is_ignored(&self.cx, i),
114113
should_fail: should_fail(i)
115114
};
@@ -233,7 +232,7 @@ fn is_test_fn(cx: &TestCtxt, i: @ast::Item) -> bool {
233232
return has_test_attr && has_test_signature(i);
234233
}
235234

236-
fn is_bench_fn(i: @ast::Item) -> bool {
235+
fn is_bench_fn(cx: &TestCtxt, i: @ast::Item) -> bool {
237236
let has_bench_attr = attr::contains_name(i.attrs.as_slice(), "bench");
238237

239238
fn has_test_signature(i: @ast::Item) -> bool {
@@ -254,6 +253,12 @@ fn is_bench_fn(i: @ast::Item) -> bool {
254253
}
255254
}
256255

256+
if has_bench_attr && !has_test_signature(i) {
257+
let sess = cx.sess;
258+
sess.span_err(i.span, "functions used as benches must have signature \
259+
`fn(&mut BenchHarness) -> ()`");
260+
}
261+
257262
return has_bench_attr && has_test_signature(i);
258263
}
259264

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: --test
12+
13+
//! Test that makes sure wrongly-typed bench functions aren't ignored
14+
15+
#[bench]
16+
fn foo() { } //~ ERROR functions used as benches
17+
18+
#[bench]
19+
fn bar(x: int, y: int) { } //~ ERROR functions used as benches
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: --test
12+
13+
//! Test that makes sure wrongly-typed bench functions are rejected
14+
15+
// error-pattern:expected &-ptr but found int
16+
#[bench]
17+
fn bar(x: int) { }

0 commit comments

Comments
 (0)