@@ -743,10 +743,10 @@ enum FfiResult<'tcx> {
743
743
/// Determine if a type is sized or not, and wether it affects references/pointers/boxes to it
744
744
#[ derive( Clone , Copy ) ]
745
745
enum TypeSizedness {
746
- /// sized type (pointers are C-compatible)
747
- Sized ,
746
+ /// type of definite size (pointers are C-compatible)
747
+ Definite ,
748
748
/// unsized type because it includes an opaque/foreign type (pointers are C-compatible)
749
- UnsizedBecauseForeign ,
749
+ UnsizedWithExternType ,
750
750
/// unsized type for other reasons (slice, string, dyn Trait, closure, ...) (pointers are not C-compatible)
751
751
UnsizedWithMetadata ,
752
752
}
@@ -757,13 +757,13 @@ fn get_type_sizedness<'tcx, 'a>(cx: &'a LateContext<'tcx>, ty: Ty<'tcx>) -> Type
757
757
let tcx = cx. tcx ;
758
758
759
759
if ty. is_sized ( tcx, cx. typing_env ( ) ) {
760
- TypeSizedness :: Sized
760
+ TypeSizedness :: Definite
761
761
} else {
762
762
match ty. kind ( ) {
763
763
ty:: Slice ( _) => TypeSizedness :: UnsizedWithMetadata ,
764
764
ty:: Str => TypeSizedness :: UnsizedWithMetadata ,
765
765
ty:: Dynamic ( ..) => TypeSizedness :: UnsizedWithMetadata ,
766
- ty:: Foreign ( ..) => TypeSizedness :: UnsizedBecauseForeign ,
766
+ ty:: Foreign ( ..) => TypeSizedness :: UnsizedWithExternType ,
767
767
// While opaque types are checked for earlier, if a projection in a struct field
768
768
// normalizes to an opaque type, then it will reach this branch.
769
769
ty:: Alias ( ty:: Opaque , ..) => todo ! ( "We... don't know enough about this type yet?" ) ,
@@ -797,8 +797,8 @@ fn get_type_sizedness<'tcx, 'a>(cx: &'a LateContext<'tcx>, ty: Ty<'tcx>) -> Type
797
797
. unwrap_or ( field_ty) ;
798
798
match get_type_sizedness ( cx, field_ty) {
799
799
s @ ( TypeSizedness :: UnsizedWithMetadata
800
- | TypeSizedness :: UnsizedBecauseForeign ) => s,
801
- TypeSizedness :: Sized => {
800
+ | TypeSizedness :: UnsizedWithExternType ) => s,
801
+ TypeSizedness :: Definite => {
802
802
bug ! ( "failed to find the reason why struct `{:?}` is unsized" , ty)
803
803
}
804
804
}
@@ -816,16 +816,16 @@ fn get_type_sizedness<'tcx, 'a>(cx: &'a LateContext<'tcx>, ty: Ty<'tcx>) -> Type
816
816
. unwrap_or ( field_ty) ;
817
817
match get_type_sizedness ( cx, field_ty) {
818
818
s @ ( TypeSizedness :: UnsizedWithMetadata
819
- | TypeSizedness :: UnsizedBecauseForeign ) => s,
820
- TypeSizedness :: Sized => {
819
+ | TypeSizedness :: UnsizedWithExternType ) => s,
820
+ TypeSizedness :: Definite => {
821
821
bug ! ( "failed to find the reason why tuple `{:?}` is unsized" , ty)
822
822
}
823
823
}
824
824
}
825
- t => {
825
+ ty => {
826
826
bug ! (
827
827
"we shouldn't be trying to determine if this is unsized for a reason or another: `{:?}`" ,
828
- t
828
+ ty
829
829
)
830
830
}
831
831
}
@@ -1135,7 +1135,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
1135
1135
match * ty. kind ( ) {
1136
1136
ty:: Adt ( def, args) => {
1137
1137
if let Some ( inner_ty) = ty. boxed_ty ( ) {
1138
- if let TypeSizedness :: UnsizedBecauseForeign | TypeSizedness :: Sized =
1138
+ if let TypeSizedness :: UnsizedWithExternType | TypeSizedness :: Definite =
1139
1139
get_type_sizedness ( self . cx , inner_ty)
1140
1140
{
1141
1141
// discussion on declaration vs definition:
@@ -1340,24 +1340,27 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
1340
1340
}
1341
1341
1342
1342
ty:: RawPtr ( inner_ty, _) | ty:: Ref ( _, inner_ty, _) => {
1343
- if let TypeSizedness :: UnsizedBecauseForeign | TypeSizedness :: Sized =
1343
+ if let TypeSizedness :: UnsizedWithExternType | TypeSizedness :: Definite =
1344
1344
get_type_sizedness ( self . cx , inner_ty)
1345
1345
{
1346
- // there's a nuance on what this lint should do for function definitions
1347
- // (`extern "C" fn fn_name(...) {...}`) versus declarations (`extern "C" {fn fn_name(...);}`).
1348
- // (this is touched upon in https://github.com/rust-lang/rust/issues/66220
1349
- // and https://github.com/rust-lang/rust/pull/72700)
1346
+ // there's a nuance on what this lint should do for
1347
+ // function definitions (`extern "C" fn fn_name(...) {...}`)
1348
+ // versus declarations (`unsafe extern "C" {fn fn_name(...);}`).
1349
+ // This is touched upon in https://github.com/rust-lang/rust/issues/66220
1350
+ // and https://github.com/rust-lang/rust/pull/72700
1350
1351
//
1351
1352
// The big question is: what does "ABI safety" mean? if you have something translated to a C pointer
1352
1353
// (which has a stable layout) but points to FFI-unsafe type, is it safe?
1353
- // on one hand, the function's ABI will match that of a similar C-declared function API,
1354
- // on the other, dereferencing the pointer in not-rust will be painful.
1355
- // In this code, the opinion is split between function declarations and function definitions.
1354
+ // On one hand, the function's ABI will match that of a similar C-declared function API,
1355
+ // on the other, dereferencing the pointer on the other side of the FFI boundary will be painful.
1356
+ // In this code, the opinion on is split between function declarations and function definitions,
1357
+ // with the idea that at least one side of the FFI boundary needs to treat the pointee as an opaque type.
1356
1358
// For declarations, we see this as unsafe, but for definitions, we see this as safe.
1357
- // This is mostly because, for extern function declarations, the actual definition of the function is written somewhere else,
1358
- // so the fact that a pointer's pointee should be treated as opaque to one side or the other can be explicitely written out.
1359
- // For extern function definitions, however, both callee and some callers can be written in rust,
1360
- // so developers need to keep as much typing information as possible.
1359
+ //
1360
+ // For extern function declarations, the actual definition of the function is written somewhere else,
1361
+ // meaning the declaration is free to express this opaqueness with an extern type (opaque caller-side) or a std::ffi::c_void (opaque callee-side)
1362
+ // For extern function definitions, however, in the case where the type is opaque caller-side, it is not opaque callee-side,
1363
+ // and having the full type information is necessary to compile the function.
1361
1364
if matches ! ( self . mode, CItemKind :: Definition ) {
1362
1365
return FfiSafe ;
1363
1366
} else if matches ! ( ty. kind( ) , ty:: RawPtr ( ..) )
0 commit comments