Skip to content

Commit a2ef115

Browse files
committed
Push the decision to skip fields further down
1 parent 6c4f81d commit a2ef115

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

compiler/rustc_pattern_analysis/src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ use crate::usefulness::{compute_match_usefulness, ValidityConstraint};
8282
pub trait Captures<'a> {}
8383
impl<'a, T: ?Sized> Captures<'a> for T {}
8484

85+
/// `bool` newtype that indicates whether we should skip this field during analysis.
86+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
87+
pub struct SkipField(pub bool);
88+
8589
/// Context that provides type information about constructors.
8690
///
8791
/// Most of the crate is parameterized on a type that implements this trait.
@@ -105,13 +109,13 @@ pub trait TypeCx: Sized + fmt::Debug {
105109
/// The number of fields for this constructor.
106110
fn ctor_arity(&self, ctor: &Constructor<Self>, ty: &Self::Ty) -> usize;
107111

108-
/// The types of the fields for this constructor. The result must have a length of
109-
/// `ctor_arity()`.
112+
/// The types of the fields for this constructor. The result must contain `ctor_arity()`-many
113+
/// fields that are not skipped.
110114
fn ctor_sub_tys<'a>(
111115
&'a self,
112116
ctor: &'a Constructor<Self>,
113117
ty: &'a Self::Ty,
114-
) -> impl Iterator<Item = Self::Ty> + ExactSizeIterator + Captures<'a>;
118+
) -> impl Iterator<Item = (Self::Ty, SkipField)> + ExactSizeIterator + Captures<'a>;
115119

116120
/// The set of all the constructors for `ty`.
117121
///

compiler/rustc_pattern_analysis/src/pat.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::fmt;
66
use smallvec::{smallvec, SmallVec};
77

88
use crate::constructor::{Constructor, Slice, SliceKind};
9-
use crate::TypeCx;
9+
use crate::{SkipField, TypeCx};
1010

1111
use self::Constructor::*;
1212

@@ -328,7 +328,11 @@ impl<Cx: TypeCx> WitnessPat<Cx> {
328328
/// For example, if `ctor` is a `Constructor::Variant` for `Option::Some`, we get the pattern
329329
/// `Some(_)`.
330330
pub(crate) fn wild_from_ctor(cx: &Cx, ctor: Constructor<Cx>, ty: Cx::Ty) -> Self {
331-
let fields = cx.ctor_sub_tys(&ctor, &ty).map(|ty| Self::wildcard(ty)).collect();
331+
let fields = cx
332+
.ctor_sub_tys(&ctor, &ty)
333+
.filter(|(_, SkipField(skip))| !skip)
334+
.map(|(ty, _)| Self::wildcard(ty))
335+
.collect();
332336
Self::new(ctor, fields, ty)
333337
}
334338

compiler/rustc_pattern_analysis/src/rustc.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_target::abi::{FieldIdx, Integer, VariantIdx, FIRST_VARIANT};
1818
use crate::constructor::{
1919
IntRange, MaybeInfiniteInt, OpaqueId, RangeEnd, Slice, SliceKind, VariantVisibility,
2020
};
21-
use crate::{errors, Captures, TypeCx};
21+
use crate::{errors, Captures, SkipField, TypeCx};
2222

2323
use crate::constructor::Constructor::*;
2424

@@ -208,12 +208,15 @@ impl<'p, 'tcx: 'p> RustcMatchCheckCtxt<'p, 'tcx> {
208208
&'a self,
209209
ctor: &'a Constructor<'p, 'tcx>,
210210
ty: RevealedTy<'tcx>,
211-
) -> impl Iterator<Item = RevealedTy<'tcx>> + ExactSizeIterator + Captures<'a> {
211+
) -> impl Iterator<Item = (RevealedTy<'tcx>, SkipField)> + ExactSizeIterator + Captures<'a>
212+
{
212213
fn reveal_and_alloc<'a, 'tcx>(
213214
cx: &'a RustcMatchCheckCtxt<'_, 'tcx>,
214215
iter: impl Iterator<Item = Ty<'tcx>>,
215-
) -> &'a [RevealedTy<'tcx>] {
216-
cx.dropless_arena.alloc_from_iter(iter.map(|ty| cx.reveal_opaque_ty(ty)))
216+
) -> &'a [(RevealedTy<'tcx>, SkipField)] {
217+
cx.dropless_arena.alloc_from_iter(
218+
iter.map(|ty| cx.reveal_opaque_ty(ty)).map(|ty| (ty, SkipField(false))),
219+
)
217220
}
218221
let cx = self;
219222
let slice = match ctor {
@@ -229,8 +232,7 @@ impl<'p, 'tcx: 'p> RustcMatchCheckCtxt<'p, 'tcx> {
229232
&adt.variant(RustcMatchCheckCtxt::variant_index_for_adt(&ctor, *adt));
230233
let tys = cx
231234
.list_variant_nonhidden_fields(ty, variant)
232-
.filter(|(_, _, skip)| !skip)
233-
.map(|(_, ty, _)| ty);
235+
.map(|(_, ty, skip)| (ty, SkipField(skip)));
234236
cx.dropless_arena.alloc_from_iter(tys)
235237
}
236238
}
@@ -872,7 +874,7 @@ impl<'p, 'tcx: 'p> TypeCx for RustcMatchCheckCtxt<'p, 'tcx> {
872874
&'a self,
873875
ctor: &'a crate::constructor::Constructor<Self>,
874876
ty: &'a Self::Ty,
875-
) -> impl Iterator<Item = Self::Ty> + ExactSizeIterator + Captures<'a> {
877+
) -> impl Iterator<Item = (Self::Ty, SkipField)> + ExactSizeIterator + Captures<'a> {
876878
self.ctor_sub_tys(ctor, *ty)
877879
}
878880
fn ctors_for_ty(

compiler/rustc_pattern_analysis/src/usefulness.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ use std::fmt;
719719

720720
use crate::constructor::{Constructor, ConstructorSet, IntRange};
721721
use crate::pat::{DeconstructedPat, PatOrWild, WitnessPat};
722-
use crate::{Captures, MatchArm, TypeCx};
722+
use crate::{Captures, MatchArm, SkipField, TypeCx};
723723

724724
use self::ValidityConstraint::*;
725725

@@ -840,7 +840,9 @@ impl<Cx: TypeCx> PlaceInfo<Cx> {
840840
) -> impl Iterator<Item = Self> + ExactSizeIterator + Captures<'a> {
841841
let ctor_sub_tys = cx.ctor_sub_tys(ctor, &self.ty);
842842
let ctor_sub_validity = self.validity.specialize(ctor);
843-
ctor_sub_tys.map(move |ty| PlaceInfo {
843+
// Collect to keep the `ExactSizeIterator` bound. This is a temporary measure.
844+
let tmp: Vec<_> = ctor_sub_tys.filter(|(_, SkipField(skip))| !skip).collect();
845+
tmp.into_iter().map(move |(ty, _)| PlaceInfo {
844846
ty,
845847
validity: ctor_sub_validity,
846848
is_scrutinee: false,

0 commit comments

Comments
 (0)