Skip to content

Commit 00c54e4

Browse files
committed
Add 'thir lifetime to RustMatchCheckCtxt
1 parent 11e66f0 commit 00c54e4

File tree

6 files changed

+95
-80
lines changed

6 files changed

+95
-80
lines changed

compiler/rustc_mir_build/src/errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,8 @@ pub enum UnusedUnsafeEnclosing {
453453
},
454454
}
455455

456-
pub(crate) struct NonExhaustivePatternsTypeNotEmpty<'p, 'tcx, 'm> {
457-
pub cx: &'m RustcMatchCheckCtxt<'p, 'tcx>,
456+
pub(crate) struct NonExhaustivePatternsTypeNotEmpty<'p, 'thir, 'tcx, 'm> {
457+
pub cx: &'m RustcMatchCheckCtxt<'p, 'thir, 'tcx>,
458458
pub expr_span: Span,
459459
pub span: Span,
460460
pub ty: Ty<'tcx>,

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+22-21
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ struct MatchVisitor<'thir, 'p, 'tcx> {
8282
thir: &'thir Thir<'tcx>,
8383
lint_level: HirId,
8484
let_source: LetSource,
85-
pattern_arena: &'p TypedArena<DeconstructedPat<'p, 'tcx>>,
85+
pattern_arena: &'p TypedArena<DeconstructedPat<'p, 'thir, 'tcx>>,
8686
dropless_arena: &'p DroplessArena,
8787
/// Tracks if we encountered an error while checking this body. That the first function to
8888
/// report it stores it here. Some functions return `Result` to allow callers to short-circuit
@@ -275,9 +275,9 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
275275

276276
fn lower_pattern(
277277
&mut self,
278-
cx: &MatchCheckCtxt<'p, 'tcx>,
278+
cx: &MatchCheckCtxt<'p, 'thir, 'tcx>,
279279
pat: &'thir Pat<'tcx>,
280-
) -> Result<&'p DeconstructedPat<'p, 'tcx>, ErrorGuaranteed> {
280+
) -> Result<&'p DeconstructedPat<'p, 'thir, 'tcx>, ErrorGuaranteed> {
281281
if let Err(err) = pat.pat_error_reported() {
282282
self.error = Err(err);
283283
Err(err)
@@ -370,7 +370,7 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
370370
whole_match_span: Option<Span>,
371371
scrutinee: Option<&Expr<'tcx>>,
372372
scrut_span: Span,
373-
) -> MatchCheckCtxt<'p, 'tcx> {
373+
) -> MatchCheckCtxt<'p, 'thir, 'tcx> {
374374
let refutable = match refutability {
375375
Irrefutable => false,
376376
Refutable => true,
@@ -550,7 +550,8 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
550550
pat: &'thir Pat<'tcx>,
551551
refutability: RefutableFlag,
552552
scrut: Option<&Expr<'tcx>>,
553-
) -> Result<(MatchCheckCtxt<'p, 'tcx>, UsefulnessReport<'p, 'tcx>), ErrorGuaranteed> {
553+
) -> Result<(MatchCheckCtxt<'p, 'thir, 'tcx>, UsefulnessReport<'p, 'thir, 'tcx>), ErrorGuaranteed>
554+
{
554555
let cx = self.new_cx(refutability, None, scrut, pat.span);
555556
let pat = self.lower_pattern(&cx, pat)?;
556557
let arms = [MatchArm { pat, arm_data: self.lint_level, has_guard: false }];
@@ -836,9 +837,9 @@ fn report_irrefutable_let_patterns(
836837
}
837838

838839
/// Report unreachable arms, if any.
839-
fn report_arm_reachability<'p, 'tcx>(
840-
cx: &MatchCheckCtxt<'p, 'tcx>,
841-
report: &UsefulnessReport<'p, 'tcx>,
840+
fn report_arm_reachability<'p, 'thir, 'tcx>(
841+
cx: &MatchCheckCtxt<'p, 'thir, 'tcx>,
842+
report: &UsefulnessReport<'p, 'thir, 'tcx>,
842843
) {
843844
let report_unreachable_pattern = |span, hir_id, catchall: Option<Span>| {
844845
cx.tcx.emit_spanned_lint(
@@ -876,7 +877,7 @@ fn report_arm_reachability<'p, 'tcx>(
876877
}
877878

878879
/// Checks for common cases of "catchall" patterns that may not be intended as such.
879-
fn pat_is_catchall(pat: &DeconstructedPat<'_, '_>) -> bool {
880+
fn pat_is_catchall(pat: &DeconstructedPat<'_, '_, '_>) -> bool {
880881
match pat.ctor() {
881882
Constructor::Wildcard => true,
882883
Constructor::Struct | Constructor::Ref => pat.iter_fields().all(|pat| pat_is_catchall(pat)),
@@ -885,12 +886,12 @@ fn pat_is_catchall(pat: &DeconstructedPat<'_, '_>) -> bool {
885886
}
886887

887888
/// Report that a match is not exhaustive.
888-
fn report_non_exhaustive_match<'p, 'tcx>(
889-
cx: &MatchCheckCtxt<'p, 'tcx>,
889+
fn report_non_exhaustive_match<'p, 'thir, 'tcx>(
890+
cx: &MatchCheckCtxt<'p, 'thir, 'tcx>,
890891
thir: &Thir<'tcx>,
891892
scrut_ty: Ty<'tcx>,
892893
sp: Span,
893-
witnesses: Vec<WitnessPat<'p, 'tcx>>,
894+
witnesses: Vec<WitnessPat<'p, 'thir, 'tcx>>,
894895
arms: &[ArmId],
895896
expr_span: Span,
896897
) -> ErrorGuaranteed {
@@ -1085,12 +1086,12 @@ fn report_non_exhaustive_match<'p, 'tcx>(
10851086
err.emit()
10861087
}
10871088

1088-
fn joined_uncovered_patterns<'p, 'tcx>(
1089-
cx: &MatchCheckCtxt<'p, 'tcx>,
1090-
witnesses: &[WitnessPat<'p, 'tcx>],
1089+
fn joined_uncovered_patterns<'p, 'thir, 'tcx>(
1090+
cx: &MatchCheckCtxt<'p, 'thir, 'tcx>,
1091+
witnesses: &[WitnessPat<'p, 'thir, 'tcx>],
10911092
) -> String {
10921093
const LIMIT: usize = 3;
1093-
let pat_to_str = |pat: &WitnessPat<'p, 'tcx>| cx.hoist_witness_pat(pat).to_string();
1094+
let pat_to_str = |pat: &WitnessPat<'p, 'thir, 'tcx>| cx.hoist_witness_pat(pat).to_string();
10941095
match witnesses {
10951096
[] => bug!(),
10961097
[witness] => format!("`{}`", cx.hoist_witness_pat(witness)),
@@ -1107,8 +1108,8 @@ fn joined_uncovered_patterns<'p, 'tcx>(
11071108
}
11081109

11091110
fn collect_non_exhaustive_tys<'tcx>(
1110-
cx: &MatchCheckCtxt<'_, 'tcx>,
1111-
pat: &WitnessPat<'_, 'tcx>,
1111+
cx: &MatchCheckCtxt<'_, '_, 'tcx>,
1112+
pat: &WitnessPat<'_, '_, 'tcx>,
11121113
non_exhaustive_tys: &mut FxIndexSet<Ty<'tcx>>,
11131114
) {
11141115
if matches!(pat.ctor(), Constructor::NonExhaustive) {
@@ -1127,7 +1128,7 @@ fn collect_non_exhaustive_tys<'tcx>(
11271128
fn report_adt_defined_here<'tcx>(
11281129
tcx: TyCtxt<'tcx>,
11291130
ty: Ty<'tcx>,
1130-
witnesses: &[WitnessPat<'_, 'tcx>],
1131+
witnesses: &[WitnessPat<'_, '_, 'tcx>],
11311132
point_at_non_local_ty: bool,
11321133
) -> Option<AdtDefinedHere<'tcx>> {
11331134
let ty = ty.peel_refs();
@@ -1149,10 +1150,10 @@ fn report_adt_defined_here<'tcx>(
11491150
Some(AdtDefinedHere { adt_def_span, ty, variants })
11501151
}
11511152

1152-
fn maybe_point_at_variant<'a, 'p: 'a, 'tcx: 'p>(
1153+
fn maybe_point_at_variant<'a, 'p: 'a, 'thir: 'p, 'tcx: 'thir>(
11531154
tcx: TyCtxt<'tcx>,
11541155
def: AdtDef<'tcx>,
1155-
patterns: impl Iterator<Item = &'a WitnessPat<'p, 'tcx>>,
1156+
patterns: impl Iterator<Item = &'a WitnessPat<'p, 'thir, 'tcx>>,
11561157
) -> Vec<Span> {
11571158
let mut covered = vec![];
11581159
for pattern in patterns {

compiler/rustc_pattern_analysis/src/errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ pub struct Uncovered<'tcx> {
1919
}
2020

2121
impl<'tcx> Uncovered<'tcx> {
22-
pub fn new<'p>(
22+
pub fn new<'p, 'thir>(
2323
span: Span,
24-
cx: &RustcMatchCheckCtxt<'p, 'tcx>,
25-
witnesses: Vec<WitnessPat<'p, 'tcx>>,
24+
cx: &RustcMatchCheckCtxt<'p, 'thir, 'tcx>,
25+
witnesses: Vec<WitnessPat<'p, 'thir, 'tcx>>,
2626
) -> Self {
2727
let witness_1 = cx.hoist_witness_pat(witnesses.get(0).unwrap());
2828
Self {

compiler/rustc_pattern_analysis/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ pub struct MatchArm<'p, Cx: TypeCx> {
107107
/// The entrypoint for this crate. Computes whether a match is exhaustive and which of its arms are
108108
/// useful, and runs some lints.
109109
#[cfg(feature = "rustc")]
110-
pub fn analyze_match<'p, 'tcx>(
111-
tycx: &RustcMatchCheckCtxt<'p, 'tcx>,
112-
arms: &[rustc::MatchArm<'p, 'tcx>],
110+
pub fn analyze_match<'p, 'thir, 'tcx>(
111+
tycx: &RustcMatchCheckCtxt<'p, 'thir, 'tcx>,
112+
arms: &[rustc::MatchArm<'p, 'thir, 'tcx>],
113113
scrut_ty: Ty<'tcx>,
114-
) -> rustc::UsefulnessReport<'p, 'tcx> {
114+
) -> rustc::UsefulnessReport<'p, 'thir, 'tcx> {
115115
// Arena to store the extra wildcards we construct during analysis.
116116
let wildcard_arena = tycx.pattern_arena;
117117
let scrut_validity = ValidityConstraint::from_bool(tycx.known_valid_scrutinee);

compiler/rustc_pattern_analysis/src/lints.rs

+28-23
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ use crate::TypeCx;
2828
///
2929
/// This is not used in the main algorithm; only in lints.
3030
#[derive(Debug)]
31-
pub(crate) struct PatternColumn<'a, 'p, 'tcx> {
32-
patterns: Vec<&'a DeconstructedPat<'p, 'tcx>>,
31+
pub(crate) struct PatternColumn<'a, 'p, 'thir, 'tcx> {
32+
patterns: Vec<&'a DeconstructedPat<'p, 'thir, 'tcx>>,
3333
}
3434

35-
impl<'a, 'p, 'tcx> PatternColumn<'a, 'p, 'tcx> {
36-
pub(crate) fn new(arms: &[MatchArm<'p, 'tcx>]) -> Self {
35+
impl<'a, 'p, 'thir, 'tcx> PatternColumn<'a, 'p, 'thir, 'tcx> {
36+
pub(crate) fn new(arms: &[MatchArm<'p, 'thir, 'tcx>]) -> Self {
3737
let mut patterns = Vec::with_capacity(arms.len());
3838
for arm in arms {
3939
if arm.pat.is_or_pat() {
@@ -48,7 +48,7 @@ impl<'a, 'p, 'tcx> PatternColumn<'a, 'p, 'tcx> {
4848
fn is_empty(&self) -> bool {
4949
self.patterns.is_empty()
5050
}
51-
fn head_ty(&self, cx: MatchCtxt<'a, 'p, 'tcx>) -> Option<Ty<'tcx>> {
51+
fn head_ty(&self, cx: MatchCtxt<'a, 'p, 'thir, 'tcx>) -> Option<Ty<'tcx>> {
5252
if self.patterns.len() == 0 {
5353
return None;
5454
}
@@ -59,12 +59,17 @@ impl<'a, 'p, 'tcx> PatternColumn<'a, 'p, 'tcx> {
5959
}
6060

6161
/// Do constructor splitting on the constructors of the column.
62-
fn analyze_ctors(&self, pcx: &PlaceCtxt<'_, 'p, 'tcx>) -> SplitConstructorSet<'p, 'tcx> {
62+
fn analyze_ctors(
63+
&self,
64+
pcx: &PlaceCtxt<'_, 'p, 'thir, 'tcx>,
65+
) -> SplitConstructorSet<'p, 'thir, 'tcx> {
6366
let column_ctors = self.patterns.iter().map(|p| p.ctor());
6467
pcx.ctors_for_ty().split(pcx, column_ctors)
6568
}
6669

67-
fn iter<'b>(&'b self) -> impl Iterator<Item = &'a DeconstructedPat<'p, 'tcx>> + Captures<'b> {
70+
fn iter<'b>(
71+
&'b self,
72+
) -> impl Iterator<Item = &'a DeconstructedPat<'p, 'thir, 'tcx>> + Captures<'b> {
6873
self.patterns.iter().copied()
6974
}
7075

@@ -75,9 +80,9 @@ impl<'a, 'p, 'tcx> PatternColumn<'a, 'p, 'tcx> {
7580
/// which may change the lengths.
7681
fn specialize(
7782
&self,
78-
pcx: &PlaceCtxt<'a, 'p, 'tcx>,
79-
ctor: &Constructor<'p, 'tcx>,
80-
) -> Vec<PatternColumn<'a, 'p, 'tcx>> {
83+
pcx: &PlaceCtxt<'a, 'p, 'thir, 'tcx>,
84+
ctor: &Constructor<'p, 'thir, 'tcx>,
85+
) -> Vec<PatternColumn<'a, 'p, 'thir, 'tcx>> {
8186
let arity = ctor.arity(pcx);
8287
if arity == 0 {
8388
return Vec::new();
@@ -113,10 +118,10 @@ impl<'a, 'p, 'tcx> PatternColumn<'a, 'p, 'tcx> {
113118
/// Traverse the patterns to collect any variants of a non_exhaustive enum that fail to be mentioned
114119
/// in a given column.
115120
#[instrument(level = "debug", skip(cx), ret)]
116-
fn collect_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
117-
cx: MatchCtxt<'a, 'p, 'tcx>,
118-
column: &PatternColumn<'a, 'p, 'tcx>,
119-
) -> Vec<WitnessPat<'p, 'tcx>> {
121+
fn collect_nonexhaustive_missing_variants<'a, 'p, 'thir, 'tcx>(
122+
cx: MatchCtxt<'a, 'p, 'thir, 'tcx>,
123+
column: &PatternColumn<'a, 'p, 'thir, 'tcx>,
124+
) -> Vec<WitnessPat<'p, 'thir, 'tcx>> {
120125
let Some(ty) = column.head_ty(cx) else {
121126
return Vec::new();
122127
};
@@ -160,13 +165,13 @@ fn collect_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
160165
witnesses
161166
}
162167

163-
pub(crate) fn lint_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
164-
cx: MatchCtxt<'a, 'p, 'tcx>,
165-
arms: &[MatchArm<'p, 'tcx>],
166-
pat_column: &PatternColumn<'a, 'p, 'tcx>,
168+
pub(crate) fn lint_nonexhaustive_missing_variants<'a, 'p, 'thir, 'tcx>(
169+
cx: MatchCtxt<'a, 'p, 'thir, 'tcx>,
170+
arms: &[MatchArm<'p, 'thir, 'tcx>],
171+
pat_column: &PatternColumn<'a, 'p, 'thir, 'tcx>,
167172
scrut_ty: Ty<'tcx>,
168173
) {
169-
let rcx: &RustcMatchCheckCtxt<'_, '_> = cx.tycx;
174+
let rcx: &RustcMatchCheckCtxt<'_, '_, '_> = cx.tycx;
170175
if !matches!(
171176
rcx.tcx.lint_level_at_node(NON_EXHAUSTIVE_OMITTED_PATTERNS, rcx.match_lint_level).0,
172177
rustc_session::lint::Level::Allow
@@ -214,15 +219,15 @@ pub(crate) fn lint_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
214219

215220
/// Traverse the patterns to warn the user about ranges that overlap on their endpoints.
216221
#[instrument(level = "debug", skip(cx))]
217-
pub(crate) fn lint_overlapping_range_endpoints<'a, 'p, 'tcx>(
218-
cx: MatchCtxt<'a, 'p, 'tcx>,
219-
column: &PatternColumn<'a, 'p, 'tcx>,
222+
pub(crate) fn lint_overlapping_range_endpoints<'a, 'p, 'thir, 'tcx>(
223+
cx: MatchCtxt<'a, 'p, 'thir, 'tcx>,
224+
column: &PatternColumn<'a, 'p, 'thir, 'tcx>,
220225
) {
221226
let Some(ty) = column.head_ty(cx) else {
222227
return;
223228
};
224229
let pcx = &PlaceCtxt::new_dummy(cx, ty);
225-
let rcx: &RustcMatchCheckCtxt<'_, '_> = cx.tycx;
230+
let rcx: &RustcMatchCheckCtxt<'_, '_, '_> = cx.tycx;
226231

227232
let set = column.analyze_ctors(pcx);
228233

0 commit comments

Comments
 (0)