@@ -4,7 +4,7 @@ use rustc_abi::{BackendRepr, TagEncoding, Variants, WrappingRange};
44use rustc_hir:: { Expr , ExprKind , LangItem } ;
55use rustc_middle:: bug;
66use 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 } ;
88use rustc_session:: { declare_lint, declare_lint_pass, impl_lint_pass} ;
99use rustc_span:: { Span , Symbol , source_map, sym} ;
1010use tracing:: debug;
@@ -928,6 +928,38 @@ pub(crate) fn repr_nullable_ptr<'tcx>(
928928 None
929929}
930930
931+ /// determines wether or not `outer_ty` is an option-like enum, with the same size as its contained type, `ty`.
932+ /// this ASSUMES that `ty` is a type that is already 'inside' of `outer_ty`.
933+ fn is_outer_optionlike_around_ty < ' tcx > (
934+ cx : & LateContext < ' tcx > ,
935+ outer_ty : Ty < ' tcx > ,
936+ ty : Ty < ' tcx > ,
937+ ) -> bool {
938+ // three things to check to be sure outer_ty is option-like (since we know we reached the current ty from there)
939+ // That outer_ty is an enum, that this enum doesn't have a defined discriminant representation,
940+ // and the the outer_ty's size is that of ty.
941+ if let ty:: Adt ( def, _) = outer_ty. kind ( ) {
942+ if !matches ! ( def. adt_kind( ) , AdtKind :: Enum )
943+ || def. repr ( ) . c ( )
944+ || def. repr ( ) . transparent ( )
945+ || def. repr ( ) . int . is_none ( )
946+ {
947+ false
948+ } else {
949+ let ( tcx, typing_env) = ( cx. tcx , cx. typing_env ( ) ) ;
950+
951+ // see the insides of super::repr_nullable_ptr()
952+ let compute_size_skeleton = |t| SizeSkeleton :: compute ( t, tcx, typing_env) . ok ( ) ;
953+ match ( compute_size_skeleton ( ty) , compute_size_skeleton ( outer_ty) ) {
954+ ( Some ( sk1) , Some ( sk2) ) => sk1. same_size ( sk2) ,
955+ _ => false ,
956+ }
957+ }
958+ } else {
959+ false
960+ }
961+ }
962+
931963declare_lint_pass ! ( VariantSizeDifferences => [ VARIANT_SIZE_DIFFERENCES ] ) ;
932964
933965impl < ' tcx > LateLintPass < ' tcx > for VariantSizeDifferences {
0 commit comments