Skip to content

Commit 2c4c8eb

Browse files
committed
Box PatKind::Range.
Because it's the biggest variant. Also, make `PatRange` non-`Copy`, because it's 104 bytes, which is pretty big.
1 parent bd1e683 commit 2c4c8eb

File tree

7 files changed

+34
-26
lines changed

7 files changed

+34
-26
lines changed

compiler/rustc_middle/src/thir.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ pub enum PatKind<'tcx> {
628628
value: mir::ConstantKind<'tcx>,
629629
},
630630

631-
Range(PatRange<'tcx>),
631+
Range(Box<PatRange<'tcx>>),
632632

633633
/// Matches against a slice, checking the length and extracting elements.
634634
/// irrefutable when there is a slice pattern and both `prefix` and `suffix` are empty.
@@ -653,7 +653,7 @@ pub enum PatKind<'tcx> {
653653
},
654654
}
655655

656-
#[derive(Copy, Clone, Debug, PartialEq, HashStable)]
656+
#[derive(Clone, Debug, PartialEq, HashStable)]
657657
pub struct PatRange<'tcx> {
658658
pub lo: mir::ConstantKind<'tcx>,
659659
pub hi: mir::ConstantKind<'tcx>,
@@ -767,7 +767,7 @@ impl<'tcx> fmt::Display for Pat<'tcx> {
767767
write!(f, "{}", subpattern)
768768
}
769769
PatKind::Constant { value } => write!(f, "{}", value),
770-
PatKind::Range(PatRange { lo, hi, end }) => {
770+
PatKind::Range(box PatRange { lo, hi, end }) => {
771771
write!(f, "{}", lo)?;
772772
write!(f, "{}", end)?;
773773
write!(f, "{}", hi)
@@ -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<'_>, 128);
813-
static_assert_size!(PatKind<'_>, 112);
812+
static_assert_size!(Pat<'_>, 112);
813+
static_assert_size!(PatKind<'_>, 96);
814814
static_assert_size!(Stmt<'_>, 56);
815815
static_assert_size!(StmtKind<'_>, 48);
816816
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ enum TestKind<'tcx> {
979979
},
980980

981981
/// Test whether the value falls within an inclusive or exclusive range
982-
Range(PatRange<'tcx>),
982+
Range(Box<PatRange<'tcx>>),
983983

984984
/// Test that the length of the slice is equal to `len`.
985985
Len { len: u64, op: BinOp },

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
208208
Err(match_pair)
209209
}
210210

211-
PatKind::Range(PatRange { lo, hi, end }) => {
211+
PatKind::Range(box PatRange { lo, hi, end }) => {
212212
let (range, bias) = match *lo.ty().kind() {
213213
ty::Char => {
214214
(Some(('\u{0000}' as u128, '\u{10FFFF}' as u128, Size::from_bits(32))), 0)

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
5858
kind: TestKind::Eq { value, ty: match_pair.pattern.ty },
5959
},
6060

61-
PatKind::Range(range) => {
61+
PatKind::Range(ref range) => {
6262
assert_eq!(range.lo.ty(), match_pair.pattern.ty);
6363
assert_eq!(range.hi.ty(), match_pair.pattern.ty);
64-
Test { span: match_pair.pattern.span, kind: TestKind::Range(range) }
64+
Test { span: match_pair.pattern.span, kind: TestKind::Range(range.clone()) }
6565
}
6666

6767
PatKind::Slice { ref prefix, ref slice, ref suffix } => {
@@ -102,9 +102,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
102102
PatKind::Variant { .. } => {
103103
panic!("you should have called add_variants_to_switch instead!");
104104
}
105-
PatKind::Range(range) => {
105+
PatKind::Range(ref range) => {
106106
// Check that none of the switch values are in the range.
107-
self.values_not_contained_in_range(range, options).unwrap_or(false)
107+
self.values_not_contained_in_range(&*range, options).unwrap_or(false)
108108
}
109109
PatKind::Slice { .. }
110110
| PatKind::Array { .. }
@@ -272,7 +272,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
272272
}
273273
}
274274

275-
TestKind::Range(PatRange { lo, hi, ref end }) => {
275+
TestKind::Range(box PatRange { lo, hi, ref end }) => {
276276
let lower_bound_success = self.cfg.start_new_block();
277277
let target_blocks = make_target_blocks(self);
278278

@@ -540,9 +540,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
540540
Some(index)
541541
}
542542

543-
(&TestKind::SwitchInt { switch_ty: _, ref options }, &PatKind::Range(range)) => {
543+
(&TestKind::SwitchInt { switch_ty: _, ref options }, &PatKind::Range(ref range)) => {
544544
let not_contained =
545-
self.values_not_contained_in_range(range, options).unwrap_or(false);
545+
self.values_not_contained_in_range(&*range, options).unwrap_or(false);
546546

547547
if not_contained {
548548
// No switch values are contained in the pattern range,
@@ -631,7 +631,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
631631
}
632632
}
633633

634-
(&TestKind::Range(test), &PatKind::Range(pat)) => {
634+
(&TestKind::Range(ref test), &PatKind::Range(ref pat)) => {
635635
use std::cmp::Ordering::*;
636636

637637
if test == pat {
@@ -658,8 +658,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
658658
no_overlap
659659
}
660660

661-
(&TestKind::Range(range), &PatKind::Constant { value }) => {
662-
if let Some(false) = self.const_range_contains(range, value) {
661+
(&TestKind::Range(ref range), &PatKind::Constant { value }) => {
662+
if let Some(false) = self.const_range_contains(&*range, value) {
663663
// `value` is not contained in the testing range,
664664
// so `value` can be matched only if this test fails.
665665
Some(1)
@@ -754,7 +754,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
754754

755755
fn const_range_contains(
756756
&self,
757-
range: PatRange<'tcx>,
757+
range: &PatRange<'tcx>,
758758
value: ConstantKind<'tcx>,
759759
) -> Option<bool> {
760760
use std::cmp::Ordering::*;
@@ -772,7 +772,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
772772

773773
fn values_not_contained_in_range(
774774
&self,
775-
range: PatRange<'tcx>,
775+
range: &PatRange<'tcx>,
776776
options: &FxIndexMap<ConstantKind<'tcx>, u128>,
777777
) -> Option<bool> {
778778
for &val in options.keys() {

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,11 @@ impl IntRange {
252252
let kind = if lo == hi {
253253
PatKind::Constant { value: lo_const }
254254
} else {
255-
PatKind::Range(PatRange { lo: lo_const, hi: hi_const, end: RangeEnd::Included })
255+
PatKind::Range(Box::new(PatRange {
256+
lo: lo_const,
257+
hi: hi_const,
258+
end: RangeEnd::Included,
259+
}))
256260
};
257261

258262
Pat { ty, span: DUMMY_SP, kind }
@@ -1402,7 +1406,7 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> {
14021406
}
14031407
}
14041408
}
1405-
&PatKind::Range(PatRange { lo, hi, end }) => {
1409+
&PatKind::Range(box PatRange { lo, hi, end }) => {
14061410
let ty = lo.ty();
14071411
ctor = if let Some(int_range) = IntRange::from_range(
14081412
cx.tcx,
@@ -1511,7 +1515,7 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> {
15111515
}
15121516
}
15131517
&Str(value) => PatKind::Constant { value },
1514-
&FloatRange(lo, hi, end) => PatKind::Range(PatRange { lo, hi, end }),
1518+
&FloatRange(lo, hi, end) => PatKind::Range(Box::new(PatRange { lo, hi, end })),
15151519
IntRange(range) => return range.to_pat(cx.tcx, self.ty),
15161520
Wildcard | NonExhaustive => PatKind::Wild,
15171521
Missing { .. } => bug!(

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
134134
match (end, cmp) {
135135
// `x..y` where `x < y`.
136136
// Non-empty because the range includes at least `x`.
137-
(RangeEnd::Excluded, Some(Ordering::Less)) => PatKind::Range(PatRange { lo, hi, end }),
137+
(RangeEnd::Excluded, Some(Ordering::Less)) => {
138+
PatKind::Range(Box::new(PatRange { lo, hi, end }))
139+
}
138140
// `x..y` where `x >= y`. The range is empty => error.
139141
(RangeEnd::Excluded, _) => {
140142
struct_span_err!(
@@ -149,7 +151,9 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
149151
// `x..=y` where `x == y`.
150152
(RangeEnd::Included, Some(Ordering::Equal)) => PatKind::Constant { value: lo },
151153
// `x..=y` where `x < y`.
152-
(RangeEnd::Included, Some(Ordering::Less)) => PatKind::Range(PatRange { lo, hi, end }),
154+
(RangeEnd::Included, Some(Ordering::Less)) => {
155+
PatKind::Range(Box::new(PatRange { lo, hi, end }))
156+
}
153157
// `x..=y` where `x > y` hence the range is empty => error.
154158
(RangeEnd::Included, _) => {
155159
let mut err = struct_span_err!(
@@ -735,7 +739,7 @@ impl<'tcx> PatternFoldable<'tcx> for PatKind<'tcx> {
735739
PatKind::Deref { subpattern: subpattern.fold_with(folder) }
736740
}
737741
PatKind::Constant { value } => PatKind::Constant { value },
738-
PatKind::Range(range) => PatKind::Range(range),
742+
PatKind::Range(ref range) => PatKind::Range(range.clone()),
739743
PatKind::Slice { ref prefix, ref slice, ref suffix } => PatKind::Slice {
740744
prefix: prefix.fold_with(folder),
741745
slice: slice.fold_with(folder),

compiler/rustc_ty_utils/src/consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
157157

158158
match pat.kind {
159159
thir::PatKind::Constant { value } => value.has_param_types_or_consts(),
160-
thir::PatKind::Range(thir::PatRange { lo, hi, .. }) => {
160+
thir::PatKind::Range(box thir::PatRange { lo, hi, .. }) => {
161161
lo.has_param_types_or_consts() || hi.has_param_types_or_consts()
162162
}
163163
_ => false,

0 commit comments

Comments
 (0)