Skip to content

Commit 0e325d0

Browse files
committed
Auto merge of #50045 - est31:label_break_value, r=eddyb
Implement label break value (RFC 2046) Implement label-break-value (#48594).
2 parents 2a3f536 + ae1553a commit 0e325d0

36 files changed

+510
-54
lines changed

src/librustc/cfg/construct.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
179179

180180
fn expr(&mut self, expr: &hir::Expr, pred: CFGIndex) -> CFGIndex {
181181
match expr.node {
182-
hir::ExprBlock(ref blk) => {
182+
hir::ExprBlock(ref blk, _) => {
183183
let blk_exit = self.block(&blk, pred);
184184
self.add_ast_node(expr.hir_id.local_id, &[blk_exit])
185185
}

src/librustc/hir/intravisit.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,10 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
10151015
expression.span,
10161016
expression.id)
10171017
}
1018-
ExprBlock(ref block) => visitor.visit_block(block),
1018+
ExprBlock(ref block, ref opt_label) => {
1019+
walk_list!(visitor, visit_label, opt_label);
1020+
visitor.visit_block(block);
1021+
}
10191022
ExprAssign(ref left_hand_expression, ref right_hand_expression) => {
10201023
visitor.visit_expr(right_hand_expression);
10211024
visitor.visit_expr(left_hand_expression)

src/librustc/hir/lowering.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3048,7 +3048,7 @@ impl<'a> LoweringContext<'a> {
30483048
);
30493049
block.expr = Some(this.wrap_in_try_constructor(
30503050
"from_ok", tail, unstable_span));
3051-
hir::ExprBlock(P(block))
3051+
hir::ExprBlock(P(block), None)
30523052
})
30533053
}
30543054
ExprKind::Match(ref expr, ref arms) => hir::ExprMatch(
@@ -3100,7 +3100,11 @@ impl<'a> LoweringContext<'a> {
31003100
})
31013101
})
31023102
}
3103-
ExprKind::Block(ref blk) => hir::ExprBlock(self.lower_block(blk, false)),
3103+
ExprKind::Block(ref blk, opt_label) => {
3104+
hir::ExprBlock(self.lower_block(blk,
3105+
opt_label.is_some()),
3106+
self.lower_label(opt_label))
3107+
}
31043108
ExprKind::Assign(ref el, ref er) => {
31053109
hir::ExprAssign(P(self.lower_expr(el)), P(self.lower_expr(er)))
31063110
}
@@ -3843,7 +3847,7 @@ impl<'a> LoweringContext<'a> {
38433847
}
38443848

38453849
fn expr_block(&mut self, b: P<hir::Block>, attrs: ThinVec<Attribute>) -> hir::Expr {
3846-
self.expr(b.span, hir::ExprBlock(b), attrs)
3850+
self.expr(b.span, hir::ExprBlock(b, None), attrs)
38473851
}
38483852

