From 9596dc2a47861d73996a550cba7caf55b2737c17 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Thu, 5 Mar 2020 01:47:15 +0100 Subject: parse_labeled_expr: simplify --- src/librustc_parse/parser/expr.rs | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) (limited to 'src/librustc_parse/parser/expr.rs') diff --git a/src/librustc_parse/parser/expr.rs b/src/librustc_parse/parser/expr.rs index 16ea2773b20..e0e6fd5eed6 100644 --- a/src/librustc_parse/parser/expr.rs +++ b/src/librustc_parse/parser/expr.rs @@ -1069,24 +1069,22 @@ impl<'a> Parser<'a> { fn parse_labeled_expr(&mut self, label: Label, attrs: AttrVec) -> PResult<'a, P> { let lo = label.ident.span; + let label = Some(label); self.expect(&token::Colon)?; if self.eat_keyword(kw::While) { - return self.parse_while_expr(Some(label), lo, attrs); - } - if self.eat_keyword(kw::For) { - return self.parse_for_expr(Some(label), lo, attrs); - } - if self.eat_keyword(kw::Loop) { - return self.parse_loop_expr(Some(label), lo, attrs); - } - if self.token == token::OpenDelim(token::Brace) { - return self.parse_block_expr(Some(label), lo, BlockCheckMode::Default, attrs); + self.parse_while_expr(label, lo, attrs) + } else if self.eat_keyword(kw::For) { + self.parse_for_expr(label, lo, attrs) + } else if self.eat_keyword(kw::Loop) { + self.parse_loop_expr(label, lo, attrs) + } else if self.check(&token::OpenDelim(token::Brace)) { + self.parse_block_expr(label, lo, BlockCheckMode::Default, attrs) + } else { + let msg = "expected `while`, `for`, `loop` or `{` after a label"; + self.struct_span_err(self.token.span, msg).span_label(self.token.span, msg).emit(); + // Continue as an expression in an effort to recover on `'label: non_block_expr`. + self.parse_expr() } - - let msg = "expected `while`, `for`, `loop` or `{` after a label"; - self.struct_span_err(self.token.span, msg).span_label(self.token.span, msg).emit(); - // Continue as an expression in an effort to recover on `'label: non_block_expr`. - self.parse_expr() } /// Recover on the syntax `do catch { ... }` suggesting `try { ... }` instead. -- cgit 1.4.1-3-g733a5 From 8ee220c447911c519ebbd118e1415d961317b18d Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Thu, 5 Mar 2020 05:49:30 +0100 Subject: more reuse in block parsing & improve diagnostics. --- src/librustc_ast/token.rs | 10 ++++++ src/librustc_parse/parser/expr.rs | 16 +++++---- src/librustc_parse/parser/stmt.rs | 17 ++++++---- .../ui/label/label_break_value_illegal_uses.stderr | 5 ++- src/test/ui/parser/bad-interpolated-block.rs | 15 +++++++++ src/test/ui/parser/bad-interpolated-block.stderr | 39 ++++++++++++++++++++++ src/test/ui/parser/closure-return-syntax.rs | 2 +- src/test/ui/parser/closure-return-syntax.stderr | 7 ++-- .../ui/unsafe/unsafe-block-without-braces.stderr | 7 ++-- 9 files changed, 98 insertions(+), 20 deletions(-) create mode 100644 src/test/ui/parser/bad-interpolated-block.rs create mode 100644 src/test/ui/parser/bad-interpolated-block.stderr (limited to 'src/librustc_parse/parser/expr.rs') diff --git a/src/librustc_ast/token.rs b/src/librustc_ast/token.rs index b67b7d346f7..3fc6444168e 100644 --- a/src/librustc_ast/token.rs +++ b/src/librustc_ast/token.rs @@ -535,6 +535,16 @@ impl Token { false } + // Is the token an interpolated block (`$b:block`)? + pub fn is_whole_block(&self) -> bool { + if let Interpolated(ref nt) = self.kind { + if let NtBlock(..) = **nt { + return true; + } + } + false + } + /// Returns `true` if the token is either the `mut` or `const` keyword. pub fn is_mutability(&self) -> bool { self.is_keyword(kw::Mut) || self.is_keyword(kw::Const) diff --git a/src/librustc_parse/parser/expr.rs b/src/librustc_parse/parser/expr.rs index e0e6fd5eed6..b1c5eaf8973 100644 --- a/src/librustc_parse/parser/expr.rs +++ b/src/librustc_parse/parser/expr.rs @@ -1077,7 +1077,7 @@ impl<'a> Parser<'a> { self.parse_for_expr(label, lo, attrs) } else if self.eat_keyword(kw::Loop) { self.parse_loop_expr(label, lo, attrs) - } else if self.check(&token::OpenDelim(token::Brace)) { + } else if self.check(&token::OpenDelim(token::Brace)) || self.token.is_whole_block() { self.parse_block_expr(label, lo, BlockCheckMode::Default, attrs) } else { let msg = "expected `while`, `for`, `loop` or `{` after a label"; @@ -1361,18 +1361,20 @@ impl<'a> Parser<'a> { opt_label: Option