Skip to content

Commit 8866af3

Browse files
committed
Add naked_functions_rustic_abi feature gate
1 parent 19cab6b commit 8866af3

17 files changed

+142
-107
lines changed

compiler/rustc_feature/src/unstable.rs

+2
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,8 @@ declare_features! (
566566
(incomplete, mut_ref, "1.79.0", Some(123076)),
567567
/// Allows using `#[naked]` on functions.
568568
(unstable, naked_functions, "1.9.0", Some(90957)),
569+
/// Allows using `#[naked]` on `extern "Rust"` functions.
570+
(unstable, naked_functions_rustic_abi, "CURRENT_RUSTC_VERSION", Some(138997)),
569571
/// Allows using `#[target_feature(enable = "...")]` on `#[naked]` on functions.
570572
(unstable, naked_functions_target_feature, "1.86.0", Some(138568)),
571573
/// Allows specifying the as-needed link modifier

compiler/rustc_lint/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,11 @@ fn register_builtins(store: &mut LintStore) {
604604
"converted into hard error, see issue #127323 \
605605
<https://github.com/rust-lang/rust/issues/127323> for more information",
606606
);
607+
store.register_removed(
608+
"undefined_naked_function_abi",
609+
"converted into hard error, see PR #139001 \
610+
<https://github.com/rust-lang/rust/issues/139001> for more information",
611+
);
607612
}
608613

