@@ -878,39 +878,39 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
878
878
) -> FfiResult < ' tcx > {
879
879
use FfiResult :: * ;
880
880
881
- if def. repr ( ) . transparent ( ) {
881
+ let transparent_safety = def. repr ( ) . transparent ( ) . then ( || {
882
882
// Can assume that at most one field is not a ZST, so only check
883
883
// that field's type for FFI-safety.
884
884
if let Some ( field) = transparent_newtype_field ( self . cx . tcx , variant) {
885
- self . check_field_type_for_ffi ( cache, field, substs)
885
+ return self . check_field_type_for_ffi ( cache, field, substs) ;
886
886
} else {
887
887
// All fields are ZSTs; this means that the type should behave
888
- // like (), which is FFI-unsafe
888
+ // like (), which is FFI-unsafe... except if all fields are PhantomData,
889
+ // which is tested for below
889
890
FfiUnsafe { ty, reason : fluent:: lint_improper_ctypes_struct_zst, help : None }
890
891
}
891
- } else {
892
- // We can't completely trust repr(C) markings; make sure the fields are
893
- // actually safe.
894
- let mut all_phantom = !variant. fields . is_empty ( ) ;
895
- for field in & variant. fields {
896
- match self . check_field_type_for_ffi ( cache, & field, substs) {
897
- FfiSafe => {
898
- all_phantom = false ;
899
- }
900
- FfiPhantom ( ..) if def. is_enum ( ) => {
901
- return FfiUnsafe {
902
- ty,
903
- reason : fluent:: lint_improper_ctypes_enum_phantomdata,
904
- help : None ,
905
- } ;
906
- }
907
- FfiPhantom ( ..) => { }
908
- r => return r,
892
+ } ) ;
893
+ // We can't completely trust repr(C) markings; make sure the fields are
894
+ // actually safe.
895
+ let mut all_phantom = !variant. fields . is_empty ( ) ;
896
+ for field in & variant. fields {
897
+ match self . check_field_type_for_ffi ( cache, & field, substs) {
898
+ FfiSafe => {
899
+ all_phantom = false ;
909
900
}
901
+ FfiPhantom ( ..) if !def. repr ( ) . transparent ( ) && def. is_enum ( ) => {
902
+ return FfiUnsafe {
903
+ ty,
904
+ reason : fluent:: lint_improper_ctypes_enum_phantomdata,
905
+ help : None ,
906
+ } ;
907
+ }
908
+ FfiPhantom ( ..) => { }
909
+ r => return transparent_safety. unwrap_or ( r) ,
910
910
}
911
-
912
- if all_phantom { FfiPhantom ( ty) } else { FfiSafe }
913
911
}
912
+
913
+ if all_phantom { FfiPhantom ( ty) } else { transparent_safety. unwrap_or ( FfiSafe ) }
914
914
}
915
915
916
916
/// Checks if the given type is "ffi-safe" (has a stable, well-defined
0 commit comments