@@ -631,32 +631,56 @@ impl<'a, 'p, 'tcx> fmt::Debug for PatCtxt<'a, 'p, 'tcx> {
631631 }
632632}
633633
634- /// In the matrix, tracks whether a given place (aka column) is known to contain a valid value or
635- /// not.
634+ /// Serves two purposes:
635+ /// - in a wildcard, tracks whether the wildcard matches only valid values (i.e. is a binding `_a`)
636+ /// or also invalid values (i.e. is a true `_` pattern).
637+ /// - in the matrix, track whether a given place (aka column) is known to contain a valid value or
638+ /// not.
636639#[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
637640pub ( super ) enum ValidityConstraint {
638641 ValidOnly ,
639642 MaybeInvalid ,
643+ /// Option for backwards compatibility: the place is not known to be valid but we allow omitting
644+ /// `useful && !reachable` arms anyway.
645+ MaybeInvalidButAllowOmittingArms ,
640646}
641647
642648impl ValidityConstraint {
643649 pub ( super ) fn from_bool ( is_valid_only : bool ) -> Self {
644650 if is_valid_only { ValidOnly } else { MaybeInvalid }
645651 }
646652
653+ fn allow_omitting_side_effecting_arms ( self ) -> Self {
654+ match self {
655+ MaybeInvalid | MaybeInvalidButAllowOmittingArms => MaybeInvalidButAllowOmittingArms ,
656+ // There are no side-effecting empty arms here, nothing to do.
657+ ValidOnly => ValidOnly ,
658+ }
659+ }
660+
661+ pub ( super ) fn is_known_valid ( self ) -> bool {
662+ matches ! ( self , ValidOnly )
663+ }
664+ pub ( super ) fn allows_omitting_empty_arms ( self ) -> bool {
665+ matches ! ( self , ValidOnly | MaybeInvalidButAllowOmittingArms )
666+ }
667+
647668 /// If the place has validity given by `self` and we read that the value at the place has
648669 /// constructor `ctor`, this computes what we can assume about the validity of the constructor
649670 /// fields.
650671 ///
651672 /// Pending further opsem decisions, the current behavior is: validity is preserved, except
652- /// under `&` where validity is reset to `MaybeInvalid`.
673+ /// inside `&` and union fields where validity is reset to `MaybeInvalid`.
653674 pub ( super ) fn specialize < ' tcx > (
654675 self ,
655676 pcx : & PatCtxt < ' _ , ' _ , ' tcx > ,
656677 ctor : & Constructor < ' tcx > ,
657678 ) -> Self {
658- // We preserve validity except when we go under a reference.
659- if matches ! ( ctor, Constructor :: Single ) && matches ! ( pcx. ty. kind( ) , ty:: Ref ( ..) ) {
679+ // We preserve validity except when we go inside a reference or a union field.
680+ if matches ! ( ctor, Constructor :: Single )
681+ && ( matches ! ( pcx. ty. kind( ) , ty:: Ref ( ..) )
682+ || matches ! ( pcx. ty. kind( ) , ty:: Adt ( def, ..) if def. is_union( ) ) )
683+ {
660684 // Validity of `x: &T` does not imply validity of `*x: T`.
661685 MaybeInvalid
662686 } else {
@@ -669,7 +693,7 @@ impl fmt::Display for ValidityConstraint {
669693 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
670694 let s = match self {
671695 ValidOnly => "✓" ,
672- MaybeInvalid => "?" ,
696+ MaybeInvalid | MaybeInvalidButAllowOmittingArms => "?" ,
673697 } ;
674698 write ! ( f, "{s}" )
675699 }
@@ -1192,9 +1216,9 @@ fn compute_exhaustiveness_and_usefulness<'p, 'tcx>(
11921216 for row in matrix. rows_mut ( ) {
11931217 // All rows are useful until they're not.
11941218 row. useful = true ;
1219+ // When there's an unguarded row, the match is exhaustive and any subsequent row is not
1220+ // useful.
11951221 if !row. is_under_guard {
1196- // There's an unguarded row, so the match is exhaustive, and any subsequent row is
1197- // unreachable.
11981222 return WitnessMatrix :: empty ( ) ;
11991223 }
12001224 }
@@ -1205,26 +1229,37 @@ fn compute_exhaustiveness_and_usefulness<'p, 'tcx>(
12051229 debug ! ( "ty: {ty:?}" ) ;
12061230 let pcx = & PatCtxt { cx, ty, is_top_level } ;
12071231
1232+ // Whether the place/column we are inspecting is known to contain valid data.
1233+ let place_validity = matrix. place_validity [ 0 ] ;
1234+ // For backwards compability we allow omitting some empty arms that we ideally shouldn't.
1235+ let place_validity = place_validity. allow_omitting_side_effecting_arms ( ) ;
1236+
12081237 // Analyze the constructors present in this column.
12091238 let ctors = matrix. heads ( ) . map ( |p| p. ctor ( ) ) ;
1210- let split_set = ConstructorSet :: for_ty ( pcx. cx , pcx. ty ) . split ( pcx, ctors) ;
1211-
1239+ let split_set = ConstructorSet :: for_ty ( cx, ty) . split ( pcx, ctors) ;
12121240 let all_missing = split_set. present . is_empty ( ) ;
1213- let always_report_all = is_top_level && !IntRange :: is_integral ( pcx. ty ) ;
1214- // Whether we should report "Enum::A and Enum::C are missing" or "_ is missing".
1215- let report_individual_missing_ctors = always_report_all || !all_missing;
12161241
1242+ // Build the set of constructors we will specialize with. It must cover the whole type.
12171243 let mut split_ctors = split_set. present ;
1218- let mut only_report_missing = false ;
12191244 if !split_set. missing . is_empty ( ) {
12201245 // We need to iterate over a full set of constructors, so we add `Missing` to represent the
12211246 // missing ones. This is explained under "Constructor Splitting" at the top of this file.
12221247 split_ctors. push ( Constructor :: Missing ) ;
1223- // For diagnostic purposes we choose to only report the constructors that are missing. Since
1224- // `Missing` matches only the wildcard rows, it matches fewer rows than any normal
1225- // constructor and is therefore guaranteed to result in more witnesses. So skipping the
1226- // other constructors does not jeopardize correctness.
1227- only_report_missing = true ;
1248+ } else if !split_set. missing_empty . is_empty ( ) && !place_validity. is_known_valid ( ) {
1249+ // The missing empty constructors are reachable if the place can contain invalid data.
1250+ split_ctors. push ( Constructor :: Missing ) ;
1251+ }
1252+
1253+ // Decide what constructors to report.
1254+ let always_report_all = is_top_level && !IntRange :: is_integral ( pcx. ty ) ;
1255+ // Whether we should report "Enum::A and Enum::C are missing" or "_ is missing".
1256+ let report_individual_missing_ctors = always_report_all || !all_missing;
1257+ // Which constructors are considered missing. We ensure that `!missing_ctors.is_empty() =>
1258+ // split_ctors.contains(Missing)`. The converse usually holds except in the
1259+ // `MaybeInvalidButAllowOmittingArms` backwards-compatibility case.
1260+ let mut missing_ctors = split_set. missing ;
1261+ if !place_validity. allows_omitting_empty_arms ( ) {
1262+ missing_ctors. extend ( split_set. missing_empty ) ;
12281263 }
12291264
12301265 let mut ret = WitnessMatrix :: empty ( ) ;
@@ -1236,11 +1271,19 @@ fn compute_exhaustiveness_and_usefulness<'p, 'tcx>(
12361271 compute_exhaustiveness_and_usefulness ( cx, & mut spec_matrix, false )
12371272 } ) ;
12381273
1239- if !only_report_missing || matches ! ( ctor, Constructor :: Missing ) {
1274+ let counts_for_exhaustiveness = match ctor {
1275+ Constructor :: Missing => !missing_ctors. is_empty ( ) ,
1276+ // If there are missing constructors we'll report those instead. Since `Missing` matches
1277+ // only the wildcard rows, it matches fewer rows than this constructor, and is therefore
1278+ // guaranteed to result in the same or more witnesses. So skipping this does not
1279+ // jeopardize correctness.
1280+ _ => missing_ctors. is_empty ( ) ,
1281+ } ;
1282+ if counts_for_exhaustiveness {
12401283 // Transform witnesses for `spec_matrix` into witnesses for `matrix`.
12411284 witnesses. apply_constructor (
12421285 pcx,
1243- & split_set . missing ,
1286+ & missing_ctors ,
12441287 & ctor,
12451288 report_individual_missing_ctors,
12461289 ) ;
0 commit comments