Skip to content

Commit c6e5bb3

Browse files
committed
Do not suggest adding labeled block if there are no labeled breaks
1 parent f21c0a2 commit c6e5bb3

File tree

5 files changed

+33
-24
lines changed

5 files changed

+33
-24
lines changed

compiler/rustc_parse/src/parser/expr.rs

+23
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc_ast::tokenstream::Spacing;
1313
use rustc_ast::util::classify;
1414
use rustc_ast::util::literal::LitError;
1515
use rustc_ast::util::parser::{prec_let_scrutinee_needs_par, AssocOp, Fixity};
16+
use rustc_ast::visit::Visitor;
1617
use rustc_ast::StmtKind;
1718
use rustc_ast::{self as ast, AttrStyle, AttrVec, CaptureBy, ExprField, Lit, UnOp, DUMMY_NODE_ID};
1819
use rustc_ast::{AnonConst, BinOp, BinOpKind, FnDecl, FnRetTy, MacCall, Param, Ty, TyKind};
@@ -1556,6 +1557,28 @@ impl<'a> Parser<'a> {
15561557

15571558
// Continue as an expression in an effort to recover on `'label: non_block_expr`.
15581559
let expr = self.parse_expr().map(|expr| {
1560+
let found_labeled_breaks = {
1561+
struct FindLabeledBreaksVisitor(bool);
1562+
1563+
impl<'ast> Visitor<'ast> for FindLabeledBreaksVisitor {
1564+
fn visit_expr_post(&mut self, ex: &'ast Expr) {
1565+
if let ExprKind::Break(Some(_label), _) = ex.kind {
1566+
self.0 = true;
1567+
}
1568+
}
1569+
}
1570+
1571+
let mut vis = FindLabeledBreaksVisitor(false);
1572+
vis.visit_expr(&expr);
1573+
vis.0
1574+
};
1575+
1576+
// Suggestion involves adding a (as of time of writing this, unstable) labeled block
1577+
// so if the label is not used, just return the unmodified expression
1578+
if !found_labeled_breaks {
1579+
return expr;
1580+
}
1581+
15591582
let span = expr.span;
15601583
let sugg_msg = "consider enclosing expression in a block";
15611584
let suggestions = vec![

src/test/ui/parser/labeled-no-colon-expr.stderr

-5
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@ error: expected `while`, `for`, `loop` or `{` after a label
4747
|
4848
LL | 'l4 0;
4949
| ^ expected `while`, `for`, `loop` or `{` after a label
50-
|
51-
help: consider enclosing expression in a block
52-
|
53-
LL | 'l4 {0};
54-
| + +
5550

5651
error: labeled expression must be followed by `:`
5752
--> $DIR/labeled-no-colon-expr.rs:8:9

src/test/ui/parser/recover-labeled-non-block-expr.fixed

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// run-rustfix
22
#![feature(label_break_value)]
33
fn main() {
4-
#[allow(unused_labels)]
5-
'label: {1 + 1}; //~ ERROR expected `while`, `for`, `loop` or `{` after a label
4+
// FIXME(waffle): add this back
5+
// #[allow(unused_labels)]
6+
// 'label: 1 + 1; // ERROR expected `while`, `for`, `loop` or `{` after a label
67

78
'label: {match () { () => break 'label, }}; //~ ERROR expected `while`, `for`, `loop` or `{` after a label
89

src/test/ui/parser/recover-labeled-non-block-expr.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// run-rustfix
22
#![feature(label_break_value)]
33
fn main() {
4-
#[allow(unused_labels)]
5-
'label: 1 + 1; //~ ERROR expected `while`, `for`, `loop` or `{` after a label
4+
// FIXME(waffle): add this back
5+
// #[allow(unused_labels)]
6+
// 'label: 1 + 1; // ERROR expected `while`, `for`, `loop` or `{` after a label
67

78
'label: match () { () => break 'label, }; //~ ERROR expected `while`, `for`, `loop` or `{` after a label
89

Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
error: expected `while`, `for`, `loop` or `{` after a label
2-
--> $DIR/recover-labeled-non-block-expr.rs:5:13
3-
|
4-
LL | 'label: 1 + 1;
5-
| ^ expected `while`, `for`, `loop` or `{` after a label
6-
|
7-
help: consider enclosing expression in a block
8-
|
9-
LL | 'label: {1 + 1};
10-
| + +
11-
12-
error: expected `while`, `for`, `loop` or `{` after a label
13-
--> $DIR/recover-labeled-non-block-expr.rs:7:13
2+
--> $DIR/recover-labeled-non-block-expr.rs:8:13
143
|
154
LL | 'label: match () { () => break 'label, };
165
| ^^^^^ expected `while`, `for`, `loop` or `{` after a label
@@ -21,7 +10,7 @@ LL | 'label: {match () { () => break 'label, }};
2110
| + +
2211

2312
error: expected `while`, `for`, `loop` or `{` after a label
24-
--> $DIR/recover-labeled-non-block-expr.rs:10:22
13+
--> $DIR/recover-labeled-non-block-expr.rs:11:22
2514
|
2615
LL | let _i = 'label: match x {
2716
| ^^^^^ expected `while`, `for`, `loop` or `{` after a label
@@ -37,7 +26,7 @@ LL | break 'label 13
3726
...
3827

3928
error: expected `while`, `for`, `loop` or `{` after a label
40-
--> $DIR/recover-labeled-non-block-expr.rs:24:24
29+
--> $DIR/recover-labeled-non-block-expr.rs:25:24
4130
|
4231
LL | let _val = 'label: (1, if other == 3 { break 'label (2, 3) } else { other });
4332
| ^ expected `while`, `for`, `loop` or `{` after a label
@@ -47,5 +36,5 @@ help: consider enclosing expression in a block
4736
LL | let _val = 'label: {(1, if other == 3 { break 'label (2, 3) } else { other })};
4837
| + +
4938

50-
error: aborting due to 4 previous errors
39+
error: aborting due to 3 previous errors
5140

0 commit comments

Comments
 (0)