1
1
use rustc_abi:: ExternAbi ;
2
2
use rustc_hir:: def:: DefKind ;
3
- use rustc_hir:: def_id:: { LocalDefId , LocalModDefId } ;
3
+ use rustc_hir:: def_id:: LocalModDefId ;
4
4
use rustc_hir:: intravisit:: Visitor ;
5
- use rustc_hir:: { self as hir, ExprKind , FnSig } ;
5
+ use rustc_hir:: { self as hir, ExprKind } ;
6
6
use rustc_middle:: hir:: nested_filter:: OnlyBodies ;
7
7
use rustc_middle:: query:: Providers ;
8
8
use rustc_middle:: ty:: { self , TyCtxt } ;
9
- use rustc_span:: { BytePos , sym} ;
9
+ use rustc_span:: sym;
10
10
11
- use crate :: errors:: {
12
- AbiCustomCall , AbiCustomClothedFunction , AbiCustomSafeForeignFunction , AbiCustomSafeFunction ,
13
- } ;
11
+ use crate :: errors:: { AbiCustomCall , AbiCustomClothedFunction } ;
14
12
15
13
pub ( crate ) fn provide ( providers : & mut Providers ) {
16
14
* providers = Providers { check_mod_custom_abi, ..* providers } ;
@@ -25,24 +23,6 @@ fn check_mod_custom_abi(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
25
23
for def_id in items. definitions ( ) {
26
24
let def_kind = tcx. def_kind ( def_id) ;
27
25
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
26
// An `extern "custom"` function cannot be a `const fn`, because `naked_asm!` cannot be
47
27
// evaluated at compile time, and `extern` blocks cannot declare `const fn` functions.
48
28
// 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) {
51
31
continue ;
52
32
}
53
33
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) {
55
35
hir:: Node :: Item ( hir:: Item {
56
36
kind : hir:: ItemKind :: Fn { sig, body : body_id, .. } ,
57
37
..
58
38
} )
59
39
| hir:: Node :: ImplItem ( hir:: ImplItem {
60
40
kind : hir:: ImplItemKind :: Fn ( sig, body_id) ,
61
41
..
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) ) ,
68
45
..
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) ) ,
79
47
_ => continue ,
80
48
} ;
81
49
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
+ }
95
58
}
96
59
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) ;
104
62
}
105
63
}
106
64
0 commit comments