1
Fork 0

Greatly simplify lifetime captures in edition 2024

This commit is contained in:
Michael Goulet 2025-02-20 18:58:46 +00:00
parent 46420c9607
commit 12e3911d81
84 changed files with 223 additions and 294 deletions

View file

@ -61,11 +61,11 @@ pub trait PatCx: Sized + fmt::Debug {
fn ctor_arity(&self, ctor: &Constructor<Self>, ty: &Self::Ty) -> usize;
/// The types of the fields for this constructor. The result must contain `ctor_arity()` fields.
fn ctor_sub_tys<'a>(
&'a self,
ctor: &'a Constructor<Self>,
ty: &'a Self::Ty,
) -> impl Iterator<Item = (Self::Ty, PrivateUninhabitedField)> + ExactSizeIterator + Captures<'a>;
fn ctor_sub_tys(
&self,
ctor: &Constructor<Self>,
ty: &Self::Ty,
) -> impl Iterator<Item = (Self::Ty, PrivateUninhabitedField)> + ExactSizeIterator;
/// The set of all the constructors for `ty`.
///

View file

@ -1,6 +1,6 @@
use crate::constructor::{Constructor, SplitConstructorSet};
use crate::pat::{DeconstructedPat, PatOrWild};
use crate::{Captures, MatchArm, PatCx};
use crate::{MatchArm, PatCx};
/// A column of patterns in a match, where a column is the intuitive notion of "subpatterns that
/// inspect the same subvalue/place".
@ -41,7 +41,7 @@ impl<'p, Cx: PatCx> PatternColumn<'p, Cx> {
pub fn head_ty(&self) -> Option<&Cx::Ty> {
self.patterns.first().map(|pat| pat.ty())
}
pub fn iter<'a>(&'a self) -> impl Iterator<Item = &'p DeconstructedPat<Cx>> + Captures<'a> {
pub fn iter(&self) -> impl Iterator<Item = &'p DeconstructedPat<Cx>> {
self.patterns.iter().copied()
}

View file

