Make PatternColumn
generic in Cx
This commit is contained in:
parent
5ad7454f75
commit
0b2579a1b6
@ -1,12 +1,12 @@
|
|||||||
use rustc_session::lint::builtin::NON_EXHAUSTIVE_OMITTED_PATTERNS;
|
use rustc_session::lint::builtin::NON_EXHAUSTIVE_OMITTED_PATTERNS;
|
||||||
use rustc_span::ErrorGuaranteed;
|
use rustc_span::ErrorGuaranteed;
|
||||||
|
|
||||||
|
use crate::constructor::{Constructor, SplitConstructorSet};
|
||||||
use crate::errors::{NonExhaustiveOmittedPattern, NonExhaustiveOmittedPatternLintOnArm, Uncovered};
|
use crate::errors::{NonExhaustiveOmittedPattern, NonExhaustiveOmittedPatternLintOnArm, Uncovered};
|
||||||
use crate::pat::PatOrWild;
|
use crate::pat::{DeconstructedPat, PatOrWild};
|
||||||
use crate::rustc::{
|
use crate::rustc::{MatchCtxt, RevealedTy, RustcMatchCheckCtxt, WitnessPat};
|
||||||
Constructor, DeconstructedPat, MatchArm, MatchCtxt, PlaceCtxt, RevealedTy, RustcMatchCheckCtxt,
|
use crate::usefulness::PlaceCtxt;
|
||||||
SplitConstructorSet, WitnessPat,
|
use crate::{MatchArm, TypeCx};
|
||||||
};
|
|
||||||
|
|
||||||
/// A column of patterns in the matrix, where a column is the intuitive notion of "subpatterns that
|
/// A column of patterns in the matrix, where a column is the intuitive notion of "subpatterns that
|
||||||
/// inspect the same subvalue/place".
|
/// inspect the same subvalue/place".
|
||||||
@ -19,12 +19,12 @@ use crate::rustc::{
|
|||||||
///
|
///
|
||||||
/// This is not used in the usefulness algorithm; only in lints.
|
/// This is not used in the usefulness algorithm; only in lints.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct PatternColumn<'p, 'tcx> {
|
pub(crate) struct PatternColumn<'p, Cx: TypeCx> {
|
||||||
patterns: Vec<&'p DeconstructedPat<'p, 'tcx>>,
|
patterns: Vec<&'p DeconstructedPat<'p, Cx>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'p, 'tcx> PatternColumn<'p, 'tcx> {
|
impl<'p, Cx: TypeCx> PatternColumn<'p, Cx> {
|
||||||
pub(crate) fn new(arms: &[MatchArm<'p, 'tcx>]) -> Self {
|
pub(crate) fn new(arms: &[MatchArm<'p, Cx>]) -> Self {
|
||||||
let patterns = Vec::with_capacity(arms.len());
|
let patterns = Vec::with_capacity(arms.len());
|
||||||
let mut column = PatternColumn { patterns };
|
let mut column = PatternColumn { patterns };
|
||||||
for arm in arms {
|
for arm in arms {
|
||||||
@ -34,7 +34,7 @@ impl<'p, 'tcx> PatternColumn<'p, 'tcx> {
|
|||||||
}
|
}
|
||||||
/// Pushes a pattern onto the column, expanding any or-patterns into its subpatterns.
|
/// Pushes a pattern onto the column, expanding any or-patterns into its subpatterns.
|
||||||
/// Internal method, prefer [`PatternColumn::new`].
|
/// Internal method, prefer [`PatternColumn::new`].
|
||||||
fn expand_and_push(&mut self, pat: PatOrWild<'p, RustcMatchCheckCtxt<'p, 'tcx>>) {
|
fn expand_and_push(&mut self, pat: PatOrWild<'p, Cx>) {
|
||||||
// We flatten or-patterns and skip algorithm-generated wildcards.
|
// We flatten or-patterns and skip algorithm-generated wildcards.
|
||||||
if pat.is_or_pat() {
|
if pat.is_or_pat() {
|
||||||
self.patterns.extend(
|
self.patterns.extend(
|
||||||
@ -45,15 +45,12 @@ impl<'p, 'tcx> PatternColumn<'p, 'tcx> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn head_ty(&self) -> Option<RevealedTy<'tcx>> {
|
fn head_ty(&self) -> Option<&Cx::Ty> {
|
||||||
self.patterns.first().map(|pat| *pat.ty())
|
self.patterns.first().map(|pat| pat.ty())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Do constructor splitting on the constructors of the column.
|
/// Do constructor splitting on the constructors of the column.
|
||||||
fn analyze_ctors(
|
fn analyze_ctors(&self, pcx: &PlaceCtxt<'_, Cx>) -> Result<SplitConstructorSet<Cx>, Cx::Error> {
|
||||||
&self,
|
|
||||||
pcx: &PlaceCtxt<'_, 'p, 'tcx>,
|
|
||||||
) -> Result<SplitConstructorSet<'p, 'tcx>, ErrorGuaranteed> {
|
|
||||||
let column_ctors = self.patterns.iter().map(|p| p.ctor());
|
let column_ctors = self.patterns.iter().map(|p| p.ctor());
|
||||||
let ctors_for_ty = &pcx.ctors_for_ty()?;
|
let ctors_for_ty = &pcx.ctors_for_ty()?;
|
||||||
Ok(ctors_for_ty.split(column_ctors))
|
Ok(ctors_for_ty.split(column_ctors))
|
||||||
@ -66,9 +63,9 @@ impl<'p, 'tcx> PatternColumn<'p, 'tcx> {
|
|||||||
/// which may change the lengths.
|
/// which may change the lengths.
|
||||||
fn specialize(
|
fn specialize(
|
||||||
&self,
|
&self,
|
||||||
pcx: &PlaceCtxt<'_, 'p, 'tcx>,
|
pcx: &PlaceCtxt<'_, Cx>,
|
||||||
ctor: &Constructor<'p, 'tcx>,
|
ctor: &Constructor<Cx>,
|
||||||
) -> Vec<PatternColumn<'p, 'tcx>> {
|
) -> Vec<PatternColumn<'p, Cx>> {
|
||||||
let arity = ctor.arity(pcx);
|
let arity = ctor.arity(pcx);
|
||||||
if arity == 0 {
|
if arity == 0 {
|
||||||
return Vec::new();
|
return Vec::new();
|
||||||
@ -96,9 +93,9 @@ impl<'p, 'tcx> PatternColumn<'p, 'tcx> {
|
|||||||
#[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<'p, 'tcx>,
|
column: &PatternColumn<'p, RustcMatchCheckCtxt<'p, 'tcx>>,
|
||||||
) -> Result<Vec<WitnessPat<'p, 'tcx>>, ErrorGuaranteed> {
|
) -> Result<Vec<WitnessPat<'p, 'tcx>>, ErrorGuaranteed> {
|
||||||
let Some(ty) = column.head_ty() else {
|
let Some(&ty) = column.head_ty() else {
|
||||||
return Ok(Vec::new());
|
return Ok(Vec::new());
|
||||||
};
|
};
|
||||||
let pcx = &PlaceCtxt::new_dummy(cx, &ty);
|
let pcx = &PlaceCtxt::new_dummy(cx, &ty);
|
||||||
@ -143,8 +140,8 @@ 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, RustcMatchCheckCtxt<'p, 'tcx>>],
|
||||||
pat_column: &PatternColumn<'p, 'tcx>,
|
pat_column: &PatternColumn<'p, RustcMatchCheckCtxt<'p, 'tcx>>,
|
||||||
scrut_ty: RevealedTy<'tcx>,
|
scrut_ty: RevealedTy<'tcx>,
|
||||||
) -> Result<(), ErrorGuaranteed> {
|
) -> Result<(), ErrorGuaranteed> {
|
||||||
let rcx: &RustcMatchCheckCtxt<'_, '_> = cx.tycx;
|
let rcx: &RustcMatchCheckCtxt<'_, '_> = cx.tycx;
|
||||||
|
@ -31,10 +31,6 @@ pub type DeconstructedPat<'p, 'tcx> =
|
|||||||
crate::pat::DeconstructedPat<'p, RustcMatchCheckCtxt<'p, 'tcx>>;
|
crate::pat::DeconstructedPat<'p, RustcMatchCheckCtxt<'p, 'tcx>>;
|
||||||
pub type MatchArm<'p, 'tcx> = crate::MatchArm<'p, RustcMatchCheckCtxt<'p, 'tcx>>;
|
pub type MatchArm<'p, 'tcx> = crate::MatchArm<'p, RustcMatchCheckCtxt<'p, 'tcx>>;
|
||||||
pub type MatchCtxt<'a, 'p, 'tcx> = crate::MatchCtxt<'a, RustcMatchCheckCtxt<'p, 'tcx>>;
|
pub type MatchCtxt<'a, 'p, 'tcx> = crate::MatchCtxt<'a, RustcMatchCheckCtxt<'p, 'tcx>>;
|
||||||
pub(crate) type PlaceCtxt<'a, 'p, 'tcx> =
|
|
||||||
crate::usefulness::PlaceCtxt<'a, RustcMatchCheckCtxt<'p, 'tcx>>;
|
|
||||||
pub(crate) type SplitConstructorSet<'p, 'tcx> =
|
|
||||||
crate::constructor::SplitConstructorSet<RustcMatchCheckCtxt<'p, 'tcx>>;
|
|
||||||
pub type Usefulness<'p, 'tcx> = crate::usefulness::Usefulness<'p, RustcMatchCheckCtxt<'p, 'tcx>>;
|
pub type Usefulness<'p, 'tcx> = crate::usefulness::Usefulness<'p, RustcMatchCheckCtxt<'p, 'tcx>>;
|
||||||
pub type UsefulnessReport<'p, 'tcx> =
|
pub type UsefulnessReport<'p, 'tcx> =
|
||||||
crate::usefulness::UsefulnessReport<'p, RustcMatchCheckCtxt<'p, 'tcx>>;
|
crate::usefulness::UsefulnessReport<'p, RustcMatchCheckCtxt<'p, 'tcx>>;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user