about summary refs log tree commit diff
path: root/src/test/ui/test-attrs
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2022-02-18 16:23:29 +0100
committerGitHub <noreply@github.com>2022-02-18 16:23:29 +0100
commit659382fa47e9a7c29451ed407c4062f86dee07b1 (patch)
tree8dbdd1fc0ffb5d05c32df766dcc41b22ea2801fc /src/test/ui/test-attrs
parente3ded4fc4fa15f0b7acee429edf30e1e0cde26f8 (diff)
parent6d05e2a9afb558e4e9668e54db6747e4d8a8383a (diff)
downloadrust-659382fa47e9a7c29451ed407c4062f86dee07b1.tar.gz
rust-659382fa47e9a7c29451ed407c4062f86dee07b1.zip
Rollup merge of #92959 - asquared31415:test-non-fn-help, r=estebank
Add more info and suggestions to use of #[test] on invalid items

This pr changes the diagnostics for using `#[test]` on an item that can't be used as a test to explain that the attribute has no meaningful effect on non-functions and suggests the use of `#[cfg(test)]` for conditional compilation instead.

Example change:
```rs
#[test]
mod test {}
```
previously output
```
error: only functions may be used as tests
 --> src/lib.rs:2:1
  |
2 | mod test {}
  | ^^^^^^^^^^^
  ```
  now outputs
  ```
error: the `#[test]` attribute may only be used on a non-associated function
  --> $DIR/test-on-not-fn.rs:3:1
     |
LL | #[test]
     | ^^^^^^^
LL | mod test {}
     | ----------- expected a non-associated function, found a module
     |
     = note: the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
help: replace with conditional compilation to make the item only exist when tests are being run
     |
