Skip to content

Commit 2bf8ade

Browse files
committed
Push the decision to skip fields further down
1 parent 753acf7 commit 2bf8ade

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

compiler/rustc_pattern_analysis/src/lib.rs

+7-3
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

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::fmt;
55
use smallvec::{smallvec, SmallVec};
66

77
use crate::constructor::{Constructor, Slice, SliceKind};
8-
use crate::TypeCx;
8+
use crate::{SkipField, TypeCx};
99

1010
use self::Constructor::*;
1111

@@ -300,7 +300,11 @@ impl<Cx: TypeCx> WitnessPat<Cx> {
300300
/// For example, if `ctor` is a `Constructor::Variant` for `Option::Some`, we get the pattern
301301
/// `Some(_)`.
302302
pub(crate) fn wild_from_ctor(cx: &Cx, ctor: Constructor<Cx>, ty: Cx::Ty) -> Self {
303-
let fields = cx.ctor_sub_tys(&ctor, &ty).map(|ty| Self::wildcard(ty)).collect();
303+
let fields = cx
304+
.ctor_sub_tys(&ctor, &ty)
305+
.filter(|(_, SkipField(skip))| !skip)
306+
.map(|(ty, _)| Self::wildcard(ty))
307+
.collect();
304308
Self::new(ctor, fields, ty)
305309
}
306310

compiler/rustc_pattern_analysis/src/rustc.rs

+9-7
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

+4-2
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ use std::fmt;
716716

717717
use crate::constructor::{Constructor, ConstructorSet, IntRange};
718718
use crate::pat::{DeconstructedPat, PatId, PatOrWild, WitnessPat};
719-
use crate::{Captures, MatchArm, TypeCx};
719+
use crate::{Captures, MatchArm, SkipField, TypeCx};
720720

721721
use self::ValidityConstraint::*;
722722

@@ -833,7 +833,9 @@ impl<Cx: TypeCx> PlaceInfo<Cx> {
833833
) -> impl Iterator<Item = Self> + ExactSizeIterator + Captures<'a> {
834834
let ctor_sub_tys = cx.ctor_sub_tys(ctor, &self.ty);
835835
let ctor_sub_validity = self.validity.specialize(ctor);
836-
ctor_sub_tys.map(move |ty| PlaceInfo {
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 {
837839
ty,
838840
validity: ctor_sub_validity,
839841
is_scrutinee: false,

0 commit comments

Comments
 (0)