Skip to content

Commit cc31d9c

Browse files
committed
Give a more descriptive error when marking non-test items as #[test]
Closes #14772.
1 parent a29df44 commit cc31d9c

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

src/libsyntax/test.rs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -274,30 +274,43 @@ fn strip_test_functions(krate: ast::Crate) -> ast::Crate {
274274
fn is_test_fn(cx: &TestCtxt, i: &ast::Item) -> bool {
275275
let has_test_attr = attr::contains_name(i.attrs.as_slice(), "test");
276276

277-
fn has_test_signature(i: &ast::Item) -> bool {
277+
#[deriving(PartialEq)]
278+
enum HasTestSignature {
279+
Yes,
280+
No,
281+
NotEvenAFunction,
282+
}
283+
284+
fn has_test_signature(i: &ast::Item) -> HasTestSignature {
278285
match &i.node {
279286
&ast::ItemFn(ref decl, _, _, ref generics, _) => {
280287
let no_output = match decl.output.node {
281288
ast::TyNil => true,
282-
_ => false
289+
_ => false,
283290
};
284-
decl.inputs.is_empty()
285-
&& no_output
286-
&& !generics.is_parameterized()
291+
if decl.inputs.is_empty()
292+
&& no_output
293+
&& !generics.is_parameterized() {
294+
Yes
295+
} else {
296+
No
297+
}
287298
}
288-
_ => false
299+
_ => NotEvenAFunction,
289300
}
290301
}
291302

292-
if has_test_attr && !has_test_signature(i) {
303+
if has_test_attr {
293304
let diag = cx.span_diagnostic;
294-
diag.span_err(
295-
i.span,
296-
"functions used as tests must have signature fn() -> ()."
297-
);
305+
match has_test_signature(i) {
306+
Yes => {},
307+
No => diag.span_err(i.span, "functions used as tests must have signature fn() -> ()"),
308+
NotEvenAFunction => diag.span_err(i.span,
309+
"only functions may be used as tests"),
310+
}
298311
}
299312

300-
return has_test_attr && has_test_signature(i);
313+
return has_test_attr && has_test_signature(i) == Yes;
301314
}
302315

303316
fn is_bench_fn(cx: &TestCtxt, i: &ast::Item) -> bool {

src/test/compile-fail/issue-14772.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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]
14+
mod foo {} //~ ERROR only functions may be used as tests
15+
16+
fn main() {}

0 commit comments

Comments
 (0)