Skip to content

Commit e8f8ffe

Browse files
authored
Rollup merge of rust-lang#94624 - estebank:regression-94508, r=Dylan-DPC
Downgrade `#[test]` on macro call to warning Follow up to rust-lang#92959. Address rust-lang#94508.
2 parents 2c09766 + 050d589 commit e8f8ffe

File tree

3 files changed

+27
-35
lines changed

3 files changed

+27
-35
lines changed

compiler/rustc_builtin_macros/src/test.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,18 @@ pub fn expand_test_or_bench(
105105

106106
// Note: non-associated fn items are already handled by `expand_test_or_bench`
107107
if !matches!(item.kind, ast::ItemKind::Fn(_)) {
108-
cx.sess
109-
.parse_sess
110-
.span_diagnostic
111-
.struct_span_err(
112-
attr_sp,
113-
"the `#[test]` attribute may only be used on a non-associated function",
114-
)
115-
.note("the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions")
108+
let diag = &cx.sess.parse_sess.span_diagnostic;
109+
let msg = "the `#[test]` attribute may only be used on a non-associated function";
110+
let mut err = match item.kind {
111+
// These were a warning before #92959 and need to continue being that to avoid breaking
112+
// stable user code (#94508).
113+
ast::ItemKind::MacCall(_) => diag.struct_span_warn(attr_sp, msg),
114+
// `.forget_guarantee()` needed to get these two arms to match types. Because of how
115+
// locally close the `.emit()` call is I'm comfortable with it, but if it can be
116+
// reworked in the future to not need it, it'd be nice.
117+
_ => diag.struct_span_err(attr_sp, msg).forget_guarantee(),
118+
};
119+
err.span_label(attr_sp, "the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions")
116120
.span_label(item.span, format!("expected a non-associated function, found {} {}", item.kind.article(), item.kind.descr()))
117121
.span_suggestion(attr_sp, "replace with conditional compilation to make the item only exist when tests are being run", String::from("#[cfg(test)]"), Applicability::MaybeIncorrect)
118122
.emit();

src/test/ui/test-attrs/test-on-not-fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ macro_rules! foo {
5858
() => {};
5959
}
6060

61-
#[test] //~ ERROR: the `#[test]` attribute may only be used on a non-associated function
61+
#[test] //~ WARN: the `#[test]` attribute may only be used on a non-associated function
6262
foo!();
6363

6464
// make sure it doesn't erroneously trigger on a real test

src/test/ui/test-attrs/test-on-not-fn.stderr

+14-26
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ error: the `#[test]` attribute may only be used on a non-associated function
22
--> $DIR/test-on-not-fn.rs:3:1
33
|
44
LL | #[test]
5-
| ^^^^^^^
5+
| ^^^^^^^ the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
66
LL | mod test {}
77
| ----------- expected a non-associated function, found a module
88
|
9-
= note: the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
109
help: replace with conditional compilation to make the item only exist when tests are being run
1110
|
1211
LL | #[cfg(test)]
@@ -16,7 +15,7 @@ error: the `#[test]` attribute may only be used on a non-associated function
1615
--> $DIR/test-on-not-fn.rs:6:1
1716
|
1817
LL | #[test]
19-
| ^^^^^^^
18+
| ^^^^^^^ the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
2019
LL | / mod loooooooooooooong_teeeeeeeeeest {
2120
LL | | /*
2221
LL | | this is a comment
@@ -26,7 +25,6 @@ LL | | */
2625
LL | | }
2726
| |_- expected a non-associated function, found a module
2827
|
29-
= note: the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
3028
help: replace with conditional compilation to make the item only exist when tests are being run
3129
|
3230
LL | #[cfg(test)]
@@ -36,11 +34,10 @@ error: the `#[test]` attribute may only be used on a non-associated function
3634
--> $DIR/test-on-not-fn.rs:20:1
3735
|
3836
LL | #[test]
39-
| ^^^^^^^
37+
| ^^^^^^^ the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
4038
LL | extern "C" {}
4139
| ------------- expected a non-associated function, found an extern block
4240
|
43-
= note: the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
4441
help: replace with conditional compilation to make the item only exist when tests are being run
4542
|
4643
LL | #[cfg(test)]
@@ -50,11 +47,10 @@ error: the `#[test]` attribute may only be used on a non-associated function
5047
--> $DIR/test-on-not-fn.rs:23:1
5148
|
5249
LL | #[test]
53-
| ^^^^^^^
50+
| ^^^^^^^ the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
5451
LL | trait Foo {}
5552
| ------------ expected a non-associated function, found a trait
5653
|
57-
= note: the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
5854
help: replace with conditional compilation to make the item only exist when tests are being run
5955
|
6056
LL | #[cfg(test)]
@@ -64,11 +60,10 @@ error: the `#[test]` attribute may only be used on a non-associated function
6460
--> $DIR/test-on-not-fn.rs:26:1
6561
|
6662
LL | #[test]
67-
| ^^^^^^^
63+
| ^^^^^^^ the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
6864
LL | impl Foo for i32 {}
6965
| ------------------- expected a non-associated function, found an implementation
7066
|
71-
= note: the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
7267
help: replace with conditional compilation to make the item only exist when tests are being run
7368
|
7469
LL | #[cfg(test)]
@@ -78,11 +73,10 @@ error: the `#[test]` attribute may only be used on a non-associated function
7873
--> $DIR/test-on-not-fn.rs:29:1
7974
|
8075
LL | #[test]
81-
| ^^^^^^^
76+
| ^^^^^^^ the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
8277
LL | const FOO: i32 = -1_i32;
8378
| ------------------------ expected a non-associated function, found a constant item
8479
|
85-
= note: the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
8680
help: replace with conditional compilation to make the item only exist when tests are being run
8781
|
8882
LL | #[cfg(test)]
@@ -92,11 +86,10 @@ error: the `#[test]` attribute may only be used on a non-associated function
9286
--> $DIR/test-on-not-fn.rs:32:1
9387
|
9488
LL | #[test]
95-
| ^^^^^^^
89+
| ^^^^^^^ the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
9690
LL | static BAR: u64 = 10_000_u64;
9791
| ----------------------------- expected a non-associated function, found a static item
9892
|
99-
= note: the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
10093
help: replace with conditional compilation to make the item only exist when tests are being run
10194
|
10295
LL | #[cfg(test)]
@@ -106,13 +99,12 @@ error: the `#[test]` attribute may only be used on a non-associated function
10699
--> $DIR/test-on-not-fn.rs:35:1
107100
|
108101
LL | #[test]
109-
| ^^^^^^^
102+
| ^^^^^^^ the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
110103
LL | / enum MyUnit {
111104
LL | | Unit,
112105
LL | | }
113106
| |_- expected a non-associated function, found an enum
114107
|
115-
= note: the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
116108
help: replace with conditional compilation to make the item only exist when tests are being run
117109
|
118110
LL | #[cfg(test)]
@@ -122,11 +114,10 @@ error: the `#[test]` attribute may only be used on a non-associated function
122114
--> $DIR/test-on-not-fn.rs:40:1
123115
|
124116
LL | #[test]
125-
| ^^^^^^^
117+
| ^^^^^^^ the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
126118
LL | struct NewI32(i32);
127119
| ------------------- expected a non-associated function, found a struct
128120
|
129-
= note: the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
130121
help: replace with conditional compilation to make the item only exist when tests are being run
131122
|
132123
LL | #[cfg(test)]
@@ -136,14 +127,13 @@ error: the `#[test]` attribute may only be used on a non-associated function
136127
--> $DIR/test-on-not-fn.rs:43:1
137128
|
138129
LL | #[test]
139-
| ^^^^^^^
130+
| ^^^^^^^ the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
140131
LL | / union Spooky {
141132
LL | | x: i32,
142133
LL | | y: u32,
143134
LL | | }
144135
| |_- expected a non-associated function, found a union
145136
|
146-
= note: the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
147137
help: replace with conditional compilation to make the item only exist when tests are being run
148138
|
149139
LL | #[cfg(test)]
@@ -153,33 +143,31 @@ error: the `#[test]` attribute may only be used on a non-associated function
153143
--> $DIR/test-on-not-fn.rs:50:1
154144
|
155145
LL | #[test]
156-
| ^^^^^^^
146+
| ^^^^^^^ the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
157147
LL | #[derive(Copy, Clone, Debug)]
158148
LL | / struct MoreAttrs {
159149
LL | | a: i32,
160150
LL | | b: u64,
161151
LL | | }
162152
| |_- expected a non-associated function, found a struct
163153
|
164-
= note: the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
165154
help: replace with conditional compilation to make the item only exist when tests are being run
166155
|
167156
LL | #[cfg(test)]
168157
| ~~~~~~~~~~~~
169158

170-
error: the `#[test]` attribute may only be used on a non-associated function
159+
warning: the `#[test]` attribute may only be used on a non-associated function
171160
--> $DIR/test-on-not-fn.rs:61:1
172161
|
173162
LL | #[test]
174-
| ^^^^^^^
163+
| ^^^^^^^ the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
175164
LL | foo!();
176165
| ------- expected a non-associated function, found an item macro invocation
177166
|
178-
= note: the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions
179167
help: replace with conditional compilation to make the item only exist when tests are being run
180168
|
181169
LL | #[cfg(test)]
182170
| ~~~~~~~~~~~~
183171

184-
error: aborting due to 12 previous errors
172+
error: aborting due to 11 previous errors; 1 warning emitted
185173

0 commit comments

Comments
 (0)