@@ -1381,43 +1381,36 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
1381
1381
1382
1382
/// Check if a function's argument types and result type are "ffi-safe".
1383
1383
///
1384
- /// Argument types and the result type are checked for functions with external ABIs.
1385
- /// For functions with internal ABIs, argument types and the result type are walked to find
1386
- /// fn-ptr types that have external ABIs, as these still need checked.
1387
- fn check_maybe_foreign_fn (
1388
- & mut self ,
1389
- abi : SpecAbi ,
1390
- def_id : LocalDefId ,
1391
- decl : & ' tcx hir:: FnDecl < ' _ > ,
1392
- ) {
1384
+ /// For a external ABI function, argument types and the result type are walked to find fn-ptr
1385
+ /// types that have external ABIs, as these still need checked.
1386
+ fn check_fn ( & mut self , def_id : LocalDefId , decl : & ' tcx hir:: FnDecl < ' _ > ) {
1393
1387
let sig = self . cx . tcx . fn_sig ( def_id) . subst_identity ( ) ;
1394
1388
let sig = self . cx . tcx . erase_late_bound_regions ( sig) ;
1395
1389
1396
- let is_internal_abi = self . is_internal_abi ( abi) ;
1397
- let check_ty = |this : & mut ImproperCTypesVisitor < ' a , ' tcx > ,
1398
- hir_ty : & ' tcx hir:: Ty < ' _ > ,
1399
- ty : Ty < ' tcx > ,
1400
- is_return_type : bool | {
1401
- // If this function has an external ABI, then its arguments and return type should be
1402
- // checked..
1403
- if !is_internal_abi {
1404
- this. check_type_for_ffi_and_report_errors ( hir_ty. span , ty, false , is_return_type) ;
1405
- return ;
1390
+ for ( input_ty, input_hir) in iter:: zip ( sig. inputs ( ) , decl. inputs ) {
1391
+ for ( fn_ptr_ty, span) in self . find_fn_ptr_ty_with_external_abi ( input_hir, * input_ty) {
1392
+ self . check_type_for_ffi_and_report_errors ( span, fn_ptr_ty, false , false ) ;
1406
1393
}
1394
+ }
1407
1395
1408
- // ..but if this function has an internal ABI, then search the argument or return type
1409
- // for any fn-ptr types with external ABI, which should be checked..
1410
- for ( fn_ptr_ty, span) in this. find_fn_ptr_ty_with_external_abi ( hir_ty, ty) {
1411
- this. check_type_for_ffi_and_report_errors ( span, fn_ptr_ty, false , is_return_type) ;
1396
+ if let hir:: FnRetTy :: Return ( ref ret_hir) = decl. output {
1397
+ for ( fn_ptr_ty, span) in self . find_fn_ptr_ty_with_external_abi ( ret_hir, sig. output ( ) ) {
1398
+ self . check_type_for_ffi_and_report_errors ( span, fn_ptr_ty, false , true ) ;
1412
1399
}
1413
- } ;
1400
+ }
1401
+ }
1402
+
1403
+ /// Check if a function's argument types and result type are "ffi-safe".
1404
+ fn check_foreign_fn ( & mut self , def_id : LocalDefId , decl : & ' tcx hir:: FnDecl < ' _ > ) {
1405
+ let sig = self . cx . tcx . fn_sig ( def_id) . subst_identity ( ) ;
1406
+ let sig = self . cx . tcx . erase_late_bound_regions ( sig) ;
1414
1407
1415
1408
for ( input_ty, input_hir) in iter:: zip ( sig. inputs ( ) , decl. inputs ) {
1416
- check_ty ( self , input_hir, * input_ty, false ) ;
1409
+ self . check_type_for_ffi_and_report_errors ( input_hir. span , * input_ty, false , false ) ;
1417
1410
}
1418
1411
1419
1412
if let hir:: FnRetTy :: Return ( ref ret_hir) = decl. output {
1420
- check_ty ( self , ret_hir, sig. output ( ) , true ) ;
1413
+ self . check_type_for_ffi_and_report_errors ( ret_hir. span , sig. output ( ) , false , true ) ;
1421
1414
}
1422
1415
}
1423
1416
@@ -1486,12 +1479,13 @@ impl<'tcx> LateLintPass<'tcx> for ImproperCTypesDeclarations {
1486
1479
let abi = cx. tcx . hir ( ) . get_foreign_abi ( it. hir_id ( ) ) ;
1487
1480
1488
1481
match it. kind {
1489
- hir:: ForeignItemKind :: Fn ( ref decl, _, _) => {
1490
- vis. check_maybe_foreign_fn ( abi , it. owner_id . def_id , decl) ;
1482
+ hir:: ForeignItemKind :: Fn ( ref decl, _, _) if !vis . is_internal_abi ( abi ) => {
1483
+ vis. check_foreign_fn ( it. owner_id . def_id , decl) ;
1491
1484
}
1492
1485
hir:: ForeignItemKind :: Static ( ref ty, _) if !vis. is_internal_abi ( abi) => {
1493
1486
vis. check_foreign_static ( it. owner_id , ty. span ) ;
1494
1487
}
1488
+ hir:: ForeignItemKind :: Fn ( ref decl, _, _) => vis. check_fn ( it. owner_id . def_id , decl) ,
1495
1489
hir:: ForeignItemKind :: Static ( ..) | hir:: ForeignItemKind :: Type => ( ) ,
1496
1490
}
1497
1491
}
@@ -1574,7 +1568,11 @@ impl<'tcx> LateLintPass<'tcx> for ImproperCTypesDefinitions {
1574
1568
} ;
1575
1569
1576
1570
let mut vis = ImproperCTypesVisitor { cx, mode : CItemKind :: Definition } ;
1577
- vis. check_maybe_foreign_fn ( abi, id, decl) ;
1571
+ if vis. is_internal_abi ( abi) {
1572
+ vis. check_fn ( id, decl) ;
1573
+ } else {
1574
+ vis. check_foreign_fn ( id, decl) ;
1575
+ }
1578
1576
}
1579
1577
}
1580
1578
0 commit comments