@@ -614,10 +614,10 @@ enum FfiResult<'tcx> {
614614/// Determine if a type is sized or not, and wether it affects references/pointers/boxes to it
615615#[ derive( Clone , Copy ) ]
616616enum TypeSizedness {
617- /// sized type (pointers are C-compatible)
618- Sized ,
617+ /// type of definite size (pointers are C-compatible)
618+ Definite ,
619619 /// unsized type because it includes an opaque/foreign type (pointers are C-compatible)
620- UnsizedBecauseForeign ,
620+ UnsizedWithExternType ,
621621 /// unsized type for other reasons (slice, string, dyn Trait, closure, ...) (pointers are not C-compatible)
622622 UnsizedWithMetadata ,
623623}
@@ -628,13 +628,13 @@ fn get_type_sizedness<'tcx, 'a>(cx: &'a LateContext<'tcx>, ty: Ty<'tcx>) -> Type
628628 let tcx = cx. tcx ;
629629
630630 if ty. is_sized ( tcx, cx. typing_env ( ) ) {
631- TypeSizedness :: Sized
631+ TypeSizedness :: Definite
632632 } else {
633633 match ty. kind ( ) {
634634 ty:: Slice ( _) => TypeSizedness :: UnsizedWithMetadata ,
635635 ty:: Str => TypeSizedness :: UnsizedWithMetadata ,
636636 ty:: Dynamic ( ..) => TypeSizedness :: UnsizedWithMetadata ,
637- ty:: Foreign ( ..) => TypeSizedness :: UnsizedBecauseForeign ,
637+ ty:: Foreign ( ..) => TypeSizedness :: UnsizedWithExternType ,
638638 // While opaque types are checked for earlier, if a projection in a struct field
639639 // normalizes to an opaque type, then it will reach this branch.
640640 ty:: Alias ( ty:: Opaque , ..) => todo ! ( "We... don't know enough about this type yet?" ) ,
@@ -668,8 +668,8 @@ fn get_type_sizedness<'tcx, 'a>(cx: &'a LateContext<'tcx>, ty: Ty<'tcx>) -> Type
668668 . unwrap_or ( field_ty) ;
669669 match get_type_sizedness ( cx, field_ty) {
670670 s @ ( TypeSizedness :: UnsizedWithMetadata
671- | TypeSizedness :: UnsizedBecauseForeign ) => s,
672- TypeSizedness :: Sized => {
671+ | TypeSizedness :: UnsizedWithExternType ) => s,
672+ TypeSizedness :: Definite => {
673673 bug ! ( "failed to find the reason why struct `{:?}` is unsized" , ty)
674674 }
675675 }
@@ -687,16 +687,16 @@ fn get_type_sizedness<'tcx, 'a>(cx: &'a LateContext<'tcx>, ty: Ty<'tcx>) -> Type
687687 . unwrap_or ( field_ty) ;
688688 match get_type_sizedness ( cx, field_ty) {
689689 s @ ( TypeSizedness :: UnsizedWithMetadata
690- | TypeSizedness :: UnsizedBecauseForeign ) => s,
691- TypeSizedness :: Sized => {
690+ | TypeSizedness :: UnsizedWithExternType ) => s,
691+ TypeSizedness :: Definite => {
692692 bug ! ( "failed to find the reason why tuple `{:?}` is unsized" , ty)
693693 }
694694 }
695695 }
696- t => {
696+ ty => {
697697 bug ! (
698698 "we shouldn't be trying to determine if this is unsized for a reason or another: `{:?}`" ,
699- t
699+ ty
700700 )
701701 }
702702 }
@@ -1006,7 +1006,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
10061006 match * ty. kind ( ) {
10071007 ty:: Adt ( def, args) => {
10081008 if let Some ( inner_ty) = ty. boxed_ty ( ) {
1009- if let TypeSizedness :: UnsizedBecauseForeign | TypeSizedness :: Sized =
1009+ if let TypeSizedness :: UnsizedWithExternType | TypeSizedness :: Definite =
10101010 get_type_sizedness ( self . cx , inner_ty)
10111011 {
10121012 // discussion on declaration vs definition:
@@ -1211,24 +1211,27 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
12111211 }
12121212
12131213 ty:: RawPtr ( inner_ty, _) | ty:: Ref ( _, inner_ty, _) => {
1214- if let TypeSizedness :: UnsizedBecauseForeign | TypeSizedness :: Sized =
1214+ if let TypeSizedness :: UnsizedWithExternType | TypeSizedness :: Definite =
12151215 get_type_sizedness ( self . cx , inner_ty)
12161216 {
1217- // there's a nuance on what this lint should do for function definitions
1218- // (`extern "C" fn fn_name(...) {...}`) versus declarations (`extern "C" {fn fn_name(...);}`).
1219- // (this is touched upon in https://github.com/rust-lang/rust/issues/66220
1220- // and https://github.com/rust-lang/rust/pull/72700)
1217+ // there's a nuance on what this lint should do for
1218+ // function definitions (`extern "C" fn fn_name(...) {...}`)
1219+ // versus declarations (`unsafe extern "C" {fn fn_name(...);}`).
1220+ // This is touched upon in https://github.com/rust-lang/rust/issues/66220
1221+ // and https://github.com/rust-lang/rust/pull/72700
12211222 //
12221223 // The big question is: what does "ABI safety" mean? if you have something translated to a C pointer
12231224 // (which has a stable layout) but points to FFI-unsafe type, is it safe?
1224- // on one hand, the function's ABI will match that of a similar C-declared function API,
1225- // on the other, dereferencing the pointer in not-rust will be painful.
1226- // In this code, the opinion is split between function declarations and function definitions.
1225+ // On one hand, the function's ABI will match that of a similar C-declared function API,
1226+ // on the other, dereferencing the pointer on the other side of the FFI boundary will be painful.
1227+ // In this code, the opinion on is split between function declarations and function definitions,
1228+ // with the idea that at least one side of the FFI boundary needs to treat the pointee as an opaque type.
12271229 // For declarations, we see this as unsafe, but for definitions, we see this as safe.
1228- // This is mostly because, for extern function declarations, the actual definition of the function is written somewhere else,
1229- // so the fact that a pointer's pointee should be treated as opaque to one side or the other can be explicitely written out.
1230- // For extern function definitions, however, both callee and some callers can be written in rust,
1231- // so developers need to keep as much typing information as possible.
1230+ //
1231+ // For extern function declarations, the actual definition of the function is written somewhere else,
1232+ // 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)
1233+ // For extern function definitions, however, in the case where the type is opaque caller-side, it is not opaque callee-side,
1234+ // and having the full type information is necessary to compile the function.
12321235 if matches ! ( self . mode, CItemKind :: Definition ) {
12331236 return FfiSafe ;
12341237 } else if matches ! ( ty. kind( ) , ty:: RawPtr ( ..) )
0 commit comments