Name rustc-specific things "rustc"

This commit is contained in:
Nadrieril 2023-12-11 20:15:52 +01:00
parent 3d7c4df326
commit cb622f3994
6 changed files with 42 additions and 48 deletions

View File

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

View File

@ -1,7 +1,8 @@
use rustc_pattern_analysis::cx::{
Constructor, DeconstructedPat, MatchCheckCtxt, Usefulness, UsefulnessReport, WitnessPat,
};
use rustc_pattern_analysis::errors::Uncovered;
use rustc_pattern_analysis::rustc::{
Constructor, DeconstructedPat, RustcCtxt as MatchCheckCtxt, Usefulness, UsefulnessReport,
WitnessPat,
};
use rustc_pattern_analysis::{analyze_match, MatchArm};
use crate::errors::*;

View File

@ -4,7 +4,7 @@ use rustc_middle::thir::Pat;
use rustc_middle::ty::Ty;
use rustc_span::Span;
use crate::cx::{MatchCheckCtxt, WitnessPat};
use crate::rustc::{RustcCtxt, WitnessPat};
#[derive(Subdiagnostic)]
#[label(pattern_analysis_uncovered)]
@ -21,7 +21,7 @@ pub struct Uncovered<'tcx> {
impl<'tcx> Uncovered<'tcx> {
pub fn new<'p>(
span: Span,
cx: &MatchCheckCtxt<'p, 'tcx>,
cx: &RustcCtxt<'p, 'tcx>,
witnesses: Vec<WitnessPat<'p, 'tcx>>,
) -> Self {
let witness_1 = cx.hoist_witness_pat(witnesses.get(0).unwrap());

View File

@ -1,10 +1,10 @@
//! Analysis of patterns, notably match exhaustiveness checking.
pub mod constructor;
pub mod cx;
pub mod errors;
pub(crate) mod lints;
pub mod pat;
pub mod rustc;
pub mod usefulness;
#[macro_use]
@ -21,11 +21,11 @@ use lints::PatternColumn;
use rustc_hir::HirId;
use rustc_index::Idx;
use rustc_middle::ty::Ty;
use usefulness::{compute_match_usefulness, UsefulnessReport, ValidityConstraint};
use usefulness::{compute_match_usefulness, ValidityConstraint};
use crate::cx::MatchCheckCtxt;
use crate::lints::{lint_nonexhaustive_missing_variants, lint_overlapping_range_endpoints};
use crate::pat::DeconstructedPat;
use crate::rustc::RustcCtxt;
pub trait MatchCx: Sized + Clone + fmt::Debug {
type Ty: Copy + Clone + fmt::Debug; // FIXME: remove Copy
@ -69,10 +69,10 @@ impl<'p, Cx: MatchCx> Copy for MatchArm<'p, Cx> {}
/// The entrypoint for this crate. Computes whether a match is exhaustive and which of its arms are
/// useful, and runs some lints.
pub fn analyze_match<'p, 'tcx>(
cx: &MatchCheckCtxt<'p, 'tcx>,
arms: &[MatchArm<'p, MatchCheckCtxt<'p, 'tcx>>],
cx: &RustcCtxt<'p, 'tcx>,
arms: &[rustc::MatchArm<'p, 'tcx>],
scrut_ty: Ty<'tcx>,
) -> UsefulnessReport<'p, MatchCheckCtxt<'p, 'tcx>> {
) -> rustc::UsefulnessReport<'p, 'tcx> {
// Arena to store the extra wildcards we construct during analysis.
let wildcard_arena = cx.pattern_arena;
let pat_column = PatternColumn::new(arms);

View File

@ -8,14 +8,13 @@ use rustc_session::lint::builtin::NON_EXHAUSTIVE_OMITTED_PATTERNS;
use rustc_span::Span;
use crate::constructor::{IntRange, MaybeInfiniteInt};
use crate::cx::{
Constructor, DeconstructedPat, MatchArm, MatchCheckCtxt, PatCtxt, SplitConstructorSet,
WitnessPat,
};
use crate::errors::{
NonExhaustiveOmittedPattern, NonExhaustiveOmittedPatternLintOnArm, Overlap,
OverlappingRangeEndpoints, Uncovered,
};
use crate::rustc::{
Constructor, DeconstructedPat, MatchArm, PatCtxt, RustcCtxt, SplitConstructorSet, WitnessPat,
};
use crate::MatchCx;
/// A column of patterns in the matrix, where a column is the intuitive notion of "subpatterns that
@ -56,10 +55,10 @@ impl<'a, 'p, 'tcx> PatternColumn<'a, 'p, 'tcx> {
// If the type is opaque and it is revealed anywhere in the column, we take the revealed
// version. Otherwise we could encounter constructors for the revealed type and crash.
let first_ty = self.patterns[0].ty();
if MatchCheckCtxt::is_opaque_ty(first_ty) {
if RustcCtxt::is_opaque_ty(first_ty) {
for pat in &self.patterns {
let ty = pat.ty();
if !MatchCheckCtxt::is_opaque_ty(ty) {
if !RustcCtxt::is_opaque_ty(ty) {
return Some(ty);
}
}
@ -126,7 +125,7 @@ impl<'a, 'p, 'tcx> PatternColumn<'a, 'p, 'tcx> {
/// in a given column.
#[instrument(level = "debug", skip(cx, wildcard_arena), ret)]
fn collect_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
cx: &MatchCheckCtxt<'p, 'tcx>,
cx: &RustcCtxt<'p, 'tcx>,
column: &PatternColumn<'a, 'p, 'tcx>,
wildcard_arena: &TypedArena<DeconstructedPat<'p, 'tcx>>,
) -> Vec<WitnessPat<'p, 'tcx>> {
@ -174,7 +173,7 @@ fn collect_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
}
pub(crate) fn lint_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
cx: &MatchCheckCtxt<'p, 'tcx>,
cx: &RustcCtxt<'p, 'tcx>,
arms: &[MatchArm<'p, 'tcx>],
pat_column: &PatternColumn<'a, 'p, 'tcx>,
scrut_ty: Ty<'tcx>,
@ -228,7 +227,7 @@ pub(crate) fn lint_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
/// Traverse the patterns to warn the user about ranges that overlap on their endpoints.
#[instrument(level = "debug", skip(cx, wildcard_arena))]
pub(crate) fn lint_overlapping_range_endpoints<'a, 'p, 'tcx>(
cx: &MatchCheckCtxt<'p, 'tcx>,
cx: &RustcCtxt<'p, 'tcx>,
column: &PatternColumn<'a, 'p, 'tcx>,
wildcard_arena: &TypedArena<DeconstructedPat<'p, 'tcx>>,
) {

View File

@ -24,21 +24,19 @@ use crate::MatchCx;
use crate::constructor::Constructor::*;
pub type Constructor<'p, 'tcx> = crate::constructor::Constructor<MatchCheckCtxt<'p, 'tcx>>;
pub type ConstructorSet<'p, 'tcx> = crate::constructor::ConstructorSet<MatchCheckCtxt<'p, 'tcx>>;
pub type DeconstructedPat<'p, 'tcx> = crate::pat::DeconstructedPat<'p, MatchCheckCtxt<'p, 'tcx>>;
pub type MatchArm<'p, 'tcx> = crate::MatchArm<'p, MatchCheckCtxt<'p, 'tcx>>;
pub(crate) type PatCtxt<'a, 'p, 'tcx> =
crate::usefulness::PatCtxt<'a, 'p, MatchCheckCtxt<'p, 'tcx>>;
pub type Constructor<'p, 'tcx> = crate::constructor::Constructor<RustcCtxt<'p, 'tcx>>;
pub type ConstructorSet<'p, 'tcx> = crate::constructor::ConstructorSet<RustcCtxt<'p, 'tcx>>;
pub type DeconstructedPat<'p, 'tcx> = crate::pat::DeconstructedPat<'p, RustcCtxt<'p, 'tcx>>;
pub type MatchArm<'p, 'tcx> = crate::MatchArm<'p, RustcCtxt<'p, 'tcx>>;
pub(crate) type PatCtxt<'a, 'p, 'tcx> = crate::usefulness::PatCtxt<'a, 'p, RustcCtxt<'p, 'tcx>>;
pub(crate) type SplitConstructorSet<'p, 'tcx> =
crate::constructor::SplitConstructorSet<MatchCheckCtxt<'p, 'tcx>>;
crate::constructor::SplitConstructorSet<RustcCtxt<'p, 'tcx>>;
pub type Usefulness = crate::usefulness::Usefulness<Span>;
pub type UsefulnessReport<'p, 'tcx> =
crate::usefulness::UsefulnessReport<'p, MatchCheckCtxt<'p, 'tcx>>;
pub type WitnessPat<'p, 'tcx> = crate::pat::WitnessPat<MatchCheckCtxt<'p, 'tcx>>;
pub type UsefulnessReport<'p, 'tcx> = crate::usefulness::UsefulnessReport<'p, RustcCtxt<'p, 'tcx>>;
pub type WitnessPat<'p, 'tcx> = crate::pat::WitnessPat<RustcCtxt<'p, 'tcx>>;
#[derive(Clone)]
pub struct MatchCheckCtxt<'p, 'tcx> {
pub struct RustcCtxt<'p, 'tcx> {
pub tcx: TyCtxt<'tcx>,
/// The module in which the match occurs. This is necessary for
/// checking inhabited-ness of types because whether a type is (visibly)
@ -62,13 +60,13 @@ pub struct MatchCheckCtxt<'p, 'tcx> {
pub known_valid_scrutinee: bool,
}
impl<'p, 'tcx> fmt::Debug for MatchCheckCtxt<'p, 'tcx> {
impl<'p, 'tcx> fmt::Debug for RustcCtxt<'p, 'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("MatchCheckCtxt").finish()
f.debug_struct("RustcCtxt").finish()
}
}
impl<'p, 'tcx> MatchCheckCtxt<'p, 'tcx> {
impl<'p, 'tcx> RustcCtxt<'p, 'tcx> {
pub(crate) fn is_uninhabited(&self, ty: Ty<'tcx>) -> bool {
!ty.is_inhabited_from(self.tcx, self.module, self.param_env)
}
@ -153,8 +151,7 @@ impl<'p, 'tcx> MatchCheckCtxt<'p, 'tcx> {
// patterns. If we're here we can assume this is a box pattern.
cx.dropless_arena.alloc_from_iter(once(args.type_at(0)))
} else {
let variant =
&adt.variant(MatchCheckCtxt::variant_index_for_adt(&ctor, *adt));
let variant = &adt.variant(RustcCtxt::variant_index_for_adt(&ctor, *adt));
let tys = cx.list_variant_nonhidden_fields(ty, variant).map(|(_, ty)| ty);
cx.dropless_arena.alloc_from_iter(tys)
}
@ -199,8 +196,7 @@ impl<'p, 'tcx> MatchCheckCtxt<'p, 'tcx> {
// patterns. If we're here we can assume this is a box pattern.
1
} else {
let variant =
&adt.variant(MatchCheckCtxt::variant_index_for_adt(&ctor, *adt));
let variant = &adt.variant(RustcCtxt::variant_index_for_adt(&ctor, *adt));
self.list_variant_nonhidden_fields(ty, variant).count()
}
}
@ -431,8 +427,7 @@ impl<'p, 'tcx> MatchCheckCtxt<'p, 'tcx> {
PatKind::Variant { variant_index, .. } => Variant(variant_index),
_ => bug!(),
};
let variant =
&adt.variant(MatchCheckCtxt::variant_index_for_adt(&ctor, *adt));
let variant = &adt.variant(RustcCtxt::variant_index_for_adt(&ctor, *adt));
// For each field in the variant, we store the relevant index into `self.fields` if any.
let mut field_id_to_id: Vec<Option<usize>> =
(0..variant.fields.len()).map(|_| None).collect();
@ -687,8 +682,7 @@ impl<'p, 'tcx> MatchCheckCtxt<'p, 'tcx> {
PatKind::Deref { subpattern: subpatterns.next().unwrap() }
}
ty::Adt(adt_def, args) => {
let variant_index =
MatchCheckCtxt::variant_index_for_adt(&pat.ctor(), *adt_def);
let variant_index = RustcCtxt::variant_index_for_adt(&pat.ctor(), *adt_def);
let variant = &adt_def.variant(variant_index);
let subpatterns = cx
.list_variant_nonhidden_fields(pat.ty(), variant)
@ -784,9 +778,9 @@ impl<'p, 'tcx> MatchCheckCtxt<'p, 'tcx> {
}
ty::Adt(..) | ty::Tuple(..) => {
let variant = match pat.ty().kind() {
ty::Adt(adt, _) => Some(
adt.variant(MatchCheckCtxt::variant_index_for_adt(pat.ctor(), *adt)),
),
ty::Adt(adt, _) => {
Some(adt.variant(RustcCtxt::variant_index_for_adt(pat.ctor(), *adt)))
}
ty::Tuple(_) => None,
_ => unreachable!(),
};
@ -854,7 +848,7 @@ impl<'p, 'tcx> MatchCheckCtxt<'p, 'tcx> {
}
}
impl<'p, 'tcx> MatchCx for MatchCheckCtxt<'p, 'tcx> {
impl<'p, 'tcx> MatchCx for RustcCtxt<'p, 'tcx> {
type Ty = Ty<'tcx>;
type Span = Span;
type VariantIdx = VariantIdx;