Skip to content

Commit b677c77

Browse files
authored
Rollup merge of #120317 - Nadrieril:dont-force-slice-of-ty, r=compiler-errors
pattern_analysis: Let `ctor_sub_tys` return any Iterator they want I noticed that we always `.cloned()` and allocate the output of `TypeCx::ctor_sub_tys` now, so there was no need to force it to return a slice. `ExactSizeIterator` is not super important but saves some manual counting. r? `@compiler-errors`
2 parents 0cbef47 + e088016 commit b677c77

File tree

4 files changed

+24
-19
lines changed

4 files changed

+24
-19
lines changed

compiler/rustc_pattern_analysis/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ pub trait TypeCx: Sized + fmt::Debug {
101101

102102
/// The types of the fields for this constructor. The result must have a length of
103103
/// `ctor_arity()`.
104-
fn ctor_sub_tys(&self, ctor: &Constructor<Self>, ty: &Self::Ty) -> &[Self::Ty];
104+
fn ctor_sub_tys<'a>(
105+
&'a self,
106+
ctor: &'a Constructor<Self>,
107+
ty: &'a Self::Ty,
108+
) -> impl Iterator<Item = Self::Ty> + ExactSizeIterator + Captures<'a>;
105109

106110
/// The set of all the constructors for `ty`.
107111
///

compiler/rustc_pattern_analysis/src/pat.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,7 @@ impl<Cx: TypeCx> WitnessPat<Cx> {
241241
/// For example, if `ctor` is a `Constructor::Variant` for `Option::Some`, we get the pattern
242242
/// `Some(_)`.
243243
pub(crate) fn wild_from_ctor(pcx: &PlaceCtxt<'_, Cx>, ctor: Constructor<Cx>) -> Self {
244-
let field_tys = pcx.ctor_sub_tys(&ctor);
245-
let fields = field_tys.iter().cloned().map(|ty| Self::wildcard(ty)).collect();
244+
let fields = pcx.ctor_sub_tys(&ctor).map(|ty| Self::wildcard(ty)).collect();
246245
Self::new(ctor, fields, pcx.ty.clone())
247246
}
248247

compiler/rustc_pattern_analysis/src/rustc.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::fmt;
33
use std::iter::once;
44

55
use rustc_arena::{DroplessArena, TypedArena};
6-
use rustc_data_structures::captures::Captures;
76
use rustc_hir::def_id::DefId;
87
use rustc_hir::HirId;
98
use rustc_index::{Idx, IndexVec};
@@ -20,7 +19,7 @@ use rustc_target::abi::{FieldIdx, Integer, VariantIdx, FIRST_VARIANT};
2019
use crate::constructor::{
2120
IntRange, MaybeInfiniteInt, OpaqueId, RangeEnd, Slice, SliceKind, VariantVisibility,
2221
};
23-
use crate::{errors, TypeCx};
22+
use crate::{errors, Captures, TypeCx};
2423

2524
use crate::constructor::Constructor::*;
2625

@@ -210,19 +209,19 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
210209
/// Returns the types of the fields for a given constructor. The result must have a length of
211210
/// `ctor.arity()`.
212211
#[instrument(level = "trace", skip(self))]
213-
pub(crate) fn ctor_sub_tys(
214-
&self,
215-
ctor: &Constructor<'p, 'tcx>,
212+
pub(crate) fn ctor_sub_tys<'a>(
213+
&'a self,
214+
ctor: &'a Constructor<'p, 'tcx>,
216215
ty: RevealedTy<'tcx>,
217-
) -> &[RevealedTy<'tcx>] {
216+
) -> impl Iterator<Item = RevealedTy<'tcx>> + ExactSizeIterator + Captures<'a> {
218217
fn reveal_and_alloc<'a, 'tcx>(
219218
cx: &'a RustcMatchCheckCtxt<'_, 'tcx>,
220219
iter: impl Iterator<Item = Ty<'tcx>>,
221220
) -> &'a [RevealedTy<'tcx>] {
222221
cx.dropless_arena.alloc_from_iter(iter.map(|ty| cx.reveal_opaque_ty(ty)))
223222
}
224223
let cx = self;
225-
match ctor {
224+
let slice = match ctor {
226225
Struct | Variant(_) | UnionField => match ty.kind() {
227226
ty::Tuple(fs) => reveal_and_alloc(cx, fs.iter()),
228227
ty::Adt(adt, args) => {
@@ -263,7 +262,8 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
263262
Or => {
264263
bug!("called `Fields::wildcards` on an `Or` ctor")
265264
}
266-
}
265+
};
266+
slice.iter().copied()
267267
}
268268

269269
/// The number of fields for this constructor.
@@ -964,11 +964,11 @@ impl<'p, 'tcx> TypeCx for RustcMatchCheckCtxt<'p, 'tcx> {
964964
fn ctor_arity(&self, ctor: &crate::constructor::Constructor<Self>, ty: &Self::Ty) -> usize {
965965
self.ctor_arity(ctor, *ty)
966966
}
967-
fn ctor_sub_tys(
968-
&self,
969-
ctor: &crate::constructor::Constructor<Self>,
970-
ty: &Self::Ty,
971-
) -> &[Self::Ty] {
967+
fn ctor_sub_tys<'a>(
968+
&'a self,
969+
ctor: &'a crate::constructor::Constructor<Self>,
970+
ty: &'a Self::Ty,
971+
) -> impl Iterator<Item = Self::Ty> + ExactSizeIterator + Captures<'a> {
972972
self.ctor_sub_tys(ctor, *ty)
973973
}
974974
fn ctors_for_ty(

compiler/rustc_pattern_analysis/src/usefulness.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,10 @@ impl<'a, Cx: TypeCx> PlaceCtxt<'a, Cx> {
750750
pub(crate) fn ctor_arity(&self, ctor: &Constructor<Cx>) -> usize {
751751
self.mcx.tycx.ctor_arity(ctor, self.ty)
752752
}
753-
pub(crate) fn ctor_sub_tys(&self, ctor: &Constructor<Cx>) -> &[Cx::Ty] {
753+
pub(crate) fn ctor_sub_tys(
754+
&'a self,
755+
ctor: &'a Constructor<Cx>,
756+
) -> impl Iterator<Item = Cx::Ty> + ExactSizeIterator + Captures<'a> {
754757
self.mcx.tycx.ctor_sub_tys(ctor, self.ty)
755758
}
756759
pub(crate) fn ctors_for_ty(&self) -> Result<ConstructorSet<Cx>, Cx::Error> {
@@ -1058,8 +1061,7 @@ impl<'p, Cx: TypeCx> Matrix<'p, Cx> {
10581061
) -> Matrix<'p, Cx> {
10591062
let ctor_sub_tys = pcx.ctor_sub_tys(ctor);
10601063
let arity = ctor_sub_tys.len();
1061-
let specialized_place_ty =
1062-
ctor_sub_tys.iter().chain(self.place_ty[1..].iter()).cloned().collect();
1064+
let specialized_place_ty = ctor_sub_tys.chain(self.place_ty[1..].iter().cloned()).collect();
10631065
let ctor_sub_validity = self.place_validity[0].specialize(ctor);
10641066
let specialized_place_validity = std::iter::repeat(ctor_sub_validity)
10651067
.take(arity)

0 commit comments

Comments
 (0)