From 8ee220c447911c519ebbd118e1415d961317b18d Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Thu, 5 Mar 2020 05:49:30 +0100 Subject: [PATCH] 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 +++++--- .../label_break_value_illegal_uses.stderr | 5 ++- src/test/ui/parser/bad-interpolated-block.rs | 15 +++++++ .../ui/parser/bad-interpolated-block.stderr | 39 +++++++++++++++++++ src/test/ui/parser/closure-return-syntax.rs | 2 +- .../ui/parser/closure-return-syntax.stderr | 7 +++- .../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 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