Skip to content

Commit

Permalink
Make the compiler support the label-break-value feature
Browse files Browse the repository at this point in the history
No error checking or feature gating yet
  • Loading branch information
est31 committed May 16, 2018
1 parent ebca9c6 commit 5665980
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/librustc/hir/lowering.rs
Expand Up @@ -3101,7 +3101,9 @@ impl<'a> LoweringContext<'a> {
})
}
ExprKind::Block(ref blk, opt_label) => {
hir::ExprBlock(self.lower_block(blk, false), self.lower_label(opt_label))
hir::ExprBlock(self.lower_block(blk,
opt_label.is_some()),
self.lower_label(opt_label))
}
ExprKind::Assign(ref el, ref er) => {
hir::ExprAssign(P(self.lower_expr(el)), P(self.lower_expr(er)))
Expand Down
5 changes: 2 additions & 3 deletions src/librustc/hir/mod.rs
Expand Up @@ -778,9 +778,8 @@ pub struct Block {
pub rules: BlockCheckMode,
pub span: Span,
/// If true, then there may exist `break 'a` values that aim to
/// break out of this block early. As of this writing, this is not
/// currently permitted in Rust itself, but it is generated as
/// part of `catch` statements.
/// break out of this block early.
/// Used by `'label {}` blocks and by `catch` statements.
pub targeted_by_break: bool,
/// If true, don't emit return value type errors as the parser had
/// to recover from a parse error so this block will not have an
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/build/block.rs
Expand Up @@ -36,7 +36,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
self.in_opt_scope(opt_destruction_scope.map(|de|(de, source_info)), block, move |this| {
this.in_scope((region_scope, source_info), LintLevel::Inherited, block, move |this| {
if targeted_by_break {
// This is a `break`-able block (currently only `catch { ... }`)
// This is a `break`-able block
let exit_block = this.cfg.start_new_block();
let block_exit = this.in_breakable_scope(
None, exit_block, destination.clone(), |this| {
Expand Down
26 changes: 21 additions & 5 deletions src/librustc_passes/loops.rs
Expand Up @@ -21,6 +21,7 @@ use syntax_pos::Span;
enum LoopKind {
Loop(hir::LoopSource),
WhileLoop,
Block,
}

impl LoopKind {
Expand All @@ -30,6 +31,7 @@ impl LoopKind {
LoopKind::Loop(hir::LoopSource::WhileLet) => "while let",
LoopKind::Loop(hir::LoopSource::ForLoop) => "for",
LoopKind::WhileLoop => "while",
LoopKind::Block => "block",
}
}
}
Expand All @@ -39,6 +41,7 @@ enum Context {
Normal,
Loop(LoopKind),
Closure,
LabeledBlock,
}

#[derive(Copy, Clone)]
Expand Down Expand Up @@ -84,6 +87,9 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
hir::ExprClosure(.., b, _, _) => {
self.with_context(Closure, |v| v.visit_nested_body(b));
}
hir::ExprBlock(ref b, Some(_label)) => {
self.with_context(LabeledBlock, |v| v.visit_block(&b));
}
hir::ExprBreak(label, ref opt_expr) => {
let loop_id = match label.target_id.into() {
Ok(loop_id) => loop_id,
Expand All @@ -94,32 +100,41 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
},
Err(hir::LoopIdError::UnresolvedLabel) => ast::DUMMY_NODE_ID,
};

if loop_id != ast::DUMMY_NODE_ID {
match self.hir_map.find(loop_id).unwrap() {
hir::map::NodeBlock(_) => return,
_=> (),
}
}

if self.cx == LabeledBlock {
return;
}

if opt_expr.is_some() {
let loop_kind = if loop_id == ast::DUMMY_NODE_ID {
None
} else {
Some(match self.hir_map.expect_expr(loop_id).node {
hir::ExprWhile(..) => LoopKind::WhileLoop,
hir::ExprLoop(_, _, source) => LoopKind::Loop(source),
hir::ExprBlock(..) => LoopKind::Block,
ref r => span_bug!(e.span,
"break label resolved to a non-loop: {:?}", r),
})
};
match loop_kind {
None | Some(LoopKind::Loop(hir::LoopSource::Loop)) => (),
None |
Some(LoopKind::Loop(hir::LoopSource::Loop)) |
Some(LoopKind::Block) => (),
Some(kind) => {
struct_span_err!(self.sess, e.span, E0571,
"`break` with value from a `{}` loop",
kind.name())
.span_label(e.span,
"can only break with a value inside `loop`")
"can only break with a value inside \
`loop` or breakable block")
.span_suggestion(e.span,
&format!("instead, use `break` on its own \
without a value inside this `{}` loop",
Expand All @@ -130,13 +145,13 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
}
}

self.require_loop("break", e.span);
self.require_break_cx("break", e.span);
}
hir::ExprAgain(label) => {
if let Err(hir::LoopIdError::UnlabeledCfInWhileCondition) = label.target_id {
self.emit_unlabled_cf_in_while_condition(e.span, "continue");
}
self.require_loop("continue", e.span)
self.require_break_cx("continue", e.span)
},
_ => intravisit::walk_expr(self, e),
}
Expand All @@ -153,8 +168,9 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
self.cx = old_cx;
}

fn require_loop(&self, name: &str, span: Span) {
fn require_break_cx(&self, name: &str, span: Span) {
match self.cx {
LabeledBlock |
Loop(_) => {}
Closure => {
struct_span_err!(self.sess, span, E0267, "`{}` inside of a closure", name)
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_resolve/lib.rs
Expand Up @@ -3753,6 +3753,8 @@ impl<'a> Resolver<'a> {
self.ribs[ValueNS].pop();
}

ExprKind::Block(ref block, label) => self.resolve_labeled_block(label, block.id, block),

// Equivalent to `visit::walk_expr` + passing some context to children.
ExprKind::Field(ref subexpression, _) => {
self.resolve_expr(subexpression, Some(expr));
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/check/mod.rs
Expand Up @@ -4326,8 +4326,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
};

// In some cases, blocks have just one exit, but other blocks
// can be targeted by multiple breaks. This cannot happen in
// normal Rust syntax today, but it can happen when we desugar
// can be targeted by multiple breaks. This can happen both
// with labeled blocks as well as when we desugar
// a `do catch { ... }` expression.
//
// Example 1:
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/loop-break-value-no-repeat.stderr
Expand Up @@ -2,7 +2,7 @@ error[E0571]: `break` with value from a `for` loop
--> $DIR/loop-break-value-no-repeat.rs:22:9
|
LL | break 22 //~ ERROR `break` with value from a `for` loop
| ^^^^^^^^ can only break with a value inside `loop`
| ^^^^^^^^ can only break with a value inside `loop` or breakable block
help: instead, use `break` on its own without a value inside this `for` loop
|
LL | break //~ ERROR `break` with value from a `for` loop
Expand Down

0 comments on commit 5665980

Please sign in to comment.