X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=compiler%2Frustc_parse%2Fsrc%2Fparser%2Fstmt.rs;h=f18f7222b7ab62c2b7c16fec29dd8e2738dd2a38;hb=21fdd549f63499a6f15160c22175cc9c3bbeb473;hp=5b7ae5f7a7b822f106aa7084543c19db28e9d85f;hpb=2ad701e45036fb2ccab8d4b4e23f9a3325e12817;p=rust.git diff --git a/compiler/rustc_parse/src/parser/stmt.rs b/compiler/rustc_parse/src/parser/stmt.rs index 5b7ae5f7a7b..f18f7222b7a 100644 --- a/compiler/rustc_parse/src/parser/stmt.rs +++ b/compiler/rustc_parse/src/parser/stmt.rs @@ -11,7 +11,7 @@ use rustc_ast as ast; use rustc_ast::ptr::P; -use rustc_ast::token::{self, TokenKind}; +use rustc_ast::token::{self, Delimiter, TokenKind}; use rustc_ast::util::classify; use rustc_ast::{ AstLike, AttrStyle, AttrVec, Attribute, LocalKind, MacCall, MacCallStmt, MacStmtStyle, @@ -92,7 +92,7 @@ pub fn parse_stmt(&mut self, force_collect: ForceCollect) -> PResult<'a, Option< // Do not attempt to parse an expression if we're done here. self.error_outer_attrs(&attrs.take_for_recovery()); self.mk_stmt(lo, StmtKind::Empty) - } else if self.token != token::CloseDelim(token::Brace) { + } else if self.token != token::CloseDelim(Delimiter::Brace) { // Remainder are line-expr stmts. let e = if force_collect == ForceCollect::Yes { self.collect_tokens_no_attrs(|this| { @@ -131,7 +131,7 @@ fn parse_stmt_path_start(&mut self, lo: Span, attrs: AttrWrapper) -> PResult<'a, } } - let expr = if this.eat(&token::OpenDelim(token::Brace)) { + let expr = if this.eat(&token::OpenDelim(Delimiter::Brace)) { this.parse_struct_expr(None, path, AttrVec::new(), true)? } else { let hi = this.prev_token.span; @@ -164,25 +164,29 @@ fn parse_stmt_mac(&mut self, lo: Span, attrs: AttrVec, path: ast::Path) -> PResu let delim = args.delim(); let hi = self.prev_token.span; - let style = - if delim == token::Brace { MacStmtStyle::Braces } else { MacStmtStyle::NoBraces }; + let style = match delim { + Some(Delimiter::Brace) => MacStmtStyle::Braces, + Some(_) => MacStmtStyle::NoBraces, + None => unreachable!(), + }; let mac = MacCall { path, args, prior_type_ascription: self.last_type_ascription }; - let kind = - if (delim == token::Brace && self.token != token::Dot && self.token != token::Question) - || self.token == token::Semi - || self.token == token::Eof - { - StmtKind::MacCall(P(MacCallStmt { mac, style, attrs, tokens: None })) - } else { - // Since none of the above applied, this is an expression statement macro. - let e = self.mk_expr(lo.to(hi), ExprKind::MacCall(mac), AttrVec::new()); - let e = self.maybe_recover_from_bad_qpath(e, true)?; - let e = self.parse_dot_or_call_expr_with(e, lo, attrs.into())?; - let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e))?; - StmtKind::Expr(e) - }; + let kind = if (style == MacStmtStyle::Braces + && self.token != token::Dot + && self.token != token::Question) + || self.token == token::Semi + || self.token == token::Eof + { + StmtKind::MacCall(P(MacCallStmt { mac, style, attrs, tokens: None })) + } else { + // Since none of the above applied, this is an expression statement macro. + let e = self.mk_expr(lo.to(hi), ExprKind::MacCall(mac), AttrVec::new()); + let e = self.maybe_recover_from_bad_qpath(e, true)?; + let e = self.parse_dot_or_call_expr_with(e, lo, attrs.into())?; + let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e))?; + StmtKind::Expr(e) + }; Ok(self.mk_stmt(lo.to(hi), kind)) } @@ -258,7 +262,10 @@ fn parse_local(&mut self, attrs: AttrVec) -> PResult<'a, P> { if let Ok(snip) = self.span_to_snippet(pat.span) { err.span_label(pat.span, format!("while parsing the type for `{}`", snip)); } - let err = if self.check(&token::Eq) { + // we use noexpect here because we don't actually expect Eq to be here + // but we are still checking for it in order to be able to handle it if + // it is there + let err = if self.check_noexpect(&token::Eq) { err.emit(); None } else { @@ -430,7 +437,7 @@ fn error_block_no_opening_brace_msg( // If the next token is an open brace (e.g., `if a b {`), the place- // inside-a-block suggestion would be more likely wrong than right. Ok(Some(_)) - if self.look_ahead(1, |t| t == &token::OpenDelim(token::Brace)) + if self.look_ahead(1, |t| t == &token::OpenDelim(Delimiter::Brace)) || do_not_suggest_help => {} // Do not suggest `if foo println!("") {;}` (as would be seen in test for #46836). Ok(Some(Stmt { kind: StmtKind::Empty, .. })) => {} @@ -484,7 +491,7 @@ pub(super) fn parse_block_common( maybe_whole!(self, NtBlock, |x| (Vec::new(), x)); self.maybe_recover_unexpected_block_label(); - if !self.eat(&token::OpenDelim(token::Brace)) { + if !self.eat(&token::OpenDelim(Delimiter::Brace)) { return self.error_block_no_opening_brace(); } @@ -505,7 +512,7 @@ pub(super) fn parse_block_common( recover: AttemptLocalParseRecovery, ) -> PResult<'a, P> { let mut stmts = vec![]; - while !self.eat(&token::CloseDelim(token::Brace)) { + while !self.eat(&token::CloseDelim(Delimiter::Brace)) { if self.token == token::Eof { break; } @@ -549,7 +556,7 @@ pub fn parse_full_stmt( { // Just check for errors and recover; do not eat semicolon yet. if let Err(mut e) = - self.expect_one_of(&[], &[token::Semi, token::CloseDelim(token::Brace)]) + self.expect_one_of(&[], &[token::Semi, token::CloseDelim(Delimiter::Brace)]) { if let TokenKind::DocComment(..) = self.token.kind { if let Ok(snippet) = self.span_to_snippet(self.token.span) {