11use rustc_abi:: ExternAbi ;
22use rustc_hir:: def:: DefKind ;
3- use rustc_hir:: def_id:: { LocalDefId , LocalModDefId } ;
3+ use rustc_hir:: def_id:: LocalModDefId ;
44use rustc_hir:: intravisit:: Visitor ;
5- use rustc_hir:: { self as hir, ExprKind , FnSig } ;
5+ use rustc_hir:: { self as hir, ExprKind } ;
66use rustc_middle:: hir:: nested_filter:: OnlyBodies ;
77use rustc_middle:: query:: Providers ;
88use rustc_middle:: ty:: { self , TyCtxt } ;
9- use rustc_span:: { BytePos , sym} ;
9+ use rustc_span:: sym;
1010
11- use crate :: errors:: {
12- AbiCustomCall , AbiCustomClothedFunction , AbiCustomSafeForeignFunction , AbiCustomSafeFunction ,
13- } ;
11+ use crate :: errors:: { AbiCustomCall , AbiCustomClothedFunction } ;
1412
1513pub ( crate ) fn provide ( providers : & mut Providers ) {
1614 * providers = Providers { check_mod_custom_abi, ..* providers } ;
@@ -25,24 +23,6 @@ fn check_mod_custom_abi(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
2523 for def_id in items. definitions ( ) {
2624 let def_kind = tcx. def_kind ( def_id) ;
2725
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-
4626 // An `extern "custom"` function cannot be a `const fn`, because `naked_asm!` cannot be
4727 // evaluated at compile time, and `extern` blocks cannot declare `const fn` functions.
4828 // Therefore, to find all calls to `extern "custom"` functions, it suffices to traverse
@@ -51,56 +31,34 @@ fn check_mod_custom_abi(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
5131 continue ;
5232 }
5333
54- let body = match tcx. hir_node_by_def_id ( def_id) {
34+ let ( sig , body) = match tcx. hir_node_by_def_id ( def_id) {
5535 hir:: Node :: Item ( hir:: Item {
5636 kind : hir:: ItemKind :: Fn { sig, body : body_id, .. } ,
5737 ..
5838 } )
5939 | hir:: Node :: ImplItem ( hir:: ImplItem {
6040 kind : hir:: ImplItemKind :: Fn ( sig, body_id) ,
6141 ..
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) ,
42+ } )
43+ | hir:: Node :: TraitItem ( hir:: TraitItem {
44+ kind : hir:: TraitItemKind :: Fn ( sig, hir:: TraitFn :: Provided ( body_id) ) ,
6845 ..
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- } ,
46+ } ) => ( sig, tcx. hir_body ( * body_id) ) ,
7947 _ => continue ,
8048 } ;
8149
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- } ) ;
50+ if sig. header . abi == ExternAbi :: Custom {
51+ // Function definitions that use `extern "custom"` must be naked functions.
52+ if !tcx. has_attr ( def_id, sym:: naked) {
53+ tcx. dcx ( ) . emit_err ( AbiCustomClothedFunction {
54+ span : sig. span ,
55+ naked_span : sig. span . shrink_to_lo ( ) ,
56+ } ) ;
57+ }
9558 }
9659
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- }
60+ let mut visitor = CheckCustomAbi { tcx } ;
61+ visitor. visit_body ( body) ;
10462 }
10563}
10664
0 commit comments