Skip to content

Commit 43a0268

Browse files
committed
Use boxed slices in PatKind.
To shrink it a little more.
1 parent a40124e commit 43a0268

File tree

6 files changed

+36
-25
lines changed

6 files changed

+36
-25
lines changed

compiler/rustc_middle/src/thir.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -634,22 +634,22 @@ pub enum PatKind<'tcx> {
634634
/// irrefutable when there is a slice pattern and both `prefix` and `suffix` are empty.
635635
/// e.g., `&[ref xs @ ..]`.
636636
Slice {
637-
prefix: Vec<Box<Pat<'tcx>>>,
637+
prefix: Box<[Box<Pat<'tcx>>]>,
638638
slice: Option<Box<Pat<'tcx>>>,
639-
suffix: Vec<Box<Pat<'tcx>>>,
639+
suffix: Box<[Box<Pat<'tcx>>]>,
640640
},
641641

642642
/// Fixed match against an array; irrefutable.
643643
Array {
644-
prefix: Vec<Box<Pat<'tcx>>>,
644+
prefix: Box<[Box<Pat<'tcx>>]>,
645645
slice: Option<Box<Pat<'tcx>>>,
646-
suffix: Vec<Box<Pat<'tcx>>>,
646+
suffix: Box<[Box<Pat<'tcx>>]>,
647647
},
648648

649649
/// An or-pattern, e.g. `p | q`.
650650
/// Invariant: `pats.len() >= 2`.
651651
Or {
652-
pats: Vec<Box<Pat<'tcx>>>,
652+
pats: Box<[Box<Pat<'tcx>>]>,
653653
},
654654
}
655655

@@ -775,7 +775,7 @@ impl<'tcx> fmt::Display for Pat<'tcx> {
775775
PatKind::Slice { ref prefix, ref slice, ref suffix }
776776
| PatKind::Array { ref prefix, ref slice, ref suffix } => {
777777
write!(f, "[")?;
778-
for p in prefix {
778+
for p in prefix.iter() {
779779
write!(f, "{}{}", start_or_comma(), p)?;
780780
}
781781
if let Some(ref slice) = *slice {
@@ -786,13 +786,13 @@ impl<'tcx> fmt::Display for Pat<'tcx> {
786786
}
787787
write!(f, "..")?;
788788
}
789-
for p in suffix {
789+
for p in suffix.iter() {
790790
write!(f, "{}{}", start_or_comma(), p)?;
791791
}
792792
write!(f, "]")
793793
}
794794
PatKind::Or { ref pats } => {
795-
for pat in pats {
795+
for pat in pats.iter() {
796796
write!(f, "{}{}", start_or_continue(" | "), pat)?;
797797
}
798798
Ok(())
@@ -809,8 +809,8 @@ mod size_asserts {
809809
static_assert_size!(Block, 56);
810810
static_assert_size!(Expr<'_>, 64);
811811
static_assert_size!(ExprKind<'_>, 40);
812-
static_assert_size!(Pat<'_>, 80);
813-
static_assert_size!(PatKind<'_>, 64);
812+
static_assert_size!(Pat<'_>, 72);
813+
static_assert_size!(PatKind<'_>, 56);
814814
static_assert_size!(Stmt<'_>, 56);
815815
static_assert_size!(StmtKind<'_>, 48);
816816
}

compiler/rustc_middle/src/thir/visit.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -232,18 +232,18 @@ pub fn walk_pat<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, pat: &Pat<'
232232
Constant { value: _ } => {}
233233
Range(_) => {}
234234
Slice { prefix, slice, suffix } | Array { prefix, slice, suffix } => {
235-
for subpattern in prefix {
235+
for subpattern in prefix.iter() {
236236
visitor.visit_pat(&subpattern);
237237
}
238238
if let Some(pat) = slice {
239239
visitor.visit_pat(&pat);
240240
}
241-
for subpattern in suffix {
241+
for subpattern in suffix.iter() {
242242
visitor.visit_pat(&subpattern);
243243
}
244244
}
245245
Or { pats } => {
246-
for pat in pats {
246+
for pat in pats.iter() {
247247
visitor.visit_pat(&pat);
248248
}
249249
}

compiler/rustc_mir_build/src/build/matches/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
764764
| PatKind::Slice { ref prefix, ref slice, ref suffix } => {
765765
let from = u64::try_from(prefix.len()).unwrap();
766766
let to = u64::try_from(suffix.len()).unwrap();
767-
for subpattern in prefix {
767+
for subpattern in prefix.iter() {
768768
self.visit_primary_bindings(subpattern, pattern_user_ty.clone().index(), f);
769769
}
770770
for subpattern in slice {
@@ -774,7 +774,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
774774
f,
775775
);
776776
}
777-
for subpattern in suffix {
777+
for subpattern in suffix.iter() {
778778
self.visit_primary_bindings(subpattern, pattern_user_ty.clone().index(), f);
779779
}
780780
}
@@ -827,7 +827,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
827827
// may not all be in the leftmost subpattern. For example in
828828
// `let (x | y) = ...`, the primary binding of `y` occurs in
829829
// the right subpattern
830-
for subpattern in pats {
830+
for subpattern in pats.iter() {
831831
self.visit_primary_bindings(subpattern, pattern_user_ty.clone(), f);
832832
}
833833
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
400400
.map(|val| self.recur(*val, false))
401401
.collect::<Result<_, _>>()?,
402402
slice: None,
403-
suffix: Vec::new(),
403+
suffix: Box::new([]),
404404
},
405405
ty::Ref(_, pointee_ty, ..) => match *pointee_ty.kind() {
406406
// These are not allowed and will error elsewhere anyway.
@@ -436,7 +436,7 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
436436
.map(|val| self.recur(*val, false))
437437
.collect::<Result<_, _>>()?,
438438
slice: None,
439-
suffix: vec![],
439+
suffix: Box::new([]),
440440
},
441441
span,
442442
ty: *pointee_ty,
@@ -462,7 +462,7 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
462462
.map(|val| self.recur(*val, false))
463463
.collect::<Result<_, _>>()?,
464464
slice: None,
465-
suffix: vec![],
465+
suffix: Box::new([]),
466466
},
467467
span,
468468
ty: tcx.mk_slice(elem_ty),

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

+10-5
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ use std::ops::RangeInclusive;
7272
fn expand_or_pat<'p, 'tcx>(pat: &'p Pat<'tcx>) -> Vec<&'p Pat<'tcx>> {
7373
fn expand<'p, 'tcx>(pat: &'p Pat<'tcx>, vec: &mut Vec<&'p Pat<'tcx>>) {
7474
if let PatKind::Or { pats } = &pat.kind {
75-
for pat in pats {
75+
for pat in pats.iter() {
7676
expand(&pat, vec);
7777
}
7878
} else {
@@ -1433,7 +1433,8 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> {
14331433
FixedLen(prefix.len() + suffix.len())
14341434
};
14351435
ctor = Slice(Slice::new(array_len, kind));
1436-
fields = Fields::from_iter(cx, prefix.iter().chain(suffix).map(|p| mkpat(&*p)));
1436+
fields =
1437+
Fields::from_iter(cx, prefix.iter().chain(suffix.iter()).map(|p| mkpat(&*p)));
14371438
}
14381439
PatKind::Or { .. } => {
14391440
ctor = Or;
@@ -1489,7 +1490,7 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> {
14891490
FixedLen(_) => PatKind::Slice {
14901491
prefix: subpatterns.collect(),
14911492
slice: None,
1492-
suffix: vec![],
1493+
suffix: Box::new([]),
14931494
},
14941495
VarLen(prefix, _) => {
14951496
let mut subpatterns = subpatterns.peekable();
@@ -1508,9 +1509,13 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> {
15081509
subpatterns.next();
15091510
}
15101511
}
1511-
let suffix: Vec<_> = subpatterns.collect();
1512+
let suffix: Box<[_]> = subpatterns.collect();
15121513
let wild = Pat::wildcard_from_ty(self.ty);
1513-
PatKind::Slice { prefix, slice: Some(Box::new(wild)), suffix }
1514+
PatKind::Slice {
1515+
prefix: prefix.into_boxed_slice(),
1516+
slice: Some(Box::new(wild)),
1517+
suffix,
1518+
}
15141519
}
15151520
}
15161521
}

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
344344
.collect()
345345
}
346346

347-
fn lower_patterns(&mut self, pats: &'tcx [hir::Pat<'tcx>]) -> Vec<Box<Pat<'tcx>>> {
347+
fn lower_patterns(&mut self, pats: &'tcx [hir::Pat<'tcx>]) -> Box<[Box<Pat<'tcx>>]> {
348348
pats.iter().map(|p| self.lower_pattern(p)).collect()
349349
}
350350

@@ -653,6 +653,12 @@ impl<'tcx, T: PatternFoldable<'tcx>> PatternFoldable<'tcx> for Vec<T> {
653653
}
654654
}
655655

656+
impl<'tcx, T: PatternFoldable<'tcx>> PatternFoldable<'tcx> for Box<[T]> {
657+
fn super_fold_with<F: PatternFolder<'tcx>>(&self, folder: &mut F) -> Self {
658+
self.iter().map(|t| t.fold_with(folder)).collect()
659+
}
660+
}
661+
656662
impl<'tcx, T: PatternFoldable<'tcx>> PatternFoldable<'tcx> for Option<T> {
657663
fn super_fold_with<F: PatternFolder<'tcx>>(&self, folder: &mut F) -> Self {
658664
self.as_ref().map(|t| t.fold_with(folder))

0 commit comments

Comments
 (0)