Skip to content

Commit 053874e

Browse files
committed
Clean up THIR patterns.
`thir::Pat::kind` is a `Box<PatKind>`, which doesn't follow the usual pattern in AST/HIR/THIR which is that the "kind" enum for a node is stored inline within the parent struct. This commit makes the `PatKind` directly inline within the `Pat`. This requires using `Box<Pat>` in all the types that hold a `Pat. Ideally, `Pat` would be stored in `Thir` like `Expr` and `Stmt` and referred to with a `PatId` rather than `Box<Pat>`. But this is hard to do because lots of `Pat`s get created after the destruction of the `Cx` that does normal THIR building. But this does get us a step closer to `PatId`, because all the `Box<Pat>` occurrences would be replaced with `PatId` if `PatId` ever happened. At 128 bytes, `Pat` is large. Subsequent commits will shrink it.
1 parent 9af618b commit 053874e

File tree

15 files changed

+123
-121
lines changed

15 files changed

+123
-121
lines changed

compiler/rustc_middle/src/thir.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ pub enum StmtKind<'tcx> {
180180
/// `let <PAT> = ...`
181181
///
182182
/// If a type annotation is included, it is added as an ascription pattern.
183-
pattern: Pat<'tcx>,
183+
pattern: Box<Pat<'tcx>>,
184184

185185
/// `let pat: ty = <INIT>`
186186
initializer: Option<ExprId>,
@@ -301,7 +301,7 @@ pub enum ExprKind<'tcx> {
301301
},
302302
Let {
303303
expr: ExprId,
304-
pat: Pat<'tcx>,
304+
pat: Box<Pat<'tcx>>,
305305
},
306306
/// A `match` expression.
307307
Match {
@@ -467,7 +467,7 @@ pub struct FruInfo<'tcx> {
467467
/// A `match` arm.
468468
#[derive(Clone, Debug, HashStable)]
469469
pub struct Arm<'tcx> {
470-
pub pattern: Pat<'tcx>,
470+
pub pattern: Box<Pat<'tcx>>,
471471
pub guard: Option<Guard<'tcx>>,
472472
pub body: ExprId,
473473
pub lint_level: LintLevel,
@@ -479,7 +479,7 @@ pub struct Arm<'tcx> {
479479
#[derive(Clone, Debug, HashStable)]
480480
pub enum Guard<'tcx> {
481481
If(ExprId),
482-
IfLet(Pat<'tcx>, ExprId),
482+
IfLet(Box<Pat<'tcx>>, ExprId),
483483
}
484484

485485
#[derive(Copy, Clone, Debug, HashStable)]
@@ -534,19 +534,19 @@ pub enum BindingMode {
534534
#[derive(Clone, Debug, HashStable)]
535535
pub struct FieldPat<'tcx> {
536536
pub field: Field,
537-
pub pattern: Pat<'tcx>,
537+
pub pattern: Box<Pat<'tcx>>,
538538
}
539539

540540
#[derive(Clone, Debug, HashStable)]
541541
pub struct Pat<'tcx> {
542542
pub ty: Ty<'tcx>,
543543
pub span: Span,
544-
pub kind: Box<PatKind<'tcx>>,
544+
pub kind: PatKind<'tcx>,
545545
}
546546

547547
impl<'tcx> Pat<'tcx> {
548548
pub fn wildcard_from_ty(ty: Ty<'tcx>) -> Self {
549-
Pat { ty, span: DUMMY_SP, kind: Box::new(PatKind::Wild) }
549+
Pat { ty, span: DUMMY_SP, kind: PatKind::Wild }
550550
}
551551
}
552552

@@ -581,7 +581,7 @@ pub enum PatKind<'tcx> {
581581

582582
AscribeUserType {
583583
ascription: Ascription<'tcx>,
584-
subpattern: Pat<'tcx>,
584+
subpattern: Box<Pat<'tcx>>,
585585
},
586586

587587
/// `x`, `ref x`, `x @ P`, etc.
@@ -591,7 +591,7 @@ pub enum PatKind<'tcx> {
591591
mode: BindingMode,
592592
var: LocalVarId,
593593
ty: Ty<'tcx>,
594-
subpattern: Option<Pat<'tcx>>,
594+
subpattern: Option<Box<Pat<'tcx>>>,
595595
/// Is this the leftmost occurrence of the binding, i.e., is `var` the
596596
/// `HirId` of this pattern?
597597
is_primary: bool,
@@ -614,7 +614,7 @@ pub enum PatKind<'tcx> {
614614

615615
/// `box P`, `&P`, `&mut P`, etc.
616616
Deref {
617-
subpattern: Pat<'tcx>,
617+
subpattern: Box<Pat<'tcx>>,
618618
},
619619

620620
/// One of the following:
@@ -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<Pat<'tcx>>,
638-
slice: Option<Pat<'tcx>>,
639-
suffix: Vec<Pat<'tcx>>,
637+
prefix: Vec<Box<Pat<'tcx>>>,
638+
slice: Option<Box<Pat<'tcx>>>,
639+
suffix: Vec<Box<Pat<'tcx>>>,
640640
},
641641

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

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

