1+ #![ allow( dead_code) ]
12use rustc_pattern_analysis:: constructor:: {
23 Constructor , ConstructorSet , IntRange , MaybeInfiniteInt , RangeEnd , VariantVisibility ,
34} ;
@@ -22,8 +23,10 @@ fn init_tracing() {
2223 . try_init ( ) ;
2324}
2425
26+ pub const UNIT : Ty = Ty :: Tuple ( & [ ] ) ;
27+ pub const NEVER : Ty = Ty :: Enum ( & [ ] ) ;
28+
2529/// A simple set of types.
26- #[ allow( dead_code) ]
2730#[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
2831pub ( super ) enum Ty {
2932 /// Booleans
@@ -38,6 +41,8 @@ pub(super) enum Ty {
3841 BigStruct { arity : usize , ty : & ' static Ty } ,
3942 /// A enum with `arity` variants of type `ty`.
4043 BigEnum { arity : usize , ty : & ' static Ty } ,
44+ /// Like `Enum` but non-exhaustive.
45+ NonExhaustiveEnum ( & ' static [ Ty ] ) ,
4146}
4247
4348/// The important logic.
@@ -47,7 +52,7 @@ impl Ty {
4752 match ( ctor, * self ) {
4853 ( Struct , Ty :: Tuple ( tys) ) => tys. iter ( ) . copied ( ) . collect ( ) ,
4954 ( Struct , Ty :: BigStruct { arity, ty } ) => ( 0 ..arity) . map ( |_| * ty) . collect ( ) ,
50- ( Variant ( i) , Ty :: Enum ( tys) ) => vec ! [ tys[ * i] ] ,
55+ ( Variant ( i) , Ty :: Enum ( tys) | Ty :: NonExhaustiveEnum ( tys ) ) => vec ! [ tys[ * i] ] ,
5156 ( Variant ( _) , Ty :: BigEnum { ty, .. } ) => vec ! [ * ty] ,
5257 ( Bool ( ..) | IntRange ( ..) | NonExhaustive | Missing | Wildcard , _) => vec ! [ ] ,
5358 _ => panic ! ( "Unexpected ctor {ctor:?} for type {self:?}" ) ,
@@ -61,6 +66,7 @@ impl Ty {
6166 Ty :: Enum ( tys) => tys. iter ( ) . all ( |ty| ty. is_empty ( ) ) ,
6267 Ty :: BigStruct { arity, ty } => arity != 0 && ty. is_empty ( ) ,
6368 Ty :: BigEnum { arity, ty } => arity == 0 || ty. is_empty ( ) ,
69+ Ty :: NonExhaustiveEnum ( ..) => false ,
6470 }
6571 }
6672
@@ -90,6 +96,19 @@ impl Ty {
9096 . collect ( ) ,
9197 non_exhaustive : false ,
9298 } ,
99+ Ty :: NonExhaustiveEnum ( tys) => ConstructorSet :: Variants {
100+ variants : tys
101+ . iter ( )
102+ . map ( |ty| {
103+ if ty. is_empty ( ) {
104+ VariantVisibility :: Empty
105+ } else {
106+ VariantVisibility :: Visible
107+ }
108+ } )
109+ . collect ( ) ,
110+ non_exhaustive : true ,
111+ } ,
93112 Ty :: BigEnum { arity : 0 , .. } => ConstructorSet :: NoConstructors ,
94113 Ty :: BigEnum { arity, ty } => {
95114 let vis = if ty. is_empty ( ) {
@@ -113,7 +132,9 @@ impl Ty {
113132 match ( * self , ctor) {
114133 ( Ty :: Tuple ( ..) , _) => Ok ( ( ) ) ,
115134 ( Ty :: BigStruct { .. } , _) => write ! ( f, "BigStruct" ) ,
116- ( Ty :: Enum ( ..) , Constructor :: Variant ( i) ) => write ! ( f, "Enum::Variant{i}" ) ,
135+ ( Ty :: Enum ( ..) | Ty :: NonExhaustiveEnum ( ..) , Constructor :: Variant ( i) ) => {
136+ write ! ( f, "Enum::Variant{i}" )
137+ }
117138 ( Ty :: BigEnum { .. } , Constructor :: Variant ( i) ) => write ! ( f, "BigEnum::Variant{i}" ) ,
118139 _ => write ! ( f, "{:?}::{:?}" , self , ctor) ,
119140 }
0 commit comments