Skip to content

Commit f82994b

Browse files
committed
Don't filter out skipped fields
1 parent 9216b42 commit f82994b

File tree

4 files changed

+28
-22
lines changed

4 files changed

+28
-22
lines changed

compiler/rustc_pattern_analysis/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ pub trait TypeCx: Sized + fmt::Debug {
109109
/// The number of fields for this constructor.
110110
fn ctor_arity(&self, ctor: &Constructor<Self>, ty: &Self::Ty) -> usize;
111111

112-
/// The types of the fields for this constructor. The result must contain `ctor_arity()`-many
113-
/// fields that are not skipped.
112+
/// The types of the fields for this constructor. The result must contain `ctor_arity()` fields.
114113
fn ctor_sub_tys<'a>(
115114
&'a self,
116115
ctor: &'a Constructor<Self>,

compiler/rustc_pattern_analysis/src/pat.rs

-5
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ impl PatId {
2323
/// Values and patterns can be represented as a constructor applied to some fields. This represents
2424
/// a pattern in this form. A `DeconstructedPat` will almost always come from user input; the only
2525
/// exception are some `Wildcard`s introduced during pattern lowering.
26-
///
27-
/// Note that the number of fields may not match the fields declared in the original struct/variant.
28-
/// This happens if a private or `non_exhaustive` field is uninhabited, because the code mustn't
29-
/// observe that it is uninhabited. In that case that field is not included in `fields`. Care must
30-
/// be taken when converting to/from `thir::Pat`.
3126
pub struct DeconstructedPat<Cx: TypeCx> {
3227
ctor: Constructor<Cx>,
3328
fields: Vec<DeconstructedPat<Cx>>,

compiler/rustc_pattern_analysis/src/rustc.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,7 @@ impl<'p, 'tcx: 'p> RustcMatchCheckCtxt<'p, 'tcx> {
271271
} else {
272272
let variant =
273273
&adt.variant(RustcMatchCheckCtxt::variant_index_for_adt(&ctor, *adt));
274-
self.list_variant_nonhidden_fields(ty, variant)
275-
.filter(|(_, _, skip)| !skip)
276-
.count()
274+
self.list_variant_nonhidden_fields(ty, variant).count()
277275
}
278276
}
279277
_ => bug!("Unexpected type for constructor `{ctor:?}`: {ty:?}"),
@@ -512,14 +510,12 @@ impl<'p, 'tcx: 'p> RustcMatchCheckCtxt<'p, 'tcx> {
512510
// For each field in the variant, we store the relevant index into `self.fields` if any.
513511
let mut field_id_to_id: Vec<Option<usize>> =
514512
(0..variant.fields.len()).map(|_| None).collect();
515-
let tys = cx
516-
.list_variant_nonhidden_fields(ty, variant)
517-
.filter(|(_, _, skip)| !skip)
518-
.enumerate()
519-
.map(|(i, (field, ty, _))| {
513+
let tys = cx.list_variant_nonhidden_fields(ty, variant).enumerate().map(
514+
|(i, (field, ty, _))| {
520515
field_id_to_id[field.index()] = Some(i);
521516
ty
522-
});
517+
},
518+
);
523519
fields = tys.map(|ty| DeconstructedPat::wildcard(ty)).collect();
524520
for pat in subpatterns {
525521
if let Some(i) = field_id_to_id[pat.field.index()] {
@@ -769,7 +765,6 @@ impl<'p, 'tcx: 'p> RustcMatchCheckCtxt<'p, 'tcx> {
769765
let variant = &adt_def.variant(variant_index);
770766
let subpatterns = cx
771767
.list_variant_nonhidden_fields(*pat.ty(), variant)
772-
.filter(|(_, _, skip)| !skip)
773768
.zip(subpatterns)
774769
.map(|((field, _ty, _), pattern)| FieldPat { field, pattern })
775770
.collect();

compiler/rustc_pattern_analysis/src/usefulness.rs

+22-5
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,9 @@ impl fmt::Display for ValidityConstraint {
817817
struct PlaceInfo<Cx: TypeCx> {
818818
/// The type of the place.
819819
ty: Cx::Ty,
820+
/// Whether we must skip this field during analysis. This is used when a private field is empty,
821+
/// so that we don't observe its emptiness.
822+
skip: SkipField,
820823
/// Whether the place is known to contain valid data.
821824
validity: ValidityConstraint,
822825
/// Whether the place is the scrutinee itself or a subplace of it.
@@ -833,10 +836,9 @@ impl<Cx: TypeCx> PlaceInfo<Cx> {
833836
) -> impl Iterator<Item = Self> + ExactSizeIterator + Captures<'a> {
834837
let ctor_sub_tys = cx.ctor_sub_tys(ctor, &self.ty);
835838
let ctor_sub_validity = self.validity.specialize(ctor);
836-
// Collect to keep the `ExactSizeIterator` bound. This is a temporary measure.
837-
let tmp: Vec<_> = ctor_sub_tys.filter(|(_, SkipField(skip))| !skip).collect();
838-
tmp.into_iter().map(move |(ty, _)| PlaceInfo {
839+
ctor_sub_tys.map(move |(ty, skip)| PlaceInfo {
839840
ty,
841+
skip,
840842
validity: ctor_sub_validity,
841843
is_scrutinee: false,
842844
})
@@ -858,6 +860,11 @@ impl<Cx: TypeCx> PlaceInfo<Cx> {
858860
where
859861
Cx: 'a,
860862
{
863+
if matches!(self.skip, SkipField(true)) {
864+
// Skip the whole column
865+
return Ok((smallvec![Constructor::Skip], vec![]));
866+
}
867+
861868
let ctors_for_ty = cx.ctors_for_ty(&self.ty)?;
862869

863870
// We treat match scrutinees of type `!` or `EmptyEnum` differently.
@@ -916,7 +923,12 @@ impl<Cx: TypeCx> PlaceInfo<Cx> {
916923

917924
impl<Cx: TypeCx> Clone for PlaceInfo<Cx> {
918925
fn clone(&self) -> Self {
919-
Self { ty: self.ty.clone(), validity: self.validity, is_scrutinee: self.is_scrutinee }
926+
Self {
927+
ty: self.ty.clone(),
928+
skip: self.skip,
929+
validity: self.validity,
930+
is_scrutinee: self.is_scrutinee,
931+
}
920932
}
921933
}
922934

@@ -1123,7 +1135,12 @@ impl<'p, Cx: TypeCx> Matrix<'p, Cx> {
11231135
scrut_ty: Cx::Ty,
11241136
scrut_validity: ValidityConstraint,
11251137
) -> Self {
1126-
let place_info = PlaceInfo { ty: scrut_ty, validity: scrut_validity, is_scrutinee: true };
1138+
let place_info = PlaceInfo {
1139+
ty: scrut_ty,
1140+
skip: SkipField(false),
1141+
validity: scrut_validity,
1142+
is_scrutinee: true,
1143+
};
11271144
let mut matrix = Matrix {
11281145
rows: Vec::with_capacity(arms.len()),
11291146
place_info: smallvec![place_info],

0 commit comments

Comments
 (0)