609614
fn register_internals(store: &mut LintStore) {

compiler/rustc_lint_defs/src/builtin.rs

-34
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ declare_lint_pass! {
110110
UNCONDITIONAL_PANIC,
111111
UNCONDITIONAL_RECURSION,
112112
UNCOVERED_PARAM_IN_PROJECTION,
113-
UNDEFINED_NAKED_FUNCTION_ABI,
114113
UNEXPECTED_CFGS,
115114
UNFULFILLED_LINT_EXPECTATIONS,
116115
UNINHABITED_STATIC,
@@ -2830,39 +2829,6 @@ declare_lint! {
28302829
"detects deprecation attributes with no effect",
28312830
}
28322831

2833-
declare_lint! {
2834-
/// The `undefined_naked_function_abi` lint detects naked function definitions that
2835-
/// either do not specify an ABI or specify the Rust ABI.
2836-
///
2837-
/// ### Example
2838-
///
2839-
/// ```rust
2840-
/// #![feature(asm_experimental_arch, naked_functions)]
2841-
///
2842-
/// use std::arch::naked_asm;
2843-
///
2844-
/// #[naked]
2845-
/// pub fn default_abi() -> u32 {
2846-
/// unsafe { naked_asm!(""); }
2847-
/// }
2848-
///
2849-
/// #[naked]
2850-
/// pub extern "Rust" fn rust_abi() -> u32 {
2851-
/// unsafe { naked_asm!(""); }
2852-
/// }
2853-
/// ```
2854-
///
2855-
/// {{produces}}
2856-
///
2857-
/// ### Explanation
2858-
///
2859-
/// The Rust ABI is currently undefined. Therefore, naked functions should
2860-
/// specify a non-Rust ABI.
2861-
pub UNDEFINED_NAKED_FUNCTION_ABI,
2862-
Warn,
2863-
"undefined naked function ABI"
2864-
}
2865-
28662832
declare_lint! {
28672833
/// The `ineffective_unstable_trait_impl` lint detects `#[unstable]` attributes which are not used.
28682834
///

compiler/rustc_passes/messages.ftl

-3
Original file line numberDiff line numberDiff line change
@@ -738,9 +738,6 @@ passes_trait_impl_const_stable =
738738
passes_transparent_incompatible =
739739
transparent {$target} cannot have other repr hints
740740
741-
passes_undefined_naked_function_abi =
742-
Rust ABI is unsupported in naked functions
743-
744741
passes_unknown_external_lang_item =
745742
unknown external lang item: `{$lang_item}`
746743

compiler/rustc_passes/src/check_attr.rs

+15
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,21 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
607607
match target {
608608
Target::Fn
609609
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => {
610+
let fn_sig = self.tcx.hir_node(hir_id).fn_sig().unwrap();
611+
let abi = fn_sig.header.abi;
612+
if abi.is_rustic_abi() && !self.tcx.features().naked_functions_rustic_abi() {
613+
feature_err(
614+
&self.tcx.sess,
615+
sym::naked_functions_rustic_abi,
616+
fn_sig.span,
617+
format!(
618+
"`#[naked]` is currently unstable on `extern \"{}\"` functions",
619+
abi.as_str()
620+
),
621+
)
622+
.emit();
623+
}
624+
610625
for other_attr in attrs {
611626
// this covers "sugared doc comments" of the form `/// ...`
612627
// it does not cover `#[doc = "..."]`, which is handled below

compiler/rustc_passes/src/errors.rs

-4
Original file line numberDiff line numberDiff line change
@@ -1197,10 +1197,6 @@ pub(crate) struct UnlabeledCfInWhileCondition<'a> {
11971197
pub cf_type: &'a str,
11981198
}
11991199

1200-
#[derive(LintDiagnostic)]
1201-
#[diag(passes_undefined_naked_function_abi)]
1202-
pub(crate) struct UndefinedNakedFunctionAbi;
1203-
12041200
#[derive(Diagnostic)]
12051201
#[diag(passes_no_patterns)]
12061202
pub(crate) struct NoPatterns {

compiler/rustc_passes/src/naked_functions.rs

+6-27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Checks validity of naked functions.
22
3-
use rustc_abi::ExternAbi;
43
use rustc_hir as hir;
54
use rustc_hir::def::DefKind;
65
use rustc_hir::def_id::{LocalDefId, LocalModDefId};
@@ -10,12 +9,11 @@ use rustc_middle::hir::nested_filter::OnlyBodies;
109
use rustc_middle::query::Providers;
1110
use rustc_middle::span_bug;
1211
use rustc_middle::ty::TyCtxt;
13-
use rustc_session::lint::builtin::UNDEFINED_NAKED_FUNCTION_ABI;
1412
use rustc_span::{Span, sym};
1513

1614
use crate::errors::{
1715
NakedAsmOutsideNakedFn, NakedFunctionsAsmBlock, NakedFunctionsMustNakedAsm, NoPatterns,
18-
ParamsNotAllowed, UndefinedNakedFunctionAbi,
16+
ParamsNotAllowed,
1917
};
2018

2119
pub(crate) fn provide(providers: &mut Providers) {
@@ -29,26 +27,21 @@ fn check_mod_naked_functions(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
2927
continue;
3028
}
3129

32-
let (fn_header, body_id) = match tcx.hir_node_by_def_id(def_id) {
30+
let body = match tcx.hir_node_by_def_id(def_id) {
3331
hir::Node::Item(hir::Item {
34-
kind: hir::ItemKind::Fn { sig, body: body_id, .. },
35-
..
32+
kind: hir::ItemKind::Fn { body: body_id, .. }, ..
3633
})
3734
| hir::Node::TraitItem(hir::TraitItem {
38-
kind: hir::TraitItemKind::Fn(sig, hir::TraitFn::Provided(body_id)),
35+
kind: hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(body_id)),
3936
..
4037
})
4138
| hir::Node::ImplItem(hir::ImplItem {
42-
kind: hir::ImplItemKind::Fn(sig, body_id),
43-
..
44-
}) => (sig.header, *body_id),
39+
kind: hir::ImplItemKind::Fn(_, body_id), ..
40+
}) => tcx.hir_body(*body_id),
4541
_ => continue,
4642
};
4743

48-
let body = tcx.hir_body(body_id);
49-
5044
if tcx.has_attr(def_id, sym::naked) {
51-
check_abi(tcx, def_id, fn_header.abi);
5245
check_no_patterns(tcx, body.params);
5346
check_no_parameters_use(tcx, body);
5447
check_asm(tcx, def_id, body);
@@ -60,20 +53,6 @@ fn check_mod_naked_functions(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
6053
}
6154
}
6255

63-
/// Checks that function uses non-Rust ABI.
64-
fn check_abi(tcx: TyCtxt<'_>, def_id: LocalDefId, abi: ExternAbi) {
65-
if abi == ExternAbi::Rust {
66-
let hir_id = tcx.local_def_id_to_hir_id(def_id);
67-
let span = tcx.def_span(def_id);
68-
tcx.emit_node_span_lint(
69-
UNDEFINED_NAKED_FUNCTION_ABI,
70-
hir_id,
71-
span,
72-
UndefinedNakedFunctionAbi,
73-
);
74-
}
75-
}
76-
7756
/// Checks that parameters don't use patterns. Mirrors the checks for function declarations.
7857
fn check_no_patterns(tcx: TyCtxt<'_>, params: &[hir::Param<'_>]) {
7958
for param in params {

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,7 @@ symbols! {
13871387
naked,
13881388
naked_asm,
13891389
naked_functions,
1390+
naked_functions_rustic_abi,
13901391
naked_functions_target_feature,
13911392
name,
13921393
names,
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//@ revisions: x86_64 aarch64
2+
//
3+
//@[aarch64] only-aarch64
4+
//@[x86_64] only-x86_64
5+
//
6+
//@ build-pass
7+
//@ needs-asm-support
8+
9+
#![feature(naked_functions, naked_functions_rustic_abi, rust_cold_cc)]
10+
#![crate_type = "lib"]
11+
12+
use std::arch::{asm, naked_asm};
13+
14+
#[naked]
15+
pub unsafe fn rust_implicit() {
16+
naked_asm!("ret");
17+
}
18+
19+
#[naked]
20+
pub unsafe extern "Rust" fn rust_explicit() {
21+
naked_asm!("ret");
22+
}
23+
24+
#[naked]
25+
pub unsafe extern "rust-cold" fn rust_cold() {
26+
naked_asm!("ret");
27+
}
+4-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//@ needs-asm-support
22
//@ compile-flags: --test
33

4-
#![allow(undefined_naked_function_abi)]
54
#![feature(naked_functions)]
65
#![feature(test)]
76
#![crate_type = "lib"]
@@ -11,29 +10,29 @@ use std::arch::naked_asm;
1110
#[test]
1211
#[naked]
1312
//~^ ERROR [E0736]
14-
fn test_naked() {
13+
extern "C" fn test_naked() {
1514
unsafe { naked_asm!("") };
1615
}
1716

1817
#[should_panic]
1918
#[test]
2019
#[naked]
2120
//~^ ERROR [E0736]
22-
fn test_naked_should_panic() {
21+
extern "C" fn test_naked_should_panic() {
2322
unsafe { naked_asm!("") };
2423
}
2524

2625
#[ignore]
2726
#[test]
2827
#[naked]
2928
//~^ ERROR [E0736]
30-
fn test_naked_ignore() {
29+
extern "C" fn test_naked_ignore() {
3130
unsafe { naked_asm!("") };
3231
}
3332

3433
#[bench]
3534
#[naked]
3635
//~^ ERROR [E0736]
37-
fn bench_naked() {
36+
extern "C" fn bench_naked() {
3837
unsafe { naked_asm!("") };
3938
}

tests/ui/asm/naked-functions-testattrs.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
error[E0736]: cannot use `#[naked]` with testing attributes
2-
--> $DIR/naked-functions-testattrs.rs:12:1
2+
--> $DIR/naked-functions-testattrs.rs:11:1
33
|
44
LL | #[test]
55
| ------- function marked with testing attribute here
66
LL | #[naked]
77
| ^^^^^^^^ `#[naked]` is incompatible with testing attributes
88

99
error[E0736]: cannot use `#[naked]` with testing attributes
10-
--> $DIR/naked-functions-testattrs.rs:20:1
10+
--> $DIR/naked-functions-testattrs.rs:19:1
1111
|
1212
LL | #[test]
1313
| ------- function marked with testing attribute here
1414
LL | #[naked]
1515
| ^^^^^^^^ `#[naked]` is incompatible with testing attributes
1616

1717
error[E0736]: cannot use `#[naked]` with testing attributes
18-
--> $DIR/naked-functions-testattrs.rs:28:1
18+
--> $DIR/naked-functions-testattrs.rs:27:1
1919
|
2020
LL | #[test]
2121
| ------- function marked with testing attribute here
2222
LL | #[naked]
2323
| ^^^^^^^^ `#[naked]` is incompatible with testing attributes
2424

2525
error[E0736]: cannot use `#[naked]` with testing attributes
26-
--> $DIR/naked-functions-testattrs.rs:35:1
26+
--> $DIR/naked-functions-testattrs.rs:34:1
2727
|
2828
LL | #[bench]
2929
| -------- function marked with testing attribute here

tests/ui/asm/naked-functions.rs

-12
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,6 @@ unsafe extern "C" fn invalid_may_unwind() {
122122
//~^ ERROR the `may_unwind` option cannot be used with `naked_asm!`
123123
}
124124

125-
#[naked]
126-
pub unsafe fn default_abi() {
127-
//~^ WARN Rust ABI is unsupported in naked functions
128-
naked_asm!("");
129-
}
130-
131-
#[naked]
132-
pub unsafe fn rust_abi() {
133-
//~^ WARN Rust ABI is unsupported in naked functions
134-
naked_asm!("");
135-
}
136-
137125
#[naked]
138126
pub extern "C" fn valid_a<T>() -> T {
139127
unsafe {

tests/ui/asm/naked-functions.stderr

+4-18
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,19 @@ LL | naked_asm!("", options(may_unwind));
5353
| ^^^^^^^^^^ the `may_unwind` option is not meaningful for global-scoped inline assembly
5454

5555
error: this is a user specified error
56-
--> $DIR/naked-functions.rs:169:5
56+
--> $DIR/naked-functions.rs:157:5
5757
|
5858
LL | compile_error!("this is a user specified error")
5959
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6060

6161
error: this is a user specified error
62-
--> $DIR/naked-functions.rs:175:5
62+
--> $DIR/naked-functions.rs:163:5
6363
|
6464
LL | compile_error!("this is a user specified error");
6565
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6666

6767
error: asm template must be a string literal
68-
--> $DIR/naked-functions.rs:182:16
68+
--> $DIR/naked-functions.rs:170:16
6969
|
7070
LL | naked_asm!(invalid_syntax)
7171
| ^^^^^^^^^^^^^^
@@ -175,20 +175,6 @@ LL |
175175
LL | *&y
176176
| --- not allowed in naked functions
177177

178-
warning: Rust ABI is unsupported in naked functions
179-
--> $DIR/naked-functions.rs:126:1
180-
|
181-
LL | pub unsafe fn default_abi() {
182-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
183-
|
184-
= note: `#[warn(undefined_naked_function_abi)]` on by default
185-
186-
warning: Rust ABI is unsupported in naked functions
187-
--> $DIR/naked-functions.rs:132:1
188-
|
189-
LL | pub unsafe fn rust_abi() {
190-
| ^^^^^^^^^^^^^^^^^^^^^^^^
191-
192-
error: aborting due to 25 previous errors; 2 warnings emitted
178+
error: aborting due to 25 previous errors
193179

194180
For more information about this error, try `rustc --explain E0787`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//@ needs-asm-support
2+
//@ only-x86_64
3+
4+
#![feature(naked_functions, rust_cold_cc)]
5+
6+
use std::arch::naked_asm;
7+
8+
#[naked]
9+
pub unsafe fn rust_implicit() {
10+
//~^ ERROR `#[naked]` is currently unstable on `extern "Rust"` functions
11+
naked_asm!("ret");
12+
}
13+
14+
#[naked]
15+
pub unsafe extern "Rust" fn rust_explicit() {
16+
//~^ ERROR `#[naked]` is currently unstable on `extern "Rust"` functions
17+
naked_asm!("ret");
18+
}
19+
20+
#[naked]
21+
pub unsafe extern "rust-cold" fn rust_cold() {
22+
//~^ ERROR `#[naked]` is currently unstable on `extern "rust-cold"` functions
23+
naked_asm!("ret");
24+
}
25+
26+
fn main() {}

0 commit comments

Comments
 (0)