@@ -4,7 +4,7 @@ use rustc_abi::{BackendRepr, TagEncoding, Variants, WrappingRange};
4
4
use rustc_hir:: { Expr , ExprKind , LangItem } ;
5
5
use rustc_middle:: bug;
6
6
use rustc_middle:: ty:: layout:: { LayoutOf , SizeSkeleton } ;
7
- use rustc_middle:: ty:: { self , Ty , TyCtxt , TypeVisitableExt } ;
7
+ use rustc_middle:: ty:: { self , AdtKind , Ty , TyCtxt , TypeVisitableExt } ;
8
8
use rustc_session:: { declare_lint, declare_lint_pass, impl_lint_pass} ;
9
9
use rustc_span:: { Span , Symbol , source_map, sym} ;
10
10
use tracing:: debug;
@@ -863,6 +863,38 @@ pub(crate) fn repr_nullable_ptr<'tcx>(
863
863
None
864
864
}
865
865
866
+ /// determines wether or not `outer_ty` is an option-like enum, with the same size as its contained type, `ty`.
867
+ /// this ASSUMES that `ty` is a type that is already 'inside' of `outer_ty`.
868
+ fn is_outer_optionlike_around_ty < ' tcx > (
869
+ cx : & LateContext < ' tcx > ,
870
+ outer_ty : Ty < ' tcx > ,
871
+ ty : Ty < ' tcx > ,
872
+ ) -> bool {
873
+ // three things to check to be sure outer_ty is option-like (since we know we reached the current ty from there)
874
+ // That outer_ty is an enum, that this enum doesn't have a defined discriminant representation,
875
+ // and the the outer_ty's size is that of ty.
876
+ if let ty:: Adt ( def, _) = outer_ty. kind ( ) {
877
+ if !matches ! ( def. adt_kind( ) , AdtKind :: Enum )
878
+ || def. repr ( ) . c ( )
879
+ || def. repr ( ) . transparent ( )
880
+ || def. repr ( ) . int . is_none ( )
881
+ {
882
+ false
883
+ } else {
884
+ let ( tcx, typing_env) = ( cx. tcx , cx. typing_env ( ) ) ;
885
+
886
+ // see the insides of super::repr_nullable_ptr()
887
+ let compute_size_skeleton = |t| SizeSkeleton :: compute ( t, tcx, typing_env) . ok ( ) ;
888
+ match ( compute_size_skeleton ( ty) , compute_size_skeleton ( outer_ty) ) {
889
+ ( Some ( sk1) , Some ( sk2) ) => sk1. same_size ( sk2) ,
890
+ _ => false ,
891
+ }
892
+ }
893
+ } else {
894
+ false
895
+ }
896
+ }
897
+
866
898
declare_lint_pass ! ( VariantSizeDifferences => [ VARIANT_SIZE_DIFFERENCES ] ) ;
867
899
868
900
impl < ' tcx > LateLintPass < ' tcx > for VariantSizeDifferences {
0 commit comments