Skip to content

Commit cb0e8c5

Browse files
committed
Limit the use of PlaceCtxt
1 parent 0b2579a commit cb0e8c5

File tree

6 files changed

+42
-52
lines changed

6 files changed

+42
-52
lines changed

compiler/rustc_pattern_analysis/src/constructor.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ use self::MaybeInfiniteInt::*;
163163
use self::SliceKind::*;
164164

165165
use crate::index;
166-
use crate::usefulness::PlaceCtxt;
167166
use crate::TypeCx;
168167

169168
/// Whether we have seen a constructor in the column or not.
@@ -818,21 +817,20 @@ impl<Cx: TypeCx> Constructor<Cx> {
818817

819818
/// The number of fields for this constructor. This must be kept in sync with
820819
/// `Fields::wildcards`.
821-
pub(crate) fn arity(&self, pcx: &PlaceCtxt<'_, Cx>) -> usize {
822-
pcx.ctor_arity(self)
820+
pub(crate) fn arity(&self, cx: &Cx, ty: &Cx::Ty) -> usize {
821+
cx.ctor_arity(self, ty)
823822
}
824823

825824
/// Returns whether `self` is covered by `other`, i.e. whether `self` is a subset of `other`.
826825
/// For the simple cases, this is simply checking for equality. For the "grouped" constructors,
827826
/// this checks for inclusion.
828827
// We inline because this has a single call site in `Matrix::specialize_constructor`.
829828
#[inline]
830-
pub(crate) fn is_covered_by(&self, pcx: &PlaceCtxt<'_, Cx>, other: &Self) -> bool {
829+
pub(crate) fn is_covered_by(&self, cx: &Cx, other: &Self) -> bool {
831830
match (self, other) {
832-
(Wildcard, _) => pcx
833-
.mcx
834-
.tycx
835-
.bug(format_args!("Constructor splitting should not have returned `Wildcard`")),
831+
(Wildcard, _) => {
832+
cx.bug(format_args!("Constructor splitting should not have returned `Wildcard`"))
833+
}
836834
// Wildcards cover anything
837835
(_, Wildcard) => true,
838836
// Only a wildcard pattern can match these special constructors.
@@ -873,7 +871,7 @@ impl<Cx: TypeCx> Constructor<Cx> {
873871
(Opaque(self_id), Opaque(other_id)) => self_id == other_id,
874872
(Opaque(..), _) | (_, Opaque(..)) => false,
875873

876-
_ => pcx.mcx.tycx.bug(format_args!(
874+
_ => cx.bug(format_args!(
877875
"trying to compare incompatible constructors {self:?} and {other:?}"
878876
)),
879877
}

compiler/rustc_pattern_analysis/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ pub fn analyze_match<'p, 'tcx>(
183183
// `if let`s. Only run if the match is exhaustive otherwise the error is redundant.
184184
if tycx.refutable && report.non_exhaustiveness_witnesses.is_empty() {
185185
let pat_column = PatternColumn::new(arms);
186-
lint_nonexhaustive_missing_variants(cx, arms, &pat_column, scrut_ty)?;
186+
lint_nonexhaustive_missing_variants(tycx, arms, &pat_column, scrut_ty)?;
187187
}
188188

189189
Ok(report)

compiler/rustc_pattern_analysis/src/lints.rs

+16-18
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ use rustc_span::ErrorGuaranteed;
44
use crate::constructor::{Constructor, SplitConstructorSet};
55
use crate::errors::{NonExhaustiveOmittedPattern, NonExhaustiveOmittedPatternLintOnArm, Uncovered};
66
use crate::pat::{DeconstructedPat, PatOrWild};
7-
use crate::rustc::{MatchCtxt, RevealedTy, RustcMatchCheckCtxt, WitnessPat};
8-
use crate::usefulness::PlaceCtxt;
7+
use crate::rustc::{RevealedTy, RustcMatchCheckCtxt, WitnessPat};
98
use crate::{MatchArm, TypeCx};
109

1110
/// A column of patterns in the matrix, where a column is the intuitive notion of "subpatterns that
@@ -50,9 +49,9 @@ impl<'p, Cx: TypeCx> PatternColumn<'p, Cx> {
5049
}
5150

5251
/// Do constructor splitting on the constructors of the column.
53-
fn analyze_ctors(&self, pcx: &PlaceCtxt<'_, Cx>) -> Result<SplitConstructorSet<Cx>, Cx::Error> {
52+
fn analyze_ctors(&self, cx: &Cx, ty: &Cx::Ty) -> Result<SplitConstructorSet<Cx>, Cx::Error> {
5453
let column_ctors = self.patterns.iter().map(|p| p.ctor());
55-
let ctors_for_ty = &pcx.ctors_for_ty()?;
54+
let ctors_for_ty = cx.ctors_for_ty(ty)?;
5655
Ok(ctors_for_ty.split(column_ctors))
5756
}
5857

@@ -63,10 +62,11 @@ impl<'p, Cx: TypeCx> PatternColumn<'p, Cx> {
6362
/// which may change the lengths.
6463
fn specialize(
6564
&self,
66-
pcx: &PlaceCtxt<'_, Cx>,
65+
cx: &Cx,
66+
ty: &Cx::Ty,
6767
ctor: &Constructor<Cx>,
6868
) -> Vec<PatternColumn<'p, Cx>> {
69-
let arity = ctor.arity(pcx);
69+
let arity = ctor.arity(cx, ty);
7070
if arity == 0 {
7171
return Vec::new();
7272
}
@@ -77,7 +77,7 @@ impl<'p, Cx: TypeCx> PatternColumn<'p, Cx> {
7777
let mut specialized_columns: Vec<_> =
7878
(0..arity).map(|_| Self { patterns: Vec::new() }).collect();
7979
let relevant_patterns =
80-
self.patterns.iter().filter(|pat| ctor.is_covered_by(pcx, pat.ctor()));
80+
self.patterns.iter().filter(|pat| ctor.is_covered_by(cx, pat.ctor()));
8181
for pat in relevant_patterns {
8282
let specialized = pat.specialize(ctor, arity);
8383
for (subpat, column) in specialized.into_iter().zip(&mut specialized_columns) {
@@ -92,15 +92,14 @@ impl<'p, Cx: TypeCx> PatternColumn<'p, Cx> {
9292
/// in a given column.
9393
#[instrument(level = "debug", skip(cx), ret)]
9494
fn collect_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
95-
cx: MatchCtxt<'a, 'p, 'tcx>,
95+
cx: &RustcMatchCheckCtxt<'p, 'tcx>,
9696
column: &PatternColumn<'p, RustcMatchCheckCtxt<'p, 'tcx>>,
9797
) -> Result<Vec<WitnessPat<'p, 'tcx>>, ErrorGuaranteed> {
9898
let Some(&ty) = column.head_ty() else {
9999
return Ok(Vec::new());
100100
};
101-
let pcx = &PlaceCtxt::new_dummy(cx, &ty);
102101

103-
let set = column.analyze_ctors(pcx)?;
102+
let set = column.analyze_ctors(cx, &ty)?;
104103
if set.present.is_empty() {
105104
// We can't consistently handle the case where no constructors are present (since this would
106105
// require digging deep through any type in case there's a non_exhaustive enum somewhere),
@@ -109,20 +108,20 @@ fn collect_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
109108
}
110109

111110
let mut witnesses = Vec::new();
112-
if cx.tycx.is_foreign_non_exhaustive_enum(ty) {
111+
if cx.is_foreign_non_exhaustive_enum(ty) {
113112
witnesses.extend(
114113
set.missing
115114
.into_iter()
116115
// This will list missing visible variants.
117116
.filter(|c| !matches!(c, Constructor::Hidden | Constructor::NonExhaustive))
118-
.map(|missing_ctor| WitnessPat::wild_from_ctor(pcx, missing_ctor)),
117+
.map(|missing_ctor| WitnessPat::wild_from_ctor(cx, missing_ctor, ty)),
119118
)
120119
}
121120

122121
// Recurse into the fields.
123122
for ctor in set.present {
124-
let specialized_columns = column.specialize(pcx, &ctor);
125-
let wild_pat = WitnessPat::wild_from_ctor(pcx, ctor);
123+
let specialized_columns = column.specialize(cx, &ty, &ctor);
124+
let wild_pat = WitnessPat::wild_from_ctor(cx, ctor, ty);
126125
for (i, col_i) in specialized_columns.iter().enumerate() {
127126
// Compute witnesses for each column.
128127
let wits_for_col_i = collect_nonexhaustive_missing_variants(cx, col_i)?;
@@ -138,18 +137,17 @@ fn collect_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
138137
Ok(witnesses)
139138
}
140139

141-
pub(crate) fn lint_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
142-
cx: MatchCtxt<'a, 'p, 'tcx>,
140+
pub(crate) fn lint_nonexhaustive_missing_variants<'p, 'tcx>(
141+
rcx: &RustcMatchCheckCtxt<'p, 'tcx>,
143142
arms: &[MatchArm<'p, RustcMatchCheckCtxt<'p, 'tcx>>],
144143
pat_column: &PatternColumn<'p, RustcMatchCheckCtxt<'p, 'tcx>>,
145144
scrut_ty: RevealedTy<'tcx>,
146145
) -> Result<(), ErrorGuaranteed> {
147-
let rcx: &RustcMatchCheckCtxt<'_, '_> = cx.tycx;
148146
if !matches!(
149147
rcx.tcx.lint_level_at_node(NON_EXHAUSTIVE_OMITTED_PATTERNS, rcx.match_lint_level).0,
150148
rustc_session::lint::Level::Allow
151149
) {
152-
let witnesses = collect_nonexhaustive_missing_variants(cx, pat_column)?;
150+
let witnesses = collect_nonexhaustive_missing_variants(rcx, pat_column)?;
153151
if !witnesses.is_empty() {
154152
// Report that a match of a `non_exhaustive` enum marked with `non_exhaustive_omitted_patterns`
155153
// is not exhaustive enough.

compiler/rustc_pattern_analysis/src/pat.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::fmt;
66
use smallvec::{smallvec, SmallVec};
77

88
use crate::constructor::{Constructor, Slice, SliceKind};
9-
use crate::usefulness::PlaceCtxt;
109
use crate::{Captures, TypeCx};
1110

1211
use self::Constructor::*;
@@ -331,9 +330,9 @@ impl<Cx: TypeCx> WitnessPat<Cx> {
331330
/// Construct a pattern that matches everything that starts with this constructor.
332331
/// For example, if `ctor` is a `Constructor::Variant` for `Option::Some`, we get the pattern
333332
/// `Some(_)`.
334-
pub(crate) fn wild_from_ctor(pcx: &PlaceCtxt<'_, Cx>, ctor: Constructor<Cx>) -> Self {
335-
let fields = pcx.ctor_sub_tys(&ctor).map(|ty| Self::wildcard(ty)).collect();
336-
Self::new(ctor, fields, pcx.ty.clone())
333+
pub(crate) fn wild_from_ctor(cx: &Cx, ctor: Constructor<Cx>, ty: Cx::Ty) -> Self {
334+
let fields = cx.ctor_sub_tys(&ctor, &ty).map(|ty| Self::wildcard(ty)).collect();
335+
Self::new(ctor, fields, ty)
337336
}
338337

339338
pub fn ctor(&self) -> &Constructor<Cx> {

compiler/rustc_pattern_analysis/src/rustc.rs

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ pub type ConstructorSet<'p, 'tcx> =
3030
pub type DeconstructedPat<'p, 'tcx> =
3131
crate::pat::DeconstructedPat<'p, RustcMatchCheckCtxt<'p, 'tcx>>;
3232
pub type MatchArm<'p, 'tcx> = crate::MatchArm<'p, RustcMatchCheckCtxt<'p, 'tcx>>;
33-
pub type MatchCtxt<'a, 'p, 'tcx> = crate::MatchCtxt<'a, RustcMatchCheckCtxt<'p, 'tcx>>;
3433
pub type Usefulness<'p, 'tcx> = crate::usefulness::Usefulness<'p, RustcMatchCheckCtxt<'p, 'tcx>>;
3534
pub type UsefulnessReport<'p, 'tcx> =
3635
crate::usefulness::UsefulnessReport<'p, RustcMatchCheckCtxt<'p, 'tcx>>;

compiler/rustc_pattern_analysis/src/usefulness.rs

+15-19
Original file line numberDiff line numberDiff line change
@@ -731,45 +731,41 @@ pub fn ensure_sufficient_stack<R>(f: impl FnOnce() -> R) -> R {
731731
}
732732

733733
/// Context that provides information local to a place under investigation.
734-
pub(crate) struct PlaceCtxt<'a, Cx: TypeCx> {
735-
pub(crate) mcx: MatchCtxt<'a, Cx>,
734+
struct PlaceCtxt<'a, Cx: TypeCx> {
735+
mcx: MatchCtxt<'a, Cx>,
736736
/// Type of the place under investigation.
737-
pub(crate) ty: &'a Cx::Ty,
737+
ty: &'a Cx::Ty,
738738
}
739739

740+
impl<'a, Cx: TypeCx> Copy for PlaceCtxt<'a, Cx> {}
740741
impl<'a, Cx: TypeCx> Clone for PlaceCtxt<'a, Cx> {
741742
fn clone(&self) -> Self {
742743
Self { mcx: self.mcx, ty: self.ty }
743744
}
744745
}
745746

746-
impl<'a, Cx: TypeCx> Copy for PlaceCtxt<'a, Cx> {}
747-
748747
impl<'a, Cx: TypeCx> fmt::Debug for PlaceCtxt<'a, Cx> {
749748
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
750749
fmt.debug_struct("PlaceCtxt").field("ty", self.ty).finish()
751750
}
752751
}
753752

754753
impl<'a, Cx: TypeCx> PlaceCtxt<'a, Cx> {
755-
/// A `PlaceCtxt` when code other than `is_useful` needs one.
756-
#[cfg_attr(not(feature = "rustc"), allow(dead_code))]
757-
pub(crate) fn new_dummy(mcx: MatchCtxt<'a, Cx>, ty: &'a Cx::Ty) -> Self {
758-
PlaceCtxt { mcx, ty }
759-
}
760-
761-
pub(crate) fn ctor_arity(&self, ctor: &Constructor<Cx>) -> usize {
754+
fn ctor_arity(&self, ctor: &Constructor<Cx>) -> usize {
762755
self.mcx.tycx.ctor_arity(ctor, self.ty)
763756
}
764-
pub(crate) fn ctor_sub_tys(
757+
fn ctor_sub_tys(
765758
&'a self,
766759
ctor: &'a Constructor<Cx>,
767760
) -> impl Iterator<Item = Cx::Ty> + ExactSizeIterator + Captures<'a> {
768761
self.mcx.tycx.ctor_sub_tys(ctor, self.ty)
769762
}
770-
pub(crate) fn ctors_for_ty(&self) -> Result<ConstructorSet<Cx>, Cx::Error> {
763+
fn ctors_for_ty(&self) -> Result<ConstructorSet<Cx>, Cx::Error> {
771764
self.mcx.tycx.ctors_for_ty(self.ty)
772765
}
766+
fn wild_from_ctor(&self, ctor: Constructor<Cx>) -> WitnessPat<Cx> {
767+
WitnessPat::wild_from_ctor(self.mcx.tycx, ctor, self.ty.clone())
768+
}
773769
}
774770

775771
/// Serves two purposes:
@@ -1089,7 +1085,7 @@ impl<'p, Cx: TypeCx> Matrix<'p, Cx> {
10891085
wildcard_row_is_relevant: self.wildcard_row_is_relevant && ctor_is_relevant,
10901086
};
10911087
for (i, row) in self.rows().enumerate() {
1092-
if ctor.is_covered_by(pcx, row.head().ctor()) {
1088+
if ctor.is_covered_by(pcx.mcx.tycx, row.head().ctor()) {
10931089
let new_row = row.pop_head_constructor(ctor, arity, ctor_is_relevant, i);
10941090
matrix.expand_and_push(new_row);
10951091
}
@@ -1240,7 +1236,7 @@ impl<Cx: TypeCx> WitnessStack<Cx> {
12401236
/// ```
12411237
fn apply_constructor(&mut self, pcx: &PlaceCtxt<'_, Cx>, ctor: &Constructor<Cx>) {
12421238
let len = self.0.len();
1243-
let arity = ctor.arity(pcx);
1239+
let arity = pcx.ctor_arity(ctor);
12441240
let fields = self.0.drain((len - arity)..).rev().collect();
12451241
let pat = WitnessPat::new(ctor.clone(), fields, pcx.ty.clone());
12461242
self.0.push(pat);
@@ -1315,20 +1311,20 @@ impl<Cx: TypeCx> WitnessMatrix<Cx> {
13151311
*self = Self::empty();
13161312
} else if !report_individual_missing_ctors {
13171313
// Report `_` as missing.
1318-
let pat = WitnessPat::wild_from_ctor(pcx, Constructor::Wildcard);
1314+
let pat = pcx.wild_from_ctor(Constructor::Wildcard);
13191315
self.push_pattern(pat);
13201316
} else if missing_ctors.iter().any(|c| c.is_non_exhaustive()) {
13211317
// We need to report a `_` anyway, so listing other constructors would be redundant.
13221318
// `NonExhaustive` is displayed as `_` just like `Wildcard`, but it will be picked
13231319
// up by diagnostics to add a note about why `_` is required here.
1324-
let pat = WitnessPat::wild_from_ctor(pcx, Constructor::NonExhaustive);
1320+
let pat = pcx.wild_from_ctor(Constructor::NonExhaustive);
13251321
self.push_pattern(pat);
13261322
} else {
13271323
// For each missing constructor `c`, we add a `c(_, _, _)` witness appropriately
13281324
// filled with wildcards.
13291325
let mut ret = Self::empty();
13301326
for ctor in missing_ctors {
1331-
let pat = WitnessPat::wild_from_ctor(pcx, ctor.clone());
1327+
let pat = pcx.wild_from_ctor(ctor.clone());
13321328
// Clone `self` and add `c(_, _, _)` to each of its witnesses.
13331329
let mut wit_matrix = self.clone();
13341330
wit_matrix.push_pattern(pat);

0 commit comments

Comments
 (0)