Do not suggest adding labeled block if there are no labeled breaks
This commit is contained in:
parent
f21c0a274e
commit
c6e5bb32fb
5 changed files with 33 additions and 24 deletions
|
@ -13,6 +13,7 @@ use rustc_ast::tokenstream::Spacing;
|
||||||
use rustc_ast::util::classify;
|
use rustc_ast::util::classify;
|
||||||
use rustc_ast::util::literal::LitError;
|
use rustc_ast::util::literal::LitError;
|
||||||
use rustc_ast::util::parser::{prec_let_scrutinee_needs_par, AssocOp, Fixity};
|
use rustc_ast::util::parser::{prec_let_scrutinee_needs_par, AssocOp, Fixity};
|
||||||
|
use rustc_ast::visit::Visitor;
|
||||||
use rustc_ast::StmtKind;
|
use rustc_ast::StmtKind;
|
||||||
use rustc_ast::{self as ast, AttrStyle, AttrVec, CaptureBy, ExprField, Lit, UnOp, DUMMY_NODE_ID};
|
use rustc_ast::{self as ast, AttrStyle, AttrVec, CaptureBy, ExprField, Lit, UnOp, DUMMY_NODE_ID};
|
||||||
use rustc_ast::{AnonConst, BinOp, BinOpKind, FnDecl, FnRetTy, MacCall, Param, Ty, TyKind};
|
use rustc_ast::{AnonConst, BinOp, BinOpKind, FnDecl, FnRetTy, MacCall, Param, Ty, TyKind};
|
||||||
|
@ -1556,6 +1557,28 @@ impl<'a> Parser<'a> {
|
||||||
|
|
||||||
// Continue as an expression in an effort to recover on `'label: non_block_expr`.
|
// Continue as an expression in an effort to recover on `'label: non_block_expr`.
|
||||||
let expr = self.parse_expr().map(|expr| {
|
let expr = self.parse_expr().map(|expr| {
|
||||||
|
let found_labeled_breaks = {
|
||||||
|
struct FindLabeledBreaksVisitor(bool);
|
||||||
|
|
||||||
|
impl<'ast> Visitor<'ast> for FindLabeledBreaksVisitor {
|
||||||
|
fn visit_expr_post(&mut self, ex: &'ast Expr) {
|
||||||
|
if let ExprKind::Break(Some(_label), _) = ex.kind {
|
||||||
|
self.0 = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut vis = FindLabeledBreaksVisitor(false);
|
||||||
|
vis.visit_expr(&expr);
|
||||||
|
vis.0
|
||||||
|
};
|
||||||
|
|
||||||
|
// Suggestion involves adding a (as of time of writing this, unstable) labeled block
|
||||||
|
// so if the label is not used, just return the unmodified expression
|
||||||
|
if !found_labeled_breaks {
|
||||||
|
return expr;
|
||||||
|
}
|
||||||
|
|
||||||
let span = expr.span;
|
let span = expr.span;
|
||||||
let sugg_msg = "consider enclosing expression in a block";
|
let sugg_msg = "consider enclosing expression in a block";
|
||||||
let suggestions = vec![
|
let suggestions = vec![
|
||||||
|
|
|
@ -47,11 +47,6 @@ error: expected `while`, `for`, `loop` or `{` after a label
|
||||||
|
|
|
|
||||||
LL | 'l4 0;
|
LL | 'l4 0;
|
||||||
| ^ expected `while`, `for`, `loop` or `{` after a label
|
| ^ expected `while`, `for`, `loop` or `{` after a label
|
||||||
|
|
|
||||||
help: consider enclosing expression in a block
|
|
||||||
|
|
|
||||||
LL | 'l4 {0};
|
|
||||||
| + +
|
|
||||||
|
|
||||||
error: labeled expression must be followed by `:`
|
error: labeled expression must be followed by `:`
|
||||||
--> $DIR/labeled-no-colon-expr.rs:8:9
|
--> $DIR/labeled-no-colon-expr.rs:8:9
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
// run-rustfix
|
// run-rustfix
|
||||||
#![feature(label_break_value)]
|
#![feature(label_break_value)]
|
||||||
fn main() {
|
fn main() {
|
||||||
#[allow(unused_labels)]
|
// FIXME(waffle): add this back
|
||||||
'label: {1 + 1}; //~ ERROR expected `while`, `for`, `loop` or `{` after a label
|
// #[allow(unused_labels)]
|
||||||
|
// 'label: 1 + 1; // ERROR expected `while`, `for`, `loop` or `{` after a label
|
||||||
|
|
||||||
'label: {match () { () => break 'label, }}; //~ ERROR expected `while`, `for`, `loop` or `{` after a label
|
'label: {match () { () => break 'label, }}; //~ ERROR expected `while`, `for`, `loop` or `{` after a label
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
// run-rustfix
|
// run-rustfix
|
||||||
#![feature(label_break_value)]
|
#![feature(label_break_value)]
|
||||||
fn main() {
|
fn main() {
|
||||||
#[allow(unused_labels)]
|
// FIXME(waffle): add this back
|
||||||
'label: 1 + 1; //~ ERROR expected `while`, `for`, `loop` or `{` after a label
|
// #[allow(unused_labels)]
|
||||||
|
// 'label: 1 + 1; // ERROR expected `while`, `for`, `loop` or `{` after a label
|
||||||
|
|
||||||
'label: match () { () => break 'label, }; //~ ERROR expected `while`, `for`, `loop` or `{` after a label
|
'label: match () { () => break 'label, }; //~ ERROR expected `while`, `for`, `loop` or `{` after a label
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,5 @@
|
||||||
error: expected `while`, `for`, `loop` or `{` after a label
|
error: expected `while`, `for`, `loop` or `{` after a label
|
||||||
--> $DIR/recover-labeled-non-block-expr.rs:5:13
|
--> $DIR/recover-labeled-non-block-expr.rs:8:13
|
||||||
|
|
|
||||||
LL | 'label: 1 + 1;
|
|
||||||
| ^ expected `while`, `for`, `loop` or `{` after a label
|
|
||||||
|
|
|
||||||
help: consider enclosing expression in a block
|
|
||||||
|
|
|
||||||
LL | 'label: {1 + 1};
|
|
||||||
| + +
|
|
||||||
|
|
||||||
error: expected `while`, `for`, `loop` or `{` after a label
|
|
||||||
--> $DIR/recover-labeled-non-block-expr.rs:7:13
|
|
||||||
|
|
|
|
||||||
LL | 'label: match () { () => break 'label, };
|
LL | 'label: match () { () => break 'label, };
|
||||||
| ^^^^^ expected `while`, `for`, `loop` or `{` after a label
|
| ^^^^^ expected `while`, `for`, `loop` or `{` after a label
|
||||||
|
@ -21,7 +10,7 @@ LL | 'label: {match () { () => break 'label, }};
|
||||||
| + +
|
| + +
|
||||||
|
|
||||||
error: expected `while`, `for`, `loop` or `{` after a label
|
error: expected `while`, `for`, `loop` or `{` after a label
|
||||||
--> $DIR/recover-labeled-non-block-expr.rs:10:22
|
--> $DIR/recover-labeled-non-block-expr.rs:11:22
|
||||||
|
|
|
|
||||||
LL | let _i = 'label: match x {
|
LL | let _i = 'label: match x {
|
||||||
| ^^^^^ expected `while`, `for`, `loop` or `{` after a label
|
| ^^^^^ expected `while`, `for`, `loop` or `{` after a label
|
||||||
|
@ -37,7 +26,7 @@ LL | break 'label 13
|
||||||
...
|
...
|
||||||
|
|
||||||
error: expected `while`, `for`, `loop` or `{` after a label
|
error: expected `while`, `for`, `loop` or `{` after a label
|
||||||
--> $DIR/recover-labeled-non-block-expr.rs:24:24
|
--> $DIR/recover-labeled-non-block-expr.rs:25:24
|
||||||
|
|
|
|
||||||
LL | let _val = 'label: (1, if other == 3 { break 'label (2, 3) } else { other });
|
LL | let _val = 'label: (1, if other == 3 { break 'label (2, 3) } else { other });
|
||||||
| ^ expected `while`, `for`, `loop` or `{` after a label
|
| ^ expected `while`, `for`, `loop` or `{` after a label
|
||||||
|
@ -47,5 +36,5 @@ help: consider enclosing expression in a block
|
||||||
LL | let _val = 'label: {(1, if other == 3 { break 'label (2, 3) } else { other })};
|
LL | let _val = 'label: {(1, if other == 3 { break 'label (2, 3) } else { other })};
|
||||||
| + +
|
| + +
|
||||||
|
|
||||||
error: aborting due to 4 previous errors
|
error: aborting due to 3 previous errors
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue