Skip to content

Commit 4d082b7

Browse files
committed
add error message when #[naked] is used with #[test]
1 parent 7e6c083 commit 4d082b7

File tree

7 files changed

+112
-0
lines changed

7 files changed

+112
-0
lines changed

compiler/rustc_builtin_macros/messages.ftl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@ builtin_macros_multiple_defaults = multiple declared defaults
216216
.note = only one variant can be default
217217
.suggestion = make `{$ident}` default
218218
219+
builtin_macros_naked_functions_testing_attribute =
220+
cannot use `#[naked]` with testing attributes
221+
.label = function marked with testing attribute here
222+
.naked_attribute = `#[naked]` is incompatible with testing attributes
223+
219224
builtin_macros_no_default_variant = no default declared
220225
.help = make a unit variant default by placing `#[default]` above it
221226
.suggestion = make `{$ident}` default

compiler/rustc_builtin_macros/src/errors.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,3 +912,13 @@ pub(crate) struct ExpectedItem<'a> {
912912
pub span: Span,
913913
pub token: &'a str,
914914
}
915+
916+
#[derive(Diagnostic)]
917+
#[diag(builtin_macros_naked_functions_testing_attribute, code = E0798)]
918+
pub struct NakedFunctionTestingAttribute {
919+
#[primary_span]
920+
#[label(builtin_macros_naked_attribute)]
921+
pub naked_span: Span,
922+
#[label]
923+
pub testing_span: Span,
924+
}

compiler/rustc_builtin_macros/src/test.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ pub(crate) fn expand_test_or_bench(
133133
};
134134
};
135135

136+
if let Some(attr) = attr::find_by_name(&item.attrs, sym::naked) {
137+
cx.dcx().emit_err(errors::NakedFunctionTestingAttribute {
138+
testing_span: attr_sp,
139+
naked_span: attr.span,
140+
});
141+
return vec![Annotatable::Item(item)];
142+
}
143+
136144
// check_*_signature will report any errors in the type so compilation
137145
// will fail. We shouldn't try to expand in this case because the errors
138146
// would be spurious.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Testing attributes cannot be applied to functions marked with `#[naked]`.
2+
3+
Erroneous code example:
4+
5+
```ignore (requires test runner)
6+
#[test]
7+
#[should_panic]
8+
#[naked]
9+
fn foo() {}
10+
```
11+
12+
See [the reference page for testing attributes] for more information.
13+
14+
[the reference page for testing attributes]: https://doc.rust-lang.org/reference/attributes/testing.html

compiler/rustc_error_codes/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ E0794: 0794,
536536
E0795: 0795,
537537
E0796: 0796,
538538
E0797: 0797,
539+
E0798: 0798,
539540
);
540541
)
541542
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//@ needs-asm-support
2+
//@ compile-flags: --test
3+
4+
#![allow(undefined_naked_function_abi)]
5+
#![feature(naked_functions)]
6+
#![feature(test)]
7+
#![crate_type = "lib"]
8+
9+
use std::arch::asm;
10+
11+
#[test]
12+
#[naked]
13+
//~^ ERROR [E0798]
14+
fn test_naked() {
15+
unsafe { asm!("", options(noreturn)) };
16+
}
17+
18+
#[should_panic]
19+
#[test]
20+
#[naked]
21+
//~^ ERROR [E0798]
22+
fn test_naked_should_panic() {
23+
unsafe { asm!("", options(noreturn)) };
24+
}
25+
26+
#[ignore]
27+
#[test]
28+
#[naked]
29+
//~^ ERROR [E0798]
30+
fn test_naked_ignore() {
31+
unsafe { asm!("", options(noreturn)) };
32+
}
33+
34+
#[bench]
35+
#[naked]
36+
//~^ ERROR [E0798]
37+
fn bench_naked() {
38+
unsafe { asm!("", options(noreturn)) };
39+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error[E0798]: cannot use `#[naked]` with testing attributes
2+
--> $DIR/naked-functions-testattrs.rs:12:1
3+
|
4+
LL | #[test]
5+
| ------- function marked with testing attribute here
6+
LL | #[naked]
7+
| ^^^^^^^^ `#[naked]` is incompatible with testing attributes
8+
9+
error[E0798]: cannot use `#[naked]` with testing attributes
10+
--> $DIR/naked-functions-testattrs.rs:20:1
11+
|
12+
LL | #[test]
13+
| ------- function marked with testing attribute here
14+
LL | #[naked]
15+
| ^^^^^^^^ `#[naked]` is incompatible with testing attributes
16+
17+
error[E0798]: cannot use `#[naked]` with testing attributes
18+
--> $DIR/naked-functions-testattrs.rs:28:1
19+
|
20+
LL | #[test]
21+
| ------- function marked with testing attribute here
22+
LL | #[naked]
23+
| ^^^^^^^^ `#[naked]` is incompatible with testing attributes
24+
25+
error[E0798]: cannot use `#[naked]` with testing attributes
26+
--> $DIR/naked-functions-testattrs.rs:35:1
27+
|
28+
LL | #[bench]
29+
| -------- function marked with testing attribute here
30+
LL | #[naked]
31+
| ^^^^^^^^ `#[naked]` is incompatible with testing attributes
32+
33+
error: aborting due to 4 previous errors
34+
35+
For more information about this error, try `rustc --explain E0798`.

0 commit comments

Comments
 (0)