Skip to content

Commit 3e897fe

Browse files
committed
fixup name in diagnostics
1 parent b536883 commit 3e897fe

File tree

3 files changed

+28
-53
lines changed

3 files changed

+28
-53
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 non-associated function";
410+
let name = if is_bench { "bench" } else { "test" };
411+
let msg = format!("the `#[{name}]` attribute may only be used on a non-associated 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: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -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 non-associated 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 non-associated 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 non-associated 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 non-associated 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 non-associated 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

0 commit comments

Comments
 (0)