From aaf3e318fc33edcc6dbf0920998c3f27d00dd818 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Wed, 13 Dec 2017 23:05:49 -0800 Subject: Do not emit type errors on recovered blocks When a parse error occurs on a block, the parser will recover and create a block with the statements collected until that point. Now a flag stating that a recovery has been performed in this block is propagated so that the type checker knows that the type of the block (which will be identified as `()`) shouldn't be checked against the expectation to reduce the amount of irrelevant diagnostic errors shown to the user. --- src/libsyntax/parse/mod.rs | 1 + src/libsyntax/parse/parser.rs | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 89a54989f96..f96c7e8598f 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -931,6 +931,7 @@ mod tests { id: ast::DUMMY_NODE_ID, rules: ast::BlockCheckMode::Default, // no idea span: sp(15,21), + recovered: false, })), vis: ast::Visibility::Inherited, span: sp(0,21)}))); diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index d9434539246..bb46cef5ee2 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -4371,13 +4371,15 @@ impl<'a> Parser<'a> { /// Precondition: already parsed the '{'. fn parse_block_tail(&mut self, lo: Span, s: BlockCheckMode) -> PResult<'a, P> { let mut stmts = vec![]; + let mut recovered = false; while !self.eat(&token::CloseDelim(token::Brace)) { let stmt = match self.parse_full_stmt(false) { Err(mut err) => { err.emit(); - self.recover_stmt_(SemiColonMode::Ignore, BlockMode::Break); + self.recover_stmt_(SemiColonMode::Ignore, BlockMode::Ignore); self.eat(&token::CloseDelim(token::Brace)); + recovered = true; break; } Ok(stmt) => stmt, @@ -4396,12 +4398,13 @@ impl<'a> Parser<'a> { id: ast::DUMMY_NODE_ID, rules: s, span: lo.to(self.prev_span), + recovered, })) } /// Parse a statement, including the trailing semicolon. pub fn parse_full_stmt(&mut self, macro_legacy_warnings: bool) -> PResult<'a, Option> { - let mut stmt = match self.parse_stmt_(macro_legacy_warnings) { + let mut stmt = match self.parse_stmt_without_recovery(macro_legacy_warnings)? { Some(stmt) => stmt, None => return Ok(None), }; -- cgit 1.4.1-3-g733a5