Skip to content

Commit ce32d48

Browse files
authored
Rollup merge of #120331 - Nadrieril:no-arena, r=compiler-errors
pattern_analysis: use a plain `Vec` in `DeconstructedPat` The use of an arena-allocated slice in `DeconstructedPat` dates to when we needed the arena anyway for lifetime reasons. Now that we don't, I'm thinking that if `thir::Pat` can use plain old `Vec`s, maybe so can I. r? ```@ghost```
2 parents 3c52832 + f65fe3b commit ce32d48

File tree

7 files changed

+61
-64
lines changed

7 files changed

+61
-64
lines changed

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
291291
err = err.and(check_never_pattern(cx, pat));
292292
});
293293
err?;
294-
Ok(cx.pattern_arena.alloc(cx.lower_pat(pat)))
294+
Ok(self.pattern_arena.alloc(cx.lower_pat(pat)))
295295
}
296296
}
297297

@@ -388,7 +388,6 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
388388
typeck_results: self.typeck_results,
389389
param_env: self.param_env,
390390
module: self.tcx.parent_module(self.lint_level).to_def_id(),
391-
pattern_arena: self.pattern_arena,
392391
dropless_arena: self.dropless_arena,
393392
match_lint_level: self.lint_level,
394393
whole_match_span,

compiler/rustc_pattern_analysis/src/errors.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ impl<'tcx> Uncovered<'tcx> {
2323
span: Span,
2424
cx: &RustcMatchCheckCtxt<'p, 'tcx>,
2525
witnesses: Vec<WitnessPat<'p, 'tcx>>,
26-
) -> Self {
26+
) -> Self
27+
where
28+
'tcx: 'p,
29+
{
2730
let witness_1 = cx.hoist_witness_pat(witnesses.get(0).unwrap());
2831
Self {
2932
span,

compiler/rustc_pattern_analysis/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ pub trait TypeCx: Sized + fmt::Debug {
119119
/// `DeconstructedPat`. Only invoqued when `pat.ctor()` is `Struct | Variant(_) | UnionField`.
120120
fn write_variant_name(
121121
f: &mut fmt::Formatter<'_>,
122-
pat: &crate::pat::DeconstructedPat<'_, Self>,
122+
pat: &crate::pat::DeconstructedPat<Self>,
123123
) -> fmt::Result;
124124

125125
/// Raise a bug.
@@ -130,17 +130,17 @@ pub trait TypeCx: Sized + fmt::Debug {
130130
/// The default implementation does nothing.
131131
fn lint_overlapping_range_endpoints(
132132
&self,
133-
_pat: &DeconstructedPat<'_, Self>,
133+
_pat: &DeconstructedPat<Self>,
134134
_overlaps_on: IntRange,
135-
_overlaps_with: &[&DeconstructedPat<'_, Self>],
135+
_overlaps_with: &[&DeconstructedPat<Self>],
136136
) {
137137
}
138138
}
139139

140140
/// The arm of a match expression.
141141
#[derive(Debug)]
142142
pub struct MatchArm<'p, Cx: TypeCx> {
143-
pub pat: &'p DeconstructedPat<'p, Cx>,
143+
pub pat: &'p DeconstructedPat<Cx>,
144144
pub has_guard: bool,
145145
pub arm_data: Cx::ArmData,
146146
}

compiler/rustc_pattern_analysis/src/pat.rs

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

88
use crate::constructor::{Constructor, Slice, SliceKind};
9-
use crate::{Captures, TypeCx};
9+
use crate::TypeCx;
1010

1111
use self::Constructor::*;
1212

@@ -21,9 +21,9 @@ use self::Constructor::*;
2121
/// This happens if a private or `non_exhaustive` field is uninhabited, because the code mustn't
2222
/// observe that it is uninhabited. In that case that field is not included in `fields`. Care must
2323
/// be taken when converting to/from `thir::Pat`.
24-
pub struct DeconstructedPat<'p, Cx: TypeCx> {
24+
pub struct DeconstructedPat<Cx: TypeCx> {
2525
ctor: Constructor<Cx>,
26-
fields: &'p [DeconstructedPat<'p, Cx>],
26+
fields: Vec<DeconstructedPat<Cx>>,
2727
ty: Cx::Ty,
2828
/// Extra data to store in a pattern. `None` if the pattern is a wildcard that does not
2929
/// correspond to a user-supplied pattern.
@@ -32,14 +32,20 @@ pub struct DeconstructedPat<'p, Cx: TypeCx> {
3232
useful: Cell<bool>,
3333
}
3434

35-
impl<'p, Cx: TypeCx> DeconstructedPat<'p, Cx> {
35+
impl<Cx: TypeCx> DeconstructedPat<Cx> {
3636
pub fn wildcard(ty: Cx::Ty) -> Self {
37-
DeconstructedPat { ctor: Wildcard, fields: &[], ty, data: None, useful: Cell::new(false) }
37+
DeconstructedPat {
38+
ctor: Wildcard,
39+
fields: Vec::new(),
40+
ty,
41+
data: None,
42+
useful: Cell::new(false),
43+
}
3844
}
3945

4046
pub fn new(
4147
ctor: Constructor<Cx>,
42-
fields: &'p [DeconstructedPat<'p, Cx>],
48+
fields: Vec<DeconstructedPat<Cx>>,
4349
ty: Cx::Ty,
4450
data: Cx::PatData,
4551
) -> Self {
@@ -62,17 +68,17 @@ impl<'p, Cx: TypeCx> DeconstructedPat<'p, Cx> {
6268
self.data.as_ref()
6369
}
6470

65-
pub fn iter_fields(&self) -> impl Iterator<Item = &'p DeconstructedPat<'p, Cx>> + Captures<'_> {
71+
pub fn iter_fields<'a>(&'a self) -> impl Iterator<Item = &'a DeconstructedPat<Cx>> {
6672
self.fields.iter()
6773
}
6874

6975
/// Specialize this pattern with a constructor.
7076
/// `other_ctor` can be different from `self.ctor`, but must be covered by it.
71-
pub(crate) fn specialize(
72-
&self,
77+
pub(crate) fn specialize<'a>(
78+
&'a self,
7379
other_ctor: &Constructor<Cx>,
7480
ctor_arity: usize,
75-
) -> SmallVec<[PatOrWild<'p, Cx>; 2]> {
81+
) -> SmallVec<[PatOrWild<'a, Cx>; 2]> {
7682
let wildcard_sub_tys = || (0..ctor_arity).map(|_| PatOrWild::Wild).collect();
7783
match (&self.ctor, other_ctor) {
7884
// Return a wildcard for each field of `other_ctor`.
@@ -139,7 +145,7 @@ impl<'p, Cx: TypeCx> DeconstructedPat<'p, Cx> {
139145
}
140146

141147
/// This is best effort and not good enough for a `Display` impl.
142-
impl<'p, Cx: TypeCx> fmt::Debug for DeconstructedPat<'p, Cx> {
148+
impl<Cx: TypeCx> fmt::Debug for DeconstructedPat<Cx> {
143149
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
144150
let pat = self;
145151
let mut first = true;
@@ -221,7 +227,7 @@ pub(crate) enum PatOrWild<'p, Cx: TypeCx> {
221227
/// A non-user-provided wildcard, created during specialization.
222228
Wild,
223229
/// A user-provided pattern.
224-
Pat(&'p DeconstructedPat<'p, Cx>),
230+
Pat(&'p DeconstructedPat<Cx>),
225231
}
226232

227233
impl<'p, Cx: TypeCx> Clone for PatOrWild<'p, Cx> {
@@ -236,7 +242,7 @@ impl<'p, Cx: TypeCx> Clone for PatOrWild<'p, Cx> {
236242
impl<'p, Cx: TypeCx> Copy for PatOrWild<'p, Cx> {}
237243

238244
impl<'p, Cx: TypeCx> PatOrWild<'p, Cx> {
239-
pub(crate) fn as_pat(&self) -> Option<&'p DeconstructedPat<'p, Cx>> {
245+
pub(crate) fn as_pat(&self) -> Option<&'p DeconstructedPat<Cx>> {
240246
match self {
241247
PatOrWild::Wild => None,
242248
PatOrWild::Pat(pat) => Some(pat),

compiler/rustc_pattern_analysis/src/pat_column.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{Captures, MatchArm, TypeCx};
1313
#[derive(Debug)]
1414
pub struct PatternColumn<'p, Cx: TypeCx> {
1515
/// This must not contain an or-pattern. `expand_and_push` takes care to expand them.
16-
patterns: Vec<&'p DeconstructedPat<'p, Cx>>,
16+
patterns: Vec<&'p DeconstructedPat<Cx>>,
1717
}
1818

1919
impl<'p, Cx: TypeCx> PatternColumn<'p, Cx> {
@@ -41,7 +41,7 @@ impl<'p, Cx: TypeCx> PatternColumn<'p, Cx> {
4141
pub fn head_ty(&self) -> Option<&Cx::Ty> {
4242
self.patterns.first().map(|pat| pat.ty())
4343
}
44-
pub fn iter<'a>(&'a self) -> impl Iterator<Item = &'p DeconstructedPat<'p, Cx>> + Captures<'a> {
44+
pub fn iter<'a>(&'a self) -> impl Iterator<Item = &'p DeconstructedPat<Cx>> + Captures<'a> {
4545
self.patterns.iter().copied()
4646
}
4747

0 commit comments

Comments
 (0)