Skip to content

Commit a8292c6

Browse files
committed
!! (WIP) squish: convert all THIR patterns and subpatterns
1 parent f3c376e commit a8292c6

File tree

17 files changed

+262
-214
lines changed

17 files changed

+262
-214
lines changed

compiler/rustc_middle/src/thir.rs

+29-29
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ macro_rules! thir_with_elements {
8484
}
8585

8686
thir_with_elements! {
87-
arms: ArmId => Arm<'tcx> => "a{}",
87+
arms: ArmId => Arm => "a{}",
8888
blocks: BlockId => Block => "b{}",
8989
exprs: ExprId => Expr<'tcx> => "e{}",
90-
stmts: StmtId => Stmt<'tcx> => "s{}",
90+
stmts: StmtId => Stmt => "s{}",
9191
params: ParamId => Param<'tcx> => "p{}",
9292
pats: PatId => Pat<'tcx> => "pat{}",
9393
}
@@ -103,7 +103,7 @@ pub enum BodyTy<'tcx> {
103103
#[derive(Debug, HashStable)]
104104
pub struct Param<'tcx> {
105105
/// The pattern that appears in the parameter list, or None for implicit parameters.
106-
pub pat: Option<Box<Pat<'tcx>>>,
106+
pub pat: Option<PatId>,
107107
/// The possibly inferred type.
108108
pub ty: Ty<'tcx>,
109109
/// Span of the explicitly provided type, or None if inferred for closures.
@@ -198,12 +198,12 @@ pub enum BlockSafety {
198198
}
199199

200200
#[derive(Debug, HashStable)]
201-
pub struct Stmt<'tcx> {
202-
pub kind: StmtKind<'tcx>,
201+
pub struct Stmt {
202+
pub kind: StmtKind,
203203
}
204204

