Skip to content

Commit 98703b0

Browse files
committed
fixup name in diagnostics
1 parent cec2d48 commit 98703b0

File tree

8 files changed

+52
-77
lines changed

8 files changed

+52
-77
lines changed

compiler/rustc_builtin_macros/src/test.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,13 @@ pub(crate) fn expand_test_or_bench(
117117
Annotatable::Item(i) => (i, false),
118118
Annotatable::Stmt(box ast::Stmt { kind: ast::StmtKind::Item(i), .. }) => (i, true),
119119
other => {
120-
not_testable_error(cx, attr_sp, None);
120+
not_testable_error(cx, is_bench, attr_sp, None);
121121
return vec![other];
122122
}
123123
};
124124

125125
let ast::ItemKind::Fn(fn_) = &item.kind else {
126-
not_testable_error(cx, attr_sp, Some(&item));
126+
not_testable_error(cx, is_bench, attr_sp, Some(&item));
127127
return if is_stmt {
128128
vec![Annotatable::Stmt(Box::new(cx.stmt_item(item.span, item)))]
129129
} else {
@@ -405,9 +405,10 @@ pub(crate) fn expand_test_or_bench(
405405
}
406406
}
407407

408-
fn not_testable_error(cx: &ExtCtxt<'_>, attr_sp: Span, item: Option<&ast::Item>) {
408+
fn not_testable_error(cx: &ExtCtxt<'_>, is_bench: bool, attr_sp: Span, item: Option<&ast::Item>) {
409409
let dcx = cx.dcx();
410-
let msg = "the `#[test]` attribute may only be used on a free function";
410+
let name = if is_bench { "bench" } else { "test" };
411+
let msg = format!("the `#[{name}]` attribute may only be used on a free function");
411412
let level = match item.map(|i| &i.kind) {
412413
// These were a warning before #92959 and need to continue being that to avoid breaking
413414
// stable user code (#94508).
@@ -426,12 +427,16 @@ fn not_testable_error(cx: &ExtCtxt<'_>, attr_sp: Span, item: Option<&ast::Item>)
426427
),
427428
);
428429
}
429-
err.with_span_label(attr_sp, "the `#[test]` macro causes a function to be run as a test and has no effect on non-functions")
430-
.with_span_suggestion(attr_sp,
430+
err.span_label(attr_sp, format!("the `#[{name}]` macro causes a function to be run as a test and has no effect on non-functions"));
431+
432+
if !is_bench {
433+
err.with_span_suggestion(attr_sp,
431434
"replace with conditional compilation to make the item only exist when tests are being run",
432435
"#[cfg(test)]",
433-
Applicability::MaybeIncorrect)
434-
.emit();
436+
Applicability::MaybeIncorrect).emit();
437+
} else {
438+
err.emit();
439+
}
435440
}
436441

437442
fn get_location_info(cx: &ExtCtxt<'_>, fn_: &ast::Fn) -> (Symbol, usize, usize, usize, usize) {

tests/ui/feature-gates/gating-of-test-attrs.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,22 @@ mod test {
2626
// non-crate-level #[bench] attributes seem to be ignored.
2727

2828
#[bench]
29-
//~^ ERROR the `#[test]` attribute may only be used on a non-associated function
29+
//~^ ERROR the `#[bench]` attribute may only be used on a non-associated function
3030
mod bench {
3131
mod inner { #![bench] }
3232
//~^ ERROR inner macro attributes are unstable
33-
//~| ERROR the `#[test]` attribute may only be used on a non-associated function
33+
//~| ERROR the `#[bench]` attribute may only be used on a non-associated function
3434

3535
#[bench]
36-
//~^ ERROR the `#[test]` attribute may only be used on a non-associated function
36+
//~^ ERROR the `#[bench]` attribute may only be used on a non-associated function
3737
struct S;
3838

3939
#[bench]
40-
//~^ ERROR the `#[test]` attribute may only be used on a non-associated function
40+
//~^ ERROR the `#[bench]` attribute may only be used on a non-associated function
4141
type T = S;
4242

4343
#[bench]
44-
//~^ ERROR the `#[test]` attribute may only be used on a non-associated function
44+
//~^ ERROR the `#[bench]` attribute may only be used on a non-associated function
4545
impl S { }
4646
}
4747

tests/ui/feature-gates/gating-of-test-attrs.stderr

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: the `#[test]` attribute may only be used on a non-associated function
1+
error: the `#[test]` attribute may only be used on a free function
22
--> $DIR/gating-of-test-attrs.rs:5:1
33
|
44
LL | #[test]
@@ -27,7 +27,7 @@ LL | mod inner { #![test] }
2727
= help: add `#![feature(custom_inner_attributes)]` to the crate attributes to enable
2828
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
2929

30-
error: the `#[test]` attribute may only be used on a non-associated function
30+
error: the `#[test]` attribute may only be used on a free function
3131
--> $DIR/gating-of-test-attrs.rs:8:17
3232
|
3333
LL | mod inner { #![test] }
@@ -42,7 +42,7 @@ LL - mod inner { #![test] }
4242
LL + mod inner { #[cfg(test)] }
4343
|
4444

45-
error: the `#[test]` attribute may only be used on a non-associated function
45+
error: the `#[test]` attribute may only be used on a free function
4646
--> $DIR/gating-of-test-attrs.rs:12:5
4747
|
4848
LL | #[test]
@@ -57,7 +57,7 @@ LL - #[test]
5757
LL + #[cfg(test)]
5858
|
5959

60-
error: the `#[test]` attribute may only be used on a non-associated function
60+
error: the `#[test]` attribute may only be used on a free function
6161
--> $DIR/gating-of-test-attrs.rs:16:5
6262
|
6363
LL | #[test]
@@ -72,7 +72,7 @@ LL - #[test]
7272
LL + #[cfg(test)]
7373
|
7474

75-
error: the `#[test]` attribute may only be used on a non-associated function
75+
error: the `#[test]` attribute may only be used on a free function
7676
--> $DIR/gating-of-test-attrs.rs:20:5
7777
|
7878
LL | #[test]
@@ -87,24 +87,18 @@ LL - #[test]
8787
LL + #[cfg(test)]
8888
|
8989

90-
error: the `#[test]` attribute may only be used on a non-associated function
90+
error: the `#[bench]` attribute may only be used on a free function
9191
--> $DIR/gating-of-test-attrs.rs:28:1
9292
|
9393
LL | #[bench]
94-
| ^^^^^^^^ the `#[test]` macro causes a function to be run as a test and has no effect on non-functions
94+
| ^^^^^^^^ the `#[bench]` macro causes a function to be run as a test and has no effect on non-functions
9595
LL |
9696
LL | / mod bench {
9797
LL | | mod inner { #![bench] }
9898
... |
9999
LL | | impl S { }
100100
LL | | }
101101
| |_- expected a non-associated function, found a module
102-
|
103-
help: replace with conditional compilation to make the item only exist when tests are being run
104-
|
105-
LL - #[bench]
106-
LL + #[cfg(test)]
107-
|
108102

109103
error[E0658]: inner macro attributes are unstable
110104
--> $DIR/gating-of-test-attrs.rs:31:20
@@ -116,65 +110,41 @@ LL | mod inner { #![bench] }
116110
= help: add `#![feature(custom_inner_attributes)]` to the crate attributes to enable
117111
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
118112

119-
error: the `#[test]` attribute may only be used on a non-associated function
113+
error: the `#[bench]` attribute may only be used on a free function
120114
--> $DIR/gating-of-test-attrs.rs:31:17
121115
|
122116
LL | mod inner { #![bench] }
123117
| ------------^^^^^^^^^--
124118
| | |
125-
| | the `#[test]` macro causes a function to be run as a test and has no effect on non-functions
119+
| | the `#[bench]` macro causes a function to be run as a test and has no effect on non-functions
126120
| expected a non-associated function, found a module
127-
|
128-
help: replace with conditional compilation to make the item only exist when tests are being run
129-
|
130-
LL - mod inner { #![bench] }
131-
LL + mod inner { #[cfg(test)] }
132-
|
133121

134-
error: the `#[test]` attribute may only be used on a non-associated function
122+
error: the `#[bench]` attribute may only be used on a free function
135123
--> $DIR/gating-of-test-attrs.rs:35:5
136124
|
137125
LL | #[bench]
138-
| ^^^^^^^^ the `#[test]` macro causes a function to be run as a test and has no effect on non-functions
126+
| ^^^^^^^^ the `#[bench]` macro causes a function to be run as a test and has no effect on non-functions
139127
LL |
140128
LL | struct S;
141129
| --------- expected a non-associated function, found a struct
142-
|
143-
help: replace with conditional compilation to make the item only exist when tests are being run
144-
|
145-
LL - #[bench]
146-
LL + #[cfg(test)]
147-
|
148130

149-
error: the `#[test]` attribute may only be used on a non-associated function
131+
error: the `#[bench]` attribute may only be used on a free function
150132
--> $DIR/gating-of-test-attrs.rs:39:5
151133
|
152134
LL | #[bench]
153-
| ^^^^^^^^ the `#[test]` macro causes a function to be run as a test and has no effect on non-functions
135+
| ^^^^^^^^ the `#[bench]` macro causes a function to be run as a test and has no effect on non-functions
154136
LL |
155137
LL | type T = S;
156138
| ----------- expected a non-associated function, found a type alias
157-
|
158-
help: replace with conditional compilation to make the item only exist when tests are being run
159-
|
160-
LL - #[bench]
161-
LL + #[cfg(test)]
162-
|
163139

164-
error: the `#[test]` attribute may only be used on a non-associated function
140+
error: the `#[bench]` attribute may only be used on a free function
165141
--> $DIR/gating-of-test-attrs.rs:43:5
166142
|
167143
LL | #[bench]
168-
| ^^^^^^^^ the `#[test]` macro causes a function to be run as a test and has no effect on non-functions
144+
| ^^^^^^^^ the `#[bench]` macro causes a function to be run as a test and has no effect on non-functions
169145
LL |
170146
LL | impl S { }
171147
| ---------- expected a non-associated function, found an implementation
172-
|
173-
help: replace with conditional compilation to make the item only exist when tests are being run
174-
|
175-
LL - #[bench]
176-
LL + #[cfg(test)]
177-
|
178148

179149
error: aborting due to 12 previous errors
180150

tests/ui/macros/issue-111749.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: the `#[test]` attribute may only be used on a non-associated function
1+
error: the `#[test]` attribute may only be used on a free function
22
--> $DIR/issue-111749.rs:8:17
33
|
44
LL | cbor_map! { #[test(test)] 4i32};

tests/ui/macros/test-on-crate-root.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0658]: inner macro attributes are unstable
2-
--> $DIR/test-on-crate-root.rs:3:4
2+
--> $DIR/test-on-crate-root.rs:4:4
33
|
44
LL | #![core::prelude::v1::test]
55
| ^^^^^^^^^^^^^^^^^^^^^^^
@@ -8,8 +8,8 @@ LL | #![core::prelude::v1::test]
88
= help: add `#![feature(custom_inner_attributes)]` to the crate attributes to enable
99
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1010

11-
error: the `#[test]` attribute may only be used on a non-associated function
12-
--> $DIR/test-on-crate-root.rs:3:1
11+
error: the `#[test]` attribute may only be used on a free function
12+
--> $DIR/test-on-crate-root.rs:4:1
1313
|
1414
LL | #![core::prelude::v1::test]
1515
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the `#[test]` macro causes a function to be run as a test and has no effect on non-functions

tests/ui/test-attrs/issue-109816.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: the `#[test]` attribute may only be used on a non-associated function
1+
error: the `#[test]` attribute may only be used on a free function
22
--> $DIR/issue-109816.rs:4:5
33
|
44
LL | #[test]

tests/ui/test-attrs/test-attr-non-associated-functions.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: the `#[test]` attribute may only be used on a non-associated function
1+
error: the `#[test]` attribute may only be used on a free function
22
--> $DIR/test-attr-non-associated-functions.rs:6:5
33
|
44
LL | #[test]
@@ -10,7 +10,7 @@ LL - #[test]
1010
LL + #[cfg(test)]
1111
|
1212

13-
error: the `#[test]` attribute may only be used on a non-associated function
13+
error: the `#[test]` attribute may only be used on a free function
1414
--> $DIR/test-attr-non-associated-functions.rs:11:5
1515
|
1616
LL | #[test]

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: the `#[test]` attribute may only be used on a non-associated function
1+
error: the `#[test]` attribute may only be used on a free function
22
--> $DIR/test-on-not-fn.rs:3:1
33
|
44
LL | #[test]
@@ -12,7 +12,7 @@ LL - #[test]
1212
LL + #[cfg(test)]
1313
|
1414

15-
error: the `#[test]` attribute may only be used on a non-associated function
15+
error: the `#[test]` attribute may only be used on a free function
1616
--> $DIR/test-on-not-fn.rs:6:1
1717
|
1818
LL | #[test]
@@ -32,7 +32,7 @@ LL - #[test]
3232
LL + #[cfg(test)]
3333
|
3434

35-
error: the `#[test]` attribute may only be used on a non-associated function
35+
error: the `#[test]` attribute may only be used on a free function
3636
--> $DIR/test-on-not-fn.rs:20:1
3737
|
3838
LL | #[test]
@@ -46,7 +46,7 @@ LL - #[test]
4646
LL + #[cfg(test)]
4747
|
4848

49-
error: the `#[test]` attribute may only be used on a non-associated function
49+
error: the `#[test]` attribute may only be used on a free function
5050
--> $DIR/test-on-not-fn.rs:23:1
5151
|
5252
LL | #[test]
@@ -60,7 +60,7 @@ LL - #[test]
6060
LL + #[cfg(test)]
6161
|
6262

63-
error: the `#[test]` attribute may only be used on a non-associated function
63+
error: the `#[test]` attribute may only be used on a free function
6464
--> $DIR/test-on-not-fn.rs:26:1
6565
|
6666
LL | #[test]
@@ -74,7 +74,7 @@ LL - #[test]
7474
LL + #[cfg(test)]
7575
|
7676

77-
error: the `#[test]` attribute may only be used on a non-associated function
77+
error: the `#[test]` attribute may only be used on a free function
7878
--> $DIR/test-on-not-fn.rs:29:1
7979
|
8080
LL | #[test]
@@ -88,7 +88,7 @@ LL - #[test]
8888
LL + #[cfg(test)]
8989
|
9090

91-
error: the `#[test]` attribute may only be used on a non-associated function
91+
error: the `#[test]` attribute may only be used on a free function
9292
--> $DIR/test-on-not-fn.rs:32:1
9393
|
9494
LL | #[test]
@@ -102,7 +102,7 @@ LL - #[test]
102102
LL + #[cfg(test)]
103103
|
104104

105-
error: the `#[test]` attribute may only be used on a non-associated function
105+
error: the `#[test]` attribute may only be used on a free function
106106
--> $DIR/test-on-not-fn.rs:35:1
107107
|
108108
LL | #[test]
@@ -118,7 +118,7 @@ LL - #[test]
118118
LL + #[cfg(test)]
119119
|
120120

121-
error: the `#[test]` attribute may only be used on a non-associated function
121+
error: the `#[test]` attribute may only be used on a free function
122122
--> $DIR/test-on-not-fn.rs:40:1
123123
|
124124
LL | #[test]
@@ -132,7 +132,7 @@ LL - #[test]
132132
LL + #[cfg(test)]
133133
|
134134

135-
error: the `#[test]` attribute may only be used on a non-associated function
135+
error: the `#[test]` attribute may only be used on a free function
136136
--> $DIR/test-on-not-fn.rs:43:1
137137
|
138138
LL | #[test]
@@ -149,7 +149,7 @@ LL - #[test]
149149
LL + #[cfg(test)]
150150
|
151151

152-
error: the `#[test]` attribute may only be used on a non-associated function
152+
error: the `#[test]` attribute may only be used on a free function
153153
--> $DIR/test-on-not-fn.rs:50:1
154154
|
155155
LL | #[test]
@@ -167,7 +167,7 @@ LL - #[test]
167167
LL + #[cfg(test)]
168168
|
169169

170-
warning: the `#[test]` attribute may only be used on a non-associated function
170+
warning: the `#[test]` attribute may only be used on a free function
171171
--> $DIR/test-on-not-fn.rs:61:1
172172
|
173173
LL | #[test]

0 commit comments

Comments
 (0)