Skip to content

Commit 9b8cfc1

Browse files
committed
Auto merge of #98489 - cjgillot:naked-nohir, r=davidtwco,tmiasko
Only fetch HIR for naked functions that have the attribute.
2 parents 6a11cad + 6eb0c89 commit 9b8cfc1

File tree

2 files changed

+86
-111
lines changed

2 files changed

+86
-111
lines changed

compiler/rustc_passes/src/naked_functions.rs

+41-53
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,71 @@
11
//! Checks validity of naked functions.
22
3-
use rustc_ast::{Attribute, InlineAsmOptions};
3+
use rustc_ast::InlineAsmOptions;
44
use rustc_errors::{struct_span_err, Applicability};
55
use rustc_hir as hir;
6+
use rustc_hir::def::DefKind;
67
use rustc_hir::def_id::LocalDefId;
7-
use rustc_hir::intravisit::{FnKind, Visitor};
8-
use rustc_hir::{ExprKind, HirId, InlineAsmOperand, StmtKind};
8+
use rustc_hir::intravisit::Visitor;
9+
use rustc_hir::{ExprKind, InlineAsmOperand, StmtKind};
910
use rustc_middle::ty::query::Providers;
1011
use rustc_middle::ty::TyCtxt;
1112
use rustc_session::lint::builtin::UNDEFINED_NAKED_FUNCTION_ABI;
1213
use rustc_span::symbol::sym;
1314
use rustc_span::Span;
1415
use rustc_target::spec::abi::Abi;
1516

16-
fn check_mod_naked_functions(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
17-
tcx.hir().visit_item_likes_in_module(module_def_id, &mut CheckNakedFunctions { tcx });
18-
}
19-
2017
pub(crate) fn provide(providers: &mut Providers) {
2118
*providers = Providers { check_mod_naked_functions, ..*providers };
2219
}
2320

24-
struct CheckNakedFunctions<'tcx> {
25-
tcx: TyCtxt<'tcx>,
26-
}
27-
28-
impl<'tcx> Visitor<'tcx> for CheckNakedFunctions<'tcx> {
29-
fn visit_fn(
30-
&mut self,
31-
fk: FnKind<'_>,
32-
_fd: &'tcx hir::FnDecl<'tcx>,
33-
body_id: hir::BodyId,
34-
span: Span,
35-
hir_id: HirId,
36-
) {
37-
let ident_span;
38-
let fn_header;
39-
40-
match fk {
41-
FnKind::Closure => {
42-
// Closures with a naked attribute are rejected during attribute
43-
// check. Don't validate them any further.
44-
return;
45-
}
46-
FnKind::ItemFn(ident, _, ref header, ..) => {
47-
ident_span = ident.span;
48-
fn_header = header;
49-
}
50-
51-
FnKind::Method(ident, ref sig, ..) => {
52-
ident_span = ident.span;
53-
fn_header = &sig.header;
54-
}
21+
fn check_mod_naked_functions(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
22+
let items = tcx.hir_module_items(module_def_id);
23+
for def_id in items.definitions() {
24+
if !matches!(tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn) {
25+
continue;
5526
}
5627

57-
let attrs = self.tcx.hir().attrs(hir_id);
58-
let naked = attrs.iter().any(|attr| attr.has_name(sym::naked));
59-
if naked {
60-
let body = self.tcx.hir().body(body_id);
61-
check_abi(self.tcx, hir_id, fn_header.abi, ident_span);
62-
check_no_patterns(self.tcx, body.params);
63-
check_no_parameters_use(self.tcx, body);
64-
check_asm(self.tcx, body, span);
65-
check_inline(self.tcx, attrs);
28+
let naked = tcx.has_attr(def_id.to_def_id(), sym::naked);
29+
if !naked {
30+
continue;
6631
}
32+
33+
let (fn_header, body_id) = match tcx.hir().get_by_def_id(def_id) {
34+
hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, _, body_id), .. })
35+
| hir::Node::TraitItem(hir::TraitItem {
36+
kind: hir::TraitItemKind::Fn(sig, hir::TraitFn::Provided(body_id)),
37+
..
38+
})
39+
| hir::Node::ImplItem(hir::ImplItem {
40+
kind: hir::ImplItemKind::Fn(sig, body_id),
41+
..
42+
}) => (sig.header, *body_id),
43+
_ => continue,
44+
};
45+
46+
let body = tcx.hir().body(body_id);
47+
check_abi(tcx, def_id, fn_header.abi);
48+
check_no_patterns(tcx, body.params);
49+
check_no_parameters_use(tcx, body);
50+
check_asm(tcx, def_id, body);
51+
check_inline(tcx, def_id);
6752
}
6853
}
6954

