Skip to content

Commit a13f40d

Browse files
authored
Rollup merge of rust-lang#127853 - folkertdev:naked-function-error-messages, r=bjorn3
`#[naked]`: report incompatible attributes tracking issue: rust-lang#90957 this is a re-implementation of rust-lang#93809 by ``@bstrie`` which was closed 2 years ago due to inactivity. This PR takes some of the final comments into account, specifically providing a little more context in error messages, and using an allow list to determine which attributes are compatible with `#[naked]`. Notable attributes that are incompatible with `#[naked]` are: * `#[inline]` * `#[track_caller]` * ~~`#[target_feature]`~~ (this is now allowed, see PR discussion) * `#[test]`, `#[ignore]`, `#[should_panic]` These attributes just directly conflict with what `#[naked]` should do. Naked functions are still important for systems programming, embedded, and operating systems, so I'd like to move them forward.
2 parents 3148b35 + c7e688e commit a13f40d

18 files changed

+376
-127
lines changed

compiler/rustc_builtin_macros/messages.ftl

+5
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ builtin_macros_multiple_defaults = multiple declared defaults
220220
.note = only one variant can be default
221221
.suggestion = make `{$ident}` default
222222
223+
builtin_macros_naked_functions_testing_attribute =
224+
cannot use `#[naked]` with testing attributes
225+
.label = function marked with testing attribute here
226+
.naked_attribute = `#[naked]` is incompatible with testing attributes
227+
223228
builtin_macros_no_default_variant = no default declared
224229
.help = make a unit variant default by placing `#[default]` above it
225230
.suggestion = make `{$ident}` default

compiler/rustc_builtin_macros/src/errors.rs

+10
Original file line numberDiff line numberDiff line change
@@ -923,3 +923,13 @@ pub(crate) struct ExpectedItem<'a> {
923923
pub span: Span,
924924
pub token: &'a str,
925925
}
926+
927+
#[derive(Diagnostic)]
928+
#[diag(builtin_macros_naked_functions_testing_attribute, code = E0736)]
929+
pub struct NakedFunctionTestingAttribute {
930+
#[primary_span]
931+
#[label(builtin_macros_naked_attribute)]
932+
pub naked_span: Span,
933+
#[label]
934+
pub testing_span: Span,
935+
}

compiler/rustc_builtin_macros/src/test.rs

+8
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.
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
`#[track_caller]` and `#[naked]` cannot both be applied to the same function.
1+
Functions marked with the `#[naked]` attribute are restricted in what other
2+
attributes they may be marked with.
3+
4+
Notable attributes that are incompatible with `#[naked]` are:
5+
6+
* `#[inline]`
7+
* `#[track_caller]`
8+
* `#[test]`, `#[ignore]`, `#[should_panic]`
29

310
Erroneous code example:
411

512
```compile_fail,E0736
13+
#[inline]
614
#[naked]
7-
#[track_caller]
815
fn foo() {}
916
```
1017

11-
This is primarily due to ABI incompatibilities between the two attributes.
12-
See [RFC 2091] for details on this and other limitations.
13-
14-
[RFC 2091]: https://github.com/rust-lang/rfcs/blob/master/text/2091-inline-semantic.md
18+
These incompatibilities are due to the fact that naked functions deliberately
19+
impose strict restrictions regarding the code that the compiler is
20+
allowed to produce for this function.

compiler/rustc_error_codes/src/error_codes/E0739.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
`#[track_caller]` can not be applied on struct.
1+
`#[track_caller]` must be applied to a function
22

33
Erroneous code example:
44

compiler/rustc_passes/messages.ftl