38493853
fn expr_tuple(&mut self, sp: Span, exprs: hir::HirVec<hir::Expr>) -> P<hir::Expr> {

src/librustc/hir/mod.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -778,9 +778,8 @@ pub struct Block {
778778
pub rules: BlockCheckMode,
779779
pub span: Span,
780780
/// If true, then there may exist `break 'a` values that aim to
781-
/// break out of this block early. As of this writing, this is not
782-
/// currently permitted in Rust itself, but it is generated as
783-
/// part of `catch` statements.
781+
/// break out of this block early.
782+
/// Used by `'label: {}` blocks and by `catch` statements.
784783
pub targeted_by_break: bool,
785784
/// If true, don't emit return value type errors as the parser had
786785
/// to recover from a parse error so this block will not have an
@@ -1381,8 +1380,8 @@ pub enum Expr_ {
13811380
/// This may also be a generator literal, indicated by the final boolean,
13821381
/// in that case there is an GeneratorClause.
13831382
ExprClosure(CaptureClause, P<FnDecl>, BodyId, Span, Option<GeneratorMovability>),
1384-
/// A block (`{ ... }`)
1385-
ExprBlock(P<Block>),
1383+
/// A block (`'label: { ... }`)
1384+
ExprBlock(P<Block>, Option<Label>),
13861385

13871386
/// An assignment (`a = foo()`)
13881387
ExprAssign(P<Expr>, P<Expr>),

src/librustc/hir/print.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ impl<'a> State<'a> {
10471047
self.print_else(e.as_ref().map(|e| &**e))
10481048
}
10491049
// "final else"
1050-
hir::ExprBlock(ref b) => {
1050+
hir::ExprBlock(ref b, _) => {
10511051
self.cbox(indent_unit - 1)?;
10521052
self.ibox(0)?;
10531053
self.s.word(" else ")?;
@@ -1377,7 +1377,11 @@ impl<'a> State<'a> {
13771377
// empty box to satisfy the close.
13781378
self.ibox(0)?;
13791379
}
1380-
hir::ExprBlock(ref blk) => {
1380+
hir::ExprBlock(ref blk, opt_label) => {
1381+
if let Some(label) = opt_label {
1382+
self.print_name(label.name)?;
1383+
self.word_space(":")?;
1384+
}
13811385
// containing cbox, will be closed by print-block at }
13821386
self.cbox(indent_unit)?;
13831387
// head-box, will be closed by print-block after {
@@ -1893,7 +1897,11 @@ impl<'a> State<'a> {
18931897
self.word_space("=>")?;
18941898

18951899
match arm.body.node {
1896-
hir::ExprBlock(ref blk) => {
1900+
hir::ExprBlock(ref blk, opt_label) => {
1901+
if let Some(label) = opt_label {
1902+
self.print_name(label.name)?;
1903+
self.word_space(":")?;
1904+
}
18971905
// the block will close the pattern's ibox
18981906
self.print_block_unclosed_indent(&blk, indent_unit)?;
18991907

@@ -2299,7 +2307,7 @@ fn expr_requires_semi_to_be_stmt(e: &hir::Expr) -> bool {
22992307
match e.node {
23002308
hir::ExprIf(..) |
23012309
hir::ExprMatch(..) |
2302-
hir::ExprBlock(_) |
2310+
hir::ExprBlock(..) |
23032311
hir::ExprWhile(..) |
23042312
hir::ExprLoop(..) => false,
23052313
_ => true,

src/librustc/ich/impls_hir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ impl_stable_hash_for!(enum hir::Expr_ {
589589
ExprLoop(body, label, loop_src),
590590
ExprMatch(matchee, arms, match_src),
591591
ExprClosure(capture_clause, decl, body_id, span, gen),
592-
ExprBlock(blk),
592+
ExprBlock(blk, label),
593593
ExprAssign(lhs, rhs),
594594
ExprAssignOp(op, lhs, rhs),
595595
ExprField(owner, field_name),

src/librustc/middle/expr_use_visitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
499499
self.consume_expr(&rhs);
500500
}
501501

502-
hir::ExprBlock(ref blk) => {
502+
hir::ExprBlock(ref blk, _) => {
503503
self.walk_block(&blk);
504504
}
505505

src/librustc/middle/liveness.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,9 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
11871187
succ
11881188
}
11891189

1190-
hir::ExprBlock(ref blk) => {
1190+
// Note that labels have been resolved, so we don't need to look
1191+
// at the label ident
1192+
hir::ExprBlock(ref blk, _) => {
11911193
self.propagate_through_block(&blk, succ)
11921194
}
11931195
}

src/librustc/middle/region.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1247,7 +1247,7 @@ fn resolve_local<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>,
12471247
hir::ExprCast(ref subexpr, _) => {
12481248
record_rvalue_scope_if_borrow_expr(visitor, &subexpr, blk_id)
12491249
}
1250-
hir::ExprBlock(ref block) => {
1250+
hir::ExprBlock(ref block, _) => {
12511251
if let Some(ref subexpr) = block.expr {
12521252
record_rvalue_scope_if_borrow_expr(
12531253
visitor, &subexpr, blk_id);

src/librustc_lint/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl UnsafeCode {
228228

229229
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnsafeCode {
230230
fn check_expr(&mut self, cx: &LateContext, e: &hir::Expr) {
231-
if let hir::ExprBlock(ref blk) = e.node {
231+
if let hir::ExprBlock(ref blk, _) = e.node {
232232
// Don't warn about generated blocks, that'll just pollute the output.
233233
if blk.rules == hir::UnsafeBlock(hir::UserProvided) {
234234
self.report_unsafe(cx, blk.span, "usage of an `unsafe` block");

0 commit comments

Comments
 (0)