LL | #[cfg(test)]
     | ~~~~~~~~~~~~
   ```
Diffstat (limited to 'src/test/ui/test-attrs')
-rw-r--r--src/test/ui/test-attrs/test-on-macro.rs13
-rw-r--r--src/test/ui/test-attrs/test-on-macro.stderr8
-rw-r--r--src/test/ui/test-attrs/test-on-not-fn.rs80
-rw-r--r--src/test/ui/test-attrs/test-on-not-fn.stderr185
4 files changed, 265 insertions, 21 deletions
diff --git a/src/test/ui/test-attrs/test-on-macro.rs b/src/test/ui/test-attrs/test-on-macro.rs
deleted file mode 100644
index 0667364d13c..00000000000
--- a/src/test/ui/test-attrs/test-on-macro.rs
+++ /dev/null
@@ -1,13 +0,0 @@
-// check-pass
-// compile-flags:--test
-
-#![deny(warnings)]
-
-macro_rules! foo {
-    () => (fn foo(){})
-}
-
-#[test]
-foo!(); //~ WARNING `#[test]` attribute should not be used on macros
-
-fn main(){}
diff --git a/src/test/ui/test-attrs/test-on-macro.stderr b/src/test/ui/test-attrs/test-on-macro.stderr
deleted file mode 100644
index 98190b060ce..00000000000
--- a/src/test/ui/test-attrs/test-on-macro.stderr
+++ /dev/null
@@ -1,8 +0,0 @@
-warning: `#[test]` attribute should not be used on macros. Use `#[cfg(test)]` instead.
-  --> $DIR/test-on-macro.rs:11:1
-   |
-LL | foo!();
-   | ^^^^^^^
-
-warning: 1 warning emitted
-
diff --git a/src/test/ui/test-attrs/test-on-not-fn.rs b/src/test/ui/test-attrs/test-on-not-fn.rs
new file mode 100644
index 00000000000..b2f681c01d1
--- /dev/null
+++ b/src/test/ui/test-attrs/test-on-not-fn.rs
@@ -0,0 +1,80 @@
+// compile-flags: --test
+
+#[test] //~ ERROR: the `#[test]` attribute may only be used on a non-associated function
+mod test {}
+
+#[test] //~ ERROR: the `#[test]` attribute may only be used on a non-associated function
+mod loooooooooooooong_teeeeeeeeeest {
+    /*
+    this is a comment
+    this comment goes on for a very long time
+    this is to pad out the span for this module for a long time
+    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
+    labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
+    laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
+    voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
+    non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+    */
+}
+
+#[test] //~ ERROR: the `#[test]` attribute may only be used on a non-associated function
+extern "C" {}
+
+#[test] //~ ERROR: the `#[test]` attribute may only be used on a non-associated function
+trait Foo {}
+
+#[test] //~ ERROR: the `#[test]` attribute may only be used on a non-associated function
+impl Foo for i32 {}
+
+#[test] //~ ERROR: the `#[test]` attribute may only be used on a non-associated function
+const FOO: i32 = -1_i32;
+
+#[test] //~ ERROR: the `#[test]` attribute may only be used on a non-associated function
+static BAR: u64 = 10_000_u64;
+
+#[test] //~ ERROR: the `#[test]` attribute may only be used on a non-associated function
+enum MyUnit {
+    Unit,
+}
+
+#[test] //~ ERROR: the `#[test]` attribute may only be used on a non-associated function
+struct NewI32(i32);
+
+#[test] //~ ERROR: the `#[test]` attribute may only be used on a non-associated function
+union Spooky {
+    x: i32,
+    y: u32,
+}
+
+#[repr(C, align(64))]
+#[test] //~ ERROR: the `#[test]` attribute may only be used on a non-associated function
+#[derive(Copy, Clone, Debug)]
+struct MoreAttrs {
+    a: i32,
+    b: u64,
+}
+
+macro_rules! foo {
+    () => {};
+}
+
+#[test] //~ ERROR: the `#[test]` attribute may only be used on a non-associated function
+foo!();
+
+// make sure it doesn't erroneously trigger on a real test
+#[test]
+fn real_test() {
+    assert_eq!(42_i32, 42_i32);
+}
+
+// make sure it works with cfg test
+#[cfg(test)]
+mod real_tests {
+    #[cfg(test)]
+    fn foo() {}
+
+    #[test]
+    fn bar() {
+        foo();
+    }
+}
diff --git a/src/test/ui/test-attrs/test-on-not-fn.stderr b/src/test/ui/test-attrs/test-on-not-fn.stderr
new file mode 100644
index 00000000000..dd693cf316d
--- /dev/null
+++ b/src/test/ui/test-attrs/test-on-not-fn.stderr
@@ -0,0 +1,185 @@
+error: the `#[test]` attribute may only be used on a non-associated function
+  --> $DIR/test-on-not-fn.rs:3:1
+   |
+LL | #[test]
+   | ^^^^^^^
+LL | mod test {}
+   | ----------- expected a non-associated function, found a module
+   |
+   = note: the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
+help: replace with conditional compilation to make the item only exist when tests are being run
+   |
+LL | #[cfg(test)]
+   | ~~~~~~~~~~~~
+
+error: the `#[test]` attribute may only be used on a non-associated function
+  --> $DIR/test-on-not-fn.rs:6:1
+   |
+LL |   #[test]
+   |   ^^^^^^^
+LL | / mod loooooooooooooong_teeeeeeeeeest {
+LL | |     /*
+LL | |     this is a comment
+LL | |     this comment goes on for a very long time
+...  |
+LL | |     */
+LL | | }
+   | |_- expected a non-associated function, found a module
+   |
+   = note: the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
+help: replace with conditional compilation to make the item only exist when tests are being run
+   |
+LL | #[cfg(test)]
+   | ~~~~~~~~~~~~
+
+error: the `#[test]` attribute may only be used on a non-associated function
+  --> $DIR/test-on-not-fn.rs:20:1
+   |
+LL | #[test]
+   | ^^^^^^^
+LL | extern "C" {}
+   | ------------- expected a non-associated function, found an extern block
+   |
+   = note: the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
+help: replace with conditional compilation to make the item only exist when tests are being run
+   |
+LL | #[cfg(test)]
+   | ~~~~~~~~~~~~
+
+error: the `#[test]` attribute may only be used on a non-associated function
+  --> $DIR/test-on-not-fn.rs:23:1
+   |
+LL | #[test]
+   | ^^^^^^^
+LL | trait Foo {}
+   | ------------ expected a non-associated function, found a trait
+   |
+   = note: the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
+help: replace with conditional compilation to make the item only exist when tests are being run
+   |
+LL | #[cfg(test)]
+   | ~~~~~~~~~~~~
+
+error: the `#[test]` attribute may only be used on a non-associated function
+  --> $DIR/test-on-not-fn.rs:26:1
+   |
+LL | #[test]
+   | ^^^^^^^
+LL | impl Foo for i32 {}
+   | ------------------- expected a non-associated function, found an implementation
+   |
+   = note: the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
+help: replace with conditional compilation to make the item only exist when tests are being run
+   |
+LL | #[cfg(test)]
+   | ~~~~~~~~~~~~
+
+error: the `#[test]` attribute may only be used on a non-associated function
+  --> $DIR/test-on-not-fn.rs:29:1
+   |
+LL | #[test]
+   | ^^^^^^^
+LL | const FOO: i32 = -1_i32;
+   | ------------------------ expected a non-associated function, found a constant item
+   |
+   = note: the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
+help: replace with conditional compilation to make the item only exist when tests are being run
+   |
+LL | #[cfg(test)]
+   | ~~~~~~~~~~~~
+
+error: the `#[test]` attribute may only be used on a non-associated function
+  --> $DIR/test-on-not-fn.rs:32:1
+   |
+LL | #[test]
+   | ^^^^^^^
+LL | static BAR: u64 = 10_000_u64;
+   | ----------------------------- expected a non-associated function, found a static item
+   |
+   = note: the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
+help: replace with conditional compilation to make the item only exist when tests are being run
+   |
+LL | #[cfg(test)]
+   | ~~~~~~~~~~~~
+
+error: the `#[test]` attribute may only be used on a non-associated function
+  --> $DIR/test-on-not-fn.rs:35:1
+   |
+LL |   #[test]
+   |   ^^^^^^^
+LL | / enum MyUnit {
+LL | |     Unit,
+LL | | }
+   | |_- expected a non-associated function, found an enum
+   |
+   = note: the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
+help: replace with conditional compilation to make the item only exist when tests are being run
+   |
+LL | #[cfg(test)]
+   | ~~~~~~~~~~~~
+
+error: the `#[test]` attribute may only be used on a non-associated function
+  --> $DIR/test-on-not-fn.rs:40:1
+   |
+LL | #[test]
+   | ^^^^^^^
+LL | struct NewI32(i32);
+   | ------------------- expected a non-associated function, found a struct
+   |
+   = note: the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
+help: replace with conditional compilation to make the item only exist when tests are being run
+   |
+LL | #[cfg(test)]
+   | ~~~~~~~~~~~~
+
+error: the `#[test]` attribute may only be used on a non-associated function
+  --> $DIR/test-on-not-fn.rs:43:1
+   |
+LL |   #[test]
+   |   ^^^^^^^
+LL | / union Spooky {
+LL | |     x: i32,
+LL | |     y: u32,
+LL | | }
+   | |_- expected a non-associated function, found a union
+   |
+   = note: the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
+help: replace with conditional compilation to make the item only exist when tests are being run
+   |
+LL | #[cfg(test)]
+   | ~~~~~~~~~~~~
+
+error: the `#[test]` attribute may only be used on a non-associated function
+  --> $DIR/test-on-not-fn.rs:50:1
+   |
+LL |   #[test]
+   |   ^^^^^^^
+LL |   #[derive(Copy, Clone, Debug)]
+LL | / struct MoreAttrs {
+LL | |     a: i32,
+LL | |     b: u64,
+LL | | }
+   | |_- expected a non-associated function, found a struct
+   |
+   = note: the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
+help: replace with conditional compilation to make the item only exist when tests are being run
+   |
+LL | #[cfg(test)]
+   | ~~~~~~~~~~~~
+
+error: the `#[test]` attribute may only be used on a non-associated function
+  --> $DIR/test-on-not-fn.rs:61:1
+   |
+LL | #[test]
+   | ^^^^^^^
+LL | foo!();
+   | ------- expected a non-associated function, found an item macro invocation
+   |
+   = note: the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
+help: replace with conditional compilation to make the item only exist when tests are being run
+   |
+LL | #[cfg(test)]
+   | ~~~~~~~~~~~~
+
+error: aborting due to 12 previous errors
+