@@ -637,32 +637,56 @@ impl<'a, 'p, 'tcx> fmt::Debug for PatCtxt<'a, 'p, 'tcx> {
637
637
}
638
638
}
639
639
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.
642
645
#[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
643
646
pub ( super ) enum ValidityConstraint {
644
647
ValidOnly ,
645
648
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 ,
646
652
}
647
653
648
654
impl ValidityConstraint {
649
655
pub ( super ) fn from_bool ( is_valid_only : bool ) -> Self {
650
656
if is_valid_only { ValidOnly } else { MaybeInvalid }
651
657
}
652
658
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
+
653
674
/// If the place has validity given by `self` and we read that the value at the place has
654
675
/// constructor `ctor`, this computes what we can assume about the validity of the constructor
655
676
/// fields.
656
677
///
657
678
/// 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`.
659
680
pub ( super ) fn specialize < ' tcx > (
660
681
self ,
661
682
pcx : & PatCtxt < ' _ , ' _ , ' tcx > ,
662
683
ctor : & Constructor < ' tcx > ,
663
684
) -> 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
+ {
666
690
// Validity of `x: &T` does not imply validity of `*x: T`.
667
691
MaybeInvalid
668
692
} else {
@@ -675,7 +699,7 @@ impl fmt::Display for ValidityConstraint {
675
699
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
676
700
let s = match self {
677
701
ValidOnly => "✓" ,
678
- MaybeInvalid => "?" ,
702
+ MaybeInvalid | MaybeInvalidButAllowOmittingArms => "?" ,
679
703
} ;
680
704
write ! ( f, "{s}" )
681
705
}
@@ -1198,9 +1222,9 @@ fn compute_exhaustiveness_and_usefulness<'p, 'tcx>(
1198
1222
for row in matrix. rows_mut ( ) {
1199
1223
// All rows are useful until they're not.
1200
1224
row. useful = true ;
1225
+ // When there's an unguarded row, the match is exhaustive and any subsequent row is not
1226
+ // useful.
1201
1227
if !row. is_under_guard {
1202
- // There's an unguarded row, so the match is exhaustive, and any subsequent row is
1203
- // unreachable.
1204
1228
return WitnessMatrix :: empty ( ) ;
1205
1229
}
1206
1230
}
@@ -1211,26 +1235,37 @@ fn compute_exhaustiveness_and_usefulness<'p, 'tcx>(
1211
1235
debug ! ( "ty: {ty:?}" ) ;
1212
1236
let pcx = & PatCtxt { cx, ty, is_top_level } ;
1213
1237
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
+
1214
1243
// Analyze the constructors present in this column.
1215
1244
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) ;
1218
1246
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;
1222
1247
1248
+ // Build the set of constructors we will specialize with. It must cover the whole type.
1223
1249
let mut split_ctors = split_set. present ;
1224
- let mut only_report_missing = false ;
1225
1250
if !split_set. missing . is_empty ( ) {
1226
1251
// We need to iterate over a full set of constructors, so we add `Missing` to represent the
1227
1252
// missing ones. This is explained under "Constructor Splitting" at the top of this file.
1228
1253
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 ) ;
1234
1269
}
1235
1270
1236
1271
let mut ret = WitnessMatrix :: empty ( ) ;
@@ -1242,11 +1277,19 @@ fn compute_exhaustiveness_and_usefulness<'p, 'tcx>(
1242
1277
compute_exhaustiveness_and_usefulness ( cx, & mut spec_matrix, false )
1243
1278
} ) ;
1244
1279
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 {
1246
1289
// Transform witnesses for `spec_matrix` into witnesses for `matrix`.
1247
1290
witnesses. apply_constructor (
1248
1291
pcx,
1249
- & split_set . missing ,
1292
+ & missing_ctors ,
1250
1293
& ctor,
1251
1294
report_individual_missing_ctors,
1252
1295
) ;
0 commit comments