forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#92959 - asquared31415:test-non-fn-help, r=e…
…stebank 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)] | ~~~~~~~~~~~~ ```
- Loading branch information
Showing
7 changed files
with
281 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|