Skip to content

Commit 0201b00

Browse files
committed
!! (WIP) store THIR patterns in Arc instead of Box
1 parent 2d4bf4a commit 0201b00

File tree

11 files changed

+148
-134
lines changed

11 files changed

+148
-134
lines changed

compiler/rustc_middle/src/thir.rs

+19-18
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use std::cmp::Ordering;
1212
use std::fmt;
1313
use std::ops::Index;
14+
use std::sync::Arc;
1415

1516
use rustc_abi::{FieldIdx, Integer, Size, VariantIdx};
1617
use rustc_ast::{AsmMacro, InlineAsmOptions, InlineAsmTemplatePiece};
@@ -108,7 +109,7 @@ pub enum BodyTy<'tcx> {
108109
#[derive(Clone, Debug, HashStable)]
109110
pub struct Param<'tcx> {
110111
/// The pattern that appears in the parameter list, or None for implicit parameters.
111-
pub pat: Option<Box<Pat<'tcx>>>,
112+
pub pat: Option<Arc<Pat<'tcx>>>,
112113
/// The possibly inferred type.
113114
pub ty: Ty<'tcx>,
114115
/// Span of the explicitly provided type, or None if inferred for closures.
@@ -231,7 +232,7 @@ pub enum StmtKind<'tcx> {
231232
/// `let <PAT> = ...`
232233
///
233234
/// If a type annotation is included, it is added as an ascription pattern.
234-
pattern: Box<Pat<'tcx>>,
235+
pattern: Arc<Pat<'tcx>>,
235236

236237
/// `let pat: ty = <INIT>`
237238
initializer: Option<ExprId>,
@@ -379,7 +380,7 @@ pub enum ExprKind<'tcx> {
379380
/// (Not to be confused with [`StmtKind::Let`], which is a normal `let` statement.)
380381
Let {
381382
expr: ExprId,
382-
pat: Box<Pat<'tcx>>,
383+
pat: Arc<Pat<'tcx>>,
383384
},
384385
/// A `match` expression.
385386
Match {
@@ -571,7 +572,7 @@ pub struct FruInfo<'tcx> {
571572
/// A `match` arm.
572573
#[derive(Clone, Debug, HashStable)]
573574
pub struct Arm<'tcx> {
574-
pub pattern: Box<Pat<'tcx>>,
575+
pub pattern: Arc<Pat<'tcx>>,
575576
pub guard: Option<ExprId>,
576577
pub body: ExprId,
577578
pub lint_level: LintLevel,
@@ -628,7 +629,7 @@ pub enum InlineAsmOperand<'tcx> {
628629
#[derive(Clone, Debug, HashStable, TypeVisitable)]
629630
pub struct FieldPat<'tcx> {
630631
pub field: FieldIdx,
631-
pub pattern: Box<Pat<'tcx>>,
632+
pub pattern: Arc<Pat<'tcx>>,
632633
}
633634

634635
#[derive(Clone, Debug, HashStable, TypeVisitable)]
@@ -778,7 +779,7 @@ pub enum PatKind<'tcx> {
778779

779780
AscribeUserType {
780781
ascription: Ascription<'tcx>,
781-
subpattern: Box<Pat<'tcx>>,
782+
subpattern: Arc<Pat<'tcx>>,
782783
},
783784

784785
/// `x`, `ref x`, `x @ P`, etc.
@@ -789,7 +790,7 @@ pub enum PatKind<'tcx> {
789790
#[type_visitable(ignore)]
790791
var: LocalVarId,
791792
ty: Ty<'tcx>,
792-
subpattern: Option<Box<Pat<'tcx>>>,
793+
subpattern: Option<Arc<Pat<'tcx>>>,
793794
/// Is this the leftmost occurrence of the binding, i.e., is `var` the
794795
/// `HirId` of this pattern?
795796
is_primary: bool,
@@ -812,12 +813,12 @@ pub enum PatKind<'tcx> {
812813

813814
/// `box P`, `&P`, `&mut P`, etc.
814815
Deref {
815-
subpattern: Box<Pat<'tcx>>,
816+
subpattern: Arc<Pat<'tcx>>,
816817
},
817818

818819
/// Deref pattern, written `box P` for now.
819820
DerefPattern {
820-
subpattern: Box<Pat<'tcx>>,
821+
subpattern: Arc<Pat<'tcx>>,
821822
mutability: hir::Mutability,
822823
},
823824

@@ -851,31 +852,31 @@ pub enum PatKind<'tcx> {
851852
/// Otherwise, the actual pattern that the constant lowered to. As with
852853
/// other constants, inline constants are matched structurally where
853854
/// possible.
854-
subpattern: Box<Pat<'tcx>>,
855+
subpattern: Arc<Pat<'tcx>>,
855856
},
856857

