Yeet some lifetimes

This commit is contained in:
Michael Goulet 2023-12-26 01:59:18 +00:00
parent e4c626dd9a
commit eebb2abe0b
4 changed files with 40 additions and 40 deletions

View File

@ -91,7 +91,7 @@ pub struct MatchCtxt<'a, 'p, Cx: TypeCx> {
/// The context for type information. /// The context for type information.
pub tycx: &'a Cx, pub tycx: &'a Cx,
/// An arena to store the wildcards we produce during analysis. /// An arena to store the wildcards we produce during analysis.
pub wildcard_arena: &'a TypedArena<DeconstructedPat<'p, Cx>>, pub wildcard_arena: &'p TypedArena<DeconstructedPat<'p, Cx>>,
} }
/// The arm of a match expression. /// The arm of a match expression.

View File

@ -28,11 +28,11 @@
/// ///
/// This is not used in the main algorithm; only in lints. /// This is not used in the main algorithm; only in lints.
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct PatternColumn<'a, 'p, 'tcx> { pub(crate) struct PatternColumn<'p, 'tcx> {
patterns: Vec<&'a DeconstructedPat<'p, 'tcx>>, patterns: Vec<&'p DeconstructedPat<'p, 'tcx>>,
} }
impl<'a, 'p, 'tcx> PatternColumn<'a, 'p, 'tcx> { impl<'p, 'tcx> PatternColumn<'p, 'tcx> {
pub(crate) fn new(arms: &[MatchArm<'p, 'tcx>]) -> Self { pub(crate) fn new(arms: &[MatchArm<'p, 'tcx>]) -> Self {
let mut patterns = Vec::with_capacity(arms.len()); let mut patterns = Vec::with_capacity(arms.len());
for arm in arms { for arm in arms {
@ -48,7 +48,7 @@ pub(crate) fn new(arms: &[MatchArm<'p, 'tcx>]) -> Self {
fn is_empty(&self) -> bool { fn is_empty(&self) -> bool {
self.patterns.is_empty() self.patterns.is_empty()
} }
fn head_ty(&self, cx: MatchCtxt<'a, 'p, 'tcx>) -> Option<Ty<'tcx>> { fn head_ty(&self, cx: MatchCtxt<'_, 'p, 'tcx>) -> Option<Ty<'tcx>> {
if self.patterns.len() == 0 { if self.patterns.len() == 0 {
return None; return None;
} }
@ -64,7 +64,7 @@ fn analyze_ctors(&self, pcx: &PlaceCtxt<'_, 'p, 'tcx>) -> SplitConstructorSet<'p
pcx.ctors_for_ty().split(pcx, column_ctors) pcx.ctors_for_ty().split(pcx, column_ctors)
} }
fn iter<'b>(&'b self) -> impl Iterator<Item = &'a DeconstructedPat<'p, 'tcx>> + Captures<'b> { fn iter<'b>(&'b self) -> impl Iterator<Item = &'p DeconstructedPat<'p, 'tcx>> + Captures<'b> {
self.patterns.iter().copied() self.patterns.iter().copied()
} }
@ -75,9 +75,9 @@ fn iter<'b>(&'b self) -> impl Iterator<Item = &'a DeconstructedPat<'p, 'tcx>> +
/// which may change the lengths. /// which may change the lengths.
fn specialize( fn specialize(
&self, &self,
pcx: &PlaceCtxt<'a, 'p, 'tcx>, pcx: &PlaceCtxt<'_, 'p, 'tcx>,
ctor: &Constructor<'p, 'tcx>, ctor: &Constructor<'p, 'tcx>,
) -> Vec<PatternColumn<'a, 'p, 'tcx>> { ) -> Vec<PatternColumn<'p, 'tcx>> {
let arity = ctor.arity(pcx); let arity = ctor.arity(pcx);
if arity == 0 { if arity == 0 {
return Vec::new(); return Vec::new();
@ -115,7 +115,7 @@ fn specialize(
#[instrument(level = "debug", skip(cx), ret)] #[instrument(level = "debug", skip(cx), ret)]
fn collect_nonexhaustive_missing_variants<'a, 'p, 'tcx>( fn collect_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
cx: MatchCtxt<'a, 'p, 'tcx>, cx: MatchCtxt<'a, 'p, 'tcx>,
column: &PatternColumn<'a, 'p, 'tcx>, column: &PatternColumn<'p, 'tcx>,
) -> Vec<WitnessPat<'p, 'tcx>> { ) -> Vec<WitnessPat<'p, 'tcx>> {
let Some(ty) = column.head_ty(cx) else { let Some(ty) = column.head_ty(cx) else {
return Vec::new(); return Vec::new();
@ -163,7 +163,7 @@ fn collect_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
pub(crate) fn lint_nonexhaustive_missing_variants<'a, 'p, 'tcx>( pub(crate) fn lint_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
cx: MatchCtxt<'a, 'p, 'tcx>, cx: MatchCtxt<'a, 'p, 'tcx>,
arms: &[MatchArm<'p, 'tcx>], arms: &[MatchArm<'p, 'tcx>],
pat_column: &PatternColumn<'a, 'p, 'tcx>, pat_column: &PatternColumn<'p, 'tcx>,
scrut_ty: Ty<'tcx>, scrut_ty: Ty<'tcx>,
) { ) {
let rcx: &RustcMatchCheckCtxt<'_, '_> = cx.tycx; let rcx: &RustcMatchCheckCtxt<'_, '_> = cx.tycx;
@ -216,7 +216,7 @@ pub(crate) fn lint_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
#[instrument(level = "debug", skip(cx))] #[instrument(level = "debug", skip(cx))]
pub(crate) fn lint_overlapping_range_endpoints<'a, 'p, 'tcx>( pub(crate) fn lint_overlapping_range_endpoints<'a, 'p, 'tcx>(
cx: MatchCtxt<'a, 'p, 'tcx>, cx: MatchCtxt<'a, 'p, 'tcx>,
column: &PatternColumn<'a, 'p, 'tcx>, column: &PatternColumn<'p, 'tcx>,
) { ) {
let Some(ty) = column.head_ty(cx) else { let Some(ty) = column.head_ty(cx) else {
return; return;

View File

@ -83,7 +83,7 @@ pub(crate) fn specialize<'a>(
&self, &self,
pcx: &PlaceCtxt<'a, 'p, Cx>, pcx: &PlaceCtxt<'a, 'p, Cx>,
other_ctor: &Constructor<Cx>, other_ctor: &Constructor<Cx>,
) -> SmallVec<[&'a DeconstructedPat<'p, Cx>; 2]> { ) -> SmallVec<[&'p DeconstructedPat<'p, Cx>; 2]> {
let wildcard_sub_tys = || { let wildcard_sub_tys = || {
let tys = pcx.ctor_sub_tys(other_ctor); let tys = pcx.ctor_sub_tys(other_ctor);
tys.iter() tys.iter()

View File

@ -826,17 +826,17 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// - Cx global compilation context // - Cx global compilation context
#[derive(derivative::Derivative)] #[derive(derivative::Derivative)]
#[derivative(Clone(bound = ""))] #[derivative(Clone(bound = ""))]
struct PatStack<'a, 'p, Cx: TypeCx> { struct PatStack<'p, Cx: TypeCx> {
// Rows of len 1 are very common, which is why `SmallVec[_; 2]` works well. // Rows of len 1 are very common, which is why `SmallVec[_; 2]` works well.
pats: SmallVec<[&'a DeconstructedPat<'p, Cx>; 2]>, pats: SmallVec<[&'p DeconstructedPat<'p, Cx>; 2]>,
/// Sometimes we know that as far as this row is concerned, the current case is already handled /// Sometimes we know that as far as this row is concerned, the current case is already handled
/// by a different, more general, case. When the case is irrelevant for all rows this allows us /// by a different, more general, case. When the case is irrelevant for all rows this allows us
/// to skip a case entirely. This is purely an optimization. See at the top for details. /// to skip a case entirely. This is purely an optimization. See at the top for details.
relevant: bool, relevant: bool,
} }
impl<'a, 'p, Cx: TypeCx> PatStack<'a, 'p, Cx> { impl<'a, 'p, Cx: TypeCx> PatStack<'p, Cx> {
fn from_pattern(pat: &'a DeconstructedPat<'p, Cx>) -> Self { fn from_pattern(pat: &'p DeconstructedPat<'p, Cx>) -> Self {
PatStack { pats: smallvec![pat], relevant: true } PatStack { pats: smallvec![pat], relevant: true }
} }
@ -848,17 +848,17 @@ fn len(&self) -> usize {
self.pats.len() self.pats.len()
} }
fn head(&self) -> &'a DeconstructedPat<'p, Cx> { fn head(&self) -> &'p DeconstructedPat<'p, Cx> {
self.pats[0] self.pats[0]
} }
fn iter<'b>(&'b self) -> impl Iterator<Item = &'a DeconstructedPat<'p, Cx>> + Captures<'b> { fn iter<'b>(&'b self) -> impl Iterator<Item = &'p DeconstructedPat<'p, Cx>> + Captures<'b> {
self.pats.iter().copied() self.pats.iter().copied()
} }
// Recursively expand the first or-pattern into its subpatterns. Only useful if the pattern is // Recursively expand the first or-pattern into its subpatterns. Only useful if the pattern is
// an or-pattern. Panics if `self` is empty. // an or-pattern. Panics if `self` is empty.
fn expand_or_pat<'b>(&'b self) -> impl Iterator<Item = PatStack<'a, 'p, Cx>> + Captures<'b> { fn expand_or_pat<'b>(&'b self) -> impl Iterator<Item = PatStack<'p, Cx>> + Captures<'b> {
self.head().flatten_or_pat().into_iter().map(move |pat| { self.head().flatten_or_pat().into_iter().map(move |pat| {
let mut new = self.clone(); let mut new = self.clone();
new.pats[0] = pat; new.pats[0] = pat;
@ -873,7 +873,7 @@ fn pop_head_constructor(
pcx: &PlaceCtxt<'a, 'p, Cx>, pcx: &PlaceCtxt<'a, 'p, Cx>,
ctor: &Constructor<Cx>, ctor: &Constructor<Cx>,
ctor_is_relevant: bool, ctor_is_relevant: bool,
) -> PatStack<'a, '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); let mut new_pats = self.head().specialize(pcx, ctor);
@ -886,7 +886,7 @@ fn pop_head_constructor(
} }
} }
impl<'a, 'p, Cx: TypeCx> fmt::Debug for PatStack<'a, 'p, Cx> { impl<'p, Cx: TypeCx> fmt::Debug for PatStack<'p, Cx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// We pretty-print similarly to the `Debug` impl of `Matrix`. // We pretty-print similarly to the `Debug` impl of `Matrix`.
write!(f, "+")?; write!(f, "+")?;
@ -899,9 +899,9 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// A row of the matrix. /// A row of the matrix.
#[derive(Clone)] #[derive(Clone)]
struct MatrixRow<'a, 'p, Cx: TypeCx> { struct MatrixRow<'p, Cx: TypeCx> {
// The patterns in the row. // The patterns in the row.
pats: PatStack<'a, 'p, Cx>, pats: PatStack<'p, Cx>,
/// Whether the original arm had a guard. This is inherited when specializing. /// Whether the original arm had a guard. This is inherited when specializing.
is_under_guard: bool, is_under_guard: bool,
/// When we specialize, we remember which row of the original matrix produced a given row of the /// When we specialize, we remember which row of the original matrix produced a given row of the
@ -914,7 +914,7 @@ struct MatrixRow<'a, 'p, Cx: TypeCx> {
useful: bool, useful: bool,
} }
impl<'a, 'p, Cx: TypeCx> MatrixRow<'a, 'p, Cx> { impl<'a, 'p, Cx: TypeCx> MatrixRow<'p, Cx> {
fn is_empty(&self) -> bool { fn is_empty(&self) -> bool {
self.pats.is_empty() self.pats.is_empty()
} }
@ -923,17 +923,17 @@ fn len(&self) -> usize {
self.pats.len() self.pats.len()
} }
fn head(&self) -> &'a DeconstructedPat<'p, Cx> { fn head(&self) -> &'p DeconstructedPat<'p, Cx> {
self.pats.head() self.pats.head()
} }
fn iter<'b>(&'b self) -> impl Iterator<Item = &'a DeconstructedPat<'p, Cx>> + Captures<'b> { fn iter<'b>(&'b self) -> impl Iterator<Item = &'p DeconstructedPat<'p, Cx>> + Captures<'b> {
self.pats.iter() self.pats.iter()
} }
// Recursively expand the first or-pattern into its subpatterns. Only useful if the pattern is // Recursively expand the first or-pattern into its subpatterns. Only useful if the pattern is
// an or-pattern. Panics if `self` is empty. // an or-pattern. Panics if `self` is empty.
fn expand_or_pat<'b>(&'b self) -> impl Iterator<Item = MatrixRow<'a, 'p, Cx>> + Captures<'b> { fn expand_or_pat<'b>(&'b self) -> impl Iterator<Item = MatrixRow<'p, Cx>> + Captures<'b> {
self.pats.expand_or_pat().map(|patstack| MatrixRow { self.pats.expand_or_pat().map(|patstack| MatrixRow {
pats: patstack, pats: patstack,
parent_row: self.parent_row, parent_row: self.parent_row,
@ -950,7 +950,7 @@ fn pop_head_constructor(
ctor: &Constructor<Cx>, ctor: &Constructor<Cx>,
ctor_is_relevant: bool, ctor_is_relevant: bool,
parent_row: usize, parent_row: usize,
) -> MatrixRow<'a, 'p, Cx> { ) -> MatrixRow<'p, Cx> {
MatrixRow { MatrixRow {
pats: self.pats.pop_head_constructor(pcx, ctor, ctor_is_relevant), pats: self.pats.pop_head_constructor(pcx, ctor, ctor_is_relevant),
parent_row, parent_row,
@ -960,7 +960,7 @@ fn pop_head_constructor(
} }
} }
impl<'a, 'p, Cx: TypeCx> fmt::Debug for MatrixRow<'a, 'p, Cx> { impl<'p, Cx: TypeCx> fmt::Debug for MatrixRow<'p, Cx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.pats.fmt(f) self.pats.fmt(f)
} }
@ -977,22 +977,22 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// specializing `(,)` and `Some` on a pattern of type `(Option<u32>, bool)`, the first column of /// specializing `(,)` and `Some` on a pattern of type `(Option<u32>, bool)`, the first column of
/// the matrix will correspond to `scrutinee.0.Some.0` and the second column to `scrutinee.1`. /// the matrix will correspond to `scrutinee.0.Some.0` and the second column to `scrutinee.1`.
#[derive(Clone)] #[derive(Clone)]
struct Matrix<'a, 'p, Cx: TypeCx> { struct Matrix<'p, Cx: TypeCx> {
/// Vector of rows. The rows must form a rectangular 2D array. Moreover, all the patterns of /// Vector of rows. The rows must form a rectangular 2D array. Moreover, all the patterns of
/// each column must have the same type. Each column corresponds to a place within the /// each column must have the same type. Each column corresponds to a place within the
/// scrutinee. /// scrutinee.
rows: Vec<MatrixRow<'a, 'p, Cx>>, rows: Vec<MatrixRow<'p, Cx>>,
/// Stores an extra fictitious row full of wildcards. Mostly used to keep track of the type of /// Stores an extra fictitious row full of wildcards. Mostly used to keep track of the type of
/// each column. This must obey the same invariants as the real rows. /// each column. This must obey the same invariants as the real rows.
wildcard_row: PatStack<'a, 'p, Cx>, wildcard_row: PatStack<'p, Cx>,
/// Track for each column/place whether it contains a known valid value. /// Track for each column/place whether it contains a known valid value.
place_validity: SmallVec<[ValidityConstraint; 2]>, place_validity: SmallVec<[ValidityConstraint; 2]>,
} }
impl<'a, 'p, Cx: TypeCx> Matrix<'a, 'p, Cx> { impl<'a, 'p, Cx: TypeCx> Matrix<'p, Cx> {
/// Pushes a new row to the matrix. If the row starts with an or-pattern, this recursively /// Pushes a new row to the matrix. If the row starts with an or-pattern, this recursively
/// expands it. Internal method, prefer [`Matrix::new`]. /// expands it. Internal method, prefer [`Matrix::new`].
fn expand_and_push(&mut self, row: MatrixRow<'a, 'p, Cx>) { fn expand_and_push(&mut self, row: MatrixRow<'p, Cx>) {
if !row.is_empty() && row.head().is_or_pat() { if !row.is_empty() && row.head().is_or_pat() {
// Expand nested or-patterns. // Expand nested or-patterns.
for new_row in row.expand_or_pat() { for new_row in row.expand_or_pat() {
@ -1005,7 +1005,7 @@ fn expand_and_push(&mut self, row: MatrixRow<'a, 'p, Cx>) {
/// Build a new matrix from an iterator of `MatchArm`s. /// Build a new matrix from an iterator of `MatchArm`s.
fn new( fn new(
wildcard_arena: &'a TypedArena<DeconstructedPat<'p, Cx>>, wildcard_arena: &'p TypedArena<DeconstructedPat<'p, Cx>>,
arms: &'a [MatchArm<'p, Cx>], arms: &'a [MatchArm<'p, Cx>],
scrut_ty: Cx::Ty, scrut_ty: Cx::Ty,
scrut_validity: ValidityConstraint, scrut_validity: ValidityConstraint,
@ -1044,13 +1044,13 @@ fn column_count(&self) -> usize {
fn rows<'b>( fn rows<'b>(
&'b self, &'b self,
) -> impl Iterator<Item = &'b MatrixRow<'a, 'p, Cx>> + Clone + DoubleEndedIterator + ExactSizeIterator ) -> impl Iterator<Item = &'b MatrixRow<'p, Cx>> + Clone + DoubleEndedIterator + ExactSizeIterator
{ {
self.rows.iter() self.rows.iter()
} }
fn rows_mut<'b>( fn rows_mut<'b>(
&'b mut self, &'b mut self,
) -> impl Iterator<Item = &'b mut MatrixRow<'a, 'p, Cx>> + DoubleEndedIterator + ExactSizeIterator ) -> impl Iterator<Item = &'b mut MatrixRow<'p, Cx>> + DoubleEndedIterator + ExactSizeIterator
{ {
self.rows.iter_mut() self.rows.iter_mut()
} }
@ -1068,7 +1068,7 @@ fn specialize_constructor(
pcx: &PlaceCtxt<'a, 'p, Cx>, pcx: &PlaceCtxt<'a, 'p, Cx>,
ctor: &Constructor<Cx>, ctor: &Constructor<Cx>,
ctor_is_relevant: bool, ctor_is_relevant: bool,
) -> Matrix<'a, 'p, Cx> { ) -> Matrix<'p, Cx> {
let wildcard_row = self.wildcard_row.pop_head_constructor(pcx, ctor, ctor_is_relevant); let wildcard_row = self.wildcard_row.pop_head_constructor(pcx, ctor, ctor_is_relevant);
let new_validity = self.place_validity[0].specialize(ctor); let new_validity = self.place_validity[0].specialize(ctor);
let new_place_validity = std::iter::repeat(new_validity) let new_place_validity = std::iter::repeat(new_validity)
@ -1097,7 +1097,7 @@ fn specialize_constructor(
/// + _ + [_, _, tail @ ..] + /// + _ + [_, _, tail @ ..] +
/// | ✓ | ? | // column validity /// | ✓ | ? | // column validity
/// ``` /// ```
impl<'a, 'p, Cx: TypeCx> fmt::Debug for Matrix<'a, 'p, Cx> { impl<'p, Cx: TypeCx> fmt::Debug for Matrix<'p, Cx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "\n")?; write!(f, "\n")?;
@ -1336,7 +1336,7 @@ fn extend(&mut self, other: Self) {
#[instrument(level = "debug", skip(mcx, is_top_level), ret)] #[instrument(level = "debug", skip(mcx, is_top_level), ret)]
fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>( fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>(
mcx: MatchCtxt<'a, 'p, Cx>, mcx: MatchCtxt<'a, 'p, Cx>,
matrix: &mut Matrix<'a, 'p, Cx>, matrix: &mut Matrix<'p, Cx>,
is_top_level: bool, is_top_level: bool,
) -> WitnessMatrix<Cx> { ) -> WitnessMatrix<Cx> {
debug_assert!(matrix.rows().all(|r| r.len() == matrix.column_count())); debug_assert!(matrix.rows().all(|r| r.len() == matrix.column_count()));