@@ -737,15 +737,13 @@ pub(crate) struct PlaceCtxt<'a, Cx: TypeCx> {
737737 pub ( crate ) mcx : MatchCtxt < ' a , Cx > ,
738738 /// Type of the place under investigation.
739739 pub ( crate ) ty : Cx :: Ty ,
740- /// Whether the place is the original scrutinee place, as opposed to a subplace of it.
741- pub ( crate ) is_scrutinee : bool ,
742740}
743741
744742impl < ' a , Cx : TypeCx > PlaceCtxt < ' a , Cx > {
745743 /// A `PlaceCtxt` when code other than `is_useful` needs one.
746744 #[ cfg_attr( not( feature = "rustc" ) , allow( dead_code) ) ]
747745 pub ( crate ) fn new_dummy ( mcx : MatchCtxt < ' a , Cx > , ty : Cx :: Ty ) -> Self {
748- PlaceCtxt { mcx, ty, is_scrutinee : false }
746+ PlaceCtxt { mcx, ty }
749747 }
750748
751749 pub ( crate ) fn ctor_arity ( & self , ctor : & Constructor < Cx > ) -> usize {
@@ -768,30 +766,16 @@ impl<'a, Cx: TypeCx> PlaceCtxt<'a, Cx> {
768766pub enum ValidityConstraint {
769767 ValidOnly ,
770768 MaybeInvalid ,
771- /// Option for backwards compatibility: the place is not known to be valid but we allow omitting
772- /// `useful && !reachable` arms anyway.
773- MaybeInvalidButAllowOmittingArms ,
774769}
775770
776771impl ValidityConstraint {
777772 pub fn from_bool ( is_valid_only : bool ) -> Self {
778773 if is_valid_only { ValidOnly } else { MaybeInvalid }
779774 }
780775
781- fn allow_omitting_side_effecting_arms ( self ) -> Self {
782- match self {
783- MaybeInvalid | MaybeInvalidButAllowOmittingArms => MaybeInvalidButAllowOmittingArms ,
784- // There are no side-effecting empty arms here, nothing to do.
785- ValidOnly => ValidOnly ,
786- }
787- }
788-
789776 fn is_known_valid ( self ) -> bool {
790777 matches ! ( self , ValidOnly )
791778 }
792- fn allows_omitting_empty_arms ( self ) -> bool {
793- matches ! ( self , ValidOnly | MaybeInvalidButAllowOmittingArms )
794- }
795779
796780 /// If the place has validity given by `self` and we read that the value at the place has
797781 /// constructor `ctor`, this computes what we can assume about the validity of the constructor
@@ -814,7 +798,7 @@ impl fmt::Display for ValidityConstraint {
814798 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
815799 let s = match self {
816800 ValidOnly => "✓" ,
817- MaybeInvalid | MaybeInvalidButAllowOmittingArms => "?" ,
801+ MaybeInvalid => "?" ,
818802 } ;
819803 write ! ( f, "{s}" )
820804 }
@@ -1447,41 +1431,44 @@ fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>(
14471431 } ;
14481432
14491433 debug ! ( "ty: {ty:?}" ) ;
1450- let pcx = & PlaceCtxt { mcx, ty, is_scrutinee : is_top_level } ;
1434+ let pcx = & PlaceCtxt { mcx, ty } ;
1435+ let ctors_for_ty = pcx. ctors_for_ty ( ) ?;
14511436
14521437 // Whether the place/column we are inspecting is known to contain valid data.
14531438 let place_validity = matrix. place_validity [ 0 ] ;
1454- // For backwards compability we allow omitting some empty arms that we ideally shouldn't.
1455- let place_validity = place_validity. allow_omitting_side_effecting_arms ( ) ;
1439+ // We treat match scrutinees of type `!` or `EmptyEnum` differently.
1440+ let is_toplevel_exception =
1441+ is_top_level && matches ! ( ctors_for_ty, ConstructorSet :: NoConstructors ) ;
1442+ // Whether empty patterns can be omitted for exhaustiveness.
1443+ let can_omit_empty_arms = is_toplevel_exception || mcx. tycx . is_exhaustive_patterns_feature_on ( ) ;
1444+ // Whether empty patterns are counted as useful or not.
1445+ let empty_arms_are_unreachable = place_validity. is_known_valid ( ) && can_omit_empty_arms;
14561446
14571447 // Analyze the constructors present in this column.
14581448 let ctors = matrix. heads ( ) . map ( |p| p. ctor ( ) ) ;
1459- let ctors_for_ty = pcx. ctors_for_ty ( ) ?;
1460- let is_integers = matches ! ( ctors_for_ty, ConstructorSet :: Integers { .. } ) ; // For diagnostics.
1461- let split_set = ctors_for_ty. split ( pcx, ctors) ;
1449+ let mut split_set = ctors_for_ty. split ( ctors) ;
14621450 let all_missing = split_set. present . is_empty ( ) ;
1463-
14641451 // Build the set of constructors we will specialize with. It must cover the whole type.
1452+ // We need to iterate over a full set of constructors, so we add `Missing` to represent the
1453+ // missing ones. This is explained under "Constructor Splitting" at the top of this file.
14651454 let mut split_ctors = split_set. present ;
1466- if !split_set. missing . is_empty ( ) {
1467- // We need to iterate over a full set of constructors, so we add `Missing` to represent the
1468- // missing ones. This is explained under "Constructor Splitting" at the top of this file.
1469- split_ctors. push ( Constructor :: Missing ) ;
1470- } else if !split_set. missing_empty . is_empty ( ) && !place_validity. is_known_valid ( ) {
1471- // The missing empty constructors are reachable if the place can contain invalid data.
1455+ if !( split_set. missing . is_empty ( )
1456+ && ( split_set. missing_empty . is_empty ( ) || empty_arms_are_unreachable) )
1457+ {
14721458 split_ctors. push ( Constructor :: Missing ) ;
14731459 }
14741460
14751461 // Decide what constructors to report.
1462+ let is_integers = matches ! ( ctors_for_ty, ConstructorSet :: Integers { .. } ) ;
14761463 let always_report_all = is_top_level && !is_integers;
14771464 // Whether we should report "Enum::A and Enum::C are missing" or "_ is missing".
14781465 let report_individual_missing_ctors = always_report_all || !all_missing;
14791466 // Which constructors are considered missing. We ensure that `!missing_ctors.is_empty() =>
1480- // split_ctors.contains(Missing)`. The converse usually holds except in the
1481- // `MaybeInvalidButAllowOmittingArms` backwards-compatibility case .
1467+ // split_ctors.contains(Missing)`. The converse usually holds except when
1468+ // `!place_validity.is_known_valid()` .
14821469 let mut missing_ctors = split_set. missing ;
1483- if !place_validity . allows_omitting_empty_arms ( ) {
1484- missing_ctors. extend ( split_set. missing_empty ) ;
1470+ if !can_omit_empty_arms {
1471+ missing_ctors. append ( & mut split_set. missing_empty ) ;
14851472 }
14861473
14871474 let mut ret = WitnessMatrix :: empty ( ) ;
0 commit comments