+5-6
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ passes_break_non_loop =
6969
.suggestion = use `break` on its own without a value inside this `{$kind}` loop
7070
.break_expr_suggestion = alternatively, you might have meant to use the available loop label
7171
72-
passes_cannot_inline_naked_function =
73-
naked functions cannot be inlined
74-
7572
passes_cannot_stabilize_deprecated =
7673
an API can't be stabilized after it is deprecated
7774
.label = invalid version
@@ -485,16 +482,18 @@ passes_naked_functions_asm_block =
485482
passes_naked_functions_asm_options =
486483
asm options unsupported in naked functions: {$unsupported_options}
487484
485+
passes_naked_functions_incompatible_attribute =
486+
attribute incompatible with `#[naked]`
487+
.label = the `{$attr}` attribute is incompatible with `#[naked]`
488+
.naked_attribute = function marked with `#[naked]` here
489+
488490
passes_naked_functions_must_use_noreturn =
489491
asm in naked functions must use `noreturn` option
490492
.suggestion = consider specifying that the asm block is responsible for returning from the function
491493
492494
passes_naked_functions_operands =
493495
only `const` and `sym` operands are supported in naked functions
494496
495-
passes_naked_tracked_caller =
496-
cannot use `#[track_caller]` with `#[naked]`
497-
498497
passes_no_link =
499498
attribute should be applied to an `extern crate` item
500499
.label = not an `extern crate` item

compiler/rustc_passes/src/check_attr.rs

+64-9
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
155155
[sym::rustc_std_internal_symbol] => {
156156
self.check_rustc_std_internal_symbol(attr, span, target)
157157
}
158-
[sym::naked] => self.check_naked(hir_id, attr, span, target),
158+
[sym::naked] => self.check_naked(hir_id, attr, span, target, attrs),
159159
[sym::rustc_never_returns_null_ptr] => {
160160
self.check_applied_to_fn_or_method(hir_id, attr, span, target)
161161
}
@@ -410,12 +410,71 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
410410
}
411411