7055
/// Check that the function isn't inlined.
71-
fn check_inline(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
72-
for attr in attrs.iter().filter(|attr| attr.has_name(sym::inline)) {
56+
fn check_inline(tcx: TyCtxt<'_>, def_id: LocalDefId) {
57+
let attrs = tcx.get_attrs(def_id.to_def_id(), sym::inline);
58+
for attr in attrs {
7359
tcx.sess.struct_span_err(attr.span, "naked functions cannot be inlined").emit();
7460
}
7561
}
7662

7763
/// Checks that function uses non-Rust ABI.
78-
fn check_abi(tcx: TyCtxt<'_>, hir_id: HirId, abi: Abi, fn_ident_span: Span) {
64+
fn check_abi(tcx: TyCtxt<'_>, def_id: LocalDefId, abi: Abi) {
7965
if abi == Abi::Rust {
80-
tcx.struct_span_lint_hir(UNDEFINED_NAKED_FUNCTION_ABI, hir_id, fn_ident_span, |lint| {
66+
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
67+
let span = tcx.def_span(def_id);
68+
tcx.struct_span_lint_hir(UNDEFINED_NAKED_FUNCTION_ABI, hir_id, span, |lint| {
8169
lint.build("Rust ABI is unsupported in naked functions").emit();
8270
});
8371
}
@@ -141,15 +129,15 @@ impl<'tcx> Visitor<'tcx> for CheckParameters<'tcx> {
141129
}
142130

143131
/// Checks that function body contains a single inline assembly block.
144-
fn check_asm<'tcx>(tcx: TyCtxt<'tcx>, body: &'tcx hir::Body<'tcx>, fn_span: Span) {
132+
fn check_asm<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId, body: &'tcx hir::Body<'tcx>) {
145133
let mut this = CheckInlineAssembly { tcx, items: Vec::new() };
146134
this.visit_body(body);
147135
if let [(ItemKind::Asm | ItemKind::Err, _)] = this.items[..] {
148136
// Ok.
149137
} else {
150138
let mut diag = struct_span_err!(
151139
tcx.sess,
152-
fn_span,
140+
tcx.def_span(def_id),
153141
E0787,
154142
"naked functions must contain a single asm block"
155143
);

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

+45-58
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,11 @@ LL | a + 1
5757
error[E0787]: naked functions must contain a single asm block
5858
--> $DIR/naked-functions.rs:33:1
5959
|
60-
LL | / pub unsafe extern "C" fn inc(a: u32) -> u32 {
61-
LL | |
62-
LL | | a + 1
63-
| | ----- non-asm is unsupported in naked functions
64-
LL | |
65-
LL | | }
66-
| |_^
60+
LL | pub unsafe extern "C" fn inc(a: u32) -> u32 {
61+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
62+
LL |
63+
LL | a + 1
64+
| ----- non-asm is unsupported in naked functions
6765

6866
error: referencing function parameters is not allowed in naked functions
6967
--> $DIR/naked-functions.rs:42:31
@@ -82,12 +80,11 @@ LL | asm!("/* {0} */", in(reg) a, options(noreturn));
8280
error[E0787]: naked functions must contain a single asm block
8381
--> $DIR/naked-functions.rs:48:1
8482
|
85-
LL | / pub unsafe extern "C" fn inc_closure(a: u32) -> u32 {
86-
LL | |
87-
LL | | (|| a + 1)()
88-
| | ------------ non-asm is unsupported in naked functions
89-
LL | | }
90-
| |_^
83+
LL | pub unsafe extern "C" fn inc_closure(a: u32) -> u32 {
84+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
85+
LL |
86+
LL | (|| a + 1)()
87+
| ------------ non-asm is unsupported in naked functions
9188

9289
error[E0787]: only `const` and `sym` operands are supported in naked functions
9390
--> $DIR/naked-functions.rs:65:10
@@ -124,30 +121,25 @@ LL | sym G, options(noreturn),
124121
error[E0787]: naked functions must contain a single asm block
125122
--> $DIR/naked-functions.rs:54:1
126123
|
127-
LL | / pub unsafe extern "C" fn unsupported_operands() {
128-
LL | |
129-
LL | | let mut a = 0usize;
130-
| | ------------------- non-asm is unsupported in naked functions
131-
LL | | let mut b = 0usize;
132-
| | ------------------- non-asm is unsupported in naked functions
133-
LL | | let mut c = 0usize;
134-
| | ------------------- non-asm is unsupported in naked functions
135-
LL | | let mut d = 0usize;
136-
| | ------------------- non-asm is unsupported in naked functions
137-
LL | | let mut e = 0usize;
138-
| | ------------------- non-asm is unsupported in naked functions
139-
... |
140-
LL | | );
141-
LL | | }
142-
| |_^
124+
LL | pub unsafe extern "C" fn unsupported_operands() {
125+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
126+
LL |
127+
LL | let mut a = 0usize;
128+
| ------------------- non-asm is unsupported in naked functions
129+
LL | let mut b = 0usize;
130+
| ------------------- non-asm is unsupported in naked functions
131+
LL | let mut c = 0usize;
132+
| ------------------- non-asm is unsupported in naked functions
133+
LL | let mut d = 0usize;
134+
| ------------------- non-asm is unsupported in naked functions
135+
LL | let mut e = 0usize;
136+
| ------------------- non-asm is unsupported in naked functions
143137

144138
error[E0787]: naked functions must contain a single asm block
145139
--> $DIR/naked-functions.rs:77:1
146140
|
147-
LL | / pub extern "C" fn missing_assembly() {
148-
LL | |
149-
LL | | }
150-
| |_^
141+
LL | pub extern "C" fn missing_assembly() {
142+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
151143

152144
error[E0787]: asm in naked functions must use `noreturn` option
153145
--> $DIR/naked-functions.rs:84:5
@@ -185,20 +177,17 @@ LL | asm!("", options(noreturn));
185177
error[E0787]: naked functions must contain a single asm block
186178
--> $DIR/naked-functions.rs:82:1
187179
|
188-
LL | / pub extern "C" fn too_many_asm_blocks() {
189-
LL | |
190-
LL | | asm!("");
191-
LL | |
192-
LL | | asm!("");
193-
| | -------- multiple asm blocks are unsupported in naked functions
194-
LL | |
195-
LL | | asm!("");
196-
| | -------- multiple asm blocks are unsupported in naked functions
197-
LL | |
198-
LL | | asm!("", options(noreturn));
199-
| | --------------------------- multiple asm blocks are unsupported in naked functions
200-
LL | | }
201-
| |_^
180+
LL | pub extern "C" fn too_many_asm_blocks() {
181+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
182+
...
183+
LL | asm!("");
184+
| -------- multiple asm blocks are unsupported in naked functions
185+
LL |
186+
LL | asm!("");
187+
| -------- multiple asm blocks are unsupported in naked functions
188+
LL |
189+
LL | asm!("", options(noreturn));
190+
| --------------------------- multiple asm blocks are unsupported in naked functions
202191

203192
error: referencing function parameters is not allowed in naked functions
204193
--> $DIR/naked-functions.rs:97:11
@@ -211,13 +200,11 @@ LL | *&y
211200
error[E0787]: naked functions must contain a single asm block
212201
--> $DIR/naked-functions.rs:95:5
213202
|
214-
LL | / pub extern "C" fn inner(y: usize) -> usize {
215-
LL | |
216-
LL | | *&y
217-
| | --- non-asm is unsupported in naked functions
218-
LL | |
219-
LL | | }
220-
| |_____^
203+
LL | pub extern "C" fn inner(y: usize) -> usize {
204+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
205+
LL |
206+
LL | *&y
207+
| --- non-asm is unsupported in naked functions
221208

222209
error[E0787]: asm options unsupported in naked functions: `nomem`, `preserves_flags`
223210
--> $DIR/naked-functions.rs:105:5
@@ -249,18 +236,18 @@ LL | asm!("", options(noreturn, may_unwind));
249236
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
250237

251238
warning: Rust ABI is unsupported in naked functions
252-
--> $DIR/naked-functions.rs:124:15
239+
--> $DIR/naked-functions.rs:124:1
253240
|
254241
LL | pub unsafe fn default_abi() {
255-
| ^^^^^^^^^^^
242+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
256243
|
257244
= note: `#[warn(undefined_naked_function_abi)]` on by default
258245

259246
warning: Rust ABI is unsupported in naked functions
260-
--> $DIR/naked-functions.rs:130:15
247+
--> $DIR/naked-functions.rs:130:1
261248
|
262249
LL | pub unsafe fn rust_abi() {
263-
| ^^^^^^^^
250+
| ^^^^^^^^^^^^^^^^^^^^^^^^
264251

265252
error: naked functions cannot be inlined
266253
--> $DIR/naked-functions.rs:170:1

0 commit comments

Comments
 (0)