@@ -815,8 +815,7 @@ pub fn transparent_newtype_field<'a, 'tcx>(
815815}
816816
817817/// Is type known to be non-null?
818- fn ty_is_known_nonnull < ' tcx > ( cx : & LateContext < ' tcx > , ty : Ty < ' tcx > , mode : CItemKind ) -> bool {
819- let tcx = cx. tcx ;
818+ fn ty_is_known_nonnull < ' tcx > ( tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > , mode : CItemKind ) -> bool {
820819 match ty. kind ( ) {
821820 ty:: FnPtr ( _) => true ,
822821 ty:: Ref ( ..) => true ,
@@ -835,24 +834,21 @@ fn ty_is_known_nonnull<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, mode: CItemKi
835834
836835 def. variants ( )
837836 . iter ( )
838- . filter_map ( |variant| transparent_newtype_field ( cx . tcx , variant) )
839- . any ( |field| ty_is_known_nonnull ( cx , field. ty ( tcx, args) , mode) )
837+ . filter_map ( |variant| transparent_newtype_field ( tcx, variant) )
838+ . any ( |field| ty_is_known_nonnull ( tcx , field. ty ( tcx, args) , mode) )
840839 }
841840 _ => false ,
842841 }
843842}
844843
845844/// Given a non-null scalar (or transparent) type `ty`, return the nullable version of that type.
846845/// If the type passed in was not scalar, returns None.
847- fn get_nullable_type < ' tcx > ( cx : & LateContext < ' tcx > , ty : Ty < ' tcx > ) -> Option < Ty < ' tcx > > {
848- let tcx = cx. tcx ;
846+ fn get_nullable_type < ' tcx > ( tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > ) -> Option < Ty < ' tcx > > {
849847 Some ( match * ty. kind ( ) {
850848 ty:: Adt ( field_def, field_args) => {
851849 let inner_field_ty = {
852- let mut first_non_zst_ty = field_def
853- . variants ( )
854- . iter ( )
855- . filter_map ( |v| transparent_newtype_field ( cx. tcx , v) ) ;
850+ let mut first_non_zst_ty =
851+ field_def. variants ( ) . iter ( ) . filter_map ( |v| transparent_newtype_field ( tcx, v) ) ;
856852 debug_assert_eq ! (
857853 first_non_zst_ty. clone( ) . count( ) ,
858854 1 ,
@@ -863,7 +859,7 @@ fn get_nullable_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'t
863859 . expect ( "No non-zst fields in transparent type." )
864860 . ty ( tcx, field_args)
865861 } ;
866- return get_nullable_type ( cx , inner_field_ty) ;
862+ return get_nullable_type ( tcx , inner_field_ty) ;
867863 }
868864 ty:: Int ( ty) => Ty :: new_int ( tcx, ty) ,
869865 ty:: Uint ( ty) => Ty :: new_uint ( tcx, ty) ,
@@ -895,43 +891,44 @@ fn get_nullable_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'t
895891/// `core::ptr::NonNull`, and `#[repr(transparent)]` newtypes.
896892/// FIXME: This duplicates code in codegen.
897893pub ( crate ) fn repr_nullable_ptr < ' tcx > (
898- cx : & LateContext < ' tcx > ,
894+ tcx : TyCtxt < ' tcx > ,
895+ param_env : ty:: ParamEnv < ' tcx > ,
899896 ty : Ty < ' tcx > ,
900897 ckind : CItemKind ,
901898) -> Option < Ty < ' tcx > > {
902- debug ! ( "is_repr_nullable_ptr(cx , ty = {:?})" , ty) ;
899+ debug ! ( "is_repr_nullable_ptr(tcx , ty = {:?})" , ty) ;
903900 if let ty:: Adt ( ty_def, args) = ty. kind ( ) {
904901 let field_ty = match & ty_def. variants ( ) . raw [ ..] {
905902 [ var_one, var_two] => match ( & var_one. fields . raw [ ..] , & var_two. fields . raw [ ..] ) {
906- ( [ ] , [ field] ) | ( [ field] , [ ] ) => field. ty ( cx . tcx , args) ,
903+ ( [ ] , [ field] ) | ( [ field] , [ ] ) => field. ty ( tcx, args) ,
907904 _ => return None ,
908905 } ,
909906 _ => return None ,
910907 } ;
911908
912- if !ty_is_known_nonnull ( cx , field_ty, ckind) {
909+ if !ty_is_known_nonnull ( tcx , field_ty, ckind) {
913910 return None ;
914911 }
915912
916913 // At this point, the field's type is known to be nonnull and the parent enum is Option-like.
917914 // If the computed size for the field and the enum are different, the nonnull optimization isn't
918915 // being applied (and we've got a problem somewhere).
919- let compute_size_skeleton = |t| SizeSkeleton :: compute ( t, cx . tcx , cx . param_env ) . unwrap ( ) ;
916+ let compute_size_skeleton = |t| SizeSkeleton :: compute ( t, tcx, param_env) . unwrap ( ) ;
920917 if !compute_size_skeleton ( ty) . same_size ( compute_size_skeleton ( field_ty) ) {
921918 bug ! ( "improper_ctypes: Option nonnull optimization not applied?" ) ;
922919 }
923920
924921 // Return the nullable type this Option-like enum can be safely represented with.
925- let field_ty_abi = & cx . layout_of ( field_ty) . unwrap ( ) . abi ;
922+ let field_ty_abi = & tcx . layout_of ( param_env . and ( field_ty) ) . unwrap ( ) . abi ;
926923 if let Abi :: Scalar ( field_ty_scalar) = field_ty_abi {
927- match field_ty_scalar. valid_range ( cx ) {
924+ match field_ty_scalar. valid_range ( & tcx ) {
928925 WrappingRange { start : 0 , end }
929- if end == field_ty_scalar. size ( & cx . tcx ) . unsigned_int_max ( ) - 1 =>
926+ if end == field_ty_scalar. size ( & tcx) . unsigned_int_max ( ) - 1 =>
930927 {
931- return Some ( get_nullable_type ( cx , field_ty) . unwrap ( ) ) ;
928+ return Some ( get_nullable_type ( tcx , field_ty) . unwrap ( ) ) ;
932929 }
933930 WrappingRange { start : 1 , .. } => {
934- return Some ( get_nullable_type ( cx , field_ty) . unwrap ( ) ) ;
931+ return Some ( get_nullable_type ( tcx , field_ty) . unwrap ( ) ) ;
935932 }
936933 WrappingRange { start, end } => {
937934 unreachable ! ( "Unhandled start and end range: ({}, {})" , start, end)
@@ -1116,7 +1113,9 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
11161113 if !def. repr ( ) . c ( ) && !def. repr ( ) . transparent ( ) && def. repr ( ) . int . is_none ( )
11171114 {
11181115 // Special-case types like `Option<extern fn()>`.
1119- if repr_nullable_ptr ( self . cx , ty, self . mode ) . is_none ( ) {
1116+ if repr_nullable_ptr ( self . cx . tcx , self . cx . param_env , ty, self . mode )
1117+ . is_none ( )
1118+ {
11201119 return FfiUnsafe {
11211120 ty,
11221121 reason : fluent:: lint_improper_ctypes_enum_repr_reason,
0 commit comments