|
| 1 | +use rustc_abi::ExternAbi; |
| 2 | +use rustc_hir::def::DefKind; |
| 3 | +use rustc_hir::def_id::{LocalDefId, LocalModDefId}; |
| 4 | +use rustc_hir::intravisit::Visitor; |
| 5 | +use rustc_hir::{self as hir, ExprKind, FnSig}; |
| 6 | +use rustc_middle::hir::nested_filter::OnlyBodies; |
| 7 | +use rustc_middle::query::Providers; |
| 8 | +use rustc_middle::ty::{self, TyCtxt}; |
| 9 | +use rustc_span::{BytePos, sym}; |
| 10 | + |
| 11 | +use crate::errors::{ |
| 12 | + AbiCustomCall, AbiCustomClothedFunction, AbiCustomSafeForeignFunction, AbiCustomSafeFunction, |
| 13 | +}; |
| 14 | + |
| 15 | +pub(crate) fn provide(providers: &mut Providers) { |
| 16 | + *providers = Providers { check_mod_custom_abi, ..*providers }; |
| 17 | +} |
| 18 | + |
| 19 | +fn check_mod_custom_abi(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) { |
| 20 | + if !tcx.features().abi_custom() { |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + let items = tcx.hir_module_items(module_def_id); |
| 25 | + for def_id in items.definitions() { |
| 26 | + let def_kind = tcx.def_kind(def_id); |
| 27 | + |
| 28 | + // An `extern "custom"` function cannot be marked as `safe`. |
| 29 | + if let DefKind::ForeignMod = def_kind |
| 30 | + && let hir::Node::Item(item) = tcx.hir_node_by_def_id(def_id) |
| 31 | + && let hir::ItemKind::ForeignMod { abi: ExternAbi::Custom, items } = item.kind |
| 32 | + { |
| 33 | + for item in items { |
| 34 | + let hir_id = item.id.hir_id(); |
| 35 | + if let hir::Node::ForeignItem(foreign_item) = tcx.hir_node(hir_id) |
| 36 | + && let hir::ForeignItemKind::Fn(sig, _, _) = foreign_item.kind |
| 37 | + && sig.header.is_safe() |
| 38 | + { |
| 39 | + let len = "safe ".len() as u32; |
| 40 | + let safe_span = sig.span.shrink_to_lo().with_hi(sig.span.lo() + BytePos(len)); |
| 41 | + tcx.dcx().emit_err(AbiCustomSafeForeignFunction { span: sig.span, safe_span }); |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + // An `extern "custom"` function cannot be a `const fn`, because `naked_asm!` cannot be |
| 47 | + // evaluated at compile time, and `extern` blocks cannot declare `const fn` functions. |
| 48 | + // Therefore, to find all calls to `extern "custom"` functions, it suffices to traverse |
| 49 | + // all function bodies (i.e. we can skip `const` and `static` initializers). |
| 50 | + if !matches!(def_kind, DefKind::Fn | DefKind::AssocFn) { |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + let body = match tcx.hir_node_by_def_id(def_id) { |
| 55 | + hir::Node::Item(hir::Item { |
| 56 | + kind: hir::ItemKind::Fn { sig, body: body_id, .. }, |
| 57 | + .. |
| 58 | + }) |
| 59 | + | hir::Node::ImplItem(hir::ImplItem { |
| 60 | + kind: hir::ImplItemKind::Fn(sig, body_id), |
| 61 | + .. |
| 62 | + }) => { |
| 63 | + check_signature(tcx, def_id, sig, true); |
| 64 | + tcx.hir_body(*body_id) |
| 65 | + } |
| 66 | + hir::Node::TraitItem(hir::TraitItem { |
| 67 | + kind: hir::TraitItemKind::Fn(sig, trait_fn), |
| 68 | + .. |
| 69 | + }) => match trait_fn { |
| 70 | + hir::TraitFn::Required(_) => { |
| 71 | + check_signature(tcx, def_id, sig, false); |
| 72 | + continue; |
| 73 | + } |
| 74 | + hir::TraitFn::Provided(body_id) => { |
| 75 | + check_signature(tcx, def_id, sig, true); |
| 76 | + tcx.hir_body(*body_id) |
| 77 | + } |
| 78 | + }, |
| 79 | + _ => continue, |
| 80 | + }; |
| 81 | + |
| 82 | + let mut visitor = CheckCustomAbi { tcx }; |
| 83 | + visitor.visit_body(body); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +fn check_signature<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId, sig: &FnSig<'tcx>, has_body: bool) { |
| 88 | + if sig.header.abi == ExternAbi::Custom { |
| 89 | + // Function definitions that use `extern "custom"` must be naked functions. |
| 90 | + if has_body && !tcx.has_attr(def_id, sym::naked) { |
| 91 | + tcx.dcx().emit_err(AbiCustomClothedFunction { |
| 92 | + span: sig.span, |
| 93 | + naked_span: sig.span.shrink_to_lo(), |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + // Function definitions that use `extern "custom"` must unsafe. |
| 98 | + if sig.header.is_safe() { |
| 99 | + tcx.dcx().emit_err(AbiCustomSafeFunction { |
| 100 | + span: sig.span, |
| 101 | + unsafe_span: sig.span.shrink_to_lo(), |
| 102 | + }); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +struct CheckCustomAbi<'tcx> { |
| 108 | + tcx: TyCtxt<'tcx>, |
| 109 | +} |
| 110 | + |
| 111 | +impl<'tcx> Visitor<'tcx> for CheckCustomAbi<'tcx> { |
| 112 | + type NestedFilter = OnlyBodies; |
| 113 | + |
| 114 | + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { |
| 115 | + self.tcx |
| 116 | + } |
| 117 | + |
| 118 | + fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) { |
| 119 | + let opt_span_and_abi = match expr.kind { |
| 120 | + ExprKind::Call(fun, _) => { |
| 121 | + let fun_ty = self.tcx.typeck(fun.hir_id.owner.def_id).node_type(fun.hir_id); |
| 122 | + |
| 123 | + match *fun_ty.kind() { |
| 124 | + ty::FnDef(def_id, _) => { |
| 125 | + let sig = self.tcx.fn_sig(def_id).skip_binder().skip_binder(); |
| 126 | + Some((expr.span, sig.abi)) |
| 127 | + } |
| 128 | + ty::FnPtr(_, header) => Some((expr.span, header.abi)), |
| 129 | + _ => None, |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + ExprKind::MethodCall(_, receiver, _, span) => { |
| 134 | + let opt_def_id = self |
| 135 | + .tcx |
| 136 | + .typeck(receiver.hir_id.owner.def_id) |
| 137 | + .type_dependent_def_id(expr.hir_id); |
| 138 | + |
| 139 | + opt_def_id.map(|def_id| { |
| 140 | + let sig = self.tcx.fn_sig(def_id).skip_binder().skip_binder(); |
| 141 | + (span, sig.abi) |
| 142 | + }) |
| 143 | + } |
| 144 | + _ => None, |
| 145 | + }; |
| 146 | + |
| 147 | + if let Some((span, ExternAbi::Custom)) = opt_span_and_abi { |
| 148 | + self.tcx.dcx().emit_err(AbiCustomCall { span }); |
| 149 | + } |
| 150 | + |
| 151 | + hir::intravisit::walk_expr(self, expr); |
| 152 | + } |
| 153 | +} |
0 commit comments