1
Fork 0

We only need the arity of the subtype list now

This commit is contained in:
Nadrieril 2024-01-03 01:25:32 +01:00
parent 4ae2840e84
commit 1a3edc169b
3 changed files with 13 additions and 18 deletions

View file

@ -88,9 +88,8 @@ impl<'p, 'tcx> PatternColumn<'p, 'tcx> {
(0..arity).map(|_| Self { patterns: Vec::new() }).collect(); (0..arity).map(|_| Self { patterns: Vec::new() }).collect();
let relevant_patterns = let relevant_patterns =
self.patterns.iter().filter(|pat| ctor.is_covered_by(pcx, pat.ctor())); self.patterns.iter().filter(|pat| ctor.is_covered_by(pcx, pat.ctor()));
let ctor_sub_tys = pcx.ctor_sub_tys(ctor);
for pat in relevant_patterns { for pat in relevant_patterns {
let specialized = pat.specialize(pcx, ctor, ctor_sub_tys); let specialized = pat.specialize(ctor, arity);
for (subpat, column) in specialized.into_iter().zip(&mut specialized_columns) { for (subpat, column) in specialized.into_iter().zip(&mut specialized_columns) {
column.expand_and_push(subpat); column.expand_and_push(subpat);
} }

View file

@ -72,11 +72,10 @@ impl<'p, Cx: TypeCx> DeconstructedPat<'p, Cx> {
/// `other_ctor` can be different from `self.ctor`, but must be covered by it. /// `other_ctor` can be different from `self.ctor`, but must be covered by it.
pub(crate) fn specialize( pub(crate) fn specialize(
&self, &self,
_pcx: &PlaceCtxt<'_, 'p, Cx>,
other_ctor: &Constructor<Cx>, other_ctor: &Constructor<Cx>,
ctor_sub_tys: &[Cx::Ty], ctor_arity: usize,
) -> SmallVec<[PatOrWild<'p, Cx>; 2]> { ) -> SmallVec<[PatOrWild<'p, Cx>; 2]> {
let wildcard_sub_tys = || ctor_sub_tys.iter().map(|_| Wild).collect(); let wildcard_sub_tys = || (0..ctor_arity).map(|_| Wild).collect();
match (&self.ctor, other_ctor) { match (&self.ctor, other_ctor) {
// Return a wildcard for each field of `other_ctor`. // Return a wildcard for each field of `other_ctor`.
(Wildcard, _) => wildcard_sub_tys(), (Wildcard, _) => wildcard_sub_tys(),
@ -195,13 +194,12 @@ impl<'p, Cx: TypeCx> PatOrWild<'p, Cx> {
/// `other_ctor` can be different from `self.ctor`, but must be covered by it. /// `other_ctor` can be different from `self.ctor`, but must be covered by it.
pub(crate) fn specialize( pub(crate) fn specialize(
&self, &self,
pcx: &PlaceCtxt<'_, 'p, Cx>,
other_ctor: &Constructor<Cx>, other_ctor: &Constructor<Cx>,
ctor_sub_tys: &[Cx::Ty], ctor_arity: usize,
) -> SmallVec<[PatOrWild<'p, Cx>; 2]> { ) -> SmallVec<[PatOrWild<'p, Cx>; 2]> {
match self { match self {
Wild => ctor_sub_tys.iter().map(|_| Wild).collect(), Wild => (0..ctor_arity).map(|_| Wild).collect(),
Pat(pat) => pat.specialize(pcx, other_ctor, ctor_sub_tys), Pat(pat) => pat.specialize(other_ctor, ctor_arity),
} }
} }

View file

@ -869,14 +869,13 @@ impl<'p, Cx: TypeCx> PatStack<'p, Cx> {
/// Only call if `ctor.is_covered_by(self.head().ctor())` is true. /// Only call if `ctor.is_covered_by(self.head().ctor())` is true.
fn pop_head_constructor( fn pop_head_constructor(
&self, &self,
pcx: &PlaceCtxt<'_, 'p, Cx>,
ctor: &Constructor<Cx>, ctor: &Constructor<Cx>,
ctor_sub_tys: &[Cx::Ty], ctor_arity: usize,
ctor_is_relevant: bool, ctor_is_relevant: bool,
) -> PatStack<'p, Cx> { ) -> PatStack<'p, Cx> {
// We pop the head pattern and push the new fields extracted from the arguments of // We pop the head pattern and push the new fields extracted from the arguments of
// `self.head()`. // `self.head()`.
let mut new_pats = self.head().specialize(pcx, ctor, ctor_sub_tys); let mut new_pats = self.head().specialize(ctor, ctor_arity);
new_pats.extend_from_slice(&self.pats[1..]); new_pats.extend_from_slice(&self.pats[1..]);
// `ctor` is relevant for this row if it is the actual constructor of this row, or if the // `ctor` is relevant for this row if it is the actual constructor of this row, or if the
// row has a wildcard and `ctor` is relevant for wildcards. // row has a wildcard and `ctor` is relevant for wildcards.
@ -946,14 +945,13 @@ impl<'p, Cx: TypeCx> MatrixRow<'p, Cx> {
/// Only call if `ctor.is_covered_by(self.head().ctor())` is true. /// Only call if `ctor.is_covered_by(self.head().ctor())` is true.
fn pop_head_constructor( fn pop_head_constructor(
&self, &self,
pcx: &PlaceCtxt<'_, 'p, Cx>,
ctor: &Constructor<Cx>, ctor: &Constructor<Cx>,
ctor_sub_tys: &[Cx::Ty], ctor_arity: usize,
ctor_is_relevant: bool, ctor_is_relevant: bool,
parent_row: usize, parent_row: usize,
) -> MatrixRow<'p, Cx> { ) -> MatrixRow<'p, Cx> {
MatrixRow { MatrixRow {
pats: self.pats.pop_head_constructor(pcx, ctor, ctor_sub_tys, ctor_is_relevant), pats: self.pats.pop_head_constructor(ctor, ctor_arity, ctor_is_relevant),
parent_row, parent_row,
is_under_guard: self.is_under_guard, is_under_guard: self.is_under_guard,
useful: false, useful: false,
@ -1063,11 +1061,12 @@ impl<'p, Cx: TypeCx> Matrix<'p, Cx> {
ctor_is_relevant: bool, ctor_is_relevant: bool,
) -> Matrix<'p, Cx> { ) -> Matrix<'p, Cx> {
let ctor_sub_tys = pcx.ctor_sub_tys(ctor); let ctor_sub_tys = pcx.ctor_sub_tys(ctor);
let arity = ctor_sub_tys.len();
let specialized_place_ty = let specialized_place_ty =
ctor_sub_tys.iter().chain(self.place_ty[1..].iter()).copied().collect(); ctor_sub_tys.iter().chain(self.place_ty[1..].iter()).copied().collect();
let ctor_sub_validity = self.place_validity[0].specialize(ctor); let ctor_sub_validity = self.place_validity[0].specialize(ctor);
let specialized_place_validity = std::iter::repeat(ctor_sub_validity) let specialized_place_validity = std::iter::repeat(ctor_sub_validity)
.take(ctor.arity(pcx)) .take(arity)
.chain(self.place_validity[1..].iter().copied()) .chain(self.place_validity[1..].iter().copied())
.collect(); .collect();
let mut matrix = Matrix { let mut matrix = Matrix {
@ -1078,8 +1077,7 @@ impl<'p, Cx: TypeCx> Matrix<'p, Cx> {
}; };
for (i, row) in self.rows().enumerate() { for (i, row) in self.rows().enumerate() {
if ctor.is_covered_by(pcx, row.head().ctor()) { if ctor.is_covered_by(pcx, row.head().ctor()) {
let new_row = let new_row = row.pop_head_constructor(ctor, arity, ctor_is_relevant, i);
row.pop_head_constructor(pcx, ctor, ctor_sub_tys, ctor_is_relevant, i);
matrix.expand_and_push(new_row); matrix.expand_and_push(new_row);
} }
} }