2022-08-31 06:20:59 -05:00
|
|
|
use super::attr::InnerAttrForbiddenReason;
|
2022-09-01 12:29:23 -05:00
|
|
|
use super::diagnostics::AttemptLocalParseRecovery;
|
2019-08-11 13:32:29 -05:00
|
|
|
use super::expr::LhsExpr;
|
2020-11-21 15:12:05 -06:00
|
|
|
use super::pat::RecoverComma;
|
2019-10-08 02:46:06 -05:00
|
|
|
use super::path::PathStyle;
|
2021-02-13 11:42:43 -06:00
|
|
|
use super::TrailingToken;
|
2021-12-04 12:05:30 -06:00
|
|
|
use super::{
|
|
|
|
AttrWrapper, BlockMode, FnParseMode, ForceCollect, Parser, Restrictions, SemiColonMode,
|
|
|
|
};
|
2022-09-01 12:29:23 -05:00
|
|
|
use crate::errors::{
|
|
|
|
AssignmentElseNotAllowed, CompoundAssignmentExpressionInLet, ConstLetMutuallyExclusive,
|
|
|
|
DocCommentDoesNotDocumentAnything, ExpectedStatementAfterOuterAttr, InvalidCurlyInLetElse,
|
2022-11-11 14:22:47 -06:00
|
|
|
InvalidExpressionInLetElse, InvalidIdentiferStartsWithNumber, InvalidVariableDeclaration,
|
|
|
|
InvalidVariableDeclarationSub, WrapExpressionInParentheses,
|
2022-09-01 12:29:23 -05:00
|
|
|
};
|
2021-01-22 12:28:08 -06:00
|
|
|
use crate::maybe_whole;
|
2019-08-11 13:32:29 -05:00
|
|
|
|
2020-04-27 12:56:11 -05:00
|
|
|
use rustc_ast as ast;
|
2020-02-29 11:37:32 -06:00
|
|
|
use rustc_ast::ptr::P;
|
2022-04-26 07:40:14 -05:00
|
|
|
use rustc_ast::token::{self, Delimiter, TokenKind};
|
2020-02-29 11:37:32 -06:00
|
|
|
use rustc_ast::util::classify;
|
2022-11-13 07:08:58 -06:00
|
|
|
use rustc_ast::{AttrStyle, AttrVec, LocalKind, MacCall, MacCallStmt, MacStmtStyle};
|
2022-05-01 12:58:24 -05:00
|
|
|
use rustc_ast::{Block, BlockCheckMode, Expr, ExprKind, HasAttrs, Local, Stmt};
|
2021-02-12 18:04:37 -06:00
|
|
|
use rustc_ast::{StmtKind, DUMMY_NODE_ID};
|
2022-01-23 12:34:26 -06:00
|
|
|
use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed, PResult};
|
2020-01-31 05:31:51 -06:00
|
|
|
use rustc_span::source_map::{BytePos, Span};
|
2020-01-31 23:18:10 -06:00
|
|
|
use rustc_span::symbol::{kw, sym};
|
2019-08-11 13:32:29 -05:00
|
|
|
|
|
|
|
use std::mem;
|
|
|
|
|
|
|
|
impl<'a> Parser<'a> {
|
2019-09-05 21:56:45 -05:00
|
|
|
/// Parses a statement. This stops just before trailing semicolons on everything but items.
|
2019-08-11 13:32:29 -05:00
|
|
|
/// e.g., a `StmtKind::Semi` parses to a `StmtKind::Expr`, leaving the trailing `;` unconsumed.
|
2020-08-30 13:04:36 -05:00
|
|
|
// Public for rustfmt usage.
|
2021-01-18 15:47:37 -06:00
|
|
|
pub fn parse_stmt(&mut self, force_collect: ForceCollect) -> PResult<'a, Option<Stmt>> {
|
2021-01-14 09:42:01 -06:00
|
|
|
Ok(self.parse_stmt_without_recovery(false, force_collect).unwrap_or_else(|mut e| {
|
2019-08-11 13:32:29 -05:00
|
|
|
e.emit();
|
|
|
|
self.recover_stmt_(SemiColonMode::Break, BlockMode::Ignore);
|
|
|
|
None
|
2019-12-02 22:39:00 -06:00
|
|
|
}))
|
2019-08-11 13:32:29 -05:00
|
|
|
}
|
|
|
|
|
2022-08-17 04:18:48 -05:00
|
|
|
/// If `force_collect` is [`ForceCollect::Yes`], forces collection of tokens regardless of whether
|
2021-01-18 15:47:37 -06:00
|
|
|
/// or not we have attributes
|
2022-05-20 18:51:09 -05:00
|
|
|
pub(crate) fn parse_stmt_without_recovery(
|
2021-01-18 15:47:37 -06:00
|
|
|
&mut self,
|
2021-01-14 09:42:01 -06:00
|
|
|
capture_semi: bool,
|
2021-01-18 15:47:37 -06:00
|
|
|
force_collect: ForceCollect,
|
|
|
|
) -> PResult<'a, Option<Stmt>> {
|
2021-01-22 12:28:08 -06:00
|
|
|
let attrs = self.parse_outer_attributes()?;
|
2019-08-11 13:32:29 -05:00
|
|
|
let lo = self.token.span;
|
|
|
|
|
2021-01-22 12:28:08 -06:00
|
|
|
// Don't use `maybe_whole` so that we have precise control
|
|
|
|
// over when we bump the parser
|
2022-02-28 04:49:56 -06:00
|
|
|
if let token::Interpolated(nt) = &self.token.kind && let token::NtStmt(stmt) = &**nt {
|
|
|
|
let mut stmt = stmt.clone();
|
|
|
|
self.bump();
|
|
|
|
stmt.visit_attrs(|stmt_attrs| {
|
|
|
|
attrs.prepend_to_nt_inner(stmt_attrs);
|
|
|
|
});
|
2022-04-05 21:08:39 -05:00
|
|
|
return Ok(Some(stmt.into_inner()));
|
2021-01-22 12:28:08 -06:00
|
|
|
}
|
2020-11-17 13:27:44 -06:00
|
|
|
|
2022-08-07 19:19:37 -05:00
|
|
|
if self.token.is_keyword(kw::Mut) && self.is_keyword_ahead(1, &[kw::Let]) {
|
|
|
|
self.bump();
|
|
|
|
let mut_let_span = lo.to(self.token.span);
|
2022-08-17 03:06:24 -05:00
|
|
|
self.sess.emit_err(InvalidVariableDeclaration {
|
|
|
|
span: mut_let_span,
|
|
|
|
sub: InvalidVariableDeclarationSub::SwitchMutLetOrder(mut_let_span),
|
|
|
|
});
|
2022-08-07 19:19:37 -05:00
|
|
|
}
|
|
|
|
|
2021-01-14 09:42:01 -06:00
|
|
|
Ok(Some(if self.token.is_keyword(kw::Let) {
|
2021-01-22 12:28:08 -06:00
|
|
|
self.parse_local_mk(lo, attrs, capture_semi, force_collect)?
|
2022-12-03 08:24:49 -06:00
|
|
|
} else if self.is_kw_followed_by_ident(kw::Mut) && self.may_recover() {
|
2022-12-03 08:37:23 -06:00
|
|
|
self.recover_stmt_local_after_let(lo, attrs, InvalidVariableDeclarationSub::MissingLet)?
|
2022-12-03 08:24:49 -06:00
|
|
|
} else if self.is_kw_followed_by_ident(kw::Auto) && self.may_recover() {
|
2021-01-14 09:42:01 -06:00
|
|
|
self.bump(); // `auto`
|
2022-12-03 08:37:23 -06:00
|
|
|
self.recover_stmt_local_after_let(
|
|
|
|
lo,
|
|
|
|
attrs,
|
|
|
|
InvalidVariableDeclarationSub::UseLetNotAuto,
|
|
|
|
)?
|
2022-12-03 08:24:49 -06:00
|
|
|
} else if self.is_kw_followed_by_ident(sym::var) && self.may_recover() {
|
2021-01-14 09:42:01 -06:00
|
|
|
self.bump(); // `var`
|
2022-12-03 08:37:23 -06:00
|
|
|
self.recover_stmt_local_after_let(
|
|
|
|
lo,
|
|
|
|
attrs,
|
|
|
|
InvalidVariableDeclarationSub::UseLetNotVar,
|
|
|
|
)?
|
2021-01-14 09:42:01 -06:00
|
|
|
} else if self.check_path() && !self.token.is_qpath_start() && !self.is_path_start_item() {
|
|
|
|
// We have avoided contextual keywords like `union`, items with `crate` visibility,
|
|
|
|
// or `auto trait` items. We aim to parse an arbitrary path `a::b` but not something
|
|
|
|
// that starts like a path (1 token), but it fact not a path.
|
|
|
|
// Also, we avoid stealing syntax from `parse_item_`.
|
2021-05-06 08:21:40 -05:00
|
|
|
if force_collect == ForceCollect::Yes {
|
|
|
|
self.collect_tokens_no_attrs(|this| this.parse_stmt_path_start(lo, attrs))
|
|
|
|
} else {
|
|
|
|
self.parse_stmt_path_start(lo, attrs)
|
|
|
|
}?
|
2021-12-04 12:05:30 -06:00
|
|
|
} else if let Some(item) = self.parse_item_common(
|
|
|
|
attrs.clone(),
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
FnParseMode { req_name: |_| true, req_body: true },
|
|
|
|
force_collect,
|
|
|
|
)? {
|
2021-01-14 09:42:01 -06:00
|
|
|
// FIXME: Bad copy of attrs
|
|
|
|
self.mk_stmt(lo.to(item.span), StmtKind::Item(P(item)))
|
|
|
|
} else if self.eat(&token::Semi) {
|
|
|
|
// Do not attempt to parse an expression if we're done here.
|
2022-11-13 07:08:58 -06:00
|
|
|
self.error_outer_attrs(attrs);
|
2021-01-14 09:42:01 -06:00
|
|
|
self.mk_stmt(lo, StmtKind::Empty)
|
2022-04-26 07:40:14 -05:00
|
|
|
} else if self.token != token::CloseDelim(Delimiter::Brace) {
|
2021-01-14 09:42:01 -06:00
|
|
|
// Remainder are line-expr stmts.
|
2021-05-06 08:21:40 -05:00
|
|
|
let e = if force_collect == ForceCollect::Yes {
|
|
|
|
self.collect_tokens_no_attrs(|this| {
|
|
|
|
this.parse_expr_res(Restrictions::STMT_EXPR, Some(attrs))
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
self.parse_expr_res(Restrictions::STMT_EXPR, Some(attrs))
|
|
|
|
}?;
|
2022-02-21 01:27:24 -06:00
|
|
|
if matches!(e.kind, ExprKind::Assign(..)) && self.eat_keyword(kw::Else) {
|
|
|
|
let bl = self.parse_block()?;
|
|
|
|
// Destructuring assignment ... else.
|
|
|
|
// This is not allowed, but point it out in a nice way.
|
2022-09-01 12:29:23 -05:00
|
|
|
self.sess.emit_err(AssignmentElseNotAllowed { span: e.span.to(bl.span) });
|
2022-02-21 01:27:24 -06:00
|
|
|
}
|
2021-01-14 09:42:01 -06:00
|
|
|
self.mk_stmt(lo.to(e.span), StmtKind::Expr(e))
|
|
|
|
} else {
|
2022-11-13 07:08:58 -06:00
|
|
|
self.error_outer_attrs(attrs);
|
2021-01-14 09:42:01 -06:00
|
|
|
return Ok(None);
|
|
|
|
}))
|
2019-08-11 13:32:29 -05:00
|
|
|
}
|
|
|
|
|
2021-05-06 08:21:40 -05:00
|
|
|
fn parse_stmt_path_start(&mut self, lo: Span, attrs: AttrWrapper) -> PResult<'a, Stmt> {
|
|
|
|
let stmt = self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
|
2021-01-14 09:42:01 -06:00
|
|
|
let path = this.parse_path(PathStyle::Expr)?;
|
2020-02-23 06:49:19 -06:00
|
|
|
|
2021-01-14 09:42:01 -06:00
|
|
|
if this.eat(&token::Not) {
|
2022-08-16 21:34:33 -05:00
|
|
|
let stmt_mac = this.parse_stmt_mac(lo, attrs, path)?;
|
2021-01-14 09:42:01 -06:00
|
|
|
if this.token == token::Semi {
|
|
|
|
return Ok((stmt_mac, TrailingToken::Semi));
|
|
|
|
} else {
|
|
|
|
return Ok((stmt_mac, TrailingToken::None));
|
|
|
|
}
|
|
|
|
}
|
2020-02-23 06:49:19 -06:00
|
|
|
|
2022-04-26 07:40:14 -05:00
|
|
|
let expr = if this.eat(&token::OpenDelim(Delimiter::Brace)) {
|
2022-08-14 18:58:38 -05:00
|
|
|
this.parse_struct_expr(None, path, true)?
|
2021-01-14 09:42:01 -06:00
|
|
|
} else {
|
|
|
|
let hi = this.prev_token.span;
|
2022-08-14 18:58:38 -05:00
|
|
|
this.mk_expr(lo.to(hi), ExprKind::Path(None, path))
|
2021-01-14 09:42:01 -06:00
|
|
|
};
|
2020-02-23 06:49:19 -06:00
|
|
|
|
2021-01-14 09:42:01 -06:00
|
|
|
let expr = this.with_res(Restrictions::STMT_EXPR, |this| {
|
2020-11-28 17:33:17 -06:00
|
|
|
this.parse_dot_or_call_expr_with(expr, lo, attrs)
|
|
|
|
})?;
|
|
|
|
// `DUMMY_SP` will get overwritten later in this function
|
|
|
|
Ok((this.mk_stmt(rustc_span::DUMMY_SP, StmtKind::Expr(expr)), TrailingToken::None))
|
|
|
|
})?;
|
|
|
|
|
|
|
|
if let StmtKind::Expr(expr) = stmt.kind {
|
|
|
|
// Perform this outside of the `collect_tokens_trailing_token` closure,
|
|
|
|
// since our outer attributes do not apply to this part of the expression
|
|
|
|
let expr = self.with_res(Restrictions::STMT_EXPR, |this| {
|
2023-01-14 05:28:14 -06:00
|
|
|
this.parse_assoc_expr_with(
|
|
|
|
0,
|
|
|
|
LhsExpr::AlreadyParsed { expr, starts_statement: true },
|
|
|
|
)
|
2021-01-14 09:42:01 -06:00
|
|
|
})?;
|
2020-11-28 17:33:17 -06:00
|
|
|
Ok(self.mk_stmt(lo.to(self.prev_token.span), StmtKind::Expr(expr)))
|
|
|
|
} else {
|
|
|
|
Ok(stmt)
|
|
|
|
}
|
2020-02-23 06:49:19 -06:00
|
|
|
}
|
|
|
|
|
2019-12-02 22:37:12 -06:00
|
|
|
/// Parses a statement macro `mac!(args)` provided a `path` representing `mac`.
|
|
|
|
/// At this point, the `!` token after the path has already been eaten.
|
2020-02-23 06:49:19 -06:00
|
|
|
fn parse_stmt_mac(&mut self, lo: Span, attrs: AttrVec, path: ast::Path) -> PResult<'a, Stmt> {
|
2022-11-17 18:24:21 -06:00
|
|
|
let args = self.parse_delim_args()?;
|
|
|
|
let delim = args.delim.to_token();
|
2020-02-29 05:56:15 -06:00
|
|
|
let hi = self.prev_token.span;
|
2019-12-02 22:37:12 -06:00
|
|
|
|
2022-04-26 00:21:15 -05:00
|
|
|
let style = match delim {
|
2022-11-17 18:24:21 -06:00
|
|
|
Delimiter::Brace => MacStmtStyle::Braces,
|
|
|
|
_ => MacStmtStyle::NoBraces,
|
2022-04-26 00:21:15 -05:00
|
|
|
};
|
2019-12-02 22:37:12 -06:00
|
|
|
|
2022-08-11 21:20:10 -05:00
|
|
|
let mac = P(MacCall { path, args, prior_type_ascription: self.last_type_ascription });
|
2019-12-02 22:37:12 -06:00
|
|
|
|
2022-04-26 00:21:15 -05:00
|
|
|
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.
|
2022-08-14 18:58:38 -05:00
|
|
|
let e = self.mk_expr(lo.to(hi), ExprKind::MacCall(mac));
|
2022-05-19 00:51:49 -05:00
|
|
|
let e = self.maybe_recover_from_bad_qpath(e)?;
|
2022-08-16 21:34:33 -05:00
|
|
|
let e = self.parse_dot_or_call_expr_with(e, lo, attrs)?;
|
2023-01-14 05:28:14 -06:00
|
|
|
let e = self.parse_assoc_expr_with(
|
|
|
|
0,
|
|
|
|
LhsExpr::AlreadyParsed { expr: e, starts_statement: false },
|
|
|
|
)?;
|
2022-04-26 00:21:15 -05:00
|
|
|
StmtKind::Expr(e)
|
|
|
|
};
|
2020-02-23 06:49:19 -06:00
|
|
|
Ok(self.mk_stmt(lo.to(hi), kind))
|
2019-12-02 22:37:12 -06:00
|
|
|
}
|
|
|
|
|
2019-12-03 00:40:18 -06:00
|
|
|
/// Error on outer attributes in this context.
|
|
|
|
/// Also error if the previous token was a doc comment.
|
2022-11-13 07:08:58 -06:00
|
|
|
fn error_outer_attrs(&self, attrs: AttrWrapper) {
|
|
|
|
if !attrs.is_empty()
|
|
|
|
&& let attrs = attrs.take_for_recovery(self.sess)
|
|
|
|
&& let attrs @ [.., last] = &*attrs {
|
2020-02-26 21:10:42 -06:00
|
|
|
if last.is_doc_comment() {
|
2022-09-01 12:29:23 -05:00
|
|
|
self.sess.emit_err(DocCommentDoesNotDocumentAnything {
|
|
|
|
span: last.span,
|
|
|
|
missing_comma: None,
|
|
|
|
});
|
2019-12-03 00:40:18 -06:00
|
|
|
} else if attrs.iter().any(|a| a.style == AttrStyle::Outer) {
|
2022-09-01 12:29:23 -05:00
|
|
|
self.sess.emit_err(ExpectedStatementAfterOuterAttr { span: last.span });
|
2019-12-03 00:40:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-03 08:37:23 -06:00
|
|
|
fn recover_stmt_local_after_let(
|
2019-12-03 11:08:19 -06:00
|
|
|
&mut self,
|
2019-12-04 05:15:01 -06:00
|
|
|
lo: Span,
|
2020-11-28 17:33:17 -06:00
|
|
|
attrs: AttrWrapper,
|
2022-08-17 03:06:24 -05:00
|
|
|
subdiagnostic: fn(Span) -> InvalidVariableDeclarationSub,
|
2020-02-23 07:10:03 -06:00
|
|
|
) -> PResult<'a, Stmt> {
|
2022-12-03 08:37:23 -06:00
|
|
|
let stmt =
|
|
|
|
self.collect_tokens_trailing_token(attrs, ForceCollect::Yes, |this, attrs| {
|
|
|
|
let local = this.parse_local(attrs)?;
|
|
|
|
// FIXME - maybe capture semicolon in recovery?
|
|
|
|
Ok((
|
|
|
|
this.mk_stmt(lo.to(this.prev_token.span), StmtKind::Local(local)),
|
|
|
|
TrailingToken::None,
|
|
|
|
))
|
|
|
|
})?;
|
2022-08-17 03:06:24 -05:00
|
|
|
self.sess.emit_err(InvalidVariableDeclaration { span: lo, sub: subdiagnostic(lo) });
|
2020-02-23 07:10:03 -06:00
|
|
|
Ok(stmt)
|
2019-12-03 11:08:19 -06:00
|
|
|
}
|
|
|
|
|
2021-01-14 09:42:01 -06:00
|
|
|
fn parse_local_mk(
|
|
|
|
&mut self,
|
|
|
|
lo: Span,
|
2021-01-22 12:28:08 -06:00
|
|
|
attrs: AttrWrapper,
|
2021-01-14 09:42:01 -06:00
|
|
|
capture_semi: bool,
|
|
|
|
force_collect: ForceCollect,
|
|
|
|
) -> PResult<'a, Stmt> {
|
2021-01-22 12:28:08 -06:00
|
|
|
self.collect_tokens_trailing_token(attrs, force_collect, |this, attrs| {
|
2021-01-14 09:42:01 -06:00
|
|
|
this.expect_keyword(kw::Let)?;
|
2022-08-16 21:34:33 -05:00
|
|
|
let local = this.parse_local(attrs)?;
|
2021-01-14 09:42:01 -06:00
|
|
|
let trailing = if capture_semi && this.token.kind == token::Semi {
|
|
|
|
TrailingToken::Semi
|
|
|
|
} else {
|
|
|
|
TrailingToken::None
|
|
|
|
};
|
|
|
|
Ok((this.mk_stmt(lo.to(this.prev_token.span), StmtKind::Local(local)), trailing))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-11 13:32:29 -05:00
|
|
|
/// Parses a local variable declaration.
|
2020-05-20 15:09:03 -05:00
|
|
|
fn parse_local(&mut self, attrs: AttrVec) -> PResult<'a, P<Local>> {
|
2020-02-29 05:56:15 -06:00
|
|
|
let lo = self.prev_token.span;
|
2022-08-03 10:45:26 -05:00
|
|
|
|
|
|
|
if self.token.is_keyword(kw::Const) && self.look_ahead(1, |t| t.is_ident()) {
|
2022-09-01 12:29:23 -05:00
|
|
|
self.sess.emit_err(ConstLetMutuallyExclusive { span: lo.to(self.token.span) });
|
2022-08-03 10:45:26 -05:00
|
|
|
self.bump();
|
|
|
|
}
|
|
|
|
|
2022-11-11 14:22:47 -06:00
|
|
|
self.report_invalid_identifier_error()?;
|
2020-11-21 15:12:05 -06:00
|
|
|
let (pat, colon) = self.parse_pat_before_ty(None, RecoverComma::Yes, "`let` bindings")?;
|
2019-08-11 13:32:29 -05:00
|
|
|
|
2021-02-12 18:04:37 -06:00
|
|
|
let (err, ty) = if colon {
|
2019-08-11 13:32:29 -05:00
|
|
|
// Save the state of the parser before parsing type normally, in case there is a `:`
|
|
|
|
// instead of an `=` typo.
|
|
|
|
let parser_snapshot_before_type = self.clone();
|
2020-02-29 05:56:15 -06:00
|
|
|
let colon_sp = self.prev_token.span;
|
2019-08-11 13:32:29 -05:00
|
|
|
match self.parse_ty() {
|
|
|
|
Ok(ty) => (None, Some(ty)),
|
|
|
|
Err(mut err) => {
|
2020-04-02 20:17:40 -05:00
|
|
|
if let Ok(snip) = self.span_to_snippet(pat.span) {
|
|
|
|
err.span_label(pat.span, format!("while parsing the type for `{}`", snip));
|
|
|
|
}
|
2022-05-01 12:05:35 -05:00
|
|
|
// 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) {
|
2020-07-14 12:35:59 -05:00
|
|
|
err.emit();
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
// Rewind to before attempting to parse the type and continue parsing.
|
|
|
|
let parser_snapshot_after_type =
|
|
|
|
mem::replace(self, parser_snapshot_before_type);
|
|
|
|
Some((parser_snapshot_after_type, colon_sp, err))
|
|
|
|
};
|
|
|
|
(err, None)
|
2019-08-11 13:32:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
(None, None)
|
|
|
|
};
|
2020-05-20 15:09:03 -05:00
|
|
|
let init = match (self.parse_initializer(err.is_some()), err) {
|
2019-08-11 13:32:29 -05:00
|
|
|
(Ok(init), None) => {
|
|
|
|
// init parsed, ty parsed
|
|
|
|
init
|
|
|
|
}
|
|
|
|
(Ok(init), Some((_, colon_sp, mut err))) => {
|
|
|
|
// init parsed, ty error
|
|
|
|
// Could parse the type as if it were the initializer, it is likely there was a
|
|
|
|
// typo in the code: `:` instead of `=`. Add suggestion and emit the error.
|
|
|
|
err.span_suggestion_short(
|
|
|
|
colon_sp,
|
|
|
|
"use `=` if you meant to assign",
|
2022-06-13 01:48:40 -05:00
|
|
|
" =",
|
2019-08-11 13:32:29 -05:00
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
err.emit();
|
|
|
|
// As this was parsed successfully, continue as if the code has been fixed for the
|
|
|
|
// rest of the file. It will still fail due to the emitted error, but we avoid
|
|
|
|
// extra noise.
|
|
|
|
init
|
|
|
|
}
|
2022-01-25 21:39:14 -06:00
|
|
|
(Err(init_err), Some((snapshot, _, ty_err))) => {
|
2019-08-11 13:32:29 -05:00
|
|
|
// init error, ty error
|
|
|
|
init_err.cancel();
|
|
|
|
// Couldn't parse the type nor the initializer, only raise the type error and
|
|
|
|
// return to the parser state before parsing the type as the initializer.
|
|
|
|
// let x: <parse_error>;
|
2020-04-17 15:59:14 -05:00
|
|
|
*self = snapshot;
|
2019-08-11 13:32:29 -05:00
|
|
|
return Err(ty_err);
|
|
|
|
}
|
|
|
|
(Err(err), None) => {
|
|
|
|
// init error, ty parsed
|
|
|
|
// Couldn't parse the initializer and we're not attempting to recover a failed
|
|
|
|
// parse of the type, return the error.
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
};
|
2021-06-22 13:00:58 -05:00
|
|
|
let kind = match init {
|
|
|
|
None => LocalKind::Decl,
|
|
|
|
Some(init) => {
|
|
|
|
if self.eat_keyword(kw::Else) {
|
2021-10-17 00:02:52 -05:00
|
|
|
if self.token.is_keyword(kw::If) {
|
|
|
|
// `let...else if`. Emit the same error that `parse_block()` would,
|
|
|
|
// but explicitly point out that this pattern is not allowed.
|
|
|
|
let msg = "conditional `else if` is not supported for `let...else`";
|
|
|
|
return Err(self.error_block_no_opening_brace_msg(msg));
|
|
|
|
}
|
2021-06-22 13:00:58 -05:00
|
|
|
let els = self.parse_block()?;
|
2021-07-30 17:12:11 -05:00
|
|
|
self.check_let_else_init_bool_expr(&init);
|
|
|
|
self.check_let_else_init_trailing_brace(&init);
|
2021-06-22 13:00:58 -05:00
|
|
|
LocalKind::InitElse(init, els)
|
|
|
|
} else {
|
|
|
|
LocalKind::Init(init)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-02-29 05:56:15 -06:00
|
|
|
let hi = if self.token == token::Semi { self.token.span } else { self.prev_token.span };
|
2021-06-22 13:00:58 -05:00
|
|
|
Ok(P(ast::Local { ty, pat, kind, id: DUMMY_NODE_ID, span: lo.to(hi), attrs, tokens: None }))
|
2019-08-11 13:32:29 -05:00
|
|
|
}
|
|
|
|
|
2022-11-11 14:22:47 -06:00
|
|
|
/// report error for `let 1x = 123`
|
|
|
|
pub fn report_invalid_identifier_error(&mut self) -> PResult<'a, ()> {
|
|
|
|
if let token::Literal(lit) = self.token.uninterpolate().kind &&
|
2022-11-22 22:39:42 -06:00
|
|
|
rustc_ast::MetaItemLit::from_token(&self.token).is_none() &&
|
2022-11-11 14:22:47 -06:00
|
|
|
(lit.kind == token::LitKind::Integer || lit.kind == token::LitKind::Float) &&
|
|
|
|
self.look_ahead(1, |t| matches!(t.kind, token::Eq) || matches!(t.kind, token::Colon ) ) {
|
2022-11-14 08:13:49 -06:00
|
|
|
return Err(self.sess.create_err(InvalidIdentiferStartsWithNumber { span: self.token.span }));
|
2022-11-11 14:22:47 -06:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-30 17:12:11 -05:00
|
|
|
fn check_let_else_init_bool_expr(&self, init: &ast::Expr) {
|
|
|
|
if let ast::ExprKind::Binary(op, ..) = init.kind {
|
|
|
|
if op.node.lazy() {
|
2022-09-01 12:29:23 -05:00
|
|
|
self.sess.emit_err(InvalidExpressionInLetElse {
|
|
|
|
span: init.span,
|
|
|
|
operator: op.node.to_string(),
|
|
|
|
sugg: WrapExpressionInParentheses {
|
|
|
|
left: init.span.shrink_to_lo(),
|
|
|
|
right: init.span.shrink_to_hi(),
|
|
|
|
},
|
|
|
|
});
|
2021-07-30 17:12:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_let_else_init_trailing_brace(&self, init: &ast::Expr) {
|
|
|
|
if let Some(trailing) = classify::expr_trailing_brace(init) {
|
2022-09-01 12:29:23 -05:00
|
|
|
self.sess.emit_err(InvalidCurlyInLetElse {
|
|
|
|
span: trailing.span.with_lo(trailing.span.hi() - BytePos(1)),
|
|
|
|
sugg: WrapExpressionInParentheses {
|
|
|
|
left: trailing.span.shrink_to_lo(),
|
|
|
|
right: trailing.span.shrink_to_hi(),
|
|
|
|
},
|
|
|
|
});
|
2021-07-30 17:12:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 14:56:30 -06:00
|
|
|
/// Parses the RHS of a local variable declaration (e.g., `= 14;`).
|
2020-05-20 15:09:03 -05:00
|
|
|
fn parse_initializer(&mut self, eq_optional: bool) -> PResult<'a, Option<P<Expr>>> {
|
|
|
|
let eq_consumed = match self.token.kind {
|
|
|
|
token::BinOpEq(..) => {
|
|
|
|
// Recover `let x <op>= 1` as `let x = 1`
|
2022-09-01 12:29:23 -05:00
|
|
|
self.sess.emit_err(CompoundAssignmentExpressionInLet { span: self.token.span });
|
2020-05-20 15:09:03 -05:00
|
|
|
self.bump();
|
|
|
|
true
|
2020-05-06 20:57:31 -05:00
|
|
|
}
|
2020-05-20 15:09:03 -05:00
|
|
|
_ => self.eat(&token::Eq),
|
2020-05-06 20:57:31 -05:00
|
|
|
};
|
|
|
|
|
2020-05-20 15:09:03 -05:00
|
|
|
Ok(if eq_consumed || eq_optional { Some(self.parse_expr()?) } else { None })
|
2019-08-11 13:32:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses a block. No inner attributes are allowed.
|
2020-07-27 07:04:54 -05:00
|
|
|
pub(super) fn parse_block(&mut self) -> PResult<'a, P<Block>> {
|
2020-03-05 04:42:56 -06:00
|
|
|
let (attrs, block) = self.parse_inner_attrs_and_block()?;
|
|
|
|
if let [.., last] = &*attrs {
|
2022-08-31 06:20:59 -05:00
|
|
|
self.error_on_forbidden_inner_attr(
|
|
|
|
last.span,
|
|
|
|
super::attr::InnerAttrPolicy::Forbidden(Some(
|
|
|
|
InnerAttrForbiddenReason::InCodeBlock,
|
|
|
|
)),
|
|
|
|
);
|
2019-12-03 01:20:19 -06:00
|
|
|
}
|
2020-03-05 04:42:56 -06:00
|
|
|
Ok(block)
|
2019-12-03 01:20:19 -06:00
|
|
|
}
|
|
|
|
|
2022-01-27 03:44:25 -06:00
|
|
|
fn error_block_no_opening_brace_msg(
|
|
|
|
&mut self,
|
|
|
|
msg: &str,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
|
2019-12-03 01:20:19 -06:00
|
|
|
let sp = self.token.span;
|
2021-10-17 00:02:52 -05:00
|
|
|
let mut e = self.struct_span_err(sp, msg);
|
2019-12-03 03:19:58 -06:00
|
|
|
let do_not_suggest_help = self.token.is_keyword(kw::In) || self.token == token::Colon;
|
2019-08-11 13:32:29 -05:00
|
|
|
|
2019-12-03 01:20:19 -06:00
|
|
|
// Check to see if the user has written something like
|
|
|
|
//
|
|
|
|
// if (cond)
|
|
|
|
// bar;
|
|
|
|
//
|
|
|
|
// which is valid in other languages, but not Rust.
|
2021-01-14 09:42:01 -06:00
|
|
|
match self.parse_stmt_without_recovery(false, ForceCollect::No) {
|
2022-05-27 23:58:48 -05:00
|
|
|
// If the next token is an open brace, e.g., we have:
|
|
|
|
//
|
|
|
|
// if expr other_expr {
|
|
|
|
// ^ ^ ^- lookahead(1) is a brace
|
|
|
|
// | |- current token is not "else"
|
|
|
|
// |- (statement we just parsed)
|
|
|
|
//
|
|
|
|
// the place-inside-a-block suggestion would be more likely wrong than right.
|
|
|
|
//
|
|
|
|
// FIXME(compiler-errors): this should probably parse an arbitrary expr and not
|
|
|
|
// just lookahead one token, so we can see if there's a brace after _that_,
|
|
|
|
// since we want to protect against:
|
|
|
|
// `if 1 1 + 1 {` being suggested as `if { 1 } 1 + 1 {`
|
|
|
|
// + +
|
2020-03-07 04:00:40 -06:00
|
|
|
Ok(Some(_))
|
2022-05-27 23:58:48 -05:00
|
|
|
if (!self.token.is_keyword(kw::Else)
|
|
|
|
&& self.look_ahead(1, |t| t == &token::OpenDelim(Delimiter::Brace)))
|
2020-03-07 04:00:40 -06:00
|
|
|
|| do_not_suggest_help => {}
|
2022-01-12 14:43:24 -06:00
|
|
|
// Do not suggest `if foo println!("") {;}` (as would be seen in test for #46836).
|
|
|
|
Ok(Some(Stmt { kind: StmtKind::Empty, .. })) => {}
|
2020-03-07 04:00:40 -06:00
|
|
|
Ok(Some(stmt)) => {
|
|
|
|
let stmt_own_line = self.sess.source_map().is_line_before_span_empty(sp);
|
|
|
|
let stmt_span = if stmt_own_line && self.eat(&token::Semi) {
|
2019-12-03 01:20:19 -06:00
|
|
|
// Expand the span to include the semicolon.
|
2020-02-29 05:56:15 -06:00
|
|
|
stmt.span.with_hi(self.prev_token.span.hi())
|
2019-12-03 01:20:19 -06:00
|
|
|
} else {
|
|
|
|
stmt.span
|
|
|
|
};
|
2022-01-12 14:43:24 -06:00
|
|
|
e.multipart_suggestion(
|
|
|
|
"try placing this code inside a block",
|
|
|
|
vec![
|
|
|
|
(stmt_span.shrink_to_lo(), "{ ".to_string()),
|
|
|
|
(stmt_span.shrink_to_hi(), " }".to_string()),
|
|
|
|
],
|
|
|
|
// Speculative; has been misleading in the past (#46836).
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
2019-08-11 13:32:29 -05:00
|
|
|
}
|
2022-01-25 21:39:14 -06:00
|
|
|
Err(e) => {
|
2019-12-03 01:20:19 -06:00
|
|
|
self.recover_stmt_(SemiColonMode::Break, BlockMode::Ignore);
|
|
|
|
e.cancel();
|
|
|
|
}
|
|
|
|
_ => {}
|
2019-08-11 13:32:29 -05:00
|
|
|
}
|
2019-12-03 01:20:19 -06:00
|
|
|
e.span_label(sp, "expected `{`");
|
2021-10-17 00:02:52 -05:00
|
|
|
e
|
|
|
|
}
|
|
|
|
|
|
|
|
fn error_block_no_opening_brace<T>(&mut self) -> PResult<'a, T> {
|
|
|
|
let tok = super::token_descr(&self.token);
|
|
|
|
let msg = format!("expected `{{`, found {}", tok);
|
|
|
|
Err(self.error_block_no_opening_brace_msg(&msg))
|
2019-08-11 13:32:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses a block. Inner attributes are allowed.
|
2022-08-16 21:34:33 -05:00
|
|
|
pub(super) fn parse_inner_attrs_and_block(&mut self) -> PResult<'a, (AttrVec, P<Block>)> {
|
2023-01-08 23:29:54 -06:00
|
|
|
self.parse_block_common(self.token.span, BlockCheckMode::Default, true)
|
2020-03-04 22:49:30 -06:00
|
|
|
}
|
2019-08-11 13:32:29 -05:00
|
|
|
|
2020-03-04 22:49:30 -06:00
|
|
|
/// Parses a block. Inner attributes are allowed.
|
|
|
|
pub(super) fn parse_block_common(
|
|
|
|
&mut self,
|
|
|
|
lo: Span,
|
|
|
|
blk_mode: BlockCheckMode,
|
2023-01-08 23:29:54 -06:00
|
|
|
can_be_struct_literal: bool,
|
2022-08-16 21:34:33 -05:00
|
|
|
) -> PResult<'a, (AttrVec, P<Block>)> {
|
|
|
|
maybe_whole!(self, NtBlock, |x| (AttrVec::new(), x));
|
2020-03-04 19:46:13 -06:00
|
|
|
|
2023-01-08 23:29:54 -06:00
|
|
|
let maybe_ident = self.prev_token.clone();
|
2022-01-12 14:43:24 -06:00
|
|
|
self.maybe_recover_unexpected_block_label();
|
2022-04-26 07:40:14 -05:00
|
|
|
if !self.eat(&token::OpenDelim(Delimiter::Brace)) {
|
2020-03-04 19:46:13 -06:00
|
|
|
return self.error_block_no_opening_brace();
|
|
|
|
}
|
|
|
|
|
2020-08-12 17:39:15 -05:00
|
|
|
let attrs = self.parse_inner_attributes()?;
|
2023-01-08 23:29:54 -06:00
|
|
|
let tail = match self.maybe_suggest_struct_literal(
|
|
|
|
lo,
|
|
|
|
blk_mode,
|
|
|
|
maybe_ident,
|
|
|
|
can_be_struct_literal,
|
|
|
|
) {
|
2022-01-12 14:43:24 -06:00
|
|
|
Some(tail) => tail?,
|
|
|
|
None => self.parse_block_tail(lo, blk_mode, AttemptLocalParseRecovery::Yes)?,
|
2020-08-12 17:39:15 -05:00
|
|
|
};
|
|
|
|
Ok((attrs, tail))
|
2019-08-11 13:32:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses the rest of a block expression or function body.
|
|
|
|
/// Precondition: already parsed the '{'.
|
2022-05-20 18:51:09 -05:00
|
|
|
pub(crate) fn parse_block_tail(
|
2020-08-12 17:39:15 -05:00
|
|
|
&mut self,
|
|
|
|
lo: Span,
|
|
|
|
s: BlockCheckMode,
|
|
|
|
recover: AttemptLocalParseRecovery,
|
|
|
|
) -> PResult<'a, P<Block>> {
|
2019-08-11 13:32:29 -05:00
|
|
|
let mut stmts = vec![];
|
2022-12-28 19:56:22 -06:00
|
|
|
let mut snapshot = None;
|
2022-04-26 07:40:14 -05:00
|
|
|
while !self.eat(&token::CloseDelim(Delimiter::Brace)) {
|
2019-08-11 13:32:29 -05:00
|
|
|
if self.token == token::Eof {
|
|
|
|
break;
|
|
|
|
}
|
2022-12-28 19:56:22 -06:00
|
|
|
if self.is_diff_marker(&TokenKind::BinOp(token::Shl), &TokenKind::Lt) {
|
2022-12-28 20:46:20 -06:00
|
|
|
// Account for `<<<<<<<` diff markers. We can't proactively error here because
|
2022-12-28 19:56:22 -06:00
|
|
|
// that can be a valid path start, so we snapshot and reparse only we've
|
|
|
|
// encountered another parse error.
|
|
|
|
snapshot = Some(self.create_snapshot_for_diagnostic());
|
|
|
|
}
|
2020-08-12 17:39:15 -05:00
|
|
|
let stmt = match self.parse_full_stmt(recover) {
|
|
|
|
Err(mut err) if recover.yes() => {
|
2019-11-04 18:19:55 -06:00
|
|
|
self.maybe_annotate_with_ascription(&mut err, false);
|
2022-12-28 19:56:22 -06:00
|
|
|
if let Some(ref mut snapshot) = snapshot {
|
|
|
|
snapshot.recover_diff_marker();
|
|
|
|
}
|
2019-08-11 13:32:29 -05:00
|
|
|
err.emit();
|
|
|
|
self.recover_stmt_(SemiColonMode::Ignore, BlockMode::Ignore);
|
2019-12-30 18:57:42 -06:00
|
|
|
Some(self.mk_stmt_err(self.token.span))
|
2019-08-11 13:32:29 -05:00
|
|
|
}
|
|
|
|
Ok(stmt) => stmt,
|
2020-08-12 17:39:15 -05:00
|
|
|
Err(err) => return Err(err),
|
2019-08-11 13:32:29 -05:00
|
|
|
};
|
|
|
|
if let Some(stmt) = stmt {
|
|
|
|
stmts.push(stmt);
|
|
|
|
} else {
|
|
|
|
// Found only `;` or `}`.
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
}
|
2020-02-29 05:56:15 -06:00
|
|
|
Ok(self.mk_block(stmts, s, lo.to(self.prev_token.span)))
|
2019-08-11 13:32:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses a statement, including the trailing semicolon.
|
2020-08-12 17:39:15 -05:00
|
|
|
pub fn parse_full_stmt(
|
|
|
|
&mut self,
|
|
|
|
recover: AttemptLocalParseRecovery,
|
|
|
|
) -> PResult<'a, Option<Stmt>> {
|
2019-09-05 21:56:45 -05:00
|
|
|
// Skip looking for a trailing semicolon when we have an interpolated statement.
|
2022-04-05 21:08:39 -05:00
|
|
|
maybe_whole!(self, NtStmt, |x| Some(x.into_inner()));
|
2019-08-11 13:32:29 -05:00
|
|
|
|
2022-02-18 17:48:49 -06:00
|
|
|
let Some(mut stmt) = self.parse_stmt_without_recovery(true, ForceCollect::No)? else {
|
|
|
|
return Ok(None);
|
2019-08-11 13:32:29 -05:00
|
|
|
};
|
|
|
|
|
2019-10-20 16:35:46 -05:00
|
|
|
let mut eat_semi = true;
|
2022-11-22 03:42:01 -06:00
|
|
|
match &mut stmt.kind {
|
2020-02-24 17:59:39 -06:00
|
|
|
// Expression without semicolon.
|
2022-11-22 03:42:01 -06:00
|
|
|
StmtKind::Expr(expr)
|
2022-10-21 18:56:26 -05:00
|
|
|
if self.token != token::Eof && classify::expr_requires_semi_to_be_stmt(expr) => {
|
2020-02-24 17:59:39 -06:00
|
|
|
// Just check for errors and recover; do not eat semicolon yet.
|
2022-10-21 18:56:26 -05:00
|
|
|
// `expect_one_of` returns PResult<'a, bool /* recovered */>
|
|
|
|
let replace_with_err =
|
|
|
|
match self.expect_one_of(&[], &[token::Semi, token::CloseDelim(Delimiter::Brace)]) {
|
|
|
|
// Recover from parser, skip type error to avoid extra errors.
|
|
|
|
Ok(true) => true,
|
|
|
|
Err(mut e) => {
|
|
|
|
if let TokenKind::DocComment(..) = self.token.kind &&
|
|
|
|
let Ok(snippet) = self.span_to_snippet(self.token.span) {
|
|
|
|
let sp = self.token.span;
|
|
|
|
let marker = &snippet[..3];
|
|
|
|
let (comment_marker, doc_comment_marker) = marker.split_at(2);
|
|
|
|
|
|
|
|
e.span_suggestion(
|
|
|
|
sp.with_hi(sp.lo() + BytePos(marker.len() as u32)),
|
|
|
|
&format!(
|
|
|
|
"add a space before `{}` to use a regular comment",
|
|
|
|
doc_comment_marker,
|
|
|
|
),
|
|
|
|
format!("{} {}", comment_marker, doc_comment_marker),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
2020-01-22 10:24:32 -06:00
|
|
|
}
|
2022-10-21 18:56:26 -05:00
|
|
|
|
|
|
|
if let Err(mut e) =
|
|
|
|
self.check_mistyped_turbofish_with_multiple_type_params(e, expr)
|
|
|
|
{
|
|
|
|
if recover.no() {
|
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
e.emit();
|
|
|
|
self.recover_stmt();
|
2020-08-12 17:39:15 -05:00
|
|
|
}
|
2022-10-21 18:56:26 -05:00
|
|
|
true
|
2020-08-31 12:24:37 -05:00
|
|
|
}
|
2022-10-21 18:56:26 -05:00
|
|
|
_ => false
|
|
|
|
};
|
|
|
|
if replace_with_err {
|
|
|
|
// We already emitted an error, so don't emit another type error
|
2020-02-29 05:56:15 -06:00
|
|
|
let sp = expr.span.to(self.prev_token.span);
|
2020-08-31 12:24:37 -05:00
|
|
|
*expr = self.mk_expr_err(sp);
|
2019-08-11 13:32:29 -05:00
|
|
|
}
|
|
|
|
}
|
2020-11-17 13:27:44 -06:00
|
|
|
StmtKind::Expr(_) | StmtKind::MacCall(_) => {}
|
2022-11-22 03:42:01 -06:00
|
|
|
StmtKind::Local(local) if let Err(e) = self.expect_semi() => {
|
2021-08-16 10:29:49 -05:00
|
|
|
// We might be at the `,` in `let x = foo<bar, baz>;`. Try to recover.
|
2021-06-22 13:00:58 -05:00
|
|
|
match &mut local.kind {
|
|
|
|
LocalKind::Init(expr) | LocalKind::InitElse(expr, _) => {
|
2022-01-12 14:43:24 -06:00
|
|
|
self.check_mistyped_turbofish_with_multiple_type_params(e, expr)?;
|
|
|
|
// We found `foo<bar, baz>`, have we fully recovered?
|
|
|
|
self.expect_semi()?;
|
|
|
|
}
|
|
|
|
LocalKind::Decl => return Err(e),
|
2020-08-31 12:24:37 -05:00
|
|
|
}
|
2019-12-03 11:47:44 -06:00
|
|
|
eat_semi = false;
|
2019-08-11 13:32:29 -05:00
|
|
|
}
|
2021-08-16 10:29:49 -05:00
|
|
|
StmtKind::Empty | StmtKind::Item(_) | StmtKind::Local(_) | StmtKind::Semi(_) => eat_semi = false,
|
2019-08-11 13:32:29 -05:00
|
|
|
}
|
|
|
|
|
2019-10-20 16:35:46 -05:00
|
|
|
if eat_semi && self.eat(&token::Semi) {
|
2019-08-11 13:32:29 -05:00
|
|
|
stmt = stmt.add_trailing_semicolon();
|
|
|
|
}
|
2020-02-29 05:56:15 -06:00
|
|
|
stmt.span = stmt.span.to(self.prev_token.span);
|
2019-08-11 13:32:29 -05:00
|
|
|
Ok(Some(stmt))
|
|
|
|
}
|
|
|
|
|
2019-12-06 16:23:30 -06:00
|
|
|
pub(super) fn mk_block(&self, stmts: Vec<Stmt>, rules: BlockCheckMode, span: Span) -> P<Block> {
|
2021-09-02 13:34:03 -05:00
|
|
|
P(Block {
|
|
|
|
stmts,
|
|
|
|
id: DUMMY_NODE_ID,
|
|
|
|
rules,
|
|
|
|
span,
|
|
|
|
tokens: None,
|
|
|
|
could_be_bare_literal: false,
|
|
|
|
})
|
2019-12-06 16:23:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn mk_stmt(&self, span: Span, kind: StmtKind) -> Stmt {
|
2020-11-17 13:27:44 -06:00
|
|
|
Stmt { id: DUMMY_NODE_ID, kind, span }
|
2019-12-02 02:32:54 -06:00
|
|
|
}
|
2019-12-30 18:57:42 -06:00
|
|
|
|
2020-08-12 17:39:15 -05:00
|
|
|
pub(super) fn mk_stmt_err(&self, span: Span) -> Stmt {
|
2019-12-30 18:57:42 -06:00
|
|
|
self.mk_stmt(span, StmtKind::Expr(self.mk_expr_err(span)))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn mk_block_err(&self, span: Span) -> P<Block> {
|
|
|
|
self.mk_block(vec![self.mk_stmt_err(span)], BlockCheckMode::Default, span)
|
|
|
|
}
|
2019-08-11 13:32:29 -05:00
|
|
|
}
|