@ -25,7 +25,7 @@ use crate::lints::lint_nonexhaustive_missing_variants;
use crate::pat_column::PatternColumn;
use crate::rustc::print::EnumInfo;
use crate::usefulness::{PlaceValidity, compute_match_usefulness};
use crate::{Captures, PatCx, PrivateUninhabitedField, errors};
use crate::{PatCx, PrivateUninhabitedField, errors};
mod print;
@ -175,8 +175,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
&self,
ty: RevealedTy<'tcx>,
variant: &'tcx VariantDef,
) -> impl Iterator<Item = (&'tcx FieldDef, RevealedTy<'tcx>)> + Captures<'p> + Captures<'_>
{
) -> impl Iterator<Item = (&'tcx FieldDef, RevealedTy<'tcx>)> {
let ty::Adt(_, args) = ty.kind() else { bug!() };
variant.fields.iter().map(move |field| {
let ty = field.ty(self.tcx, args);
@ -203,13 +202,11 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
/// Returns the types of the fields for a given constructor. The result must have a length of
/// `ctor.arity()`.
pub(crate) fn ctor_sub_tys<'a>(
&'a self,
ctor: &'a Constructor<'p, 'tcx>,
pub(crate) fn ctor_sub_tys(
&self,
ctor: &Constructor<'p, 'tcx>,
ty: RevealedTy<'tcx>,
) -> impl Iterator<Item = (RevealedTy<'tcx>, PrivateUninhabitedField)>
+ ExactSizeIterator
+ Captures<'a> {
) -> impl Iterator<Item = (RevealedTy<'tcx>, PrivateUninhabitedField)> + ExactSizeIterator {
fn reveal_and_alloc<'a, 'tcx>(
cx: &'a RustcPatCtxt<'_, 'tcx>,
iter: impl Iterator<Item = Ty<'tcx>>,
@ -936,12 +933,11 @@ impl<'p, 'tcx: 'p> PatCx for RustcPatCtxt<'p, 'tcx> {
fn ctor_arity(&self, ctor: &crate::constructor::Constructor<Self>, ty: &Self::Ty) -> usize {
self.ctor_arity(ctor, *ty)
}
fn ctor_sub_tys<'a>(
&'a self,
ctor: &'a crate::constructor::Constructor<Self>,
ty: &'a Self::Ty,
) -> impl Iterator<Item = (Self::Ty, PrivateUninhabitedField)> + ExactSizeIterator + Captures<'a>
{
fn ctor_sub_tys(
&self,
ctor: &crate::constructor::Constructor<Self>,
ty: &Self::Ty,
) -> impl Iterator<Item = (Self::Ty, PrivateUninhabitedField)> + ExactSizeIterator {
self.ctor_sub_tys(ctor, *ty)
}
fn ctors_for_ty(

View file

@ -719,7 +719,7 @@ use tracing::{debug, instrument};
use self::PlaceValidity::*;
use crate::constructor::{Constructor, ConstructorSet, IntRange};
use crate::pat::{DeconstructedPat, PatId, PatOrWild, WitnessPat};
use crate::{Captures, MatchArm, PatCx, PrivateUninhabitedField};
use crate::{MatchArm, PatCx, PrivateUninhabitedField};
#[cfg(not(feature = "rustc"))]
pub fn ensure_sufficient_stack<R>(f: impl FnOnce() -> R) -> R {
f()
@ -902,11 +902,11 @@ struct PlaceInfo<Cx: PatCx> {
impl<Cx: PatCx> PlaceInfo<Cx> {
/// Given a constructor for the current place, we return one `PlaceInfo` for each field of the
/// constructor.
fn specialize<'a>(
&'a self,
cx: &'a Cx,
ctor: &'a Constructor<Cx>,
) -> impl Iterator<Item = Self> + ExactSizeIterator + Captures<'a> {
fn specialize(
&self,
cx: &Cx,
ctor: &Constructor<Cx>,
) -> impl Iterator<Item = Self> + ExactSizeIterator {
let ctor_sub_tys = cx.ctor_sub_tys(ctor, &self.ty);
let ctor_sub_validity = self.validity.specialize(ctor);
ctor_sub_tys.map(move |(ty, PrivateUninhabitedField(private_uninhabited))| PlaceInfo {
@ -1046,13 +1046,13 @@ impl<'p, Cx: PatCx> PatStack<'p, Cx> {
self.pats[0]
}
fn iter(&self) -> impl Iterator<Item = PatOrWild<'p, Cx>> + Captures<'_> {
fn iter(&self) -> impl Iterator<Item = PatOrWild<'p, Cx>> {
self.pats.iter().copied()
}
// Expand the first or-pattern into its subpatterns. Only useful if the pattern is an
// or-pattern. Panics if `self` is empty.
fn expand_or_pat(&self) -> impl Iterator<Item = PatStack<'p, Cx>> + Captures<'_> {
fn expand_or_pat(&self) -> impl Iterator<Item = PatStack<'p, Cx>> {
self.head().expand_or_pat().into_iter().map(move |pat| {
let mut new = self.clone();
new.pats[0] = pat;
@ -1157,15 +1157,12 @@ impl<'p, Cx: PatCx> MatrixRow<'p, Cx> {
self.pats.head()
}
fn iter(&self) -> impl Iterator<Item = PatOrWild<'p, Cx>> + Captures<'_> {
fn iter(&self) -> impl Iterator<Item = PatOrWild<'p, Cx>> {
self.pats.iter()
}
// Expand the first or-pattern (if any) into its subpatterns. Panics if `self` is empty.
fn expand_or_pat(
&self,
parent_row: usize,
) -> impl Iterator<Item = MatrixRow<'p, Cx>> + Captures<'_> {
fn expand_or_pat(&self, parent_row: usize) -> impl Iterator<Item = MatrixRow<'p, Cx>> {
let is_or_pat = self.pats.head().is_or_pat();
self.pats.expand_or_pat().map(move |patstack| MatrixRow {
pats: patstack,
@ -1275,7 +1272,7 @@ impl<'p, Cx: PatCx> Matrix<'p, Cx> {
}
/// Iterate over the first pattern of each row.
fn heads(&self) -> impl Iterator<Item = PatOrWild<'p, Cx>> + Clone + Captures<'_> {
fn heads(&self) -> impl Iterator<Item = PatOrWild<'p, Cx>> + Clone {
self.rows().map(|r| r.head())
}

View file

@ -2,7 +2,7 @@ use rustc_pattern_analysis::constructor::{
Constructor, ConstructorSet, IntRange, MaybeInfiniteInt, RangeEnd, VariantVisibility,
};
use rustc_pattern_analysis::usefulness::{PlaceValidity, UsefulnessReport};
use rustc_pattern_analysis::{Captures, MatchArm, PatCx, PrivateUninhabitedField};
use rustc_pattern_analysis::{MatchArm, PatCx, PrivateUninhabitedField};
/// Sets up `tracing` for easier debugging. Tries to look like the `rustc` setup.
pub fn init_tracing() {
@ -156,12 +156,11 @@ impl PatCx for Cx {
ty.sub_tys(ctor).len()
}
fn ctor_sub_tys<'a>(
&'a self,
ctor: &'a Constructor<Self>,
ty: &'a Self::Ty,
) -> impl Iterator<Item = (Self::Ty, PrivateUninhabitedField)> + ExactSizeIterator + Captures<'a>
{
fn ctor_sub_tys(
&self,
ctor: &Constructor<Self>,
ty: &Self::Ty,
) -> impl Iterator<Item = (Self::Ty, PrivateUninhabitedField)> + ExactSizeIterator {
ty.sub_tys(ctor).into_iter().map(|ty| (ty, PrivateUninhabitedField(false)))
}