Skip to content

Commit

Permalink
Cleanup comments and dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed Jan 30, 2024
1 parent f92b014 commit 11a3f5d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 31 deletions.
25 changes: 3 additions & 22 deletions compiler/rustc_pattern_analysis/src/pat.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! As explained in [`crate::usefulness`], values and patterns are made from constructors applied to
//! fields. This file defines types that represent patterns in this way.
use std::cell::Cell;
use std::fmt;

use smallvec::{smallvec, SmallVec};
Expand All @@ -11,11 +10,7 @@ use crate::{Captures, TypeCx};
use self::Constructor::*;

/// Values and patterns can be represented as a constructor applied to some fields. This represents
/// a pattern in this form.
/// This also uses interior mutability to keep track of whether the pattern has been found reachable
/// during analysis. For this reason they cannot be cloned.
/// A `DeconstructedPat` will almost always come from user input; the only exception are some
/// `Wildcard`s introduced during specialization.
/// a pattern in this form. A `DeconstructedPat` will always come from user input.
///
/// Note that the number of fields may not match the fields declared in the original struct/variant.
/// This happens if a private or `non_exhaustive` field is uninhabited, because the code mustn't
Expand All @@ -28,13 +23,11 @@ pub struct DeconstructedPat<'p, Cx: TypeCx> {
/// Extra data to store in a pattern. `None` if the pattern is a wildcard that does not
/// correspond to a user-supplied pattern.
data: Option<Cx::PatData>,
/// Whether removing this arm would change the behavior of the match expression.
pub(crate) useful: Cell<bool>,
}

impl<'p, Cx: TypeCx> DeconstructedPat<'p, Cx> {
pub fn wildcard(ty: Cx::Ty) -> Self {
DeconstructedPat { ctor: Wildcard, fields: &[], ty, data: None, useful: Cell::new(false) }
DeconstructedPat { ctor: Wildcard, fields: &[], ty, data: None }
}

pub fn new(
Expand All @@ -43,7 +36,7 @@ impl<'p, Cx: TypeCx> DeconstructedPat<'p, Cx> {
ty: Cx::Ty,
data: Cx::PatData,
) -> Self {
DeconstructedPat { ctor, fields, ty, data: Some(data), useful: Cell::new(false) }
DeconstructedPat { ctor, fields, ty, data: Some(data) }
}

pub(crate) fn is_or_pat(&self) -> bool {
Expand Down Expand Up @@ -101,12 +94,6 @@ impl<'p, Cx: TypeCx> DeconstructedPat<'p, Cx> {
}
}

/// We keep track for each pattern if it was ever useful during the analysis. This is used with
/// `redundant_subpatterns` to report redundant subpatterns arising from or patterns.
pub(crate) fn set_useful(&self) {
self.useful.set(true)
}

/// Walk top-down and call `it` in each place where a pattern occurs
/// starting with the root pattern `walk` is called on. If `it` returns
/// false then we will descend no further but siblings will be processed.
Expand Down Expand Up @@ -261,12 +248,6 @@ impl<'p, Cx: TypeCx> PatOrWild<'p, Cx> {
PatOrWild::Pat(pat) => pat.specialize(other_ctor, ctor_arity),
}
}

pub(crate) fn set_useful(&self) {
if let PatOrWild::Pat(pat) = self {
pat.set_useful()
}
}
}

impl<'p, Cx: TypeCx> fmt::Debug for PatOrWild<'p, Cx> {
Expand Down
14 changes: 5 additions & 9 deletions compiler/rustc_pattern_analysis/src/usefulness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,13 +466,9 @@
//! first pattern of a row in the matrix is an or-pattern, we expand it by duplicating the rest of
//! the row as necessary. This is handled automatically in [`Matrix`].
//!
//! This makes usefulness tracking subtle, because we also want to compute whether an alternative
//! of an or-pattern is redundant, e.g. in `Some(_) | Some(0)`. We track usefulness of each
//! subpattern by interior mutability in [`DeconstructedPat`] with `set_useful`/`is_useful`.
//!
//! It's unfortunate that we have to use interior mutability, but believe me (Nadrieril), I have
//! tried [other](https://github.com/rust-lang/rust/pull/80104)
//! [solutions](https://github.com/rust-lang/rust/pull/80632) and nothing is remotely as simple.
//! This makes usefulness tracking subtle, because we also want to compute whether an alternative of
//! an or-pattern is redundant, e.g. in `Some(_) | Some(0)`. We therefore track usefulness of each
//! subpattern of the match.
//!
//!
//!
Expand Down Expand Up @@ -1452,8 +1448,8 @@ fn collect_overlapping_range_endpoints<'p, Cx: TypeCx>(
/// The core of the algorithm.
///
/// This recursively computes witnesses of the non-exhaustiveness of `matrix` (if any). Also tracks
/// usefulness of each row in the matrix (in `row.useful`). We track usefulness of each
/// subpattern using interior mutability in `DeconstructedPat`.
/// usefulness of each row in the matrix (in `row.useful`). We track usefulness of each subpattern
/// in `mcx.useful_subpatterns`.
///
/// The input `Matrix` and the output `WitnessMatrix` together match the type exhaustively.
///
Expand Down

0 comments on commit 11a3f5d

Please sign in to comment.