@@ -637,32 +637,56 @@ impl<'a, 'p, 'tcx> fmt::Debug for PatCtxt<'a, 'p, 'tcx> {
637637 }
638638}
639639
640- /// In the matrix, tracks whether a given place (aka column) is known to contain a valid value or
641- /// not.
640+ /// Serves two purposes:
641+ /// - in a wildcard, tracks whether the wildcard matches only valid values (i.e. is a binding `_a`)
642+ /// or also invalid values (i.e. is a true `_` pattern).
643+ /// - in the matrix, track whether a given place (aka column) is known to contain a valid value or
644+ /// not.
642645#[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
643646pub ( super ) enum ValidityConstraint {
644647 ValidOnly ,
645648 MaybeInvalid ,
649+ /// Option for backwards compatibility: the place is not known to be valid but we allow omitting
650+ /// `useful && !reachable` arms anyway.
651+ MaybeInvalidButAllowOmittingArms ,
646652}
647653
648654impl ValidityConstraint {
649655 pub ( super ) fn from_bool ( is_valid_only : bool ) -> Self {
650656 if is_valid_only { ValidOnly } else { MaybeInvalid }
651657 }
652658
659+ fn allow_omitting_side_effecting_arms ( self ) -> Self {
660+ match self {
661+ MaybeInvalid | MaybeInvalidButAllowOmittingArms => MaybeInvalidButAllowOmittingArms ,
662+ // There are no side-effecting empty arms here, nothing to do.
663+ ValidOnly => ValidOnly ,
664+ }
665+ }
666+
667+ pub ( super ) fn is_known_valid ( self ) -> bool {
668+ matches ! ( self , ValidOnly )
669+ }
670+ pub ( super ) fn allows_omitting_empty_arms ( self ) -> bool {
671+ matches ! ( self , ValidOnly | MaybeInvalidButAllowOmittingArms )
672+ }
673+
653674 /// If the place has validity given by `self` and we read that the value at the place has
654675 /// constructor `ctor`, this computes what we can assume about the validity of the constructor
655676 /// fields.
656677 ///
657678 /// Pending further opsem decisions, the current behavior is: validity is preserved, except
658- /// under `&` where validity is reset to `MaybeInvalid`.
679+ /// inside `&` and union fields where validity is reset to `MaybeInvalid`.
659680 pub ( super ) fn specialize < ' tcx > (
660681 self ,
661682 pcx : & PatCtxt < ' _ , ' _ , ' tcx > ,
662683 ctor : & Constructor < ' tcx > ,
663684 ) -> Self {
664- // We preserve validity except when we go under a reference.
665- if matches ! ( ctor, Constructor :: Single ) && matches ! ( pcx. ty. kind( ) , ty:: Ref ( ..) ) {
685+ // We preserve validity except when we go inside a reference or a union field.
686+ if matches ! ( ctor, Constructor :: Single )
687+ && ( matches ! ( pcx. ty. kind( ) , ty:: Ref ( ..) )
688+ || matches ! ( pcx. ty. kind( ) , ty:: Adt ( def, ..) if def. is_union( ) ) )
689+ {
666690 // Validity of `x: &T` does not imply validity of `*x: T`.
667691 MaybeInvalid
668692 } else {
@@ -675,7 +699,7 @@ impl fmt::Display for ValidityConstraint {
675699 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
676700 let s = match self {
677701 ValidOnly => "✓" ,
678- MaybeInvalid => "?" ,
702+ MaybeInvalid | MaybeInvalidButAllowOmittingArms => "?" ,
679703 } ;
680704 write ! ( f, "{s}" )
681705 }
@@ -1198,9 +1222,9 @@ fn compute_exhaustiveness_and_usefulness<'p, 'tcx>(
11981222 for row in matrix. rows_mut ( ) {
11991223 // All rows are useful until they're not.
12001224 row. useful = true ;
1225+ // When there's an unguarded row, the match is exhaustive and any subsequent row is not
1226+ // useful.
12011227 if !row. is_under_guard {
1202- // There's an unguarded row, so the match is exhaustive, and any subsequent row is
1203- // unreachable.
12041228 return WitnessMatrix :: empty ( ) ;
12051229 }
12061230 }
@@ -1211,26 +1235,37 @@ fn compute_exhaustiveness_and_usefulness<'p, 'tcx>(
12111235 debug ! ( "ty: {ty:?}" ) ;
12121236 let pcx = & PatCtxt { cx, ty, is_top_level } ;
12131237
1238+ // Whether the place/column we are inspecting is known to contain valid data.
1239+ let place_validity = matrix. place_validity [ 0 ] ;
1240+ // For backwards compability we allow omitting some empty arms that we ideally shouldn't.
1241+ let place_validity = place_validity. allow_omitting_side_effecting_arms ( ) ;
1242+
12141243 // Analyze the constructors present in this column.
12151244 let ctors = matrix. heads ( ) . map ( |p| p. ctor ( ) ) ;
1216- let split_set = ConstructorSet :: for_ty ( pcx. cx , pcx. ty ) . split ( pcx, ctors) ;
1217-
1245+ let split_set = ConstructorSet :: for_ty ( cx, ty) . split ( pcx, ctors) ;
12181246 let all_missing = split_set. present . is_empty ( ) ;
1219- let always_report_all = is_top_level && !IntRange :: is_integral ( pcx. ty ) ;
1220- // Whether we should report "Enum::A and Enum::C are missing" or "_ is missing".
1221- let report_individual_missing_ctors = always_report_all || !all_missing;
12221247
1248+ // Build the set of constructors we will specialize with. It must cover the whole type.
12231249 let mut split_ctors = split_set. present ;
1224- let mut only_report_missing = false ;
12251250 if !split_set. missing . is_empty ( ) {
12261251 // We need to iterate over a full set of constructors, so we add `Missing` to represent the
12271252 // missing ones. This is explained under "Constructor Splitting" at the top of this file.
12281253 split_ctors. push ( Constructor :: Missing ) ;
1229- // For diagnostic purposes we choose to only report the constructors that are missing. Since
1230- // `Missing` matches only the wildcard rows, it matches fewer rows than any normal
1231- // constructor and is therefore guaranteed to result in more witnesses. So skipping the
1232- // other constructors does not jeopardize correctness.
1233- only_report_missing = true ;
1254+ } else if !split_set. missing_empty . is_empty ( ) && !place_validity. is_known_valid ( ) {
1255+ // The missing empty constructors are reachable if the place can contain invalid data.
1256+ split_ctors. push ( Constructor :: Missing ) ;
1257+ }
1258+
1259+ // Decide what constructors to report.
1260+ let always_report_all = is_top_level && !IntRange :: is_integral ( pcx. ty ) ;
1261+ // Whether we should report "Enum::A and Enum::C are missing" or "_ is missing".
1262+ let report_individual_missing_ctors = always_report_all || !all_missing;
1263+ // Which constructors are considered missing. We ensure that `!missing_ctors.is_empty() =>
1264+ // split_ctors.contains(Missing)`. The converse usually holds except in the
1265+ // `MaybeInvalidButAllowOmittingArms` backwards-compatibility case.
1266+ let mut missing_ctors = split_set. missing ;
1267+ if !place_validity. allows_omitting_empty_arms ( ) {
1268+ missing_ctors. extend ( split_set. missing_empty ) ;
12341269 }
12351270
12361271 let mut ret = WitnessMatrix :: empty ( ) ;
@@ -1242,11 +1277,19 @@ fn compute_exhaustiveness_and_usefulness<'p, 'tcx>(
12421277 compute_exhaustiveness_and_usefulness ( cx, & mut spec_matrix, false )
12431278 } ) ;
12441279
1245- if !only_report_missing || matches ! ( ctor, Constructor :: Missing ) {
1280+ let counts_for_exhaustiveness = match ctor {
1281+ Constructor :: Missing => !missing_ctors. is_empty ( ) ,
1282+ // If there are missing constructors we'll report those instead. Since `Missing` matches
1283+ // only the wildcard rows, it matches fewer rows than this constructor, and is therefore
1284+ // guaranteed to result in the same or more witnesses. So skipping this does not
1285+ // jeopardize correctness.
1286+ _ => missing_ctors. is_empty ( ) ,
1287+ } ;
1288+ if counts_for_exhaustiveness {
12461289 // Transform witnesses for `spec_matrix` into witnesses for `matrix`.
12471290 witnesses. apply_constructor (
12481291 pcx,
1249- & split_set . missing ,
1292+ & missing_ctors ,
12501293 & ctor,
12511294 report_individual_missing_ctors,
12521295 ) ;
0 commit comments