857-
Range(Box<PatRange<'tcx>>),
858+
Range(Arc<PatRange<'tcx>>),
858859

859860
/// Matches against a slice, checking the length and extracting elements.
860861
/// irrefutable when there is a slice pattern and both `prefix` and `suffix` are empty.
861862
/// e.g., `&[ref xs @ ..]`.
862863
Slice {
863-
prefix: Box<[Box<Pat<'tcx>>]>,
864-
slice: Option<Box<Pat<'tcx>>>,
865-
suffix: Box<[Box<Pat<'tcx>>]>,
864+
prefix: Box<[Arc<Pat<'tcx>>]>,
865+
slice: Option<Arc<Pat<'tcx>>>,
866+
suffix: Box<[Arc<Pat<'tcx>>]>,
866867
},
867868

868869
/// Fixed match against an array; irrefutable.
869870
Array {
870-
prefix: Box<[Box<Pat<'tcx>>]>,
871-
slice: Option<Box<Pat<'tcx>>>,
872-
suffix: Box<[Box<Pat<'tcx>>]>,
871+
prefix: Box<[Arc<Pat<'tcx>>]>,
872+
slice: Option<Arc<Pat<'tcx>>>,
873+
suffix: Box<[Arc<Pat<'tcx>>]>,
873874
},
874875

875876
/// An or-pattern, e.g. `p | q`.
876877
/// Invariant: `pats.len() >= 2`.
877878
Or {
878-
pats: Box<[Box<Pat<'tcx>>]>,
879+
pats: Box<[Arc<Pat<'tcx>>]>,
879880
},
880881

881882
/// A never pattern `!`.

compiler/rustc_mir_build/src/builder/matches/match_pair.rs

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::Arc;
2+
13
use rustc_middle::mir::*;
24
use rustc_middle::thir::{self, *};
35
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
@@ -12,11 +14,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1214
/// [`PatKind::Leaf`].
1315
///
1416
/// Used internally by [`MatchPairTree::for_pattern`].
15-
fn field_match_pairs<'pat>(
17+
fn field_match_pairs(
1618
&mut self,
1719
place: PlaceBuilder<'tcx>,
18-
subpatterns: &'pat [FieldPat<'tcx>],
19-
) -> Vec<MatchPairTree<'pat, 'tcx>> {
20+
subpatterns: &[FieldPat<'tcx>],
21+
) -> Vec<MatchPairTree<'tcx>> {
2022
subpatterns
2123
.iter()
2224
.map(|fieldpat| {
@@ -31,13 +33,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
3133
/// array pattern or slice pattern, and adds those trees to `match_pairs`.
3234
///
3335
/// Used internally by [`MatchPairTree::for_pattern`].
34-
fn prefix_slice_suffix<'pat>(
36+
fn prefix_slice_suffix(
3537
&mut self,
36-
match_pairs: &mut Vec<MatchPairTree<'pat, 'tcx>>,
38+
match_pairs: &mut Vec<MatchPairTree<'tcx>>,
3739
place: &PlaceBuilder<'tcx>,
38-
prefix: &'pat [Box<Pat<'tcx>>],
39-
opt_slice: &'pat Option<Box<Pat<'tcx>>>,
40-
suffix: &'pat [Box<Pat<'tcx>>],
40+
prefix: &[Arc<Pat<'tcx>>],
41+
opt_slice: &Option<Arc<Pat<'tcx>>>,
42+
suffix: &[Arc<Pat<'tcx>>],
4143
) {
4244
let tcx = self.tcx;
4345
let (min_length, exact_size) = if let Some(place_resolved) = place.try_to_place(self) {
@@ -83,14 +85,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
8385
}
8486
}
8587

86-
impl<'pat, 'tcx> MatchPairTree<'pat, 'tcx> {
88+
impl<'tcx> MatchPairTree<'tcx> {
8789
/// Recursively builds a match pair tree for the given pattern and its
8890
/// subpatterns.
8991
pub(in crate::builder) fn for_pattern(
9092
mut place_builder: PlaceBuilder<'tcx>,
91-
pattern: &'pat Pat<'tcx>,
93+
pattern: &Arc<Pat<'tcx>>,
9294
cx: &mut Builder<'_, 'tcx>,
93-
) -> MatchPairTree<'pat, 'tcx> {
95+
) -> MatchPairTree<'tcx> {
96+
let pattern = Arc::clone(pattern);
97+
9498
// Force the place type to the pattern's type.
9599
// FIXME(oli-obk): can we use this to simplify slice/array pattern hacks?
96100
if let Some(resolved) = place_builder.resolve_upvar(cx) {
@@ -125,7 +129,7 @@ impl<'pat, 'tcx> MatchPairTree<'pat, 'tcx> {
125129
if range.is_full_range(cx.tcx) == Some(true) {
126130
default_irrefutable()
127131
} else {
128-
TestCase::Range(range)
132+
TestCase::Range(Arc::clone(range))
129133
}
130134
}
131135

0 commit comments

Comments
 (0)