@@ -1441,39 +1441,38 @@ fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>(
14411441
14421442 debug ! ( "ty: {ty:?}" ) ;
14431443 let pcx = & PlaceCtxt { mcx, ty } ;
1444+ let ctors_for_ty = pcx. ctors_for_ty ( ) ?;
14441445
14451446 // Whether the place/column we are inspecting is known to contain valid data.
14461447 let place_validity = matrix. place_validity [ 0 ] ;
1448+ // We treat match scrutinees of type `!` or `EmptyEnum` differently.
1449+ let is_toplevel_exception =
1450+ is_top_level && matches ! ( ctors_for_ty, ConstructorSet :: NoConstructors ) ;
1451+ // Whether empty patterns can be omitted for exhaustiveness.
1452+ let can_omit_empty_arms = is_toplevel_exception || mcx. tycx . is_exhaustive_patterns_feature_on ( ) ;
14471453
14481454 // Analyze the constructors present in this column.
14491455 let ctors = matrix. heads ( ) . map ( |p| p. ctor ( ) ) ;
1450- let ctors_for_ty = pcx. ctors_for_ty ( ) ?;
1451- let is_integers = matches ! ( ctors_for_ty, ConstructorSet :: Integers { .. } ) ; // For diagnostics.
14521456 let mut split_set = ctors_for_ty. split ( ctors) ;
1453- // We have now grouped all the constructors into 3 buckets: present, missing, missing_empty.
1454- // In the absence of the `exhaustive_patterns` feature however, we don't count nested empty
1455- // types as empty. Only non-nested `!` or `enum Foo {}` are considered empty.
1456- if !pcx. mcx . tycx . is_exhaustive_patterns_feature_on ( )
1457- && !( is_top_level && matches ! ( ctors_for_ty, ConstructorSet :: NoConstructors ) )
1458- {
1457+ if !can_omit_empty_arms {
14591458 // Treat all missing constructors as nonempty.
14601459 // This clears `missing_empty`.
14611460 split_set. missing . append ( & mut split_set. missing_empty ) ;
14621461 }
14631462 let all_missing = split_set. present . is_empty ( ) ;
14641463
14651464 // Build the set of constructors we will specialize with. It must cover the whole type.
1465+ // We need to iterate over a full set of constructors, so we add `Missing` to represent the
1466+ // missing ones. This is explained under "Constructor Splitting" at the top of this file.
14661467 let mut split_ctors = split_set. present ;
1467- if !split_set. missing . is_empty ( ) {
1468- // We need to iterate over a full set of constructors, so we add `Missing` to represent the
1469- // missing ones. This is explained under "Constructor Splitting" at the top of this file.
1470- split_ctors. push ( Constructor :: Missing ) ;
1471- } else if !split_set. missing_empty . is_empty ( ) && !place_validity. is_known_valid ( ) {
1472- // The missing empty constructors are reachable if the place can contain invalid data.
1468+ if !( split_set. missing . is_empty ( )
1469+ && ( split_set. missing_empty . is_empty ( ) || place_validity. is_known_valid ( ) ) )
1470+ {
14731471 split_ctors. push ( Constructor :: Missing ) ;
14741472 }
14751473
14761474 // Decide what constructors to report.
1475+ let is_integers = matches ! ( ctors_for_ty, ConstructorSet :: Integers { .. } ) ;
14771476 let always_report_all = is_top_level && !is_integers;
14781477 // Whether we should report "Enum::A and Enum::C are missing" or "_ is missing".
14791478 let report_individual_missing_ctors = always_report_all || !all_missing;
0 commit comments