Skip to content

Commit 2df805f

Browse files
committed
Store blocks in Thir.
Like expressions, statements, and match arms. This shrinks `thir::Stmt` and is a precursor to further shrinking `thir::Expr`.
1 parent e7c25c3 commit 2df805f

File tree

11 files changed

+66
-47
lines changed

11 files changed

+66
-47
lines changed

compiler/rustc_middle/src/thir.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ macro_rules! thir_with_elements {
7575

7676
thir_with_elements! {
7777
arms: ArmId => Arm<'tcx> => "a{}",
78+
blocks: BlockId => Block => "b{}",
7879
exprs: ExprId => Expr<'tcx> => "e{}",
7980
stmts: StmtId => Stmt<'tcx> => "s{}",
8081
}
@@ -168,7 +169,7 @@ pub enum StmtKind<'tcx> {
168169
initializer: Option<ExprId>,
169170

170171
/// `let pat: ty = <INIT> else { <ELSE> }
171-
else_block: Option<Block>,
172+
else_block: Option<BlockId>,
172173

173174
/// The lint level for this `let` statement.
174175
lint_level: LintLevel,
@@ -292,7 +293,7 @@ pub enum ExprKind<'tcx> {
292293
},
293294
/// A block.
294295
Block {
295-
body: Block,
296+
block: BlockId,
296297
},
297298
/// An assignment: `lhs = rhs`.
298299
Assign {
@@ -802,5 +803,5 @@ mod size_asserts {
802803
static_assert_size!(Block, 56);
803804
static_assert_size!(Expr<'_>, 88);
804805
static_assert_size!(Pat<'_>, 24);
805-
static_assert_size!(Stmt<'_>, 120);
806+
static_assert_size!(Stmt<'_>, 72);
806807
}

compiler/rustc_middle/src/thir/visit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp
7575
visitor.visit_arm(&visitor.thir()[arm]);
7676
}
7777
}
78-
Block { ref body } => visitor.visit_block(body),
78+
Block { block } => visitor.visit_block(&visitor.thir()[block]),
7979
Assign { lhs, rhs } | AssignOp { lhs, rhs, op: _ } => {
8080
visitor.visit_expr(&visitor.thir()[lhs]);
8181
visitor.visit_expr(&visitor.thir()[rhs]);
@@ -174,7 +174,7 @@ pub fn walk_stmt<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, stmt: &Stm
174174
}
175175
visitor.visit_pat(pattern);
176176
if let Some(block) = else_block {
177-
visitor.visit_block(block)
177+
visitor.visit_block(&visitor.thir()[*block])
178178
}
179179
}
180180
}

compiler/rustc_mir_build/src/build/block.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1010
&mut self,
1111
destination: Place<'tcx>,
1212
block: BasicBlock,
13-
ast_block: &Block,
13+
ast_block: BlockId,
1414
source_info: SourceInfo,
1515
) -> BlockAnd<()> {
1616
let Block {
@@ -21,7 +21,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
2121
expr,
2222
targeted_by_break,
2323
safety_mode,
24-
} = *ast_block;
24+
} = self.thir[ast_block];
2525
let expr = expr.map(|expr| &self.thir[expr]);
2626
self.in_opt_scope(opt_destruction_scope.map(|de| (de, source_info)), move |this| {
2727
this.in_scope((region_scope, source_info), LintLevel::Inherited, move |this| {
@@ -130,7 +130,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
130130
block,
131131
init,
132132
initializer_span,
133-
else_block,
133+
*else_block,
134134
visibility_scope,
135135
*remainder_scope,
136136
remainder_span,

compiler/rustc_mir_build/src/build/expr/as_temp.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
8383
// Don't bother with StorageLive and Dead for these temporaries,
8484
// they are never assigned.
8585
ExprKind::Break { .. } | ExprKind::Continue { .. } | ExprKind::Return { .. } => (),
86-
ExprKind::Block { body: Block { expr: None, targeted_by_break: false, .. } }
87-
if expr_ty.is_never() => {}
86+
ExprKind::Block { block }
87+
if let Block { expr: None, targeted_by_break: false, .. } = this.thir[block]
88+
&& expr_ty.is_never() => {}
8889
_ => {
8990
this.cfg
9091
.push(block, Statement { source_info, kind: StatementKind::StorageLive(temp) });

compiler/rustc_mir_build/src/build/expr/into.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
4646
})
4747
})
4848
}
49-
ExprKind::Block { body: ref ast_block } => {
49+
ExprKind::Block { block: ast_block } => {
5050
this.ast_block(destination, block, ast_block, source_info)
5151
}
5252
ExprKind::Match { scrutinee, ref arms } => {

compiler/rustc_mir_build/src/build/expr/stmt.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,22 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
116116
// it is usually better to focus on `the_value` rather
117117
// than the entirety of block(s) surrounding it.
118118
let adjusted_span = (|| {
119-
if let ExprKind::Block { body } = &expr.kind && let Some(tail_ex) = body.expr {
119+
if let ExprKind::Block { block } = expr.kind
120+
&& let Some(tail_ex) = this.thir[block].expr
121+
{
120122
let mut expr = &this.thir[tail_ex];
121-
while let ExprKind::Block {
122-
body: Block { expr: Some(nested_expr), .. },
123-
}
124-
| ExprKind::Scope { value: nested_expr, .. } = expr.kind
125-
{
126-
expr = &this.thir[nested_expr];
123+
loop {
124+
match expr.kind {
125+
ExprKind::Block { block }
126+
if let Some(nested_expr) = this.thir[block].expr =>
127+
{
128+
expr = &this.thir[nested_expr];
129+
}
130+
ExprKind::Scope { value: nested_expr, .. } => {
131+
expr = &this.thir[nested_expr];
132+
}
133+
_ => break,
134+
}
127135
}
128136
this.block_context.push(BlockFrame::TailExpr {
129137
tail_result_is_ignored: true,

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2280,15 +2280,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
22802280
mut block: BasicBlock,
22812281
init: &Expr<'tcx>,
22822282
initializer_span: Span,
2283-
else_block: &Block,
2283+
else_block: BlockId,
22842284
visibility_scope: Option<SourceScope>,
22852285
remainder_scope: region::Scope,
22862286
remainder_span: Span,
22872287
pattern: &Pat<'tcx>,
22882288
) -> BlockAnd<()> {
2289+
let else_block_span = self.thir[else_block].span;
22892290
let (matching, failure) = self.in_if_then_scope(remainder_scope, |this| {
22902291
let scrutinee = unpack!(block = this.lower_scrutinee(block, init, initializer_span));
2291-
let pat = Pat { ty: init.ty, span: else_block.span, kind: Box::new(PatKind::Wild) };
2292+
let pat = Pat { ty: init.ty, span: else_block_span, kind: Box::new(PatKind::Wild) };
22922293
let mut wildcard = Candidate::new(scrutinee.clone(), &pat, false);
22932294
this.declare_bindings(
22942295
visibility_scope,
@@ -2318,7 +2319,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
23182319
);
23192320
// This block is for the failure case
23202321
let failure = this.bind_pattern(
2321-
this.source_info(else_block.span),
2322+
this.source_info(else_block_span),
23222323
wildcard,
23232324
None,
23242325
&fake_borrow_temps,
@@ -2334,19 +2335,19 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
23342335
// This place is not really used because this destination place
23352336
// should never be used to take values at the end of the failure
23362337
// block.
2337-
let dummy_place = self.temp(self.tcx.types.never, else_block.span);
2338+
let dummy_place = self.temp(self.tcx.types.never, else_block_span);
23382339
let failure_block;
23392340
unpack!(
23402341
failure_block = self.ast_block(
23412342
dummy_place,
23422343
failure,
23432344
else_block,
2344-
self.source_info(else_block.span),
2345+
self.source_info(else_block_span),
23452346
)
23462347
);
23472348
self.cfg.terminate(
23482349
failure_block,
2349-
self.source_info(else_block.span),
2350+
self.source_info(else_block_span),
23502351
TerminatorKind::Unreachable,
23512352
);
23522353
matching.unit()

compiler/rustc_mir_build/src/thir/cx/block.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ use rustc_index::vec::Idx;
99
use rustc_middle::ty::CanonicalUserTypeAnnotation;
1010

1111
impl<'tcx> Cx<'tcx> {
12-
pub(crate) fn mirror_block(&mut self, block: &'tcx hir::Block<'tcx>) -> Block {
12+
pub(crate) fn mirror_block(&mut self, block: &'tcx hir::Block<'tcx>) -> BlockId {
1313
// We have to eagerly lower the "spine" of the statements
1414
// in order to get the lexical scoping correctly.
1515
let stmts = self.mirror_stmts(block.hir_id.local_id, block.stmts);
1616
let opt_destruction_scope =
1717
self.region_scope_tree.opt_destruction_scope(block.hir_id.local_id);
18-
Block {
18+
let block = Block {
1919
targeted_by_break: block.targeted_by_break,
2020
region_scope: region::Scope {
2121
id: block.hir_id.local_id,
@@ -34,7 +34,9 @@ impl<'tcx> Cx<'tcx> {
3434
BlockSafety::ExplicitUnsafe(block.hir_id)
3535
}
3636
},
37-
}
37+
};
38+
39+
self.thir.blocks.push(block)
3840
}
3941

4042
fn mirror_stmts(

compiler/rustc_mir_build/src/thir/cx/expr.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ impl<'tcx> Cx<'tcx> {
108108
// // ^ error message points at this expression.
109109
// }
110110
let mut adjust_span = |expr: &mut Expr<'tcx>| {
111-
if let ExprKind::Block { body } = &expr.kind {
112-
if let Some(last_expr) = body.expr {
111+
if let ExprKind::Block { block } = expr.kind {
112+
if let Some(last_expr) = self.thir[block].expr {
113113
span = self.thir[last_expr].span;
114114
expr.span = span;
115115
}
@@ -369,7 +369,7 @@ impl<'tcx> Cx<'tcx> {
369369
ExprKind::AddressOf { mutability, arg: self.mirror_expr(arg) }
370370
}
371371

372-
hir::ExprKind::Block(ref blk, _) => ExprKind::Block { body: self.mirror_block(blk) },
372+
hir::ExprKind::Block(ref blk, _) => ExprKind::Block { block: self.mirror_block(blk) },
373373

374374
hir::ExprKind::Assign(ref lhs, ref rhs, _) => {
375375
ExprKind::Assign { lhs: self.mirror_expr(lhs), rhs: self.mirror_expr(rhs) }
@@ -680,8 +680,8 @@ impl<'tcx> Cx<'tcx> {
680680
let body = self.thir.exprs.push(Expr {
681681
ty: block_ty,
682682
temp_lifetime,
683-
span: block.span,
684-
kind: ExprKind::Block { body: block },
683+
span: self.thir[block].span,
684+
kind: ExprKind::Block { block },
685685
});
686686
ExprKind::Loop { body }
687687
}

compiler/rustc_ty_utils/src/consts.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,15 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
311311
// bar::<{ N + 1 }>();
312312
// }
313313
// ```
314-
ExprKind::Block { body: thir::Block { stmts: box [], expr: Some(e), .. } } => {
315-
self.recurse_build(*e)?
314+
ExprKind::Block { block } => {
315+
if let thir::Block { stmts: box [], expr: Some(e), .. } = &self.body.blocks[*block] {
316+
self.recurse_build(*e)?
317+
} else {
318+
self.maybe_supported_error(
319+
node.span,
320+
"blocks are not supported in generic constant",
321+
)?
322+
}
316323
}
317324
// `ExprKind::Use` happens when a `hir::ExprKind::Cast` is a
318325
// "coercion cast" i.e. using a coercion or is a no-op.
@@ -349,10 +356,6 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
349356
node.span,
350357
"array construction is not supported in generic constants",
351358
)?,
352-
ExprKind::Block { .. } => self.maybe_supported_error(
353-
node.span,
354-
"blocks are not supported in generic constant",
355-
)?,
356359
ExprKind::NeverToAny { .. } => self.maybe_supported_error(
357360
node.span,
358361
"converting nevers to any is not supported in generic constant",

src/test/ui/thir-tree.stdout

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
DefId(0:3 ~ thir_tree[8f1d]::main):
22
Thir {
33
arms: [],
4+
blocks: [
5+
Block {
6+
targeted_by_break: false,
7+
region_scope: Node(1),
8+
opt_destruction_scope: None,
9+
span: $DIR/thir-tree.rs:4:15: 4:17 (#0),
10+
stmts: [],
11+
expr: None,
12+
safety_mode: Safe,
13+
},
14+
],
415
exprs: [
516
Expr {
617
ty: (),
@@ -9,15 +20,7 @@ Thir {
920
),
1021
span: $DIR/thir-tree.rs:4:15: 4:17 (#0),
1122
kind: Block {
12-
body: Block {
13-
targeted_by_break: false,
14-
region_scope: Node(1),
15-
opt_destruction_scope: None,
16-
span: $DIR/thir-tree.rs:4:15: 4:17 (#0),
17-
stmts: [],
18-
expr: None,
19-
safety_mode: Safe,
20-
},
23+
block: b0,
2124
},
2225
},
2326
Expr {

0 commit comments

Comments
 (0)