Skip to content

Commit 5aa296b

Browse files
committed
fixup name in diagnostics
1 parent cec2d48 commit 5aa296b

13 files changed

+74
-99
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: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,45 @@
33
// test is a built-in macro, not a built-in attribute, but it kind of acts like both.
44
// check its target checking anyway here
55
#[test]
6-
//~^ ERROR the `#[test]` attribute may only be used on a non-associated function
6+
//~^ ERROR the `#[test]` attribute may only be used on a free function
77
mod test {
88
mod inner { #![test] }
99
//~^ ERROR inner macro attributes are unstable
10-
//~| ERROR the `#[test]` attribute may only be used on a non-associated function
10+
//~| ERROR the `#[test]` attribute may only be used on a free function
1111

1212
#[test]
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
struct S;
1515

1616
#[test]
17-
//~^ ERROR the `#[test]` attribute may only be used on a non-associated function
17+
//~^ ERROR the `#[test]` attribute may only be used on a free function
1818
type T = S;
1919

2020
#[test]
21-
//~^ ERROR the `#[test]` attribute may only be used on a non-associated function
21+
//~^ ERROR the `#[test]` attribute may only be used on a free function
2222
impl S { }
2323
}
2424

2525
// At time of unit test authorship, if compiling without `--test` then
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 free 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 free 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 free 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 free 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 free 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.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ macro_rules! cbor_map {
66

77
fn main() {
88
cbor_map! { #[test(test)] 4i32};
9-
//~^ ERROR the `#[test]` attribute may only be used on a non-associated function
9+
//~^ ERROR the `#[test]` attribute may only be used on a free function
1010
//~| ERROR attribute must be of the form `#[test]`
1111
//~| WARNING this was previously accepted by the compiler but is being phased out
1212
}

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.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Fixes #114920
44
#![core::prelude::v1::test]
55
//~^ ERROR inner macro attributes are unstable
6-
//~| ERROR the `#[test]` attribute may only be used on a non-associated function
6+
//~| ERROR the `#[test]` attribute may only be used on a free function
77

88

99
fn main() {} // not important to reproduce the issue

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.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
fn align_offset_weird_strides() {
44
#[test]
5-
//~^ ERROR the `#[test]` attribute may only be used on a non-associated function
5+
//~^ ERROR the `#[test]` attribute may only be used on a free function
66
struct A5(u32, u8);
77
}

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.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ struct A {}
44

55
impl A {
66
#[test]
7-
//~^ ERROR the `#[test]` attribute may only be used on a non-associated function
7+
//~^ ERROR the `#[test]` attribute may only be used on a free function
88
fn new() -> A {
99
A {}
1010
}
1111
#[test]
12-
//~^ ERROR the `#[test]` attribute may only be used on a non-associated function
12+
//~^ ERROR the `#[test]` attribute may only be used on a free function
1313
fn recovery_witness() -> A {
1414
A {}
1515
}

0 commit comments

Comments
 (0)