@@ -631,32 +631,56 @@ impl<'a, 'p, 'tcx> fmt::Debug for PatCtxt<'a, 'p, 'tcx> {
631
631
}
632
632
}
633
633
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.
636
639
#[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
637
640
pub ( super ) enum ValidityConstraint {
638
641
ValidOnly ,
639
642
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 ,
640
646
}
641
647
642
648
impl ValidityConstraint {
643
649
pub ( super ) fn from_bool ( is_valid_only : bool ) -> Self {
644
650
if is_valid_only { ValidOnly } else { MaybeInvalid }
645
651
}
646
652
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
+
647
668
/// If the place has validity given by `self` and we read that the value at the place has
648
669
/// constructor `ctor`, this computes what we can assume about the validity of the constructor
649
670
/// fields.
650
671
///
651
672
/// 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`.
653
674
pub ( super ) fn specialize < ' tcx > (
654
675
self ,
655
676
pcx : & PatCtxt < ' _ , ' _ , ' tcx > ,
656
677
ctor : & Constructor < ' tcx > ,
657
678
) -> 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
+ {
660
684
// Validity of `x: &T` does not imply validity of `*x: T`.
661
685
MaybeInvalid
662
686
} else {
@@ -669,7 +693,7 @@ impl fmt::Display for ValidityConstraint {
669
693
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
670
694
let s = match self {
671
695
ValidOnly => "✓" ,
672
- MaybeInvalid => "?" ,
696
+ MaybeInvalid | MaybeInvalidButAllowOmittingArms => "?" ,
673
697
} ;
674
698
write ! ( f, "{s}" )
675
699
}
@@ -1192,9 +1216,9 @@ fn compute_exhaustiveness_and_usefulness<'p, 'tcx>(
1192
1216
for row in matrix. rows_mut ( ) {
1193
1217
// All rows are useful until they're not.
1194
1218
row. useful = true ;
1219
+ // When there's an unguarded row, the match is exhaustive and any subsequent row is not
1220
+ // useful.
1195
1221
if !row. is_under_guard {
1196
- // There's an unguarded row, so the match is exhaustive, and any subsequent row is
1197
- // unreachable.
1198
1222
return WitnessMatrix :: empty ( ) ;
1199
1223
}
1200
1224
}
@@ -1205,26 +1229,37 @@ fn compute_exhaustiveness_and_usefulness<'p, 'tcx>(
1205
1229
debug ! ( "ty: {ty:?}" ) ;
1206
1230
let pcx = & PatCtxt { cx, ty, is_top_level } ;
1207
1231
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
+
1208
1237
// Analyze the constructors present in this column.
1209
1238
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) ;
1212
1240
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;
1216
1241
1242
+ // Build the set of constructors we will specialize with. It must cover the whole type.
1217
1243
let mut split_ctors = split_set. present ;
1218
- let mut only_report_missing = false ;
1219
1244
if !split_set. missing . is_empty ( ) {
1220
1245
// We need to iterate over a full set of constructors, so we add `Missing` to represent the
1221
1246
// missing ones. This is explained under "Constructor Splitting" at the top of this file.
1222
1247
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 ) ;
1228
1263
}
1229
1264
1230
1265
let mut ret = WitnessMatrix :: empty ( ) ;
@@ -1236,11 +1271,19 @@ fn compute_exhaustiveness_and_usefulness<'p, 'tcx>(
1236
1271
compute_exhaustiveness_and_usefulness ( cx, & mut spec_matrix, false )
1237
1272
} ) ;
1238
1273
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 {
1240
1283
// Transform witnesses for `spec_matrix` into witnesses for `matrix`.
1241
1284
witnesses. apply_constructor (
1242
1285
pcx,
1243
- & split_set . missing ,
1286
+ & missing_ctors ,
1244
1287
& ctor,
1245
1288
report_individual_missing_ctors,
1246
1289
) ;
0 commit comments