1
Fork 0

Rename RustcMatchCheckCtxt -> RustcPatCtxt

This commit is contained in:
Nadrieril 2024-03-13 14:07:44 +01:00
parent 4fc35c46ff
commit f27540697e
6 changed files with 42 additions and 50 deletions

View file

@ -6,7 +6,7 @@ use rustc_errors::{
}; };
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic}; use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
use rustc_middle::ty::{self, Ty}; use rustc_middle::ty::{self, Ty};
use rustc_pattern_analysis::{errors::Uncovered, rustc::RustcMatchCheckCtxt}; use rustc_pattern_analysis::{errors::Uncovered, rustc::RustcPatCtxt};
use rustc_span::symbol::Symbol; use rustc_span::symbol::Symbol;
use rustc_span::Span; use rustc_span::Span;
@ -455,7 +455,7 @@ pub enum UnusedUnsafeEnclosing {
} }
pub(crate) struct NonExhaustivePatternsTypeNotEmpty<'p, 'tcx, 'm> { pub(crate) struct NonExhaustivePatternsTypeNotEmpty<'p, 'tcx, 'm> {
pub cx: &'m RustcMatchCheckCtxt<'p, 'tcx>, pub cx: &'m RustcPatCtxt<'p, 'tcx>,
pub expr_span: Span, pub expr_span: Span,
pub span: Span, pub span: Span,
pub ty: Ty<'tcx>, pub ty: Ty<'tcx>,

View file

@ -1,7 +1,7 @@
use rustc_pattern_analysis::errors::Uncovered; use rustc_pattern_analysis::errors::Uncovered;
use rustc_pattern_analysis::rustc::{ use rustc_pattern_analysis::rustc::{
Constructor, DeconstructedPat, MatchArm, RustcMatchCheckCtxt as MatchCheckCtxt, Usefulness, Constructor, DeconstructedPat, MatchArm, RustcPatCtxt as PatCtxt, Usefulness, UsefulnessReport,
UsefulnessReport, WitnessPat, WitnessPat,
}; };
use crate::errors::*; use crate::errors::*;
@ -276,7 +276,7 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
fn lower_pattern( fn lower_pattern(
&mut self, &mut self,
cx: &MatchCheckCtxt<'p, 'tcx>, cx: &PatCtxt<'p, 'tcx>,
pat: &'p Pat<'tcx>, pat: &'p Pat<'tcx>,
) -> Result<&'p DeconstructedPat<'p, 'tcx>, ErrorGuaranteed> { ) -> Result<&'p DeconstructedPat<'p, 'tcx>, ErrorGuaranteed> {
if let Err(err) = pat.pat_error_reported() { if let Err(err) = pat.pat_error_reported() {
@ -375,7 +375,7 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
whole_match_span: Option<Span>, whole_match_span: Option<Span>,
scrutinee: Option<&Expr<'tcx>>, scrutinee: Option<&Expr<'tcx>>,
scrut_span: Span, scrut_span: Span,
) -> MatchCheckCtxt<'p, 'tcx> { ) -> PatCtxt<'p, 'tcx> {
let refutable = match refutability { let refutable = match refutability {
Irrefutable => false, Irrefutable => false,
Refutable => true, Refutable => true,
@ -384,7 +384,7 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
// require validity. // require validity.
let known_valid_scrutinee = let known_valid_scrutinee =
scrutinee.map(|scrut| self.is_known_valid_scrutinee(scrut)).unwrap_or(true); scrutinee.map(|scrut| self.is_known_valid_scrutinee(scrut)).unwrap_or(true);
MatchCheckCtxt { PatCtxt {
tcx: self.tcx, tcx: self.tcx,
typeck_results: self.typeck_results, typeck_results: self.typeck_results,
param_env: self.param_env, param_env: self.param_env,
@ -400,7 +400,7 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
fn analyze_patterns( fn analyze_patterns(
&mut self, &mut self,
cx: &MatchCheckCtxt<'p, 'tcx>, cx: &PatCtxt<'p, 'tcx>,
arms: &[MatchArm<'p, 'tcx>], arms: &[MatchArm<'p, 'tcx>],
scrut_ty: Ty<'tcx>, scrut_ty: Ty<'tcx>,
) -> Result<UsefulnessReport<'p, 'tcx>, ErrorGuaranteed> { ) -> Result<UsefulnessReport<'p, 'tcx>, ErrorGuaranteed> {
@ -584,7 +584,7 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
pat: &'p Pat<'tcx>, pat: &'p Pat<'tcx>,
refutability: RefutableFlag, refutability: RefutableFlag,
scrut: Option<&Expr<'tcx>>, scrut: Option<&Expr<'tcx>>,
) -> Result<(MatchCheckCtxt<'p, 'tcx>, UsefulnessReport<'p, 'tcx>), ErrorGuaranteed> { ) -> Result<(PatCtxt<'p, 'tcx>, UsefulnessReport<'p, 'tcx>), ErrorGuaranteed> {
let cx = self.new_cx(refutability, None, scrut, pat.span); let cx = self.new_cx(refutability, None, scrut, pat.span);
let pat = self.lower_pattern(&cx, pat)?; let pat = self.lower_pattern(&cx, pat)?;
let arms = [MatchArm { pat, arm_data: self.lint_level, has_guard: false }]; let arms = [MatchArm { pat, arm_data: self.lint_level, has_guard: false }];
@ -849,7 +849,7 @@ fn check_for_bindings_named_same_as_variants(
/// Check that never patterns are only used on inhabited types. /// Check that never patterns are only used on inhabited types.
fn check_never_pattern<'tcx>( fn check_never_pattern<'tcx>(
cx: &MatchCheckCtxt<'_, 'tcx>, cx: &PatCtxt<'_, 'tcx>,
pat: &Pat<'tcx>, pat: &Pat<'tcx>,
) -> Result<(), ErrorGuaranteed> { ) -> Result<(), ErrorGuaranteed> {
if let PatKind::Never = pat.kind { if let PatKind::Never = pat.kind {
@ -884,7 +884,7 @@ fn report_irrefutable_let_patterns(
/// Report unreachable arms, if any. /// Report unreachable arms, if any.
fn report_unreachable_pattern<'p, 'tcx>( fn report_unreachable_pattern<'p, 'tcx>(
cx: &MatchCheckCtxt<'p, 'tcx>, cx: &PatCtxt<'p, 'tcx>,
hir_id: HirId, hir_id: HirId,
span: Span, span: Span,
catchall: Option<Span>, catchall: Option<Span>,
@ -898,10 +898,7 @@ fn report_unreachable_pattern<'p, 'tcx>(
} }
/// Report unreachable arms, if any. /// Report unreachable arms, if any.
fn report_arm_reachability<'p, 'tcx>( fn report_arm_reachability<'p, 'tcx>(cx: &PatCtxt<'p, 'tcx>, report: &UsefulnessReport<'p, 'tcx>) {
cx: &MatchCheckCtxt<'p, 'tcx>,
report: &UsefulnessReport<'p, 'tcx>,
) {
let mut catchall = None; let mut catchall = None;
for (arm, is_useful) in report.arm_usefulness.iter() { for (arm, is_useful) in report.arm_usefulness.iter() {
if matches!(is_useful, Usefulness::Redundant) { if matches!(is_useful, Usefulness::Redundant) {
@ -926,7 +923,7 @@ fn pat_is_catchall(pat: &DeconstructedPat<'_, '_>) -> bool {
/// Report that a match is not exhaustive. /// Report that a match is not exhaustive.
fn report_non_exhaustive_match<'p, 'tcx>( fn report_non_exhaustive_match<'p, 'tcx>(
cx: &MatchCheckCtxt<'p, 'tcx>, cx: &PatCtxt<'p, 'tcx>,
thir: &Thir<'tcx>, thir: &Thir<'tcx>,
scrut_ty: Ty<'tcx>, scrut_ty: Ty<'tcx>,
sp: Span, sp: Span,
@ -1126,7 +1123,7 @@ fn report_non_exhaustive_match<'p, 'tcx>(
} }
fn joined_uncovered_patterns<'p, 'tcx>( fn joined_uncovered_patterns<'p, 'tcx>(
cx: &MatchCheckCtxt<'p, 'tcx>, cx: &PatCtxt<'p, 'tcx>,
witnesses: &[WitnessPat<'p, 'tcx>], witnesses: &[WitnessPat<'p, 'tcx>],
) -> String { ) -> String {
const LIMIT: usize = 3; const LIMIT: usize = 3;
@ -1147,7 +1144,7 @@ fn joined_uncovered_patterns<'p, 'tcx>(
} }
fn collect_non_exhaustive_tys<'tcx>( fn collect_non_exhaustive_tys<'tcx>(
cx: &MatchCheckCtxt<'_, 'tcx>, cx: &PatCtxt<'_, 'tcx>,
pat: &WitnessPat<'_, 'tcx>, pat: &WitnessPat<'_, 'tcx>,
non_exhaustive_tys: &mut FxIndexSet<Ty<'tcx>>, non_exhaustive_tys: &mut FxIndexSet<Ty<'tcx>>,
) { ) {

View file

@ -4,7 +4,7 @@ use rustc_middle::thir::Pat;
use rustc_middle::ty::Ty; use rustc_middle::ty::Ty;
use rustc_span::Span; use rustc_span::Span;
use crate::rustc::{RustcMatchCheckCtxt, WitnessPat}; use crate::rustc::{RustcPatCtxt, WitnessPat};
#[derive(Subdiagnostic)] #[derive(Subdiagnostic)]
#[label(pattern_analysis_uncovered)] #[label(pattern_analysis_uncovered)]
@ -21,7 +21,7 @@ pub struct Uncovered<'tcx> {
impl<'tcx> Uncovered<'tcx> { impl<'tcx> Uncovered<'tcx> {
pub fn new<'p>( pub fn new<'p>(
span: Span, span: Span,
cx: &RustcMatchCheckCtxt<'p, 'tcx>, cx: &RustcPatCtxt<'p, 'tcx>,
witnesses: Vec<WitnessPat<'p, 'tcx>>, witnesses: Vec<WitnessPat<'p, 'tcx>>,
) -> Self ) -> Self
where where

View file

@ -173,7 +173,7 @@ impl<'p, Cx: PatCx> Copy for MatchArm<'p, Cx> {}
/// useful, and runs some lints. /// useful, and runs some lints.
#[cfg(feature = "rustc")] #[cfg(feature = "rustc")]
pub fn analyze_match<'p, 'tcx>( pub fn analyze_match<'p, 'tcx>(
tycx: &rustc::RustcMatchCheckCtxt<'p, 'tcx>, tycx: &rustc::RustcPatCtxt<'p, 'tcx>,
arms: &[rustc::MatchArm<'p, 'tcx>], arms: &[rustc::MatchArm<'p, 'tcx>],
scrut_ty: Ty<'tcx>, scrut_ty: Ty<'tcx>,
pattern_complexity_limit: Option<usize>, pattern_complexity_limit: Option<usize>,

View file

@ -4,15 +4,15 @@ use rustc_span::ErrorGuaranteed;
use crate::constructor::Constructor; use crate::constructor::Constructor;
use crate::errors::{NonExhaustiveOmittedPattern, NonExhaustiveOmittedPatternLintOnArm, Uncovered}; use crate::errors::{NonExhaustiveOmittedPattern, NonExhaustiveOmittedPatternLintOnArm, Uncovered};
use crate::pat_column::PatternColumn; use crate::pat_column::PatternColumn;
use crate::rustc::{RevealedTy, RustcMatchCheckCtxt, WitnessPat}; use crate::rustc::{RevealedTy, RustcPatCtxt, WitnessPat};
use crate::MatchArm; use crate::MatchArm;
/// Traverse the patterns to collect any variants of a non_exhaustive enum that fail to be mentioned /// Traverse the patterns to collect any variants of a non_exhaustive enum that fail to be mentioned
/// in a given column. /// in a given column.
#[instrument(level = "debug", skip(cx), ret)] #[instrument(level = "debug", skip(cx), ret)]
fn collect_nonexhaustive_missing_variants<'p, 'tcx>( fn collect_nonexhaustive_missing_variants<'p, 'tcx>(
cx: &RustcMatchCheckCtxt<'p, 'tcx>, cx: &RustcPatCtxt<'p, 'tcx>,
column: &PatternColumn<'p, RustcMatchCheckCtxt<'p, 'tcx>>, column: &PatternColumn<'p, RustcPatCtxt<'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());
@ -57,9 +57,9 @@ fn collect_nonexhaustive_missing_variants<'p, 'tcx>(
} }
pub(crate) fn lint_nonexhaustive_missing_variants<'p, 'tcx>( pub(crate) fn lint_nonexhaustive_missing_variants<'p, 'tcx>(
rcx: &RustcMatchCheckCtxt<'p, 'tcx>, rcx: &RustcPatCtxt<'p, 'tcx>,
arms: &[MatchArm<'p, RustcMatchCheckCtxt<'p, 'tcx>>], arms: &[MatchArm<'p, RustcPatCtxt<'p, 'tcx>>],
pat_column: &PatternColumn<'p, RustcMatchCheckCtxt<'p, 'tcx>>, pat_column: &PatternColumn<'p, RustcPatCtxt<'p, 'tcx>>,
scrut_ty: RevealedTy<'tcx>, scrut_ty: RevealedTy<'tcx>,
) -> Result<(), ErrorGuaranteed> { ) -> Result<(), ErrorGuaranteed> {
if !matches!( if !matches!(

View file

@ -23,15 +23,14 @@ use crate::{errors, Captures, PatCx, PrivateUninhabitedField};
use crate::constructor::Constructor::*; use crate::constructor::Constructor::*;
// Re-export rustc-specific versions of all these types. // Re-export rustc-specific versions of all these types.
pub type Constructor<'p, 'tcx> = crate::constructor::Constructor<RustcMatchCheckCtxt<'p, 'tcx>>; pub type Constructor<'p, 'tcx> = crate::constructor::Constructor<RustcPatCtxt<'p, 'tcx>>;
pub type ConstructorSet<'p, 'tcx> = pub type ConstructorSet<'p, 'tcx> = crate::constructor::ConstructorSet<RustcPatCtxt<'p, 'tcx>>;
crate::constructor::ConstructorSet<RustcMatchCheckCtxt<'p, 'tcx>>; pub type DeconstructedPat<'p, 'tcx> = crate::pat::DeconstructedPat<RustcPatCtxt<'p, 'tcx>>;
pub type DeconstructedPat<'p, 'tcx> = crate::pat::DeconstructedPat<RustcMatchCheckCtxt<'p, 'tcx>>; pub type MatchArm<'p, 'tcx> = crate::MatchArm<'p, RustcPatCtxt<'p, 'tcx>>;
pub type MatchArm<'p, 'tcx> = crate::MatchArm<'p, RustcMatchCheckCtxt<'p, 'tcx>>; pub type Usefulness<'p, 'tcx> = crate::usefulness::Usefulness<'p, RustcPatCtxt<'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, RustcPatCtxt<'p, 'tcx>>;
pub type WitnessPat<'p, 'tcx> = crate::pat::WitnessPat<RustcMatchCheckCtxt<'p, 'tcx>>; pub type WitnessPat<'p, 'tcx> = crate::pat::WitnessPat<RustcPatCtxt<'p, 'tcx>>;
/// A type which has gone through `cx.reveal_opaque_ty`, i.e. if it was opaque it was replaced by /// A type which has gone through `cx.reveal_opaque_ty`, i.e. if it was opaque it was replaced by
/// the hidden type if allowed in the current body. This ensures we consistently inspect the hidden /// the hidden type if allowed in the current body. This ensures we consistently inspect the hidden
@ -62,7 +61,7 @@ impl<'tcx> RevealedTy<'tcx> {
} }
#[derive(Clone)] #[derive(Clone)]
pub struct RustcMatchCheckCtxt<'p, 'tcx: 'p> { pub struct RustcPatCtxt<'p, 'tcx: 'p> {
pub tcx: TyCtxt<'tcx>, pub tcx: TyCtxt<'tcx>,
pub typeck_results: &'tcx ty::TypeckResults<'tcx>, pub typeck_results: &'tcx ty::TypeckResults<'tcx>,
/// The module in which the match occurs. This is necessary for /// The module in which the match occurs. This is necessary for
@ -87,22 +86,19 @@ pub struct RustcMatchCheckCtxt<'p, 'tcx: 'p> {
pub known_valid_scrutinee: bool, pub known_valid_scrutinee: bool,
} }
impl<'p, 'tcx: 'p> fmt::Debug for RustcMatchCheckCtxt<'p, 'tcx> { impl<'p, 'tcx: 'p> fmt::Debug for RustcPatCtxt<'p, 'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RustcMatchCheckCtxt").finish() f.debug_struct("RustcPatCtxt").finish()
} }
} }
impl<'p, 'tcx: 'p> RustcMatchCheckCtxt<'p, 'tcx> { impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
/// Type inference occasionally gives us opaque types in places where corresponding patterns /// Type inference occasionally gives us opaque types in places where corresponding patterns
/// have more specific types. To avoid inconsistencies as well as detect opaque uninhabited /// have more specific types. To avoid inconsistencies as well as detect opaque uninhabited
/// types, we use the corresponding concrete type if possible. /// types, we use the corresponding concrete type if possible.
#[inline] #[inline]
pub fn reveal_opaque_ty(&self, ty: Ty<'tcx>) -> RevealedTy<'tcx> { pub fn reveal_opaque_ty(&self, ty: Ty<'tcx>) -> RevealedTy<'tcx> {
fn reveal_inner<'tcx>( fn reveal_inner<'tcx>(cx: &RustcPatCtxt<'_, 'tcx>, ty: Ty<'tcx>) -> RevealedTy<'tcx> {
cx: &RustcMatchCheckCtxt<'_, 'tcx>,
ty: Ty<'tcx>,
) -> RevealedTy<'tcx> {
let ty::Alias(ty::Opaque, alias_ty) = *ty.kind() else { bug!() }; let ty::Alias(ty::Opaque, alias_ty) = *ty.kind() else { bug!() };
if let Some(local_def_id) = alias_ty.def_id.as_local() { if let Some(local_def_id) = alias_ty.def_id.as_local() {
let key = ty::OpaqueTypeKey { def_id: local_def_id, args: alias_ty.args }; let key = ty::OpaqueTypeKey { def_id: local_def_id, args: alias_ty.args };
@ -199,7 +195,7 @@ impl<'p, 'tcx: 'p> RustcMatchCheckCtxt<'p, 'tcx> {
+ ExactSizeIterator + ExactSizeIterator
+ Captures<'a> { + Captures<'a> {
fn reveal_and_alloc<'a, 'tcx>( fn reveal_and_alloc<'a, 'tcx>(
cx: &'a RustcMatchCheckCtxt<'_, 'tcx>, cx: &'a RustcPatCtxt<'_, 'tcx>,
iter: impl Iterator<Item = Ty<'tcx>>, iter: impl Iterator<Item = Ty<'tcx>>,
) -> &'a [(RevealedTy<'tcx>, PrivateUninhabitedField)] { ) -> &'a [(RevealedTy<'tcx>, PrivateUninhabitedField)] {
cx.dropless_arena.alloc_from_iter( cx.dropless_arena.alloc_from_iter(
@ -218,7 +214,7 @@ impl<'p, 'tcx: 'p> RustcMatchCheckCtxt<'p, 'tcx> {
reveal_and_alloc(cx, once(args.type_at(0))) reveal_and_alloc(cx, once(args.type_at(0)))
} else { } else {
let variant = let variant =
&adt.variant(RustcMatchCheckCtxt::variant_index_for_adt(&ctor, *adt)); &adt.variant(RustcPatCtxt::variant_index_for_adt(&ctor, *adt));
// In the cases of either a `#[non_exhaustive]` field list or a non-public // In the cases of either a `#[non_exhaustive]` field list or a non-public
// field, we skip uninhabited fields in order not to reveal the // field, we skip uninhabited fields in order not to reveal the
@ -270,7 +266,7 @@ impl<'p, 'tcx: 'p> RustcMatchCheckCtxt<'p, 'tcx> {
// patterns. If we're here we can assume this is a box pattern. // patterns. If we're here we can assume this is a box pattern.
1 1
} else { } else {
let variant_idx = RustcMatchCheckCtxt::variant_index_for_adt(&ctor, *adt); let variant_idx = RustcPatCtxt::variant_index_for_adt(&ctor, *adt);
adt.variant(variant_idx).fields.len() adt.variant(variant_idx).fields.len()
} }
} }
@ -506,7 +502,7 @@ impl<'p, 'tcx: 'p> RustcMatchCheckCtxt<'p, 'tcx> {
_ => bug!(), _ => bug!(),
}; };
let variant = let variant =
&adt.variant(RustcMatchCheckCtxt::variant_index_for_adt(&ctor, *adt)); &adt.variant(RustcPatCtxt::variant_index_for_adt(&ctor, *adt));
arity = variant.fields.len(); arity = variant.fields.len();
fields = subpatterns fields = subpatterns
.iter() .iter()
@ -774,8 +770,7 @@ impl<'p, 'tcx: 'p> RustcMatchCheckCtxt<'p, 'tcx> {
PatKind::Deref { subpattern: subpatterns.next().unwrap() } PatKind::Deref { subpattern: subpatterns.next().unwrap() }
} }
ty::Adt(adt_def, args) => { ty::Adt(adt_def, args) => {
let variant_index = let variant_index = RustcPatCtxt::variant_index_for_adt(&pat.ctor(), *adt_def);
RustcMatchCheckCtxt::variant_index_for_adt(&pat.ctor(), *adt_def);
let subpatterns = subpatterns let subpatterns = subpatterns
.enumerate() .enumerate()
.map(|(i, pattern)| FieldPat { field: FieldIdx::new(i), pattern }) .map(|(i, pattern)| FieldPat { field: FieldIdx::new(i), pattern })
@ -843,7 +838,7 @@ impl<'p, 'tcx: 'p> RustcMatchCheckCtxt<'p, 'tcx> {
} }
} }
impl<'p, 'tcx: 'p> PatCx for RustcMatchCheckCtxt<'p, 'tcx> { impl<'p, 'tcx: 'p> PatCx for RustcPatCtxt<'p, 'tcx> {
type Ty = RevealedTy<'tcx>; type Ty = RevealedTy<'tcx>;
type Error = ErrorGuaranteed; type Error = ErrorGuaranteed;
type VariantIdx = VariantIdx; type VariantIdx = VariantIdx;