From cdca5cfbfd0679645f91031b2619aac42cb5f68a Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Tue, 3 Dec 2019 07:40:18 +0100 Subject: [PATCH] parser: extract error_outer_attrs --- src/librustc_parse/parser/stmt.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/librustc_parse/parser/stmt.rs b/src/librustc_parse/parser/stmt.rs index a51d663b1a7..c5c3683057e 100644 --- a/src/librustc_parse/parser/stmt.rs +++ b/src/librustc_parse/parser/stmt.rs @@ -90,21 +90,9 @@ fn parse_stmt_without_recovery( return Ok(Some(self.mk_stmt(lo.to(item.span), StmtKind::Item(item)))); } - let unused_attrs = |attrs: &[Attribute], s: &mut Self| { - if !attrs.is_empty() { - if s.prev_token_kind == PrevTokenKind::DocComment { - s.span_fatal_err(s.prev_span, Error::UselessDocComment).emit(); - } else if attrs.iter().any(|a| a.style == AttrStyle::Outer) { - s.span_err( - s.token.span, "expected statement after outer attribute" - ); - } - } - }; - // Do not attempt to parse an expression if we're done here. if self.token == token::Semi { - unused_attrs(&attrs, self); + self.error_outer_attrs(&attrs); self.bump(); let mut last_semi = lo; while self.token == token::Semi { @@ -122,7 +110,7 @@ fn parse_stmt_without_recovery( } if self.token == token::CloseDelim(token::Brace) { - unused_attrs(&attrs, self); + self.error_outer_attrs(&attrs); return Ok(None); } @@ -190,6 +178,18 @@ fn parse_stmt_mac( Ok(Some(self.mk_stmt(lo.to(hi), kind))) } + /// Error on outer attributes in this context. + /// Also error if the previous token was a doc comment. + fn error_outer_attrs(&self, attrs: &[Attribute]) { + if !attrs.is_empty() { + if self.prev_token_kind == PrevTokenKind::DocComment { + self.span_fatal_err(self.prev_span, Error::UselessDocComment).emit(); + } else if attrs.iter().any(|a| a.style == AttrStyle::Outer) { + self.span_err(self.token.span, "expected statement after outer attribute"); + } + } + } + /// Parses a local variable declaration. fn parse_local(&mut self, attrs: ThinVec) -> PResult<'a, P> { let lo = self.prev_span;