Skip to content

Commit

Permalink
Unify return, break and continue handling
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewjasper committed Jun 25, 2019
1 parent 82a1a89 commit be23bd4
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 181 deletions.
65 changes: 4 additions & 61 deletions src/librustc_mir/build/expr/stmt.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::build::scope::BreakableScope;
use crate::build::scope::BreakableTarget;
use crate::build::{BlockAnd, BlockAndExtension, BlockFrame, Builder};
use crate::hair::*;
use rustc::middle::region;
Expand Down Expand Up @@ -98,70 +98,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
block.unit()
}
ExprKind::Continue { label } => {
let BreakableScope {
continue_block,
region_scope,
..
} = *this.find_breakable_scope(expr_span, label);
let continue_block = continue_block
.expect("Attempted to continue in non-continuable breakable block");
this.exit_scope(
expr_span,
(region_scope, source_info),
block,
continue_block,
);
this.cfg.start_new_block().unit()
this.break_scope(block, None, BreakableTarget::Continue(label), source_info)
}
ExprKind::Break { label, value } => {
let (break_block, region_scope, destination) = {
let BreakableScope {
break_block,
region_scope,
ref break_destination,
..
} = *this.find_breakable_scope(expr_span, label);
(break_block, region_scope, break_destination.clone())
};
if let Some(value) = value {
debug!("stmt_expr Break val block_context.push(SubExpr) : {:?}", expr2);
this.block_context.push(BlockFrame::SubExpr);
unpack!(block = this.into(&destination, block, value));
this.block_context.pop();
} else {
this.cfg.push_assign_unit(block, source_info, &destination)
}
this.exit_scope(expr_span, (region_scope, source_info), block, break_block);
this.cfg.start_new_block().unit()
this.break_scope(block, value, BreakableTarget::Break(label), source_info)
}
ExprKind::Return { value } => {
block = match value {
Some(value) => {
debug!("stmt_expr Return val block_context.push(SubExpr) : {:?}", expr2);
this.block_context.push(BlockFrame::SubExpr);
let result = unpack!(
this.into(
&Place::RETURN_PLACE,
block,
value
)
);
this.block_context.pop();
result
}
None => {
this.cfg.push_assign_unit(
block,
source_info,
&Place::RETURN_PLACE,
);
block
}
};
let region_scope = this.region_scope_of_return_scope();
let return_block = this.return_block();
this.exit_scope(expr_span, (region_scope, source_info), block, return_block);
this.cfg.start_new_block().unit()
this.break_scope(block, value, BreakableTarget::Return, source_info)
}
ExprKind::InlineAsm {
asm,
Expand Down
15 changes: 7 additions & 8 deletions src/librustc_mir/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ struct Builder<'a, 'tcx> {

/// The current set of scopes, updated as we traverse;
/// see the `scope` module for more details.
scopes: Vec<scope::Scope<'tcx>>,
scopes: scope::Scopes<'tcx>,

/// The block-context: each time we build the code within an hair::Block,
/// we push a frame here tracking whether we are building a statement or
Expand All @@ -274,10 +274,6 @@ struct Builder<'a, 'tcx> {
/// The number of `push_unsafe_block` levels in scope.
push_unsafe_count: usize,

/// The current set of breakables; see the `scope` module for more
/// details.
breakable_scopes: Vec<scope::BreakableScope<'tcx>>,

/// The vector of all scopes that we have created thus far;
/// we track this for debuginfo later.
source_scopes: IndexVec<SourceScope, SourceScopeData>,
Expand Down Expand Up @@ -714,15 +710,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
fn_span: span,
arg_count,
is_generator,
scopes: vec![],
scopes: Default::default(),
block_context: BlockContext::new(),
source_scopes: IndexVec::new(),
source_scope: OUTERMOST_SOURCE_SCOPE,
source_scope_local_data: IndexVec::new(),
guard_context: vec![],
push_unsafe_count: 0,
unpushed_unsafe: safety,
breakable_scopes: vec![],
local_decls: IndexVec::from_elem_n(
LocalDecl::new_return_place(return_ty, return_span),
1,
Expand Down Expand Up @@ -865,7 +860,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}

let body = self.hir.mirror(ast_body);
self.into(&Place::RETURN_PLACE, block, body)
// `return_block` is called when we evaluate a `return` expression, so
// we just use `START_BLOCK` here.
self.in_breakable_scope(None, START_BLOCK, Place::RETURN_PLACE, |this| {
this.into(&Place::RETURN_PLACE, block, body)
})
}

fn get_unit_temp(&mut self) -> Place<'tcx> {
Expand Down
Loading

0 comments on commit be23bd4

Please sign in to comment.