205205
#[derive(Debug, HashStable)]
206-
pub enum StmtKind<'tcx> {
206+
pub enum StmtKind {
207207
/// An expression with a trailing semicolon.
208208
Expr {
209209
/// The scope for this statement; may be used as lifetime of temporaries.
@@ -226,7 +226,7 @@ pub enum StmtKind<'tcx> {
226226
/// `let <PAT> = ...`
227227
///
228228
/// If a type annotation is included, it is added as an ascription pattern.
229-
pattern: Box<Pat<'tcx>>,
229+
pattern: PatId,
230230

231231
/// `let pat: ty = <INIT>`
232232
initializer: Option<ExprId>,
@@ -374,7 +374,7 @@ pub enum ExprKind<'tcx> {
374374
/// (Not to be confused with [`StmtKind::Let`], which is a normal `let` statement.)
375375
Let {
376376
expr: ExprId,
377-
pat: Box<Pat<'tcx>>,
377+
pat: PatId,
378378
},
379379
/// A `match` expression.
380380
Match {
@@ -564,8 +564,8 @@ pub struct FruInfo<'tcx> {
564564

565565
/// A `match` arm.
566566
#[derive(Debug, HashStable)]
567-
pub struct Arm<'tcx> {
568-
pub pattern: Box<Pat<'tcx>>,
567+
pub struct Arm {
568+
pub pattern: PatId,
569569
pub guard: Option<ExprId>,
570570
pub body: ExprId,
571571
pub lint_level: LintLevel,
@@ -619,9 +619,9 @@ pub enum InlineAsmOperand<'tcx> {
619619
}
620620

621621
#[derive(Debug, HashStable, TypeVisitable)]
622-
pub struct FieldPat<'tcx> {
622+
pub struct FieldPat {
623623
pub field: FieldIdx,
624-
pub pattern: Pat<'tcx>,
624+
pub pattern: PatId,
625625
}
626626

627627
#[derive(Debug, HashStable, TypeVisitable)]
@@ -721,7 +721,7 @@ impl<'tcx> Thir<'tcx> {
721721
false
722722
}
723723
PatKind::Or { pats } => {
724-
is_never_pattern = pats.iter().all(|p| self.is_never_pattern(p));
724+
is_never_pattern = pats.iter().all(|&p| self.is_never_pattern(&self[p]));
725725
false
726726
}
727727
_ => true,
@@ -761,7 +761,7 @@ pub enum PatKind<'tcx> {
761761

762762
AscribeUserType {
763763
ascription: Ascription<'tcx>,
764-
subpattern: Box<Pat<'tcx>>,
764+
subpattern: PatId,
765765
},
766766

767767
/// `x`, `ref x`, `x @ P`, etc.
@@ -772,7 +772,7 @@ pub enum PatKind<'tcx> {
772772
#[type_visitable(ignore)]
773773
var: LocalVarId,
774774
ty: Ty<'tcx>,
775-
subpattern: Option<Box<Pat<'tcx>>>,
775+
subpattern: Option<PatId>,
776776

777777
/// Is this the leftmost occurrence of the binding, i.e., is `var` the
778778
/// `HirId` of this pattern?
@@ -788,23 +788,23 @@ pub enum PatKind<'tcx> {
788788
adt_def: AdtDef<'tcx>,
789789
args: GenericArgsRef<'tcx>,
790790
variant_index: VariantIdx,
791-
subpatterns: Vec<FieldPat<'tcx>>,
791+
subpatterns: Vec<FieldPat>,
792792
},
793793

794794
/// `(...)`, `Foo(...)`, `Foo{...}`, or `Foo`, where `Foo` is a variant name from an ADT with
795795
/// a single variant.
796796
Leaf {
797-
subpatterns: Vec<FieldPat<'tcx>>,
797+
subpatterns: Vec<FieldPat>,
798798
},
799799

800800
/// `box P`, `&P`, `&mut P`, etc.
801801
Deref {
802-
subpattern: Box<Pat<'tcx>>,
802+
subpattern: PatId,
803803
},
804804

805805
/// Deref pattern, written `box P` for now.
806806
DerefPattern {
807-
subpattern: Box<Pat<'tcx>>,
807+
subpattern: PatId,
808808
mutability: hir::Mutability,
809809
},
810810

@@ -838,7 +838,7 @@ pub enum PatKind<'tcx> {
838838
/// Otherwise, the actual pattern that the constant lowered to. As with
839839
/// other constants, inline constants are matched structurally where
840840
/// possible.
841-
subpattern: Box<Pat<'tcx>>,
841+
subpattern: PatId,
842842
},
843843

844844
Range(Arc<PatRange<'tcx>>),
@@ -847,22 +847,22 @@ pub enum PatKind<'tcx> {
847847
/// irrefutable when there is a slice pattern and both `prefix` and `suffix` are empty.
848848
/// e.g., `&[ref xs @ ..]`.
849849
Slice {
850-
prefix: Box<[Pat<'tcx>]>,
851-
slice: Option<Box<Pat<'tcx>>>,
852-
suffix: Box<[Pat<'tcx>]>,
850+
prefix: Box<[PatId]>,
851+
slice: Option<PatId>,
852+
suffix: Box<[PatId]>,
853853
},
854854

855855
/// Fixed match against an array; irrefutable.
856856
Array {
857-
prefix: Box<[Pat<'tcx>]>,
858-
slice: Option<Box<Pat<'tcx>>>,
859-
suffix: Box<[Pat<'tcx>]>,
857+
prefix: Box<[PatId]>,
858+
slice: Option<PatId>,
859+
suffix: Box<[PatId]>,
860860
},
861861

862862
/// An or-pattern, e.g. `p | q`.
863863
/// Invariant: `pats.len() >= 2`.
864864
Or {
865-
pats: Box<[Pat<'tcx>]>,
865+
pats: Box<[PatId]>,
866866
},
867867

868868
/// A never pattern `!`.
@@ -1128,7 +1128,7 @@ mod size_asserts {
11281128
static_assert_size!(ExprKind<'_>, 40);
11291129
static_assert_size!(Pat<'_>, 64);
11301130
static_assert_size!(PatKind<'_>, 48);
1131-
static_assert_size!(Stmt<'_>, 48);
1132-
static_assert_size!(StmtKind<'_>, 48);
1131+
static_assert_size!(Stmt, 44);
1132+
static_assert_size!(StmtKind, 44);
11331133
// tidy-alphabetical-end
11341134
}

compiler/rustc_middle/src/thir/visit.rs

+19-25
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ pub trait Visitor<'thir, 'tcx: 'thir>: Sized {
1010
walk_expr(self, expr);
1111
}
1212

13-
fn visit_stmt(&mut self, stmt: &'thir Stmt<'tcx>) {
13+
fn visit_stmt(&mut self, stmt: &'thir Stmt) {
1414
walk_stmt(self, stmt);
1515
}
1616

1717
fn visit_block(&mut self, block: &'thir Block) {
1818
walk_block(self, block);
1919
}
2020

21-
fn visit_arm(&mut self, arm: &'thir Arm<'tcx>) {
21+
fn visit_arm(&mut self, arm: &'thir Arm) {
2222
walk_arm(self, arm);
2323
}
2424

@@ -71,9 +71,9 @@ pub fn walk_expr<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
7171
PointerCoercion { source, cast: _, is_from_as_cast: _ } => {
7272
visitor.visit_expr(&visitor.thir()[source])
7373
}
74-
Let { expr, ref pat } => {
74+
Let { expr, pat } => {
7575
visitor.visit_expr(&visitor.thir()[expr]);
76-
visitor.visit_pat(pat);
76+
visitor.visit_pat(&visitor.thir()[pat]);
7777
}
7878
Loop { body } => visitor.visit_expr(&visitor.thir()[body]),
7979
Match { scrutinee, ref arms, .. } => {
@@ -184,10 +184,7 @@ pub fn walk_expr<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
184184
}
185185
}
186186

187-
pub fn walk_stmt<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
188-
visitor: &mut V,
189-
stmt: &'thir Stmt<'tcx>,
190-
) {
187+
pub fn walk_stmt<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(visitor: &mut V, stmt: &'thir Stmt) {
191188
match &stmt.kind {
192189
StmtKind::Expr { expr, scope: _ } => visitor.visit_expr(&visitor.thir()[*expr]),
193190
StmtKind::Let {
@@ -202,7 +199,7 @@ pub fn walk_stmt<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
202199
if let Some(init) = initializer {
203200
visitor.visit_expr(&visitor.thir()[*init]);
204201
}
205-
visitor.visit_pat(pattern);
202+
visitor.visit_pat(&visitor.thir()[*pattern]);
206203
if let Some(block) = else_block {
207204
visitor.visit_block(&visitor.thir()[*block])
208205
}
@@ -222,14 +219,11 @@ pub fn walk_block<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
222219
}
223220
}
224221

225-
pub fn walk_arm<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
226-
visitor: &mut V,
227-
arm: &'thir Arm<'tcx>,
228-
) {
222+
pub fn walk_arm<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(visitor: &mut V, arm: &'thir Arm) {
229223
if let Some(expr) = arm.guard {
230224
visitor.visit_expr(&visitor.thir()[expr])
231225
}
232-
visitor.visit_pat(&arm.pattern);
226+
visitor.visit_pat(&visitor.thir()[arm.pattern]);
233227
visitor.visit_expr(&visitor.thir()[arm.body]);
234228
}
235229

@@ -241,7 +235,7 @@ pub fn walk_pat<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
241235
}
242236

243237
pub(crate) fn for_each_immediate_subpat<'a, 'tcx>(
244-
_thir: &'a Thir<'tcx>,
238+
thir: &'a Thir<'tcx>,
245239
pat: &'a Pat<'tcx>,
246240
mut callback: impl FnMut(&'a Pat<'tcx>),
247241
) {
@@ -253,28 +247,28 @@ pub(crate) fn for_each_immediate_subpat<'a, 'tcx>(
253247
| PatKind::Never
254248
| PatKind::Error(_) => {}
255249

256-
PatKind::AscribeUserType { ref subpattern, .. }
257-
| PatKind::Binding { subpattern: Some(ref subpattern), .. }
258-
| PatKind::Deref { ref subpattern }
259-
| PatKind::DerefPattern { ref subpattern, .. }
260-
| PatKind::ExpandedConstant { ref subpattern, .. } => callback(subpattern),
250+
PatKind::AscribeUserType { subpattern, .. }
251+
| PatKind::Binding { subpattern: Some(subpattern), .. }
252+
| PatKind::Deref { subpattern }
253+
| PatKind::DerefPattern { subpattern, .. }
254+
| PatKind::ExpandedConstant { subpattern, .. } => callback(&thir[subpattern]),
261255

262256
PatKind::Variant { ref subpatterns, .. } | PatKind::Leaf { ref subpatterns } => {
263257
for field_pat in subpatterns {
264-
callback(&field_pat.pattern);
258+
callback(&thir[field_pat.pattern]);
265259
}
266260
}
267261

268262
PatKind::Slice { ref prefix, ref slice, ref suffix }
269263
| PatKind::Array { ref prefix, ref slice, ref suffix } => {
270-
for pat in prefix.iter().chain(slice.as_deref()).chain(suffix) {
271-
callback(pat);
264+
for &pat in prefix.iter().chain(slice).chain(suffix) {
265+
callback(&thir[pat]);
272266
}
273267
}
274268

275269
PatKind::Or { ref pats } => {
276-
for pat in pats {
277-
callback(pat);
270+
for &pat in pats {
271+
callback(&thir[pat]);
278272
}
279273
}
280274
}

compiler/rustc_mir_build/src/builder/block.rs

+2
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
155155
// │Continue... │ └────────────────┘
156156
// └────────────────┘
157157

158+
let pattern = &this.thir[*pattern];
158159
let ignores_expr_result = matches!(pattern.kind, PatKind::Wild);
159160
this.block_context.push(BlockFrame::Statement { ignores_expr_result });
160161

@@ -246,6 +247,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
246247
else_block: None,
247248
span: _,
248249
} => {
250+
let pattern = &this.thir[*pattern];
249251
let ignores_expr_result = matches!(pattern.kind, PatKind::Wild);
250252
this.block_context.push(BlockFrame::Statement { ignores_expr_result });
251253

compiler/rustc_mir_build/src/builder/custom/parse.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl<'a, 'tcx> ParseCtxt<'a, 'tcx> {
8383
match &self.thir[stmt_id].kind {
8484
StmtKind::Expr { expr, .. } => Ok(*expr),
8585
kind @ StmtKind::Let { pattern, .. } => Err(ParseError {
86-
span: pattern.span,
86+
span: self.thir[*pattern].span,
8787
item_description: format!("{kind:?}"),
8888
expected: "expression".to_string(),
8989
}),
@@ -93,7 +93,7 @@ impl<'a, 'tcx> ParseCtxt<'a, 'tcx> {
9393
pub(crate) fn parse_args(&mut self, params: &IndexSlice<ParamId, Param<'tcx>>) -> PResult<()> {
9494
for param in params.iter() {
9595
let (var, span) = {
96-
let pat = param.pat.as_ref().unwrap();
96+
let pat = &self.thir[param.pat.unwrap()];
9797
match &pat.kind {
9898
PatKind::Binding { var, .. } => (*var, pat.span),
9999
_ => {
@@ -198,7 +198,7 @@ impl<'a, 'tcx> ParseCtxt<'a, 'tcx> {
198198
fn parse_basic_block_decl(&mut self, stmt: StmtId) -> PResult<()> {
199199
match &self.thir[stmt].kind {
200200
StmtKind::Let { pattern, initializer: Some(initializer), .. } => {
201-
let (var, ..) = self.parse_var(pattern)?;
201+
let (var, ..) = self.parse_var(&self.thir[*pattern])?;
202202
let data = BasicBlockData::new(
203203
None,
204204
parse_by_kind!(self, *initializer, _, "basic block declaration",
@@ -277,7 +277,7 @@ impl<'a, 'tcx> ParseCtxt<'a, 'tcx> {
277277

278278
fn parse_let_statement(&mut self, stmt_id: StmtId) -> PResult<(LocalVarId, Ty<'tcx>, Span)> {
279279
let pattern = match &self.thir[stmt_id].kind {
280-
StmtKind::Let { pattern, .. } => pattern,
280+
StmtKind::Let { pattern, .. } => &self.thir[*pattern],
281281
StmtKind::Expr { expr, .. } => {
282282
return Err(self.expr_error(*expr, "let statement"));
283283
}
@@ -286,13 +286,14 @@ impl<'a, 'tcx> ParseCtxt<'a, 'tcx> {
286286
self.parse_var(pattern)
287287
}
288288

289-
fn parse_var(&mut self, mut pat: &Pat<'tcx>) -> PResult<(LocalVarId, Ty<'tcx>, Span)> {
289+
fn parse_var(&mut self, pat: &Pat<'tcx>) -> PResult<(LocalVarId, Ty<'tcx>, Span)> {
290290
// Make sure we throw out any `AscribeUserType` we find
291+
let mut pat = pat;
291292
loop {
292293
match &pat.kind {
293294
PatKind::Binding { var, ty, .. } => break Ok((*var, *ty, pat.span)),
294295
PatKind::AscribeUserType { subpattern, .. } => {
295-
pat = subpattern;
296+
pat = &self.thir[*subpattern];
296297
}
297298
_ => {
298299
break Err(ParseError {

0 commit comments

Comments
 (0)