Store blocks in Thir
.
Like expressions, statements, and match arms. This shrinks `thir::Stmt` and is a precursor to further shrinking `thir::Expr`.
This commit is contained in:
parent
e7c25c3a97
commit
2df805fc7a
11 changed files with 66 additions and 47 deletions
|
@ -10,7 +10,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
&mut self,
|
||||
destination: Place<'tcx>,
|
||||
block: BasicBlock,
|
||||
ast_block: &Block,
|
||||
ast_block: BlockId,
|
||||
source_info: SourceInfo,
|
||||
) -> BlockAnd<()> {
|
||||
let Block {
|
||||
|
@ -21,7 +21,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
expr,
|
||||
targeted_by_break,
|
||||
safety_mode,
|
||||
} = *ast_block;
|
||||
} = self.thir[ast_block];
|
||||
let expr = expr.map(|expr| &self.thir[expr]);
|
||||
self.in_opt_scope(opt_destruction_scope.map(|de| (de, source_info)), move |this| {
|
||||
this.in_scope((region_scope, source_info), LintLevel::Inherited, move |this| {
|
||||
|
@ -130,7 +130,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
block,
|
||||
init,
|
||||
initializer_span,
|
||||
else_block,
|
||||
*else_block,
|
||||
visibility_scope,
|
||||
*remainder_scope,
|
||||
remainder_span,
|
||||
|
|
|
@ -83,8 +83,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
// Don't bother with StorageLive and Dead for these temporaries,
|
||||
// they are never assigned.
|
||||
ExprKind::Break { .. } | ExprKind::Continue { .. } | ExprKind::Return { .. } => (),
|
||||
ExprKind::Block { body: Block { expr: None, targeted_by_break: false, .. } }
|
||||
if expr_ty.is_never() => {}
|
||||
ExprKind::Block { block }
|
||||
if let Block { expr: None, targeted_by_break: false, .. } = this.thir[block]
|
||||
&& expr_ty.is_never() => {}
|
||||
_ => {
|
||||
this.cfg
|
||||
.push(block, Statement { source_info, kind: StatementKind::StorageLive(temp) });
|
||||
|
|
|
@ -46,7 +46,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
})
|
||||
})
|
||||
}
|
||||
ExprKind::Block { body: ref ast_block } => {
|
||||
ExprKind::Block { block: ast_block } => {
|
||||
this.ast_block(destination, block, ast_block, source_info)
|
||||
}
|
||||
ExprKind::Match { scrutinee, ref arms } => {
|
||||
|
|
|
@ -116,14 +116,22 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
// it is usually better to focus on `the_value` rather
|
||||
// than the entirety of block(s) surrounding it.
|
||||
let adjusted_span = (|| {
|
||||
if let ExprKind::Block { body } = &expr.kind && let Some(tail_ex) = body.expr {
|
||||
if let ExprKind::Block { block } = expr.kind
|
||||
&& let Some(tail_ex) = this.thir[block].expr
|
||||
{
|
||||
let mut expr = &this.thir[tail_ex];
|
||||
while let ExprKind::Block {
|
||||
body: Block { expr: Some(nested_expr), .. },
|
||||
}
|
||||
| ExprKind::Scope { value: nested_expr, .. } = expr.kind
|
||||
{
|
||||
expr = &this.thir[nested_expr];
|
||||
loop {
|
||||
match expr.kind {
|
||||
ExprKind::Block { block }
|
||||
if let Some(nested_expr) = this.thir[block].expr =>
|
||||
{
|
||||
expr = &this.thir[nested_expr];
|
||||
}
|
||||
ExprKind::Scope { value: nested_expr, .. } => {
|
||||
expr = &this.thir[nested_expr];
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
this.block_context.push(BlockFrame::TailExpr {
|
||||
tail_result_is_ignored: true,
|
||||
|
|
|
@ -2280,15 +2280,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
mut block: BasicBlock,
|
||||
init: &Expr<'tcx>,
|
||||
initializer_span: Span,
|
||||
else_block: &Block,
|
||||
else_block: BlockId,
|
||||
visibility_scope: Option<SourceScope>,
|
||||
remainder_scope: region::Scope,
|
||||
remainder_span: Span,
|
||||
pattern: &Pat<'tcx>,
|
||||
) -> BlockAnd<()> {
|
||||
let else_block_span = self.thir[else_block].span;
|
||||
let (matching, failure) = self.in_if_then_scope(remainder_scope, |this| {
|
||||
let scrutinee = unpack!(block = this.lower_scrutinee(block, init, initializer_span));
|
||||
let pat = Pat { ty: init.ty, span: else_block.span, kind: Box::new(PatKind::Wild) };
|
||||
let pat = Pat { ty: init.ty, span: else_block_span, kind: Box::new(PatKind::Wild) };
|
||||
let mut wildcard = Candidate::new(scrutinee.clone(), &pat, false);
|
||||
this.declare_bindings(
|
||||
visibility_scope,
|
||||
|
@ -2318,7 +2319,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
);
|
||||
// This block is for the failure case
|
||||
let failure = this.bind_pattern(
|
||||
this.source_info(else_block.span),
|
||||
this.source_info(else_block_span),
|
||||
wildcard,
|
||||
None,
|
||||
&fake_borrow_temps,
|
||||
|
@ -2334,19 +2335,19 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
// This place is not really used because this destination place
|
||||
// should never be used to take values at the end of the failure
|
||||
// block.
|
||||
let dummy_place = self.temp(self.tcx.types.never, else_block.span);
|
||||
let dummy_place = self.temp(self.tcx.types.never, else_block_span);
|
||||
let failure_block;
|
||||
unpack!(
|
||||
failure_block = self.ast_block(
|
||||
dummy_place,
|
||||
failure,
|
||||
else_block,
|
||||
self.source_info(else_block.span),
|
||||
self.source_info(else_block_span),
|
||||
)
|
||||
);
|
||||
self.cfg.terminate(
|
||||
failure_block,
|
||||
self.source_info(else_block.span),
|
||||
self.source_info(else_block_span),
|
||||
TerminatorKind::Unreachable,
|
||||
);
|
||||
matching.unit()
|
||||
|
|
|
@ -9,13 +9,13 @@ use rustc_index::vec::Idx;
|
|||
use rustc_middle::ty::CanonicalUserTypeAnnotation;
|
||||
|
||||
impl<'tcx> Cx<'tcx> {
|
||||
pub(crate) fn mirror_block(&mut self, block: &'tcx hir::Block<'tcx>) -> Block {
|
||||
pub(crate) fn mirror_block(&mut self, block: &'tcx hir::Block<'tcx>) -> BlockId {
|
||||
// We have to eagerly lower the "spine" of the statements
|
||||
// in order to get the lexical scoping correctly.
|
||||
let stmts = self.mirror_stmts(block.hir_id.local_id, block.stmts);
|
||||
let opt_destruction_scope =
|
||||
self.region_scope_tree.opt_destruction_scope(block.hir_id.local_id);
|
||||
Block {
|
||||
let block = Block {
|
||||
targeted_by_break: block.targeted_by_break,
|
||||
region_scope: region::Scope {
|
||||
id: block.hir_id.local_id,
|
||||
|
@ -34,7 +34,9 @@ impl<'tcx> Cx<'tcx> {
|
|||
BlockSafety::ExplicitUnsafe(block.hir_id)
|
||||
}
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
self.thir.blocks.push(block)
|
||||
}
|
||||
|
||||
fn mirror_stmts(
|
||||
|
|
|
@ -108,8 +108,8 @@ impl<'tcx> Cx<'tcx> {
|
|||
// // ^ error message points at this expression.
|
||||
// }
|
||||
let mut adjust_span = |expr: &mut Expr<'tcx>| {
|
||||
if let ExprKind::Block { body } = &expr.kind {
|
||||
if let Some(last_expr) = body.expr {
|
||||
if let ExprKind::Block { block } = expr.kind {
|
||||
if let Some(last_expr) = self.thir[block].expr {
|
||||
span = self.thir[last_expr].span;
|
||||
expr.span = span;
|
||||
}
|
||||
|
@ -369,7 +369,7 @@ impl<'tcx> Cx<'tcx> {
|
|||
ExprKind::AddressOf { mutability, arg: self.mirror_expr(arg) }
|
||||
}
|
||||
|
||||
hir::ExprKind::Block(ref blk, _) => ExprKind::Block { body: self.mirror_block(blk) },
|
||||
hir::ExprKind::Block(ref blk, _) => ExprKind::Block { block: self.mirror_block(blk) },
|
||||
|
||||
hir::ExprKind::Assign(ref lhs, ref rhs, _) => {
|
||||
ExprKind::Assign { lhs: self.mirror_expr(lhs), rhs: self.mirror_expr(rhs) }
|
||||
|
@ -680,8 +680,8 @@ impl<'tcx> Cx<'tcx> {
|
|||
let body = self.thir.exprs.push(Expr {
|
||||
ty: block_ty,
|
||||
temp_lifetime,
|
||||
span: block.span,
|
||||
kind: ExprKind::Block { body: block },
|
||||
span: self.thir[block].span,
|
||||
kind: ExprKind::Block { block },
|
||||
});
|
||||
ExprKind::Loop { body }
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue