Skip to content

Commit 72c3a68

Browse files
committed
PatCtxt is not useful anymore
It was introduced to carry max_slice_length so we don't need it anymore.
1 parent 6b4426a commit 72c3a68

File tree

1 file changed

+29
-55
lines changed

1 file changed

+29
-55
lines changed

src/librustc_mir/hair/pattern/_match.rs

Lines changed: 29 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ impl<'tcx> Constructor<'tcx> {
634634
fn split_meta_constructor(
635635
self,
636636
cx: &MatchCheckCtxt<'_, 'tcx>,
637-
pcx: PatCtxt<'tcx>,
637+
ty: Ty<'tcx>,
638638
head_ctors: &Vec<Constructor<'tcx>>,
639639
) -> SmallVec<[Constructor<'tcx>; 1]> {
640640
debug!("split_meta_constructor {:?}", self);
@@ -735,7 +735,7 @@ impl<'tcx> Constructor<'tcx> {
735735
(Border::JustBefore(n), Border::AfterMax) => Some(n..=u128::MAX),
736736
(Border::AfterMax, _) => None,
737737
})
738-
.map(|range| IntRange::range_to_ctor(cx.tcx, pcx.ty, range))
738+
.map(|range| IntRange::range_to_ctor(cx.tcx, ty, range))
739739
.collect()
740740
}
741741
ConstantRange(..) => smallvec![self],
@@ -843,20 +843,19 @@ impl<'tcx> Constructor<'tcx> {
843843
.collect()
844844
}
845845
Wildcard => {
846-
let is_declared_nonexhaustive =
847-
!cx.is_local(pcx.ty) && cx.is_non_exhaustive_enum(pcx.ty);
846+
let is_declared_nonexhaustive = !cx.is_local(ty) && cx.is_non_exhaustive_enum(ty);
848847

849848
// `all_ctors` are all the constructors for the given type, which
850849
// should all be represented (or caught with the wild pattern `_`).
851-
let all_ctors = all_constructors(cx, pcx);
850+
let all_ctors = all_constructors(cx, ty);
852851

853-
let is_privately_empty = all_ctors.is_empty() && !cx.is_uninhabited(pcx.ty);
852+
let is_privately_empty = all_ctors.is_empty() && !cx.is_uninhabited(ty);
854853

855854
// For privately empty and non-exhaustive enums, we work as if there were an "extra"
856855
// `_` constructor for the type, so we can never match over all constructors.
857856
let is_non_exhaustive = is_privately_empty
858857
|| is_declared_nonexhaustive
859-
|| (pcx.ty.is_ptr_sized_integral()
858+
|| (ty.is_ptr_sized_integral()
860859
&& !cx.tcx.features().precise_pointer_size_matching);
861860

862861
// `missing_ctors` is the set of constructors from the same type as the
@@ -880,13 +879,8 @@ impl<'tcx> Constructor<'tcx> {
880879

881880
// Missing constructors are those that are not matched by any
882881
// non-wildcard patterns in the current column.
883-
let missing_ctors = MissingConstructors::new(
884-
pcx,
885-
cx.tcx,
886-
cx.param_env,
887-
all_ctors,
888-
head_ctors.clone(),
889-
);
882+
let missing_ctors =
883+
MissingConstructors::new(cx.tcx, cx.param_env, all_ctors, head_ctors.clone());
890884
debug!(
891885
"missing_ctors.is_empty()={:#?} is_non_exhaustive={:#?}",
892886
missing_ctors.is_empty(),
@@ -920,7 +914,7 @@ impl<'tcx> Constructor<'tcx> {
920914
// contain any wildcards so we don't recurse infinitely.
921915
all_ctors
922916
.into_iter()
923-
.flat_map(|ctor| ctor.split_meta_constructor(cx, pcx, head_ctors))
917+
.flat_map(|ctor| ctor.split_meta_constructor(cx, ty, head_ctors))
924918
.collect()
925919
}
926920
}
@@ -933,7 +927,6 @@ impl<'tcx> Constructor<'tcx> {
933927
/// notation).
934928
fn subtract_meta_constructor(
935929
self,
936-
_pcx: PatCtxt<'tcx>,
937930
tcx: TyCtxt<'tcx>,
938931
param_env: ty::ParamEnv<'tcx>,
939932
used_ctors: &Vec<Constructor<'tcx>>,
@@ -1170,7 +1163,6 @@ impl<'tcx> Constructor<'tcx> {
11701163
fn apply<'a>(
11711164
&self,
11721165
cx: &MatchCheckCtxt<'a, 'tcx>,
1173-
pcx: PatCtxt<'tcx>,
11741166
ty: Ty<'tcx>,
11751167
pats: impl IntoIterator<Item = Pat<'tcx>>,
11761168
) -> SmallVec<[Pat<'tcx>; 1]> {
@@ -1236,7 +1228,7 @@ impl<'tcx> Constructor<'tcx> {
12361228
// `Option::Some`, we get the pattern `Some(_)`.
12371229
return missing_ctors
12381230
.iter()
1239-
.flat_map(|ctor| ctor.apply_wildcards(cx, pcx, ty))
1231+
.flat_map(|ctor| ctor.apply_wildcards(cx, ty))
12401232
.collect();
12411233
}
12421234
};
@@ -1248,11 +1240,10 @@ impl<'tcx> Constructor<'tcx> {
12481240
fn apply_wildcards<'a>(
12491241
&self,
12501242
cx: &MatchCheckCtxt<'a, 'tcx>,
1251-
pcx: PatCtxt<'tcx>,
12521243
ty: Ty<'tcx>,
12531244
) -> SmallVec<[Pat<'tcx>; 1]> {
12541245
let pats = self.wildcard_subpatterns(cx, ty).rev();
1255-
self.apply(cx, pcx, ty, pats)
1246+
self.apply(cx, ty, pats)
12561247
}
12571248
}
12581249

@@ -1281,15 +1272,14 @@ impl<'tcx> Usefulness<'tcx> {
12811272
fn apply_constructor(
12821273
self,
12831274
cx: &MatchCheckCtxt<'_, 'tcx>,
1284-
pcx: PatCtxt<'tcx>,
12851275
ctor: &Constructor<'tcx>,
1286-
lty: Ty<'tcx>,
1276+
ty: Ty<'tcx>,
12871277
) -> Self {
12881278
match self {
12891279
UsefulWithWitness(witnesses) => UsefulWithWitness(
12901280
witnesses
12911281
.into_iter()
1292-
.flat_map(|witness| witness.apply_constructor(cx, pcx, &ctor, lty))
1282+
.flat_map(|witness| witness.apply_constructor(cx, &ctor, ty))
12931283
.collect(),
12941284
),
12951285
x => x,
@@ -1303,11 +1293,6 @@ pub enum WitnessPreference {
13031293
LeaveOutWitness,
13041294
}
13051295

1306-
#[derive(Copy, Clone, Debug)]
1307-
struct PatCtxt<'tcx> {
1308-
ty: Ty<'tcx>,
1309-
}
1310-
13111296
/// A witness of non-exhaustiveness for error reporting, represented
13121297
/// as a list of patterns (in reverse order of construction) with
13131298
/// wildcards inside to represent elements that can take any inhabitant
@@ -1365,15 +1350,14 @@ impl<'tcx> Witness<'tcx> {
13651350
fn apply_constructor<'a>(
13661351
mut self,
13671352
cx: &MatchCheckCtxt<'a, 'tcx>,
1368-
pcx: PatCtxt<'tcx>,
13691353
ctor: &Constructor<'tcx>,
13701354
ty: Ty<'tcx>,
13711355
) -> SmallVec<[Self; 1]> {
13721356
let arity = ctor.arity(cx, ty);
13731357
let applied_pats = {
13741358
let len = self.0.len() as u64;
13751359
let pats = self.0.drain((len - arity) as usize..).rev();
1376-
ctor.apply(cx, pcx, ty, pats)
1360+
ctor.apply(cx, ty, pats)
13771361
};
13781362

13791363
applied_pats
@@ -1396,10 +1380,10 @@ impl<'tcx> Witness<'tcx> {
13961380
/// `Option<!>`, we do not include `Some(_)` in the returned list of constructors.
13971381
fn all_constructors<'a, 'tcx>(
13981382
cx: &MatchCheckCtxt<'a, 'tcx>,
1399-
pcx: PatCtxt<'tcx>,
1383+
ty: Ty<'tcx>,
14001384
) -> Vec<Constructor<'tcx>> {
1401-
debug!("all_constructors({:?})", pcx.ty);
1402-
let ctors = match pcx.ty.kind {
1385+
debug!("all_constructors({:?})", ty);
1386+
let ctors = match ty.kind {
14031387
ty::Bool => {
14041388
[true, false].iter().map(|&b| ConstantValue(ty::Const::from_bool(cx.tcx, b))).collect()
14051389
}
@@ -1447,15 +1431,15 @@ fn all_constructors<'a, 'tcx>(
14471431
let bits = Integer::from_attr(&cx.tcx, SignedInt(ity)).size().bits() as u128;
14481432
let min = 1u128 << (bits - 1);
14491433
let max = min - 1;
1450-
vec![ConstantRange(min, max, pcx.ty, RangeEnd::Included)]
1434+
vec![ConstantRange(min, max, ty, RangeEnd::Included)]
14511435
}
14521436
ty::Uint(uty) => {
14531437
let size = Integer::from_attr(&cx.tcx, UnsignedInt(uty)).size();
14541438
let max = truncate(u128::max_value(), size);
1455-
vec![ConstantRange(0, max, pcx.ty, RangeEnd::Included)]
1439+
vec![ConstantRange(0, max, ty, RangeEnd::Included)]
14561440
}
14571441
_ => {
1458-
if cx.is_uninhabited(pcx.ty) {
1442+
if cx.is_uninhabited(ty) {
14591443
vec![]
14601444
} else {
14611445
vec![Single]
@@ -1648,9 +1632,8 @@ impl<'tcx> IntRange<'tcx> {
16481632
// A struct to compute a set of constructors equivalent to `all_ctors \ used_ctors`.
16491633
#[derive(Clone)]
16501634
struct MissingConstructors<'tcx> {
1651-
pcx: PatCtxt<'tcx>,
1652-
tcx: TyCtxt<'tcx>,
16531635
param_env: ty::ParamEnv<'tcx>,
1636+
tcx: TyCtxt<'tcx>,
16541637
all_ctors: Vec<Constructor<'tcx>>,
16551638
used_ctors: Vec<Constructor<'tcx>>,
16561639
}
@@ -1663,13 +1646,12 @@ type MissingConstructorsIter<'a, 'tcx, F> = std::iter::FlatMap<
16631646

16641647
impl<'tcx> MissingConstructors<'tcx> {
16651648
fn new(
1666-
pcx: PatCtxt<'tcx>,
16671649
tcx: TyCtxt<'tcx>,
16681650
param_env: ty::ParamEnv<'tcx>,
16691651
all_ctors: Vec<Constructor<'tcx>>,
16701652
used_ctors: Vec<Constructor<'tcx>>,
16711653
) -> Self {
1672-
MissingConstructors { pcx, tcx, param_env, all_ctors, used_ctors }
1654+
MissingConstructors { tcx, param_env, all_ctors, used_ctors }
16731655
}
16741656

16751657
fn into_inner(self) -> (Vec<Constructor<'tcx>>, Vec<Constructor<'tcx>>) {
@@ -1690,12 +1672,7 @@ impl<'tcx> MissingConstructors<'tcx> {
16901672
impl FnMut(&'a Constructor<'tcx>) -> SmallVec<[Constructor<'tcx>; 1]>,
16911673
> {
16921674
self.all_ctors.iter().flat_map(move |req_ctor| {
1693-
req_ctor.clone().subtract_meta_constructor(
1694-
self.pcx,
1695-
self.tcx,
1696-
self.param_env,
1697-
&self.used_ctors,
1698-
)
1675+
req_ctor.clone().subtract_meta_constructor(self.tcx, self.param_env, &self.used_ctors)
16991676
})
17001677
}
17011678
}
@@ -1801,12 +1778,10 @@ pub fn is_useful<'p, 'a, 'tcx>(
18011778
.collect();
18021779
debug!("matrix_head_ctors = {:#?}", matrix_head_ctors);
18031780

1804-
let pcx = PatCtxt { ty };
1805-
18061781
v_constructors
18071782
.into_iter()
1808-
.flat_map(|ctor| ctor.split_meta_constructor(cx, pcx, &matrix_head_ctors))
1809-
.map(|c| is_useful_specialized(cx, pcx, matrix, v, c, pcx.ty, witness_preference))
1783+
.flat_map(|ctor| ctor.split_meta_constructor(cx, ty, &matrix_head_ctors))
1784+
.map(|c| is_useful_specialized(cx, matrix, v, c, ty, witness_preference))
18101785
.find(|result| result.is_useful())
18111786
.unwrap_or(NotUseful)
18121787
}
@@ -1815,23 +1790,22 @@ pub fn is_useful<'p, 'a, 'tcx>(
18151790
/// to the specialised version of both the pattern matrix `M` and the new pattern `q`.
18161791
fn is_useful_specialized<'p, 'a, 'tcx>(
18171792
cx: &MatchCheckCtxt<'a, 'tcx>,
1818-
pcx: PatCtxt<'tcx>,
18191793
matrix: &Matrix<'p, 'tcx>,
18201794
v: &PatStack<'_, 'tcx>,
18211795
ctor: Constructor<'tcx>,
1822-
lty: Ty<'tcx>,
1796+
ty: Ty<'tcx>,
18231797
witness_preference: WitnessPreference,
18241798
) -> Usefulness<'tcx> {
1825-
debug!("is_useful_specialized({:#?}, {:#?}, {:?})", v, ctor, lty);
1799+
debug!("is_useful_specialized({:#?}, {:#?}, {:?})", v, ctor, ty);
18261800

1827-
let ctor_wild_subpatterns_owned: Vec<_> = ctor.wildcard_subpatterns(cx, lty).collect();
1801+
let ctor_wild_subpatterns_owned: Vec<_> = ctor.wildcard_subpatterns(cx, ty).collect();
18281802
let ctor_wild_subpatterns: Vec<_> = ctor_wild_subpatterns_owned.iter().collect();
18291803
let matrix = matrix.specialize(cx, &ctor, &ctor_wild_subpatterns);
18301804
let ret = v
18311805
.specialize(cx, &ctor, &ctor_wild_subpatterns)
18321806
.into_iter()
18331807
.map(|v| is_useful(cx, &matrix, &v, witness_preference))
1834-
.map(|u| u.apply_constructor(cx, pcx, &ctor, lty))
1808+
.map(|u| u.apply_constructor(cx, &ctor, ty))
18351809
.find(|result| result.is_useful())
18361810
.unwrap_or(NotUseful);
18371811
ret

0 commit comments

Comments
 (0)