about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorP1start <rewi-github@whanau.org>2014-10-05 12:27:42 +1300
committerP1start <rewi-github@whanau.org>2014-10-05 14:16:32 +1300
commitcc31d9cabcc6c8ad396c1aae6257a4b5ea25cb46 (patch)
treeaa51200e0faf139ddd8662f79229cbceb172bed1 /src
parenta29df44f51f950549957128ad1b65b2576a48383 (diff)
downloadrust-cc31d9cabcc6c8ad396c1aae6257a4b5ea25cb46.tar.gz
rust-cc31d9cabcc6c8ad396c1aae6257a4b5ea25cb46.zip
Give a more descriptive error when marking non-test items as #[test]
Closes #14772.
Diffstat (limited to 'src')
-rw-r--r--src/libsyntax/test.rs37
-rw-r--r--src/test/compile-fail/issue-14772.rs16
2 files changed, 41 insertions, 12 deletions
diff --git a/src/libsyntax/test.rs b/src/libsyntax/test.rs
index 00fdf436636..d2c0ebd4393 100644
--- a/src/libsyntax/test.rs
+++ b/src/libsyntax/test.rs
@@ -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 {
diff --git a/src/test/compile-fail/issue-14772.rs b/src/test/compile-fail/issue-14772.rs
new file mode 100644
index 00000000000..aaaad67be52
--- /dev/null
+++ b/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() {}