Skip to content

Commit

Permalink
s/MatchCx/TypeCx/
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed Dec 15, 2023
1 parent 4bcf66f commit a6a944d
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 48 deletions.
16 changes: 8 additions & 8 deletions compiler/rustc_pattern_analysis/src/constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
//! - That have no non-trivial intersection with any of the constructors in the column (i.e. they're
//! each either disjoint with or covered by any given column constructor).
//!
//! We compute this in two steps: first [`MatchCx::ctors_for_ty`] determines the
//! We compute this in two steps: first [`TypeCx::ctors_for_ty`] determines the
//! set of all possible constructors for the type. Then [`ConstructorSet::split`] looks at the
//! column of constructors and splits the set into groups accordingly. The precise invariants of
//! [`ConstructorSet::split`] is described in [`SplitConstructorSet`].
Expand Down Expand Up @@ -136,7 +136,7 @@
//! the algorithm can't distinguish them from a nonempty constructor. The only known case where this
//! could happen is the `[..]` pattern on `[!; N]` with `N > 0` so we must take care to not emit it.
//!
//! This is all handled by [`MatchCx::ctors_for_ty`] and
//! This is all handled by [`TypeCx::ctors_for_ty`] and
//! [`ConstructorSet::split`]. The invariants of [`SplitConstructorSet`] are also of interest.
//!
//!
Expand All @@ -163,7 +163,7 @@ use self::MaybeInfiniteInt::*;
use self::SliceKind::*;

use crate::usefulness::PlaceCtxt;
use crate::MatchCx;
use crate::TypeCx;

