Skip to content

Commit be77cf8

Browse files
committed
Use a Vec instead of a slice in DeconstructedPat
1 parent 11f32b7 commit be77cf8

File tree

5 files changed

+51
-54
lines changed

5 files changed

+51
-54
lines changed

Diff for: 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
}

Diff for: 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),

Diff for: 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

Diff for: compiler/rustc_pattern_analysis/src/rustc.rs

+24-33
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use smallvec::SmallVec;
21
use std::fmt;
32
use std::iter::once;
43

@@ -27,8 +26,7 @@ use crate::constructor::Constructor::*;
2726
pub type Constructor<'p, 'tcx> = crate::constructor::Constructor<RustcMatchCheckCtxt<'p, 'tcx>>;
2827
pub type ConstructorSet<'p, 'tcx> =
2928
crate::constructor::ConstructorSet<RustcMatchCheckCtxt<'p, 'tcx>>;
30-
pub type DeconstructedPat<'p, 'tcx> =
31-
crate::pat::DeconstructedPat<'p, RustcMatchCheckCtxt<'p, 'tcx>>;
29+
pub type DeconstructedPat<'p, 'tcx> = crate::pat::DeconstructedPat<RustcMatchCheckCtxt<'p, 'tcx>>;
3230
pub type MatchArm<'p, 'tcx> = crate::MatchArm<'p, RustcMatchCheckCtxt<'p, 'tcx>>;
3331
pub type Usefulness<'p, 'tcx> = crate::usefulness::Usefulness<'p, RustcMatchCheckCtxt<'p, 'tcx>>;
3432
pub type UsefulnessReport<'p, 'tcx> =
@@ -458,21 +456,20 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
458456
/// Note: the input patterns must have been lowered through
459457
/// `rustc_mir_build::thir::pattern::check_match::MatchVisitor::lower_pattern`.
460458
pub fn lower_pat(&self, pat: &'p Pat<'tcx>) -> DeconstructedPat<'p, 'tcx> {
461-
let singleton = |pat| std::slice::from_ref(self.pattern_arena.alloc(pat));
462459
let cx = self;
463460
let ty = cx.reveal_opaque_ty(pat.ty);
464461
let ctor;
465-
let fields: &[_];
462+
let mut fields: Vec<_>;
466463
match &pat.kind {
467464
PatKind::AscribeUserType { subpattern, .. }
468465
| PatKind::InlineConstant { subpattern, .. } => return self.lower_pat(subpattern),
469466
PatKind::Binding { subpattern: Some(subpat), .. } => return self.lower_pat(subpat),
470467
PatKind::Binding { subpattern: None, .. } | PatKind::Wild => {
471468
ctor = Wildcard;
472-
fields = &[];
469+
fields = vec![];
473470
}
474471
PatKind::Deref { subpattern } => {
475-
fields = singleton(self.lower_pat(subpattern));
472+
fields = vec![self.lower_pat(subpattern)];
476473
ctor = match ty.kind() {
477474
// This is a box pattern.
478475
ty::Adt(adt, ..) if adt.is_box() => Struct,
@@ -484,15 +481,14 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
484481
match ty.kind() {
485482
ty::Tuple(fs) => {
486483
ctor = Struct;
487-
let mut wilds: SmallVec<[_; 2]> = fs
484+
fields = fs
488485
.iter()
489486
.map(|ty| cx.reveal_opaque_ty(ty))
490487
.map(|ty| DeconstructedPat::wildcard(ty))
491488
.collect();
492489
for pat in subpatterns {
493-
wilds[pat.field.index()] = self.lower_pat(&pat.pattern);
490+
fields[pat.field.index()] = self.lower_pat(&pat.pattern);
494491
}
495-
fields = cx.pattern_arena.alloc_from_iter(wilds);
496492
}
497493
ty::Adt(adt, args) if adt.is_box() => {
498494
// The only legal patterns of type `Box` (outside `std`) are `_` and box
@@ -514,7 +510,7 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
514510
DeconstructedPat::wildcard(self.reveal_opaque_ty(args.type_at(0)))
515511
};
516512
ctor = Struct;
517-
fields = singleton(pat);
513+
fields = vec![pat];
518514
}
519515
ty::Adt(adt, _) => {
520516
ctor = match pat.kind {
@@ -534,14 +530,12 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
534530
ty
535531
},
536532
);
537-
let mut wilds: SmallVec<[_; 2]> =
538-
tys.map(|ty| DeconstructedPat::wildcard(ty)).collect();
533+
fields = tys.map(|ty| DeconstructedPat::wildcard(ty)).collect();
539534
for pat in subpatterns {
540535
if let Some(i) = field_id_to_id[pat.field.index()] {
541-
wilds[i] = self.lower_pat(&pat.pattern);
536+
fields[i] = self.lower_pat(&pat.pattern);
542537
}
543538
}
544-
fields = cx.pattern_arena.alloc_from_iter(wilds);
545539
}
546540
_ => bug!("pattern has unexpected type: pat: {:?}, ty: {:?}", pat, ty),
547541
}
@@ -553,7 +547,7 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
553547
Some(b) => Bool(b),
554548
None => Opaque(OpaqueId::new()),
555549
};
556-
fields = &[];
550+
fields = vec![];
557551
}
558552
ty::Char | ty::Int(_) | ty::Uint(_) => {
559553
ctor = match value.try_eval_bits(cx.tcx, cx.param_env) {
@@ -569,7 +563,7 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
569563
}
570564
None => Opaque(OpaqueId::new()),
571565
};
572-
fields = &[];
566+
fields = vec![];
573567
}
574568
ty::Float(ty::FloatTy::F32) => {
575569
ctor = match value.try_eval_bits(cx.tcx, cx.param_env) {
@@ -580,7 +574,7 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
580574
}
581575
None => Opaque(OpaqueId::new()),
582576
};
583-
fields = &[];
577+
fields = vec![];
584578
}
585579
ty::Float(ty::FloatTy::F64) => {
586580
ctor = match value.try_eval_bits(cx.tcx, cx.param_env) {
@@ -591,7 +585,7 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
591585
}
592586
None => Opaque(OpaqueId::new()),
593587
};
594-
fields = &[];
588+
fields = vec![];
595589
}
596590
ty::Ref(_, t, _) if t.is_str() => {
597591
// We want a `&str` constant to behave like a `Deref` pattern, to be compatible
@@ -602,16 +596,16 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
602596
// subfields.
603597
// Note: `t` is `str`, not `&str`.
604598
let ty = self.reveal_opaque_ty(*t);
605-
let subpattern = DeconstructedPat::new(Str(*value), &[], ty, pat);
599+
let subpattern = DeconstructedPat::new(Str(*value), Vec::new(), ty, pat);
606600
ctor = Ref;
607-
fields = singleton(subpattern)
601+
fields = vec![subpattern]
608602
}
609603
// All constants that can be structurally matched have already been expanded
610604
// into the corresponding `Pat`s by `const_to_pat`. Constants that remain are
611605
// opaque.
612606
_ => {
613607
ctor = Opaque(OpaqueId::new());
614-
fields = &[];
608+
fields = vec![];
615609
}
616610
}
617611
}
@@ -648,7 +642,7 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
648642
}
649643
_ => bug!("invalid type for range pattern: {}", ty.inner()),
650644
};
651-
fields = &[];
645+
fields = vec![];
652646
}
653647
PatKind::Array { prefix, slice, suffix } | PatKind::Slice { prefix, slice, suffix } => {
654648
let array_len = match ty.kind() {
@@ -664,25 +658,22 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
664658
SliceKind::FixedLen(prefix.len() + suffix.len())
665659
};
666660
ctor = Slice(Slice::new(array_len, kind));
667-
fields = cx.pattern_arena.alloc_from_iter(
668-
prefix.iter().chain(suffix.iter()).map(|p| self.lower_pat(&*p)),
669-
)
661+
fields = prefix.iter().chain(suffix.iter()).map(|p| self.lower_pat(&*p)).collect();
670662
}
671663
PatKind::Or { .. } => {
672664
ctor = Or;
673665
let pats = expand_or_pat(pat);
674-
fields =
675-
cx.pattern_arena.alloc_from_iter(pats.into_iter().map(|p| self.lower_pat(p)))
666+
fields = pats.into_iter().map(|p| self.lower_pat(p)).collect();
676667
}
677668
PatKind::Never => {
678669
// FIXME(never_patterns): handle `!` in exhaustiveness. This is a sane default
679670
// in the meantime.
680671
ctor = Wildcard;
681-
fields = &[];
672+
fields = vec![];
682673
}
683674
PatKind::Error(_) => {
684675
ctor = Opaque(OpaqueId::new());
685-
fields = &[];
676+
fields = vec![];
686677
}
687678
}
688679
DeconstructedPat::new(ctor, fields, ty, pat)
@@ -887,7 +878,7 @@ impl<'p, 'tcx> TypeCx for RustcMatchCheckCtxt<'p, 'tcx> {
887878

888879
fn write_variant_name(
889880
f: &mut fmt::Formatter<'_>,
890-
pat: &crate::pat::DeconstructedPat<'_, Self>,
881+
pat: &crate::pat::DeconstructedPat<Self>,
891882
) -> fmt::Result {
892883
if let ty::Adt(adt, _) = pat.ty().kind() {
893884
if adt.is_box() {
@@ -906,9 +897,9 @@ impl<'p, 'tcx> TypeCx for RustcMatchCheckCtxt<'p, 'tcx> {
906897

907898
fn lint_overlapping_range_endpoints(
908899
&self,
909-
pat: &crate::pat::DeconstructedPat<'_, Self>,
900+
pat: &crate::pat::DeconstructedPat<Self>,
910901
overlaps_on: IntRange,
911-
overlaps_with: &[&crate::pat::DeconstructedPat<'_, Self>],
902+
overlaps_with: &[&crate::pat::DeconstructedPat<Self>],
912903
) {
913904
let overlap_as_pat = self.hoist_pat_range(&overlaps_on, *pat.ty());
914905
let overlaps: Vec<_> = overlaps_with

Diff for: compiler/rustc_pattern_analysis/src/usefulness.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ impl<'p, Cx: TypeCx> Clone for PatStack<'p, Cx> {
848848
}
849849

850850
impl<'p, Cx: TypeCx> PatStack<'p, Cx> {
851-
fn from_pattern(pat: &'p DeconstructedPat<'p, Cx>) -> Self {
851+
fn from_pattern(pat: &'p DeconstructedPat<Cx>) -> Self {
852852
PatStack { pats: smallvec![PatOrWild::Pat(pat)], relevant: true }
853853
}
854854

@@ -1585,7 +1585,7 @@ pub enum Usefulness<'p, Cx: TypeCx> {
15851585
/// The arm is useful. This additionally carries a set of or-pattern branches that have been
15861586
/// found to be redundant despite the overall arm being useful. Used only in the presence of
15871587
/// or-patterns, otherwise it stays empty.
1588-
Useful(Vec<&'p DeconstructedPat<'p, Cx>>),
1588+
Useful(Vec<&'p DeconstructedPat<Cx>>),
15891589
/// The arm is redundant and can be removed without changing the behavior of the match
15901590
/// expression.
15911591
Redundant,

0 commit comments

Comments
 (0)