Skip to content

Commit 83e88c6

Browse files
committed
Repurpose MatchCtxt for usefulness only
1 parent cb0e8c5 commit 83e88c6

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

compiler/rustc_pattern_analysis/src/lib.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -135,20 +135,6 @@ pub trait TypeCx: Sized + fmt::Debug {
135135
}
136136
}
137137

138-
/// Context that provides information global to a match.
139-
pub struct MatchCtxt<'a, Cx: TypeCx> {
140-
/// The context for type information.
141-
pub tycx: &'a Cx,
142-
}
143-
144-
impl<'a, Cx: TypeCx> Clone for MatchCtxt<'a, Cx> {
145-
fn clone(&self) -> Self {
146-
Self { tycx: self.tycx }
147-
}
148-
}
149-
150-
impl<'a, Cx: TypeCx> Copy for MatchCtxt<'a, Cx> {}
151-
152138
/// The arm of a match expression.
153139
#[derive(Debug)]
154140
pub struct MatchArm<'p, Cx: TypeCx> {
@@ -175,9 +161,7 @@ pub fn analyze_match<'p, 'tcx>(
175161
) -> Result<rustc::UsefulnessReport<'p, 'tcx>, ErrorGuaranteed> {
176162
let scrut_ty = tycx.reveal_opaque_ty(scrut_ty);
177163
let scrut_validity = ValidityConstraint::from_bool(tycx.known_valid_scrutinee);
178-
let cx = MatchCtxt { tycx };
179-
180-
let report = compute_match_usefulness(cx, arms, scrut_ty, scrut_validity)?;
164+
let report = compute_match_usefulness(tycx, arms, scrut_ty, scrut_validity)?;
181165

182166
// Run the non_exhaustive_omitted_patterns lint. Only run on refutable patterns to avoid hitting
183167
// `if let`s. Only run if the match is exhaustive otherwise the error is redundant.

compiler/rustc_pattern_analysis/src/usefulness.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ use std::fmt;
719719

720720
use crate::constructor::{Constructor, ConstructorSet, IntRange};
721721
use crate::pat::{DeconstructedPat, PatOrWild, WitnessPat};
722-
use crate::{Captures, MatchArm, MatchCtxt, TypeCx};
722+
use crate::{Captures, MatchArm, TypeCx};
723723

724724
use self::ValidityConstraint::*;
725725

@@ -730,9 +730,22 @@ pub fn ensure_sufficient_stack<R>(f: impl FnOnce() -> R) -> R {
730730
f()
731731
}
732732

733+
/// Context that provides information for usefulness checking.
734+
pub struct UsefulnessCtxt<'a, Cx: TypeCx> {
735+
/// The context for type information.
736+
pub tycx: &'a Cx,
737+
}
738+
739+
impl<'a, Cx: TypeCx> Copy for UsefulnessCtxt<'a, Cx> {}
740+
impl<'a, Cx: TypeCx> Clone for UsefulnessCtxt<'a, Cx> {
741+
fn clone(&self) -> Self {
742+
Self { tycx: self.tycx }
743+
}
744+
}
745+
733746
/// Context that provides information local to a place under investigation.
734747
struct PlaceCtxt<'a, Cx: TypeCx> {
735-
mcx: MatchCtxt<'a, Cx>,
748+
mcx: UsefulnessCtxt<'a, Cx>,
736749
/// Type of the place under investigation.
737750
ty: &'a Cx::Ty,
738751
}
@@ -1358,7 +1371,7 @@ impl<Cx: TypeCx> WitnessMatrix<Cx> {
13581371
/// We can however get false negatives because exhaustiveness does not explore all cases. See the
13591372
/// section on relevancy at the top of the file.
13601373
fn collect_overlapping_range_endpoints<'p, Cx: TypeCx>(
1361-
mcx: MatchCtxt<'_, Cx>,
1374+
mcx: UsefulnessCtxt<'_, Cx>,
13621375
overlap_range: IntRange,
13631376
matrix: &Matrix<'p, Cx>,
13641377
specialized_matrix: &Matrix<'p, Cx>,
@@ -1431,7 +1444,7 @@ fn collect_overlapping_range_endpoints<'p, Cx: TypeCx>(
14311444
/// This is all explained at the top of the file.
14321445
#[instrument(level = "debug", skip(mcx, is_top_level), ret)]
14331446
fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>(
1434-
mcx: MatchCtxt<'a, Cx>,
1447+
mcx: UsefulnessCtxt<'a, Cx>,
14351448
matrix: &mut Matrix<'p, Cx>,
14361449
is_top_level: bool,
14371450
) -> Result<WitnessMatrix<Cx>, Cx::Error> {
@@ -1588,13 +1601,14 @@ pub struct UsefulnessReport<'p, Cx: TypeCx> {
15881601
}
15891602

15901603
/// Computes whether a match is exhaustive and which of its arms are useful.
1591-
#[instrument(skip(cx, arms), level = "debug")]
1604+
#[instrument(skip(tycx, arms), level = "debug")]
15921605
pub fn compute_match_usefulness<'p, Cx: TypeCx>(
1593-
cx: MatchCtxt<'_, Cx>,
1606+
tycx: &Cx,
15941607
arms: &[MatchArm<'p, Cx>],
15951608
scrut_ty: Cx::Ty,
15961609
scrut_validity: ValidityConstraint,
15971610
) -> Result<UsefulnessReport<'p, Cx>, Cx::Error> {
1611+
let cx = UsefulnessCtxt { tycx };
15981612
let mut matrix = Matrix::new(arms, scrut_ty, scrut_validity);
15991613
let non_exhaustiveness_witnesses =
16001614
compute_exhaustiveness_and_usefulness(cx, &mut matrix, true)?;

0 commit comments

Comments
 (0)