@@ -815,8 +815,7 @@ pub fn transparent_newtype_field<'a, 'tcx>(
815
815
}
816
816
817
817
/// 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 {
820
819
match ty. kind ( ) {
821
820
ty:: FnPtr ( _) => true ,
822
821
ty:: Ref ( ..) => true ,
@@ -835,24 +834,21 @@ fn ty_is_known_nonnull<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, mode: CItemKi
835
834
836
835
def. variants ( )
837
836
. 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) )
840
839
}
841
840
_ => false ,
842
841
}
843
842
}
844
843
845
844
/// Given a non-null scalar (or transparent) type `ty`, return the nullable version of that type.
846
845
/// 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 > > {
849
847
Some ( match * ty. kind ( ) {
850
848
ty:: Adt ( field_def, field_args) => {
851
849
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) ) ;
856
852
debug_assert_eq ! (
857
853
first_non_zst_ty. clone( ) . count( ) ,
858
854
1 ,
@@ -863,7 +859,7 @@ fn get_nullable_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'t
863
859
. expect ( "No non-zst fields in transparent type." )
864
860
. ty ( tcx, field_args)
865
861
} ;
866
- return get_nullable_type ( cx , inner_field_ty) ;
862
+ return get_nullable_type ( tcx , inner_field_ty) ;
867
863
}
868
864
ty:: Int ( ty) => Ty :: new_int ( tcx, ty) ,
869
865
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
895
891
/// `core::ptr::NonNull`, and `#[repr(transparent)]` newtypes.
896
892
/// FIXME: This duplicates code in codegen.
897
893
pub ( crate ) fn repr_nullable_ptr < ' tcx > (
898
- cx : & LateContext < ' tcx > ,
894
+ tcx : TyCtxt < ' tcx > ,
895
+ param_env : ty:: ParamEnv < ' tcx > ,
899
896
ty : Ty < ' tcx > ,
900
897
ckind : CItemKind ,
901
898
) -> Option < Ty < ' tcx > > {
902
- debug ! ( "is_repr_nullable_ptr(cx , ty = {:?})" , ty) ;
899
+ debug ! ( "is_repr_nullable_ptr(tcx , ty = {:?})" , ty) ;
903
900
if let ty:: Adt ( ty_def, args) = ty. kind ( ) {
904
901
let field_ty = match & ty_def. variants ( ) . raw [ ..] {
905
902
[ 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) ,
907
904
_ => return None ,
908
905
} ,
909
906
_ => return None ,
910
907
} ;
911
908
912
- if !ty_is_known_nonnull ( cx , field_ty, ckind) {
909
+ if !ty_is_known_nonnull ( tcx , field_ty, ckind) {
913
910
return None ;
914
911
}
915
912
916
913
// At this point, the field's type is known to be nonnull and the parent enum is Option-like.
917
914
// If the computed size for the field and the enum are different, the nonnull optimization isn't
918
915
// 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 ( ) ;
920
917
if !compute_size_skeleton ( ty) . same_size ( compute_size_skeleton ( field_ty) ) {
921
918
bug ! ( "improper_ctypes: Option nonnull optimization not applied?" ) ;
922
919
}
923
920
924
921
// 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 ;
926
923
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 ) {
928
925
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 =>
930
927
{
931
- return Some ( get_nullable_type ( cx , field_ty) . unwrap ( ) ) ;
928
+ return Some ( get_nullable_type ( tcx , field_ty) . unwrap ( ) ) ;
932
929
}
933
930
WrappingRange { start : 1 , .. } => {
934
- return Some ( get_nullable_type ( cx , field_ty) . unwrap ( ) ) ;
931
+ return Some ( get_nullable_type ( tcx , field_ty) . unwrap ( ) ) ;
935
932
}
936
933
WrappingRange { start, end } => {
937
934
unreachable ! ( "Unhandled start and end range: ({}, {})" , start, end)
@@ -1116,7 +1113,9 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
1116
1113
if !def. repr ( ) . c ( ) && !def. repr ( ) . transparent ( ) && def. repr ( ) . int . is_none ( )
1117
1114
{
1118
1115
// 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
+ {
1120
1119
return FfiUnsafe {
1121
1120
ty,
1122
1121
reason : fluent:: lint_improper_ctypes_enum_repr_reason,
0 commit comments