@@ -674,7 +674,7 @@ impl<'tcx> fmt::Display for Pat<'tcx> {
674674
};
675675
let mut start_or_comma = || start_or_continue(", ");
676676

677-
match *self.kind {
677+
match self.kind {
678678
PatKind::Wild => write!(f, "_"),
679679
PatKind::AscribeUserType { ref subpattern, .. } => write!(f, "{}: _", subpattern),
680680
PatKind::Binding { mutability, name, mode, ref subpattern, .. } => {
@@ -695,7 +695,7 @@ impl<'tcx> fmt::Display for Pat<'tcx> {
695695
Ok(())
696696
}
697697
PatKind::Variant { ref subpatterns, .. } | PatKind::Leaf { ref subpatterns } => {
698-
let variant = match *self.kind {
698+
let variant = match self.kind {
699699
PatKind::Variant { adt_def, variant_index, .. } => {
700700
Some(adt_def.variant(variant_index))
701701
}
@@ -714,7 +714,7 @@ impl<'tcx> fmt::Display for Pat<'tcx> {
714714

715715
let mut printed = 0;
716716
for p in subpatterns {
717-
if let PatKind::Wild = *p.pattern.kind {
717+
if let PatKind::Wild = p.pattern.kind {
718718
continue;
719719
}
720720
let name = variant.fields[p.field.index()].name;
@@ -780,7 +780,7 @@ impl<'tcx> fmt::Display for Pat<'tcx> {
780780
}
781781
if let Some(ref slice) = *slice {
782782
write!(f, "{}", start_or_comma())?;
783-
match *slice.kind {
783+
match slice.kind {
784784
PatKind::Wild => {}
785785
_ => write!(f, "{}", slice)?,
786786
}
@@ -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<'_>, 24);
812+
static_assert_size!(Pat<'_>, 128);
813813
static_assert_size!(PatKind<'_>, 112);
814-
static_assert_size!(Stmt<'_>, 72);
815-
static_assert_size!(StmtKind<'_>, 64);
814+
static_assert_size!(Stmt<'_>, 56);
815+
static_assert_size!(StmtKind<'_>, 48);
816816
}

compiler/rustc_middle/src/thir/visit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ pub fn walk_arm<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, arm: &Arm<'
211211

212212
pub fn walk_pat<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, pat: &Pat<'tcx>) {
213213
use PatKind::*;
214-
match pat.kind.as_ref() {
214+
match &pat.kind {
215215
AscribeUserType { subpattern, ascription: _ }
216216
| Deref { subpattern }
217217
| Binding {
@@ -236,7 +236,7 @@ pub fn walk_pat<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, pat: &Pat<'
236236
visitor.visit_pat(&subpattern);
237237
}
238238
if let Some(pat) = slice {
239-
visitor.visit_pat(pat);
239+
visitor.visit_pat(&pat);
240240
}
241241
for subpattern in suffix {
242242
visitor.visit_pat(&subpattern);

compiler/rustc_mir_build/src/build/block.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
117117
lint_level,
118118
else_block,
119119
} => {
120-
let ignores_expr_result = matches!(*pattern.kind, PatKind::Wild);
120+
let ignores_expr_result = matches!(pattern.kind, PatKind::Wild);
121121
this.block_context.push(BlockFrame::Statement { ignores_expr_result });
122122

123123
// Enter the remainder scope, i.e., the bindings' destruction scope.
@@ -160,7 +160,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
160160
ArmHasGuard(false),
161161
Some((None, initializer_span)),
162162
);
163-
this.expr_into_pattern(block, pattern.clone(), init) // irrefutable pattern
163+
this.expr_into_pattern(block, (**pattern).clone(), init) // irrefutable pattern
164164
}
165165
})
166166
},

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
493493
irrefutable_pat: Pat<'tcx>,
494494
initializer: &Expr<'tcx>,
495495
) -> BlockAnd<()> {
496-
match *irrefutable_pat.kind {
496+
match irrefutable_pat.kind {
497497
// Optimize the case of `let x = ...` to write directly into `x`
498498
PatKind::Binding { mode: BindingMode::ByValue, var, subpattern: None, .. } => {
499499
let place =
@@ -518,13 +518,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
518518
// broken.
519519
PatKind::AscribeUserType {
520520
subpattern:
521-
Pat {
521+
box Pat {
522522
kind:
523-
box PatKind::Binding {
524-
mode: BindingMode::ByValue,
525-
var,
526-
subpattern: None,
527-
..
523+
PatKind::Binding {
524+
mode: BindingMode::ByValue, var, subpattern: None, ..
528525
},
529526
..
530527
},
@@ -744,7 +741,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
744741
"visit_primary_bindings: pattern={:?} pattern_user_ty={:?}",
745742
pattern, pattern_user_ty
746743
);
747-
match *pattern.kind {
744+
match pattern.kind {
748745
PatKind::Binding {
749746
mutability,
750747
name,
@@ -1330,7 +1327,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
13301327

13311328
// All of the or-patterns have been sorted to the end, so if the first
13321329
// pattern is an or-pattern we only have or-patterns.
1333-
match *first_candidate.match_pairs[0].pattern.kind {
1330+
match first_candidate.match_pairs[0].pattern.kind {
13341331
PatKind::Or { .. } => (),
13351332
_ => {
13361333
self.test_candidates(
@@ -1350,7 +1347,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
13501347

13511348
let mut otherwise = None;
13521349
for match_pair in match_pairs {
1353-
let PatKind::Or { ref pats } = &*match_pair.pattern.kind else {
1350+
let PatKind::Or { ref pats } = &match_pair.pattern.kind else {
13541351
bug!("Or-patterns should have been sorted to the end");
13551352
};
13561353
let or_span = match_pair.pattern.span;
@@ -1384,7 +1381,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
13841381
&mut self,
13851382
candidate: &mut Candidate<'pat, 'tcx>,
13861383
otherwise: &mut Option<BasicBlock>,
1387-
pats: &'pat [Pat<'tcx>],
1384+
pats: &'pat [Box<Pat<'tcx>>],
13881385
or_span: Span,
13891386
place: PlaceBuilder<'tcx>,
13901387
fake_borrows: &mut Option<FxIndexSet<Place<'tcx>>>,
@@ -2289,7 +2286,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
22892286
let else_block_span = self.thir[else_block].span;
22902287
let (matching, failure) = self.in_if_then_scope(remainder_scope, |this| {
22912288
let scrutinee = unpack!(block = this.lower_scrutinee(block, init, initializer_span));
2292-
let pat = Pat { ty: init.ty, span: else_block_span, kind: Box::new(PatKind::Wild) };
2289+
let pat = Pat { ty: init.ty, span: else_block_span, kind: PatKind::Wild };
22932290
let mut wildcard = Candidate::new(scrutinee.clone(), &pat, false);
22942291
this.declare_bindings(
22952292
visibility_scope,

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
6767
loop {
6868
let match_pairs = mem::take(&mut candidate.match_pairs);
6969

70-
if let [MatchPair { pattern: Pat { kind: box PatKind::Or { pats }, .. }, place }] =
70+
if let [MatchPair { pattern: Pat { kind: PatKind::Or { pats }, .. }, place }] =
7171
&*match_pairs
7272
{
7373
existing_bindings.extend_from_slice(&new_bindings);
@@ -113,7 +113,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
113113
// late as possible.
114114
candidate
115115
.match_pairs
116-
.sort_by_key(|pair| matches!(*pair.pattern.kind, PatKind::Or { .. }));
116+
.sort_by_key(|pair| matches!(pair.pattern.kind, PatKind::Or { .. }));
117117
debug!(simplified = ?candidate, "simplify_candidate");
118118
return false; // if we were not able to simplify any, done.
119119
}
@@ -127,10 +127,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
127127
&mut self,
128128
candidate: &Candidate<'pat, 'tcx>,
129129
place: PlaceBuilder<'tcx>,
130-
pats: &'pat [Pat<'tcx>],
130+
pats: &'pat [Box<Pat<'tcx>>],
131131
) -> Vec<Candidate<'pat, 'tcx>> {
132132
pats.iter()
133-
.map(|pat| {
133+
.map(|box pat| {
134134
let mut candidate = Candidate::new(place.clone(), pat, candidate.has_guard);
135135
self.simplify_candidate(&mut candidate);
136136
candidate
@@ -149,7 +149,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
149149
candidate: &mut Candidate<'pat, 'tcx>,
150150
) -> Result<(), MatchPair<'pat, 'tcx>> {
151151
let tcx = self.tcx;
152-
match *match_pair.pattern.kind {
152+
match match_pair.pattern.kind {
153153
PatKind::AscribeUserType {
154154
ref subpattern,
155155
ascription: thir::Ascription { ref annotation, variance },
@@ -254,7 +254,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
254254
&mut candidate.match_pairs,
255255
&match_pair.place,
256256
prefix,
257-
slice.as_ref(),
257+
slice,
258258
suffix,
259259
);
260260
Ok(())
@@ -294,7 +294,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
294294
&mut candidate.match_pairs,
295295
&match_pair.place,
296296
prefix,
297-
slice.as_ref(),
297+
slice,
298298
suffix,
299299
);
300300
Ok(())

0 commit comments

Comments
 (0)