@@ -242,7 +242,7 @@ use rustc_hir::{HirId, RangeEnd};
242
242
use rustc_middle:: mir:: interpret:: { truncate, AllocId , ConstValue , Pointer , Scalar } ;
243
243
use rustc_middle:: mir:: Field ;
244
244
use rustc_middle:: ty:: layout:: IntegerExt ;
245
- use rustc_middle:: ty:: { self , Const , FieldDef , Ty , TyCtxt , VariantDef } ;
245
+ use rustc_middle:: ty:: { self , Const , Ty , TyCtxt } ;
246
246
use rustc_session:: lint;
247
247
use rustc_span:: { Span , DUMMY_SP } ;
248
248
use rustc_target:: abi:: { Integer , Size , VariantIdx } ;
@@ -614,36 +614,6 @@ impl<'a, 'tcx> MatchCheckCtxt<'a, 'tcx> {
614
614
_ => false ,
615
615
}
616
616
}
617
-
618
- /// Returns whether the given variant is from another crate and has its fields declared
619
- /// `#[non_exhaustive]`.
620
- fn is_foreign_non_exhaustive_variant ( & self , ty : Ty < ' tcx > , variant : & VariantDef ) -> bool {
621
- match ty. kind {
622
- ty:: Adt ( def, ..) => variant. is_field_list_non_exhaustive ( ) && !def. did . is_local ( ) ,
623
- _ => false ,
624
- }
625
- }
626
-
627
- /// In the cases of either a `#[non_exhaustive]` field list or a non-public field, we hide
628
- /// uninhabited fields in order not to reveal the uninhabitedness of the whole variant.
629
- fn hide_uninhabited_field (
630
- & self ,
631
- adt_ty : Ty < ' tcx > ,
632
- variant : & VariantDef ,
633
- field : & FieldDef ,
634
- ) -> bool {
635
- match adt_ty. kind {
636
- ty:: Adt ( adt, substs) => {
637
- let is_non_exhaustive = self . is_foreign_non_exhaustive_variant ( adt_ty, variant) ;
638
- let field_ty = field. ty ( self . tcx , substs) ;
639
- let is_visible =
640
- adt. is_enum ( ) || field. vis . is_accessible_from ( self . module , self . tcx ) ;
641
- let is_uninhabited = self . is_uninhabited ( field_ty) ;
642
- is_uninhabited && ( !is_visible || is_non_exhaustive)
643
- }
644
- _ => false ,
645
- }
646
- }
647
617
}
648
618
649
619
#[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
@@ -789,21 +759,6 @@ impl<'tcx> Constructor<'tcx> {
789
759
}
790
760
}
791
761
792
- /// Returns whether the fields of this constructor should be treated as `non_exhaustive`.
793
- fn is_field_list_non_exhaustive < ' a > (
794
- & self ,
795
- cx : & MatchCheckCtxt < ' a , ' tcx > ,
796
- ty : Ty < ' tcx > ,
797
- ) -> bool {
798
- match ty. kind {
799
- ty:: Adt ( adt, _) => {
800
- let variant = & adt. variants [ self . variant_index_for_adt ( cx, adt) ] ;
801
- cx. is_foreign_non_exhaustive_variant ( ty, variant)
802
- }
803
- _ => false ,
804
- }
805
- }
806
-
807
762
fn variant_index_for_adt < ' a > (
808
763
& self ,
809
764
cx : & MatchCheckCtxt < ' a , ' tcx > ,
@@ -1000,7 +955,6 @@ impl<'tcx> Constructor<'tcx> {
1000
955
#[ derive( Debug , Clone ) ]
1001
956
struct StructFields < ' p , ' tcx > {
1002
957
fields : SmallVec < [ StructField < ' p , ' tcx > ; 2 ] > ,
1003
- is_non_exhaustive : bool ,
1004
958
}
1005
959
1006
960
#[ derive( Debug , Copy , Clone ) ]
@@ -1051,13 +1005,21 @@ impl<'p, 'tcx> StructFields<'p, 'tcx> {
1051
1005
smallvec ! [ StructField :: wildcard_from_ty( cx, substs. type_at( 0 ) ) ]
1052
1006
} else {
1053
1007
let variant = & adt. variants [ constructor. variant_index_for_adt ( cx, adt) ] ;
1008
+ // Whether we must not match the fields of this variant exhaustively.
1009
+ let is_non_exhaustive =
1010
+ variant. is_field_list_non_exhaustive ( ) && !adt. did . is_local ( ) ;
1054
1011
variant
1055
1012
. fields
1056
1013
. iter ( )
1057
1014
. map ( |field| {
1058
1015
let field_ty = field. ty ( cx. tcx , substs) ;
1059
- // Filter out hidden fields so we don't know they are uninhabited.
1060
- if cx. hide_uninhabited_field ( ty, variant, field) {
1016
+ let is_visible =
1017
+ adt. is_enum ( ) || field. vis . is_accessible_from ( cx. module , cx. tcx ) ;
1018
+ let is_uninhabited = cx. is_uninhabited ( field_ty) ;
1019
+
1020
+ // In the cases of either a `#[non_exhaustive]` field list or a non-public field, we hide
1021
+ // uninhabited fields in order not to reveal the uninhabitedness of the whole variant.
1022
+ if is_uninhabited && ( !is_visible || is_non_exhaustive) {
1061
1023
StructField :: Hidden ( field_ty)
1062
1024
} else {
1063
1025
StructField :: wildcard_from_ty ( cx, field_ty)
@@ -1068,8 +1030,7 @@ impl<'p, 'tcx> StructFields<'p, 'tcx> {
1068
1030
}
1069
1031
_ => smallvec ! [ ] ,
1070
1032
} ;
1071
- let is_non_exhaustive = constructor. is_field_list_non_exhaustive ( cx, ty) ;
1072
- StructFields { fields, is_non_exhaustive }
1033
+ StructFields { fields }
1073
1034
}
1074
1035
1075
1036
/// Number of (filtered) patterns contained.
0 commit comments