Skip to content

Commit 798446f

Browse files
committed
Auto merge of #87772 - npmccallum:naked_abi, r=Amanieu
Move naked function ABI check to its own lint This check was previously categorized under the lint named `UNSUPPORTED_NAKED_FUNCTIONS`. That lint is future incompatible and will be turned into an error in a future release. However, as defined in the Constrained Naked Functions RFC, this check should only be a warning. This is because it is possible for a naked function to be implemented in such a way that it does not break even the undefined ABI. For example, a `jmp` to a `const`. Therefore, this patch defines a new lint named `UNDEFINED_NAKED_FUNCTION_ABI` which contains just this single check. Unlike `UNSUPPORTED_NAKED_FUNCTIONS`, `UNDEFINED_NAKED_FUNCTION_ABI` will not be converted to an error in the future. rust-lang/rfcs#2774 rust-lang/rfcs#2972
2 parents 574d375 + 4968537 commit 798446f

File tree

4 files changed

+43
-16
lines changed

4 files changed

+43
-16
lines changed

compiler/rustc_lint_defs/src/builtin.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -2689,6 +2689,38 @@ declare_lint! {
26892689
"detects deprecation attributes with no effect",
26902690
}
26912691

2692+
declare_lint! {
2693+
/// The `undefined_naked_function_abi` lint detects naked function definitions that
2694+
/// either do not specify an ABI or specify the Rust ABI.
2695+
///
2696+
/// ### Example
2697+
///
2698+
/// ```rust
2699+
/// #![feature(naked_functions)]
2700+
/// #![feature(asm)]
2701+
///
2702+
/// #[naked]
2703+
/// pub fn default_abi() -> u32 {
2704+
/// unsafe { asm!("", options(noreturn)); }
2705+
/// }
2706+
///
2707+
/// #[naked]
2708+
/// pub extern "Rust" fn rust_abi() -> u32 {
2709+
/// unsafe { asm!("", options(noreturn)); }
2710+
/// }
2711+
/// ```
2712+
///
2713+
/// {{produces}}
2714+
///
2715+
/// ### Explanation
2716+
///
2717+
/// The Rust ABI is currently undefined. Therefore, naked functions should
2718+
/// specify a non-Rust ABI.
2719+
pub UNDEFINED_NAKED_FUNCTION_ABI,
2720+
Warn,
2721+
"undefined naked function ABI"
2722+
}
2723+
26922724
declare_lint! {
26932725
/// The `unsupported_naked_functions` lint detects naked function
26942726
/// definitions that are unsupported but were previously accepted.
@@ -2699,7 +2731,7 @@ declare_lint! {
26992731
/// #![feature(naked_functions)]
27002732
///
27012733
/// #[naked]
2702-
/// pub fn f() -> u32 {
2734+
/// pub extern "C" fn f() -> u32 {
27032735
/// 42
27042736
/// }
27052737
/// ```

compiler/rustc_passes/src/naked_functions.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_hir::intravisit::{ErasedMap, FnKind, NestedVisitorMap, Visitor};
77
use rustc_hir::{ExprKind, HirId, InlineAsmOperand, StmtKind};
88
use rustc_middle::ty::query::Providers;
99
use rustc_middle::ty::TyCtxt;
10+
use rustc_session::lint::builtin::UNDEFINED_NAKED_FUNCTION_ABI;
1011
use rustc_session::lint::builtin::UNSUPPORTED_NAKED_FUNCTIONS;
1112
use rustc_span::symbol::sym;
1213
use rustc_span::Span;
@@ -87,7 +88,7 @@ fn check_inline(tcx: TyCtxt<'_>, hir_id: HirId, attrs: &[Attribute]) {
8788
/// Checks that function uses non-Rust ABI.
8889
fn check_abi(tcx: TyCtxt<'_>, hir_id: HirId, abi: Abi, fn_ident_span: Span) {
8990
if abi == Abi::Rust {
90-
tcx.struct_span_lint_hir(UNSUPPORTED_NAKED_FUNCTIONS, hir_id, fn_ident_span, |lint| {
91+
tcx.struct_span_lint_hir(UNDEFINED_NAKED_FUNCTION_ABI, hir_id, fn_ident_span, |lint| {
9192
lint.build("Rust ABI is unsupported in naked functions").emit();
9293
});
9394
}

src/test/ui/asm/naked-functions.rs

-2
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,12 @@ unsafe extern "C" fn invalid_options_continued() {
134134
#[naked]
135135
pub unsafe fn default_abi() {
136136
//~^ WARN Rust ABI is unsupported in naked functions
137-
//~| WARN this was previously accepted
138137
asm!("", options(noreturn));
139138
}
140139

141140
#[naked]
142141
pub unsafe extern "Rust" fn rust_abi() {
143142
//~^ WARN Rust ABI is unsupported in naked functions
144-
//~| WARN this was previously accepted
145143
asm!("", options(noreturn));
146144
}
147145

src/test/ui/asm/naked-functions.stderr

+8-12
Original file line numberDiff line numberDiff line change
@@ -284,20 +284,16 @@ warning: Rust ABI is unsupported in naked functions
284284
LL | pub unsafe fn default_abi() {
285285
| ^^^^^^^^^^^
286286
|
287-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
288-
= note: for more information, see issue #32408 <https://github.com/rust-lang/rust/issues/32408>
287+
= note: `#[warn(undefined_naked_function_abi)]` on by default
289288

290289
warning: Rust ABI is unsupported in naked functions
291-
--> $DIR/naked-functions.rs:142:29
290+
--> $DIR/naked-functions.rs:141:29
292291
|
293292
LL | pub unsafe extern "Rust" fn rust_abi() {
294293
| ^^^^^^^^
295-
|
296-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
297-
= note: for more information, see issue #32408 <https://github.com/rust-lang/rust/issues/32408>
298294

299295
warning: naked functions cannot be inlined
300-
--> $DIR/naked-functions.rs:177:1
296+
--> $DIR/naked-functions.rs:175:1
301297
|
302298
LL | #[inline]
303299
| ^^^^^^^^^
@@ -306,7 +302,7 @@ LL | #[inline]
306302
= note: for more information, see issue #32408 <https://github.com/rust-lang/rust/issues/32408>
307303

308304
warning: naked functions cannot be inlined
309-
--> $DIR/naked-functions.rs:185:1
305+
--> $DIR/naked-functions.rs:183:1
310306
|
311307
LL | #[inline(always)]
312308
| ^^^^^^^^^^^^^^^^^
@@ -315,7 +311,7 @@ LL | #[inline(always)]
315311
= note: for more information, see issue #32408 <https://github.com/rust-lang/rust/issues/32408>
316312

317313
warning: naked functions cannot be inlined
318-
--> $DIR/naked-functions.rs:193:1
314+
--> $DIR/naked-functions.rs:191:1
319315
|
320316
LL | #[inline(never)]
321317
| ^^^^^^^^^^^^^^^^
@@ -324,7 +320,7 @@ LL | #[inline(never)]
324320
= note: for more information, see issue #32408 <https://github.com/rust-lang/rust/issues/32408>
325321

326322
warning: naked functions cannot be inlined
327-
--> $DIR/naked-functions.rs:201:1
323+
--> $DIR/naked-functions.rs:199:1
328324
|
329325
LL | #[inline]
330326
| ^^^^^^^^^
@@ -333,7 +329,7 @@ LL | #[inline]
333329
= note: for more information, see issue #32408 <https://github.com/rust-lang/rust/issues/32408>
334330

335331
warning: naked functions cannot be inlined
336-
--> $DIR/naked-functions.rs:204:1
332+
--> $DIR/naked-functions.rs:202:1
337333
|
338334
LL | #[inline(always)]
339335
| ^^^^^^^^^^^^^^^^^
@@ -342,7 +338,7 @@ LL | #[inline(always)]
342338
= note: for more information, see issue #32408 <https://github.com/rust-lang/rust/issues/32408>
343339

344340
warning: naked functions cannot be inlined
345-
--> $DIR/naked-functions.rs:207:1
341+
--> $DIR/naked-functions.rs:205:1
346342
|
347343
LL | #[inline(never)]
348344
| ^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)