Skip to content

Commit 7e6c083

Browse files
committed
improve error message when #[naked] is used with #[track-caller] and #[target-feature]``
1 parent 4bd3632 commit 7e6c083

File tree

7 files changed

+53
-20
lines changed

7 files changed

+53
-20
lines changed

compiler/rustc_passes/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ passes_naked_functions_asm_options =
485485
passes_naked_functions_codegen_attribute =
486486
cannot use additional code generation attributes with `#[naked]`
487487
.label = this attribute is incompatible with `#[naked]`
488-
.label2 = function marked with `#[naked]` here
488+
.naked_attribute = function marked with `#[naked]` here
489489
490490
passes_naked_functions_must_use_noreturn =
491491
asm in naked functions must use `noreturn` option

compiler/rustc_passes/src/check_attr.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -421,20 +421,22 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
421421
const FORBIDDEN: [rustc_span::Symbol; 3] =
422422
[sym::track_caller, sym::inline, sym::target_feature];
423423

424-
for other_attr in attrs {
425-
if FORBIDDEN.into_iter().any(|name| other_attr.has_name(name)) {
426-
self.dcx().emit_err(errors::NakedFunctionCodegenAttribute {
427-
span: other_attr.span,
428-
naked_span: attr.span,
429-
});
430-
431-
return false;
432-
}
433-
}
434-
435424
match target {
436425
Target::Fn
437-
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => true,
426+
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => {
427+
for other_attr in attrs {
428+
if FORBIDDEN.into_iter().any(|name| other_attr.has_name(name)) {
429+
self.dcx().emit_err(errors::NakedFunctionCodegenAttribute {
430+
span: other_attr.span,
431+
naked_span: attr.span,
432+
});
433+
434+
return false;
435+
}
436+
}
437+
438+
true
439+
}
438440
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
439441
// `#[naked]` attribute with just a lint, because we previously
440442
// erroneously allowed it and some crates used it accidentally, to be compatible

compiler/rustc_passes/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,7 @@ pub struct NakedFunctionCodegenAttribute {
11881188
#[primary_span]
11891189
#[label]
11901190
pub span: Span,
1191-
#[label(passes_label2)]
1191+
#[label(passes_naked_attribute)]
11921192
pub naked_span: Span,
11931193
}
11941194

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@ only-x86_64
2+
//@ needs-asm-support
3+
#![feature(naked_functions)]
4+
#![crate_type = "lib"]
5+
6+
use std::arch::asm;
7+
8+
#[target_feature(enable = "sse2")]
9+
//~^ ERROR [E0736]
10+
#[naked]
11+
pub unsafe extern "C" fn naked_target_feature() {
12+
asm!("", options(noreturn));
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0736]: cannot use additional code generation attributes with `#[naked]`
2+
--> $DIR/naked-functions-target-feature.rs:8:1
3+
|
4+
LL | #[target_feature(enable = "sse2")]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this attribute is incompatible with `#[naked]`
6+
LL |
7+
LL | #[naked]
8+
| -------- function marked with `#[naked]` here
9+
10+
error: aborting due to 1 previous error
11+
12+
For more information about this error, try `rustc --explain E0736`.

tests/ui/rfcs/rfc-2091-track-caller/error-with-naked.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use std::arch::asm;
55

6-
#[track_caller] //~ ERROR cannot use `#[track_caller]` with `#[naked]`
6+
#[track_caller] //~ ERROR [E0736]
77
//~^ ERROR `#[track_caller]` requires Rust ABI
88
#[naked]
99
extern "C" fn f() {
@@ -15,7 +15,7 @@ extern "C" fn f() {
1515
struct S;
1616

1717
impl S {
18-
#[track_caller] //~ ERROR cannot use `#[track_caller]` with `#[naked]`
18+
#[track_caller] //~ ERROR [E0736]
1919
//~^ ERROR `#[track_caller]` requires Rust ABI
2020
#[naked]
2121
extern "C" fn g() {

tests/ui/rfcs/rfc-2091-track-caller/error-with-naked.stderr

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
error[E0736]: cannot use `#[track_caller]` with `#[naked]`
1+
error[E0736]: cannot use additional code generation attributes with `#[naked]`
22
--> $DIR/error-with-naked.rs:6:1
33
|
44
LL | #[track_caller]
5-
| ^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^ this attribute is incompatible with `#[naked]`
6+
LL |
7+
LL | #[naked]
8+
| -------- function marked with `#[naked]` here
69

7-
error[E0736]: cannot use `#[track_caller]` with `#[naked]`
10+
error[E0736]: cannot use additional code generation attributes with `#[naked]`
811
--> $DIR/error-with-naked.rs:18:5
912
|
1013
LL | #[track_caller]
11-
| ^^^^^^^^^^^^^^^
14+
| ^^^^^^^^^^^^^^^ this attribute is incompatible with `#[naked]`
15+
LL |
16+
LL | #[naked]
17+
| -------- function marked with `#[naked]` here
1218

1319
error[E0737]: `#[track_caller]` requires Rust ABI
1420
--> $DIR/error-with-naked.rs:6:1

0 commit comments

Comments
 (0)