Skip to content

Commit 8a65984

Browse files
committed
Repurpose MatchCtxt for usefulness only
1 parent 78e32a1 commit 8a65984

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

compiler/rustc_pattern_analysis/src/lib.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,6 @@ pub trait TypeCx: Sized + fmt::Debug {
134134
}
135135
}
136136

137-
/// Context that provides information global to a match.
138-
#[derive(derivative::Derivative)]
139-
#[derivative(Clone(bound = ""), Copy(bound = ""))]
140-
pub struct MatchCtxt<'a, Cx: TypeCx> {
141-
/// The context for type information.
142-
pub tycx: &'a Cx,
143-
}
144-
145137
/// The arm of a match expression.
146138
#[derive(Debug)]
147139
#[derive(derivative::Derivative)]
@@ -162,9 +154,7 @@ pub fn analyze_match<'p, 'tcx>(
162154
) -> Result<rustc::UsefulnessReport<'p, 'tcx>, ErrorGuaranteed> {
163155
let scrut_ty = tycx.reveal_opaque_ty(scrut_ty);
164156
let scrut_validity = ValidityConstraint::from_bool(tycx.known_valid_scrutinee);
165-
let cx = MatchCtxt { tycx };
166-
167-
let report = compute_match_usefulness(cx, arms, scrut_ty, scrut_validity)?;
157+
let report = compute_match_usefulness(tycx, arms, scrut_ty, scrut_validity)?;
168158

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

compiler/rustc_pattern_analysis/src/usefulness.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ use std::fmt;
718718

719719
use crate::constructor::{Constructor, ConstructorSet, IntRange};
720720
use crate::pat::{DeconstructedPat, PatOrWild, WitnessPat};
721-
use crate::{Captures, MatchArm, MatchCtxt, TypeCx};
721+
use crate::{Captures, MatchArm, TypeCx};
722722

723723
use self::ValidityConstraint::*;
724724

@@ -729,12 +729,20 @@ pub fn ensure_sufficient_stack<R>(f: impl FnOnce() -> R) -> R {
729729
f()
730730
}
731731

732+
/// Context that provides information for usefulness checking.
733+
#[derive(derivative::Derivative)]
734+
#[derivative(Clone(bound = ""), Copy(bound = ""))]
735+
pub struct UsefulnessCtxt<'a, Cx: TypeCx> {
736+
/// The context for type information.
737+
pub tycx: &'a Cx,
738+
}
739+
732740
/// Context that provides information local to a place under investigation.
733741
#[derive(derivative::Derivative)]
734742
#[derivative(Debug(bound = ""), Clone(bound = ""), Copy(bound = ""))]
735743
struct PlaceCtxt<'a, Cx: TypeCx> {
736744
#[derivative(Debug = "ignore")]
737-
pub(crate) mcx: MatchCtxt<'a, Cx>,
745+
pub(crate) mcx: UsefulnessCtxt<'a, Cx>,
738746
/// Type of the place under investigation.
739747
#[derivative(Clone(clone_with = "Clone::clone"))] // See rust-derivative#90
740748
pub(crate) ty: &'a Cx::Ty,
@@ -1324,7 +1332,7 @@ impl<Cx: TypeCx> WitnessMatrix<Cx> {
13241332
/// We can however get false negatives because exhaustiveness does not explore all cases. See the
13251333
/// section on relevancy at the top of the file.
13261334
fn collect_overlapping_range_endpoints<'p, Cx: TypeCx>(
1327-
mcx: MatchCtxt<'_, Cx>,
1335+
mcx: UsefulnessCtxt<'_, Cx>,
13281336
overlap_range: IntRange,
13291337
matrix: &Matrix<'p, Cx>,
13301338
specialized_matrix: &Matrix<'p, Cx>,
@@ -1397,7 +1405,7 @@ fn collect_overlapping_range_endpoints<'p, Cx: TypeCx>(
13971405
/// This is all explained at the top of the file.
13981406
#[instrument(level = "debug", skip(mcx, is_top_level), ret)]
13991407
fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>(
1400-
mcx: MatchCtxt<'a, Cx>,
1408+
mcx: UsefulnessCtxt<'a, Cx>,
14011409
matrix: &mut Matrix<'p, Cx>,
14021410
is_top_level: bool,
14031411
) -> Result<WitnessMatrix<Cx>, Cx::Error> {
@@ -1547,13 +1555,14 @@ pub struct UsefulnessReport<'p, Cx: TypeCx> {
15471555
}
15481556

15491557
/// Computes whether a match is exhaustive and which of its arms are useful.
1550-
#[instrument(skip(cx, arms), level = "debug")]
1558+
#[instrument(skip(tycx, arms), level = "debug")]
15511559
pub fn compute_match_usefulness<'p, Cx: TypeCx>(
1552-
cx: MatchCtxt<'_, Cx>,
1560+
tycx: &Cx,
15531561
arms: &[MatchArm<'p, Cx>],
15541562
scrut_ty: Cx::Ty,
15551563
scrut_validity: ValidityConstraint,
15561564
) -> Result<UsefulnessReport<'p, Cx>, Cx::Error> {
1565+
let cx = UsefulnessCtxt { tycx };
15571566
let mut matrix = Matrix::new(arms, scrut_ty, scrut_validity);
15581567
let non_exhaustiveness_witnesses =
15591568
compute_exhaustiveness_and_usefulness(cx, &mut matrix, true)?;

0 commit comments

Comments
 (0)