412412
/// Checks if `#[naked]` is applied to a function definition.
413-
fn check_naked(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) -> bool {
413+
fn check_naked(
414+
&self,
415+
hir_id: HirId,
416+
attr: &Attribute,
417+
span: Span,
418+
target: Target,
419+
attrs: &[Attribute],
420+
) -> bool {
421+
// many attributes don't make sense in combination with #[naked].
422+
// Notable attributes that are incompatible with `#[naked]` are:
423+
//
424+
// * `#[inline]`
425+
// * `#[track_caller]`
426+
// * `#[test]`, `#[ignore]`, `#[should_panic]`
427+
//
428+
// NOTE: when making changes to this list, check that `error_codes/E0736.md` remains accurate
429+
const ALLOW_LIST: &[rustc_span::Symbol] = &[
430+
// conditional compilation
431+
sym::cfg,
432+
sym::cfg_attr,
433+
// testing (allowed here so better errors can be generated in `rustc_builtin_macros::test`)
434+
sym::test,
435+
sym::ignore,
436+
sym::should_panic,
437+
sym::bench,
438+
// diagnostics
439+
sym::allow,
440+
sym::warn,
441+
sym::deny,
442+
sym::forbid,
443+
sym::deprecated,
444+
sym::must_use,
445+
// abi, linking and FFI
446+
sym::export_name,
447+
sym::link_section,
448+
sym::linkage,
449+
sym::no_mangle,
450+
sym::naked,
451+
sym::instruction_set,
452+
// code generation
453+
sym::cold,
454+
sym::target_feature,
455+
// documentation
456+
sym::doc,
457+
];
458+
414459
match target {
415460
Target::Fn
416-
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => true,
461+
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => {
462+
for other_attr in attrs {
463+
if !ALLOW_LIST.iter().any(|name| other_attr.has_name(*name)) {
464+
self.dcx().emit_err(errors::NakedFunctionIncompatibleAttribute {
465+
span: other_attr.span,
466+
naked_span: attr.span,
467+
attr: other_attr.name_or_empty(),
468+
});
469+
470+
return false;
471+
}
472+
}
473+
474+
true
475+
}
417476
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
418-
// `#[allow_internal_unstable]` attribute with just a lint, because we previously
477+
// `#[naked]` attribute with just a lint, because we previously
419478
// erroneously allowed it and some crates used it accidentally, to be compatible
420479
// with crates depending on them, we can't throw an error here.
421480
Target::Field | Target::Arm | Target::MacroDef => {
@@ -488,7 +547,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
488547
}
489548
}
490549

491-
/// Checks if a `#[track_caller]` is applied to a non-naked function. Returns `true` if valid.
550+
/// Checks if a `#[track_caller]` is applied to a function. Returns `true` if valid.
492551
fn check_track_caller(
493552
&self,
494553
hir_id: HirId,
@@ -498,10 +557,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
498557
target: Target,
499558
) -> bool {
500559
match target {
501-
_ if attrs.iter().any(|attr| attr.has_name(sym::naked)) => {
502-
self.dcx().emit_err(errors::NakedTrackedCaller { attr_span });
503-
false
504-
}
505560
Target::Fn => {
506561
// `#[track_caller]` is not valid on weak lang items because they are called via
507562
// `extern` declarations and `#[track_caller]` would alter their ABI.

compiler/rustc_passes/src/errors.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,6 @@ pub struct AttrShouldBeAppliedToFn {
7979
pub on_crate: bool,
8080
}
8181

82-
#[derive(Diagnostic)]
83-
#[diag(passes_naked_tracked_caller, code = E0736)]
84-
pub struct NakedTrackedCaller {
85-
#[primary_span]
86-
pub attr_span: Span,
87-
}
88-
8982
#[derive(Diagnostic)]
9083
#[diag(passes_should_be_applied_to_fn, code = E0739)]
9184
pub struct TrackedCallerWrongLocation {
@@ -1124,13 +1117,6 @@ pub struct UnlabeledCfInWhileCondition<'a> {
11241117
pub cf_type: &'a str,
11251118
}
11261119

1127-
#[derive(Diagnostic)]
1128-
#[diag(passes_cannot_inline_naked_function)]
1129-
pub struct CannotInlineNakedFunction {
1130-
#[primary_span]
1131-
pub span: Span,
1132-
}
1133-
11341120
#[derive(LintDiagnostic)]
11351121
#[diag(passes_undefined_naked_function_abi)]
11361122
pub struct UndefinedNakedFunctionAbi;
@@ -1196,6 +1182,17 @@ pub struct NakedFunctionsMustUseNoreturn {
11961182
pub last_span: Span,
11971183
}
11981184

1185+
#[derive(Diagnostic)]
1186+
#[diag(passes_naked_functions_incompatible_attribute, code = E0736)]
1187+
pub struct NakedFunctionIncompatibleAttribute {
1188+
#[primary_span]
1189+
#[label]
1190+
pub span: Span,
1191+
#[label(passes_naked_attribute)]
1192+
pub naked_span: Span,
1193+
pub attr: Symbol,
1194+
}
1195+
11991196
#[derive(Diagnostic)]
12001197
#[diag(passes_attr_only_in_functions)]
12011198
pub struct AttrOnlyInFunctions {

compiler/rustc_passes/src/naked_functions.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ use rustc_span::Span;
1414
use rustc_target::spec::abi::Abi;
1515

1616
use crate::errors::{
17-
CannotInlineNakedFunction, NakedFunctionsAsmBlock, NakedFunctionsAsmOptions,
18-
NakedFunctionsMustUseNoreturn, NakedFunctionsOperands, NoPatterns, ParamsNotAllowed,
19-
UndefinedNakedFunctionAbi,
17+
NakedFunctionsAsmBlock, NakedFunctionsAsmOptions, NakedFunctionsMustUseNoreturn,
18+
NakedFunctionsOperands, NoPatterns, ParamsNotAllowed, UndefinedNakedFunctionAbi,
2019
};
2120

2221
pub(crate) fn provide(providers: &mut Providers) {
@@ -53,15 +52,6 @@ fn check_mod_naked_functions(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
5352
check_no_patterns(tcx, body.params);
5453
check_no_parameters_use(tcx, body);
5554
check_asm(tcx, def_id, body);
56-
check_inline(tcx, def_id);
57-
}
58-
}
59-
60-
/// Check that the function isn't inlined.
61-
fn check_inline(tcx: TyCtxt<'_>, def_id: LocalDefId) {
62-
let attrs = tcx.get_attrs(def_id, sym::inline);
63-
for attr in attrs {
64-
tcx.dcx().emit_err(CannotInlineNakedFunction { span: attr.span });
6555
}
6656
}
6757

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//@ needs-asm-support
2+
#![feature(naked_functions)]
3+
#![crate_type = "lib"]
4+
5+
use std::arch::asm;
6+
7+
#[naked]
8+
pub unsafe extern "C" fn inline_none() {
9+
asm!("", options(noreturn));
10+
}
11+
12+
#[naked]
13+
#[inline]
14+
//~^ ERROR [E0736]
15+
pub unsafe extern "C" fn inline_hint() {
16+
asm!("", options(noreturn));
17+
}
18+
19+
#[naked]
20+
#[inline(always)]
21+
//~^ ERROR [E0736]
22+
pub unsafe extern "C" fn inline_always() {
23+
asm!("", options(noreturn));
24+
}
25+
26+
#[naked]
27+
#[inline(never)]
28+
//~^ ERROR [E0736]
29+
pub unsafe extern "C" fn inline_never() {
30+
asm!("", options(noreturn));
31+
}
32+
33+
#[naked]
34+
#[cfg_attr(all(), inline(never))]
35+
//~^ ERROR [E0736]
36+
pub unsafe extern "C" fn conditional_inline_never() {
37+
asm!("", options(noreturn));
38+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error[E0736]: attribute incompatible with `#[naked]`
2+
--> $DIR/naked-functions-inline.rs:13:1
3+
|
4+
LL | #[naked]
5+
| -------- function marked with `#[naked]` here
6+
LL | #[inline]
7+
| ^^^^^^^^^ the `inline` attribute is incompatible with `#[naked]`
8+
9+
error[E0736]: attribute incompatible with `#[naked]`
10+
--> $DIR/naked-functions-inline.rs:20:1
11+
|
12+
LL | #[naked]
13+
| -------- function marked with `#[naked]` here
14+
LL | #[inline(always)]
15+
| ^^^^^^^^^^^^^^^^^ the `inline` attribute is incompatible with `#[naked]`
16+
17+
error[E0736]: attribute incompatible with `#[naked]`
18+
--> $DIR/naked-functions-inline.rs:27:1
19+
|
20+
LL | #[naked]
21+
| -------- function marked with `#[naked]` here
22+
LL | #[inline(never)]
23+
| ^^^^^^^^^^^^^^^^ the `inline` attribute is incompatible with `#[naked]`
24+
25+
error[E0736]: attribute incompatible with `#[naked]`
26+
--> $DIR/naked-functions-inline.rs:34:19
27+
|
28+
LL | #[naked]
29+
| -------- function marked with `#[naked]` here
30+
LL | #[cfg_attr(all(), inline(never))]
31+
| ^^^^^^^^^^^^^ the `inline` attribute is incompatible with `#[naked]`
32+
33+
error: aborting due to 4 previous errors
34+
35+
For more information about this error, try `rustc --explain E0736`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//@ compile-flags: --target armv5te-unknown-linux-gnueabi
2+
//@ needs-llvm-components: arm
3+
//@ needs-asm-support
4+
//@ build-pass
5+
6+
#![crate_type = "lib"]
7+
#![feature(no_core, lang_items, rustc_attrs, naked_functions)]
8+
#![no_core]
9+
10+
#[rustc_builtin_macro]
11+
macro_rules! asm {
12+
() => {};
13+
}
14+
15+
#[lang = "sized"]
16+
trait Sized {}
17+
18+
#[no_mangle]
19+
#[naked]
20+
#[instruction_set(arm::t32)]
21+
unsafe extern "C" fn test_thumb() {
22+
asm!("bx lr", options(noreturn));
23+
}
24+
25+
#[no_mangle]
26+
#[naked]
27+
#[instruction_set(arm::t32)]
28+
unsafe extern "C" fn test_arm() {
29+
asm!("bx lr", options(noreturn));
30+
}

0 commit comments

Comments
 (0)