Skip to content

Commit 845009d

Browse files
committed
Only fetch HIR for naked functions that have the attribute.
1 parent 792bc5a commit 845009d

File tree

1 file changed

+41
-53
lines changed

1 file changed

+41
-53
lines changed

Diff for: 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
);

0 commit comments

Comments
 (0)