Skip to content

Commit

Permalink
Give a more descriptive error when marking non-test items as #[test]
Browse files Browse the repository at this point in the history
Closes #14772.
  • Loading branch information
ftxqxd committed Oct 5, 2014
1 parent a29df44 commit cc31d9c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
37 changes: 25 additions & 12 deletions src/libsyntax/test.rs
Expand Up @@ -274,30 +274,43 @@ fn strip_test_functions(krate: ast::Crate) -> ast::Crate {
fn is_test_fn(cx: &TestCtxt, i: &ast::Item) -> bool {
let has_test_attr = attr::contains_name(i.attrs.as_slice(), "test");

fn has_test_signature(i: &ast::Item) -> bool {
#[deriving(PartialEq)]
enum HasTestSignature {
Yes,
No,
NotEvenAFunction,
}

fn has_test_signature(i: &ast::Item) -> HasTestSignature {
match &i.node {
&ast::ItemFn(ref decl, _, _, ref generics, _) => {
let no_output = match decl.output.node {
ast::TyNil => true,
_ => false
_ => false,
};
decl.inputs.is_empty()
&& no_output
&& !generics.is_parameterized()
if decl.inputs.is_empty()
&& no_output
&& !generics.is_parameterized() {
Yes
} else {
No
}
}
_ => false
_ => NotEvenAFunction,
}
}

if has_test_attr && !has_test_signature(i) {
if has_test_attr {
let diag = cx.span_diagnostic;
diag.span_err(
i.span,
"functions used as tests must have signature fn() -> ()."
);
match has_test_signature(i) {
Yes => {},
No => diag.span_err(i.span, "functions used as tests must have signature fn() -> ()"),
NotEvenAFunction => diag.span_err(i.span,
"only functions may be used as tests"),
}
}

return has_test_attr && has_test_signature(i);
return has_test_attr && has_test_signature(i) == Yes;
}

fn is_bench_fn(cx: &TestCtxt, i: &ast::Item) -> bool {
Expand Down
16 changes: 16 additions & 0 deletions src/test/compile-fail/issue-14772.rs
@@ -0,0 +1,16 @@
// 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]
mod foo {} //~ ERROR only functions may be used as tests

fn main() {}

5 comments on commit cc31d9c

@bors
Copy link
Contributor

@bors bors commented on cc31d9c Oct 5, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at ftxqxd@cc31d9c

@bors
Copy link
Contributor

@bors bors commented on cc31d9c Oct 5, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging P1start/rust/diagnostics = cc31d9c into auto

@bors
Copy link
Contributor

@bors bors commented on cc31d9c Oct 5, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1start/rust/diagnostics = cc31d9c merged ok, testing candidate = c586490

@bors
Copy link
Contributor

@bors bors commented on cc31d9c Oct 5, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on cc31d9c Oct 5, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = c586490

Please sign in to comment.