/// Whether we have seen a constructor in the column or not.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
Expand Down Expand Up @@ -643,7 +643,7 @@ impl OpaqueId {
/// constructor. `Constructor::apply` reconstructs the pattern from a pair of `Constructor` and
/// `Fields`.
#[derive(Clone, Debug, PartialEq)]
pub enum Constructor<Cx: MatchCx> {
pub enum Constructor<Cx: TypeCx> {
/// Tuples and structs.
Struct,
/// Enum variants.
Expand Down Expand Up @@ -685,7 +685,7 @@ pub enum Constructor<Cx: MatchCx> {
Missing,
}

impl<Cx: MatchCx> Constructor<Cx> {
impl<Cx: TypeCx> Constructor<Cx> {
pub(crate) fn is_non_exhaustive(&self) -> bool {
matches!(self, NonExhaustive)
}
Expand Down Expand Up @@ -798,7 +798,7 @@ pub enum VariantVisibility {
/// In terms of division of responsibility, [`ConstructorSet::split`] handles all of the
/// `exhaustive_patterns` feature.
#[derive(Debug)]
pub enum ConstructorSet<Cx: MatchCx> {
pub enum ConstructorSet<Cx: TypeCx> {
/// The type is a tuple or struct. `empty` tracks whether the type is empty.
Struct { empty: bool },
/// This type has the following list of constructors. If `variants` is empty and
Expand Down Expand Up @@ -846,13 +846,13 @@ pub enum ConstructorSet<Cx: MatchCx> {
/// of the `ConstructorSet` for the type, yet if we forgot to include them in `present` we would be
/// ignoring any row with `Opaque`s in the algorithm. Hence the importance of point 4.
#[derive(Debug)]
pub(crate) struct SplitConstructorSet<Cx: MatchCx> {
pub(crate) struct SplitConstructorSet<Cx: TypeCx> {
pub(crate) present: SmallVec<[Constructor<Cx>; 1]>,
pub(crate) missing: Vec<Constructor<Cx>>,
pub(crate) missing_empty: Vec<Constructor<Cx>>,
}

impl<Cx: MatchCx> ConstructorSet<Cx> {
impl<Cx: TypeCx> ConstructorSet<Cx> {
/// This analyzes a column of constructors to 1/ determine which constructors of the type (if
/// any) are missing; 2/ split constructors to handle non-trivial intersections e.g. on ranges
/// or slices. This can get subtle; see [`SplitConstructorSet`] for details of this operation
Expand Down
14 changes: 7 additions & 7 deletions compiler/rustc_pattern_analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ impl<'a, T: ?Sized> Captures<'a> for T {}
/// Context that provides type information about constructors.
///
/// Most of the crate is parameterized on a type that implements this trait.
pub trait MatchCx: Sized + Clone + fmt::Debug {
pub trait TypeCx: Sized + Clone + fmt::Debug {
/// The type of a pattern.
type Ty: Copy + Clone + fmt::Debug; // FIXME: remove Copy
/// The index of an enum variant.
type VariantIdx: Clone + Idx;
/// A string literal
type StrLit: Clone + PartialEq + fmt::Debug;
/// Extra data to store on a match arm.
/// Extra data to store in a match arm.
type ArmData: Copy + Clone + fmt::Debug;
/// Extra data to store on a pattern. `Default` needed when we create fictitious wildcard
/// Extra data to store in a pattern. `Default` needed when we create fictitious wildcard
/// patterns during analysis.
type PatData: Clone + Default;

Expand Down Expand Up @@ -86,24 +86,24 @@ pub trait MatchCx: Sized + Clone + fmt::Debug {

/// Context that provides information global to a match.
#[derive(Clone)]
pub struct MatchCtxt<'a, 'p, Cx: MatchCx> {
pub struct MatchCtxt<'a, 'p, Cx: TypeCx> {
/// The context for type information.
pub tycx: &'a Cx,
/// An arena to store the wildcards we produce during analysis.
pub wildcard_arena: &'a TypedArena<DeconstructedPat<'p, Cx>>,
}

impl<'a, 'p, Cx: MatchCx> Copy for MatchCtxt<'a, 'p, Cx> {}
impl<'a, 'p, Cx: TypeCx> Copy for MatchCtxt<'a, 'p, Cx> {}

/// The arm of a match expression.
#[derive(Clone, Debug)]
pub struct MatchArm<'p, Cx: MatchCx> {
pub struct MatchArm<'p, Cx: TypeCx> {
pub pat: &'p DeconstructedPat<'p, Cx>,
pub has_guard: bool,
pub arm_data: Cx::ArmData,
}

impl<'p, Cx: MatchCx> Copy for MatchArm<'p, Cx> {}
impl<'p, Cx: TypeCx> 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.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_pattern_analysis/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::rustc::{
Constructor, DeconstructedPat, MatchArm, MatchCtxt, PlaceCtxt, RustcMatchCheckCtxt,
SplitConstructorSet, WitnessPat,
};
use crate::MatchCx;
use crate::TypeCx;

/// A column of patterns in the matrix, where a column is the intuitive notion of "subpatterns that
/// inspect the same subvalue/place".
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_pattern_analysis/src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use smallvec::{smallvec, SmallVec};

use crate::constructor::{Constructor, Slice, SliceKind};
use crate::usefulness::PlaceCtxt;
use crate::{Captures, MatchCx};
use crate::{Captures, TypeCx};

use self::Constructor::*;

Expand All @@ -22,7 +22,7 @@ use self::Constructor::*;
/// This happens if a private or `non_exhaustive` field is uninhabited, because the code mustn't
/// observe that it is uninhabited. In that case that field is not included in `fields`. Care must
/// be taken when converting to/from `thir::Pat`.
pub struct DeconstructedPat<'p, Cx: MatchCx> {
pub struct DeconstructedPat<'p, Cx: TypeCx> {
ctor: Constructor<Cx>,
fields: &'p [DeconstructedPat<'p, Cx>],
ty: Cx::Ty,
Expand All @@ -31,7 +31,7 @@ pub struct DeconstructedPat<'p, Cx: MatchCx> {
useful: Cell<bool>,
}

impl<'p, Cx: MatchCx> DeconstructedPat<'p, Cx> {
impl<'p, Cx: TypeCx> DeconstructedPat<'p, Cx> {
pub fn wildcard(ty: Cx::Ty, data: Cx::PatData) -> Self {
Self::new(Wildcard, &[], ty, data)
}
Expand Down Expand Up @@ -152,7 +152,7 @@ impl<'p, Cx: MatchCx> DeconstructedPat<'p, Cx> {

/// This is mostly copied from the `Pat` impl. This is best effort and not good enough for a
/// `Display` impl.
impl<'p, Cx: MatchCx> fmt::Debug for DeconstructedPat<'p, Cx> {
impl<'p, Cx: TypeCx> fmt::Debug for DeconstructedPat<'p, Cx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Cx::debug_pat(f, self)
}
Expand All @@ -161,13 +161,13 @@ impl<'p, Cx: MatchCx> fmt::Debug for DeconstructedPat<'p, Cx> {
/// Same idea as `DeconstructedPat`, except this is a fictitious pattern built up for diagnostics
/// purposes. As such they don't use interning and can be cloned.
#[derive(Debug, Clone)]
pub struct WitnessPat<Cx: MatchCx> {
pub struct WitnessPat<Cx: TypeCx> {
ctor: Constructor<Cx>,
pub(crate) fields: Vec<WitnessPat<Cx>>,
ty: Cx::Ty,
}

impl<Cx: MatchCx> WitnessPat<Cx> {
impl<Cx: TypeCx> WitnessPat<Cx> {
pub(crate) fn new(ctor: Constructor<Cx>, fields: Vec<Self>, ty: Cx::Ty) -> Self {
Self { ctor, fields, ty }
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_pattern_analysis/src/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use smallvec::SmallVec;
use crate::constructor::{
IntRange, MaybeInfiniteInt, OpaqueId, RangeEnd, Slice, SliceKind, VariantVisibility,
};
use crate::MatchCx;
use crate::TypeCx;

use crate::constructor::Constructor::*;

Expand Down Expand Up @@ -863,7 +863,7 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
}
}

impl<'p, 'tcx> MatchCx for RustcMatchCheckCtxt<'p, 'tcx> {
impl<'p, 'tcx> TypeCx for RustcMatchCheckCtxt<'p, 'tcx> {
type Ty = Ty<'tcx>;
type VariantIdx = VariantIdx;
type StrLit = Const<'tcx>;
Expand Down
48 changes: 24 additions & 24 deletions compiler/rustc_pattern_analysis/src/usefulness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@
//! Therefore `usefulness(tp_1, tp_2, tq)` returns the single witness-tuple `[Variant2(Some(true), 0)]`.
//!
//!
//! Computing the set of constructors for a type is done in [`MatchCx::ctors_for_ty`]. See
//! Computing the set of constructors for a type is done in [`TypeCx::ctors_for_ty`]. See
//! the following sections for more accurate versions of the algorithm and corresponding links.
//!
//!
Expand Down Expand Up @@ -557,7 +557,7 @@ use std::fmt;

use crate::constructor::{Constructor, ConstructorSet};
use crate::pat::{DeconstructedPat, WitnessPat};
use crate::{Captures, MatchArm, MatchCtxt, MatchCx, TypedArena};
use crate::{Captures, MatchArm, MatchCtxt, TypeCx, TypedArena};

use self::ValidityConstraint::*;

Expand All @@ -570,15 +570,15 @@ pub fn ensure_sufficient_stack<R>(f: impl FnOnce() -> R) -> R {

/// Context that provides information local to a place under investigation.
#[derive(Clone)]
pub(crate) struct PlaceCtxt<'a, 'p, Cx: MatchCx> {
pub(crate) struct PlaceCtxt<'a, 'p, Cx: TypeCx> {
pub(crate) mcx: MatchCtxt<'a, 'p, Cx>,
/// Type of the place under investigation.
pub(crate) ty: Cx::Ty,
/// Whether the place is the original scrutinee place, as opposed to a subplace of it.
pub(crate) is_scrutinee: bool,
}

impl<'a, 'p, Cx: MatchCx> PlaceCtxt<'a, 'p, Cx> {
impl<'a, 'p, Cx: TypeCx> PlaceCtxt<'a, 'p, Cx> {
/// A `PlaceCtxt` when code other than `is_useful` needs one.
#[cfg_attr(not(feature = "rustc"), allow(dead_code))]
pub(crate) fn new_dummy(mcx: MatchCtxt<'a, 'p, Cx>, ty: Cx::Ty) -> Self {
Expand All @@ -596,9 +596,9 @@ impl<'a, 'p, Cx: MatchCx> PlaceCtxt<'a, 'p, Cx> {
}
}

impl<'a, 'p, Cx: MatchCx> Copy for PlaceCtxt<'a, 'p, Cx> {}
impl<'a, 'p, Cx: TypeCx> Copy for PlaceCtxt<'a, 'p, Cx> {}

impl<'a, 'p, Cx: MatchCx> fmt::Debug for PlaceCtxt<'a, 'p, Cx> {
impl<'a, 'p, Cx: TypeCx> fmt::Debug for PlaceCtxt<'a, 'p, Cx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PlaceCtxt").field("ty", &self.ty).finish()
}
Expand Down Expand Up @@ -644,7 +644,7 @@ impl ValidityConstraint {
///
/// Pending further opsem decisions, the current behavior is: validity is preserved, except
/// inside `&` and union fields where validity is reset to `MaybeInvalid`.
fn specialize<Cx: MatchCx>(self, ctor: &Constructor<Cx>) -> Self {
fn specialize<Cx: TypeCx>(self, ctor: &Constructor<Cx>) -> Self {
// We preserve validity except when we go inside a reference or a union field.
if matches!(ctor, Constructor::Ref | Constructor::UnionField) {
// Validity of `x: &T` does not imply validity of `*x: T`.
Expand All @@ -671,12 +671,12 @@ impl fmt::Display for ValidityConstraint {
// - 'p coming from the input
// - Cx global compilation context
#[derive(Clone)]
struct PatStack<'a, 'p, Cx: MatchCx> {
struct PatStack<'a, 'p, Cx: TypeCx> {
// Rows of len 1 are very common, which is why `SmallVec[_; 2]` works well.
pats: SmallVec<[&'a DeconstructedPat<'p, Cx>; 2]>,
}

impl<'a, 'p, Cx: MatchCx> PatStack<'a, 'p, Cx> {
impl<'a, 'p, Cx: TypeCx> PatStack<'a, 'p, Cx> {
fn from_pattern(pat: &'a DeconstructedPat<'p, Cx>) -> Self {
PatStack { pats: smallvec![pat] }
}
Expand Down Expand Up @@ -722,7 +722,7 @@ impl<'a, 'p, Cx: MatchCx> PatStack<'a, 'p, Cx> {
}
}

impl<'a, 'p, Cx: MatchCx> fmt::Debug for PatStack<'a, 'p, Cx> {
impl<'a, 'p, Cx: TypeCx> fmt::Debug for PatStack<'a, 'p, Cx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// We pretty-print similarly to the `Debug` impl of `Matrix`.
write!(f, "+")?;
Expand All @@ -735,7 +735,7 @@ impl<'a, 'p, Cx: MatchCx> fmt::Debug for PatStack<'a, 'p, Cx> {

/// A row of the matrix.
#[derive(Clone)]
struct MatrixRow<'a, 'p, Cx: MatchCx> {
struct MatrixRow<'a, 'p, Cx: TypeCx> {
// The patterns in the row.
pats: PatStack<'a, 'p, Cx>,
/// Whether the original arm had a guard. This is inherited when specializing.
Expand All @@ -750,7 +750,7 @@ struct MatrixRow<'a, 'p, Cx: MatchCx> {
useful: bool,
}

impl<'a, 'p, Cx: MatchCx> MatrixRow<'a, 'p, Cx> {
impl<'a, 'p, Cx: TypeCx> MatrixRow<'a, 'p, Cx> {
fn is_empty(&self) -> bool {
self.pats.is_empty()
}
Expand Down Expand Up @@ -795,7 +795,7 @@ impl<'a, 'p, Cx: MatchCx> MatrixRow<'a, 'p, Cx> {
}
}

impl<'a, 'p, Cx: MatchCx> fmt::Debug for MatrixRow<'a, 'p, Cx> {
impl<'a, 'p, Cx: TypeCx> fmt::Debug for MatrixRow<'a, 'p, Cx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.pats.fmt(f)
}
Expand All @@ -812,7 +812,7 @@ impl<'a, 'p, Cx: MatchCx> fmt::Debug for MatrixRow<'a, 'p, Cx> {
/// 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`.
#[derive(Clone)]
struct Matrix<'a, 'p, Cx: MatchCx> {
struct Matrix<'a, 'p, Cx: TypeCx> {
/// 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
/// scrutinee.
Expand All @@ -824,7 +824,7 @@ struct Matrix<'a, 'p, Cx: MatchCx> {
place_validity: SmallVec<[ValidityConstraint; 2]>,
}

impl<'a, 'p, Cx: MatchCx> Matrix<'a, 'p, Cx> {
impl<'a, 'p, Cx: TypeCx> Matrix<'a, 'p, Cx> {
/// Pushes a new row to the matrix. If the row starts with an or-pattern, this recursively
/// expands it. Internal method, prefer [`Matrix::new`].
fn expand_and_push(&mut self, row: MatrixRow<'a, 'p, Cx>) {
Expand Down Expand Up @@ -942,7 +942,7 @@ impl<'a, 'p, Cx: MatchCx> Matrix<'a, 'p, Cx> {
/// + _ + [_, _, tail @ ..] +
/// | ✓ | ? | // column validity
/// ```
impl<'a, 'p, Cx: MatchCx> fmt::Debug for Matrix<'a, 'p, Cx> {
impl<'a, 'p, Cx: TypeCx> fmt::Debug for Matrix<'a, 'p, Cx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "\n")?;

Expand Down Expand Up @@ -1033,9 +1033,9 @@ impl<'a, 'p, Cx: MatchCx> fmt::Debug for Matrix<'a, 'p, Cx> {
///
/// See the top of the file for more detailed explanations and examples.
#[derive(Debug, Clone)]
struct WitnessStack<Cx: MatchCx>(Vec<WitnessPat<Cx>>);
struct WitnessStack<Cx: TypeCx>(Vec<WitnessPat<Cx>>);

impl<Cx: MatchCx> WitnessStack<Cx> {
impl<Cx: TypeCx> WitnessStack<Cx> {
/// Asserts that the witness contains a single pattern, and returns it.
fn single_pattern(self) -> WitnessPat<Cx> {
assert_eq!(self.0.len(), 1);
Expand Down Expand Up @@ -1080,9 +1080,9 @@ impl<Cx: MatchCx> WitnessStack<Cx> {
/// Just as the `Matrix` starts with a single column, by the end of the algorithm, this has a single
/// column, which contains the patterns that are missing for the match to be exhaustive.
#[derive(Debug, Clone)]
struct WitnessMatrix<Cx: MatchCx>(Vec<WitnessStack<Cx>>);
struct WitnessMatrix<Cx: TypeCx>(Vec<WitnessStack<Cx>>);

impl<Cx: MatchCx> WitnessMatrix<Cx> {
impl<Cx: TypeCx> WitnessMatrix<Cx> {
/// New matrix with no witnesses.
fn empty() -> Self {
WitnessMatrix(vec![])
Expand Down Expand Up @@ -1174,7 +1174,7 @@ impl<Cx: MatchCx> WitnessMatrix<Cx> {
/// (using `apply_constructor` and by updating `row.useful` for each parent row).
/// This is all explained at the top of the file.
#[instrument(level = "debug", skip(mcx, is_top_level), ret)]
fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: MatchCx>(
fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>(
mcx: MatchCtxt<'a, 'p, Cx>,
matrix: &mut Matrix<'a, 'p, Cx>,
is_top_level: bool,
Expand Down Expand Up @@ -1283,7 +1283,7 @@ fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: MatchCx>(

/// Indicates whether or not a given arm is useful.
#[derive(Clone, Debug)]
pub enum Usefulness<'p, Cx: MatchCx> {
pub enum Usefulness<'p, Cx: TypeCx> {
/// The arm is useful. This additionally carries a set of or-pattern branches that have been
/// found to be redundant despite the overall arm being useful. Used only in the presence of
/// or-patterns, otherwise it stays empty.
Expand All @@ -1294,7 +1294,7 @@ pub enum Usefulness<'p, Cx: MatchCx> {
}

/// The output of checking a match for exhaustiveness and arm usefulness.
pub struct UsefulnessReport<'p, Cx: MatchCx> {
pub struct UsefulnessReport<'p, Cx: TypeCx> {
/// For each arm of the input, whether that arm is useful after the arms above it.
pub arm_usefulness: Vec<(MatchArm<'p, Cx>, Usefulness<'p, Cx>)>,
/// If the match is exhaustive, this is empty. If not, this contains witnesses for the lack of
Expand All @@ -1304,7 +1304,7 @@ pub struct UsefulnessReport<'p, Cx: MatchCx> {

/// Computes whether a match is exhaustive and which of its arms are useful.
#[instrument(skip(cx, arms), level = "debug")]
pub fn compute_match_usefulness<'p, Cx: MatchCx>(
pub fn compute_match_usefulness<'p, Cx: TypeCx>(
cx: MatchCtxt<'_, 'p, Cx>,
arms: &[MatchArm<'p, Cx>],
scrut_ty: Cx::Ty,
Expand Down

0 comments on commit a6a944d

Please sign in to comment.