@@ -743,10 +743,10 @@ enum FfiResult<'tcx> {
743743/// Determine if a type is sized or not, and wether it affects references/pointers/boxes to it
744744#[ derive( Clone , Copy ) ]
745745enum TypeSizedness {
746- /// sized type (pointers are C-compatible)
747- Sized ,
746+ /// type of definite size (pointers are C-compatible)
747+ Definite ,
748748 /// unsized type because it includes an opaque/foreign type (pointers are C-compatible)
749- UnsizedBecauseForeign ,
749+ UnsizedWithExternType ,
750750 /// unsized type for other reasons (slice, string, dyn Trait, closure, ...) (pointers are not C-compatible)
751751 UnsizedWithMetadata ,
752752}
@@ -757,13 +757,13 @@ fn get_type_sizedness<'tcx, 'a>(cx: &'a LateContext<'tcx>, ty: Ty<'tcx>) -> Type
757757 let tcx = cx. tcx ;
758758
759759 if ty. is_sized ( tcx, cx. typing_env ( ) ) {
760- TypeSizedness :: Sized
760+ TypeSizedness :: Definite
761761 } else {
762762 match ty. kind ( ) {
763763 ty:: Slice ( _) => TypeSizedness :: UnsizedWithMetadata ,
764764 ty:: Str => TypeSizedness :: UnsizedWithMetadata ,
765765 ty:: Dynamic ( ..) => TypeSizedness :: UnsizedWithMetadata ,
766- ty:: Foreign ( ..) => TypeSizedness :: UnsizedBecauseForeign ,
766+ ty:: Foreign ( ..) => TypeSizedness :: UnsizedWithExternType ,
767767 // While opaque types are checked for earlier, if a projection in a struct field
768768 // normalizes to an opaque type, then it will reach this branch.
769769 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
797797 . unwrap_or ( field_ty) ;
798798 match get_type_sizedness ( cx, field_ty) {
799799 s @ ( TypeSizedness :: UnsizedWithMetadata
800- | TypeSizedness :: UnsizedBecauseForeign ) => s,
801- TypeSizedness :: Sized => {
800+ | TypeSizedness :: UnsizedWithExternType ) => s,
801+ TypeSizedness :: Definite => {
802802 bug ! ( "failed to find the reason why struct `{:?}` is unsized" , ty)
803803 }
804804 }
@@ -816,16 +816,16 @@ fn get_type_sizedness<'tcx, 'a>(cx: &'a LateContext<'tcx>, ty: Ty<'tcx>) -> Type
816816 . unwrap_or ( field_ty) ;
817817 match get_type_sizedness ( cx, field_ty) {
818818 s @ ( TypeSizedness :: UnsizedWithMetadata
819- | TypeSizedness :: UnsizedBecauseForeign ) => s,
820- TypeSizedness :: Sized => {
819+ | TypeSizedness :: UnsizedWithExternType ) => s,
820+ TypeSizedness :: Definite => {
821821 bug ! ( "failed to find the reason why tuple `{:?}` is unsized" , ty)
822822 }
823823 }
824824 }
825- t => {
825+ ty => {
826826 bug ! (
827827 "we shouldn't be trying to determine if this is unsized for a reason or another: `{:?}`" ,
828- t
828+ ty
829829 )
830830 }
831831 }
@@ -1135,7 +1135,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
11351135 match * ty. kind ( ) {
11361136 ty:: Adt ( def, args) => {
11371137 if let Some ( inner_ty) = ty. boxed_ty ( ) {
1138- if let TypeSizedness :: UnsizedBecauseForeign | TypeSizedness :: Sized =
1138+ if let TypeSizedness :: UnsizedWithExternType | TypeSizedness :: Definite =
11391139 get_type_sizedness ( self . cx , inner_ty)
11401140 {
11411141 // discussion on declaration vs definition:
@@ -1340,24 +1340,27 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
13401340 }
13411341
13421342 ty:: RawPtr ( inner_ty, _) | ty:: Ref ( _, inner_ty, _) => {
1343- if let TypeSizedness :: UnsizedBecauseForeign | TypeSizedness :: Sized =
1343+ if let TypeSizedness :: UnsizedWithExternType | TypeSizedness :: Definite =
13441344 get_type_sizedness ( self . cx , inner_ty)
13451345 {
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
13501351 //
13511352 // The big question is: what does "ABI safety" mean? if you have something translated to a C pointer
13521353 // (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.
13561358 // 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.
13611364 if matches ! ( self . mode, CItemKind :: Definition ) {
13621365 return FfiSafe ;
13631366 } else if matches ! ( ty. kind( ) , ty:: RawPtr ( ..) )
0 commit comments