2019-10-08 07:46:06 +00:00
|
|
|
|
use super::diagnostics::Error;
|
2019-12-22 22:42:04 +00:00
|
|
|
|
use super::pat::{GateOr, PARAM_EXPECTED};
|
|
|
|
|
use super::{BlockMode, Parser, PathStyle, PrevTokenKind, Restrictions, TokenType};
|
|
|
|
|
use super::{SemiColonMode, SeqSep, TokenExpectType};
|
2019-10-15 20:48:13 +00:00
|
|
|
|
use crate::maybe_recover_from_interpolated_ty_qpath;
|
2019-08-11 11:14:30 +00:00
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
|
use rustc_errors::{Applicability, PResult};
|
|
|
|
|
use std::mem;
|
|
|
|
|
use syntax::ast::{self, AttrStyle, AttrVec, CaptureBy, Field, Ident, Lit, DUMMY_NODE_ID};
|
|
|
|
|
use syntax::ast::{
|
|
|
|
|
AnonConst, BinOp, BinOpKind, FnDecl, FunctionRetTy, Mac, Param, Ty, TyKind, UnOp,
|
|
|
|
|
};
|
|
|
|
|
use syntax::ast::{Arm, BlockCheckMode, Expr, ExprKind, IsAsync, Label, Movability, RangeLimits};
|
2019-10-15 20:48:13 +00:00
|
|
|
|
use syntax::print::pprust;
|
|
|
|
|
use syntax::ptr::P;
|
2019-12-22 22:42:04 +00:00
|
|
|
|
use syntax::token::{self, Token, TokenKind};
|
2019-10-15 20:48:13 +00:00
|
|
|
|
use syntax::util::classify;
|
|
|
|
|
use syntax::util::literal::LitError;
|
2019-12-22 22:42:04 +00:00
|
|
|
|
use syntax::util::parser::{prec_let_scrutinee_needs_par, AssocOp, Fixity};
|
2019-12-05 05:38:06 +00:00
|
|
|
|
use syntax_pos::source_map::{self, Span};
|
|
|
|
|
use syntax_pos::symbol::{kw, sym, Symbol};
|
2019-08-11 11:14:30 +00:00
|
|
|
|
|
|
|
|
|
/// Possibly accepts an `token::Interpolated` expression (a pre-parsed expression
|
|
|
|
|
/// dropped into the token stream, which happens while parsing the result of
|
|
|
|
|
/// macro expansion). Placement of these is not as complex as I feared it would
|
|
|
|
|
/// be. The important thing is to make sure that lookahead doesn't balk at
|
|
|
|
|
/// `token::Interpolated` tokens.
|
|
|
|
|
macro_rules! maybe_whole_expr {
|
|
|
|
|
($p:expr) => {
|
|
|
|
|
if let token::Interpolated(nt) = &$p.token.kind {
|
|
|
|
|
match &**nt {
|
|
|
|
|
token::NtExpr(e) | token::NtLiteral(e) => {
|
|
|
|
|
let e = e.clone();
|
|
|
|
|
$p.bump();
|
|
|
|
|
return Ok(e);
|
|
|
|
|
}
|
|
|
|
|
token::NtPath(path) => {
|
|
|
|
|
let path = path.clone();
|
|
|
|
|
$p.bump();
|
|
|
|
|
return Ok($p.mk_expr(
|
2019-12-22 22:42:04 +00:00
|
|
|
|
$p.token.span,
|
|
|
|
|
ExprKind::Path(None, path),
|
|
|
|
|
AttrVec::new(),
|
2019-08-11 11:14:30 +00:00
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
token::NtBlock(block) => {
|
|
|
|
|
let block = block.clone();
|
|
|
|
|
$p.bump();
|
|
|
|
|
return Ok($p.mk_expr(
|
2019-12-22 22:42:04 +00:00
|
|
|
|
$p.token.span,
|
|
|
|
|
ExprKind::Block(block, None),
|
|
|
|
|
AttrVec::new(),
|
2019-08-11 11:14:30 +00:00
|
|
|
|
));
|
|
|
|
|
}
|
2019-09-06 02:56:45 +00:00
|
|
|
|
// N.B., `NtIdent(ident)` is normalized to `Ident` in `fn bump`.
|
2019-12-22 22:42:04 +00:00
|
|
|
|
_ => {}
|
2019-08-11 11:14:30 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
|
};
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub(super) enum LhsExpr {
|
|
|
|
|
NotYetParsed,
|
2019-12-03 15:38:34 +00:00
|
|
|
|
AttributesParsed(AttrVec),
|
2019-08-11 11:14:30 +00:00
|
|
|
|
AlreadyParsed(P<Expr>),
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 15:38:34 +00:00
|
|
|
|
impl From<Option<AttrVec>> for LhsExpr {
|
2019-09-03 22:42:58 +00:00
|
|
|
|
/// Converts `Some(attrs)` into `LhsExpr::AttributesParsed(attrs)`
|
|
|
|
|
/// and `None` into `LhsExpr::NotYetParsed`.
|
|
|
|
|
///
|
|
|
|
|
/// This conversion does not allocate.
|
2019-12-03 15:38:34 +00:00
|
|
|
|
fn from(o: Option<AttrVec>) -> Self {
|
2019-12-22 22:42:04 +00:00
|
|
|
|
if let Some(attrs) = o { LhsExpr::AttributesParsed(attrs) } else { LhsExpr::NotYetParsed }
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<P<Expr>> for LhsExpr {
|
2019-09-03 22:42:58 +00:00
|
|
|
|
/// Converts the `expr: P<Expr>` into `LhsExpr::AlreadyParsed(expr)`.
|
|
|
|
|
///
|
|
|
|
|
/// This conversion does not allocate.
|
2019-08-11 11:14:30 +00:00
|
|
|
|
fn from(expr: P<Expr>) -> Self {
|
|
|
|
|
LhsExpr::AlreadyParsed(expr)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> Parser<'a> {
|
|
|
|
|
/// Parses an expression.
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn parse_expr(&mut self) -> PResult<'a, P<Expr>> {
|
|
|
|
|
self.parse_expr_res(Restrictions::empty(), None)
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-08 07:19:53 +00:00
|
|
|
|
pub(super) fn parse_anon_const_expr(&mut self) -> PResult<'a, AnonConst> {
|
|
|
|
|
self.parse_expr().map(|value| AnonConst { id: DUMMY_NODE_ID, value })
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-16 20:45:43 +00:00
|
|
|
|
fn parse_expr_catch_underscore(&mut self) -> PResult<'a, P<Expr>> {
|
|
|
|
|
match self.parse_expr() {
|
|
|
|
|
Ok(expr) => Ok(expr),
|
|
|
|
|
Err(mut err) => match self.token.kind {
|
|
|
|
|
token::Ident(name, false)
|
2019-12-22 22:42:04 +00:00
|
|
|
|
if name == kw::Underscore && self.look_ahead(1, |t| t == &token::Comma) =>
|
|
|
|
|
{
|
2019-09-16 20:45:43 +00:00
|
|
|
|
// Special-case handling of `foo(_, _, _)`
|
|
|
|
|
err.emit();
|
|
|
|
|
let sp = self.token.span;
|
|
|
|
|
self.bump();
|
2019-12-03 15:38:34 +00:00
|
|
|
|
Ok(self.mk_expr(sp, ExprKind::Err, AttrVec::new()))
|
2019-09-16 20:45:43 +00:00
|
|
|
|
}
|
|
|
|
|
_ => Err(err),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-08 07:19:53 +00:00
|
|
|
|
/// Parses a sequence of expressions delimited by parentheses.
|
2019-08-11 11:14:30 +00:00
|
|
|
|
fn parse_paren_expr_seq(&mut self) -> PResult<'a, Vec<P<Expr>>> {
|
2019-12-22 22:42:04 +00:00
|
|
|
|
self.parse_paren_comma_seq(|p| p.parse_expr_catch_underscore()).map(|(r, _)| r)
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Parses an expression, subject to the given restrictions.
|
|
|
|
|
#[inline]
|
|
|
|
|
pub(super) fn parse_expr_res(
|
|
|
|
|
&mut self,
|
|
|
|
|
r: Restrictions,
|
2019-12-22 22:42:04 +00:00
|
|
|
|
already_parsed_attrs: Option<AttrVec>,
|
2019-08-11 11:14:30 +00:00
|
|
|
|
) -> PResult<'a, P<Expr>> {
|
|
|
|
|
self.with_res(r, |this| this.parse_assoc_expr(already_parsed_attrs))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Parses an associative expression.
|
|
|
|
|
///
|
|
|
|
|
/// This parses an expression accounting for associativity and precedence of the operators in
|
|
|
|
|
/// the expression.
|
|
|
|
|
#[inline]
|
2019-12-22 22:42:04 +00:00
|
|
|
|
fn parse_assoc_expr(&mut self, already_parsed_attrs: Option<AttrVec>) -> PResult<'a, P<Expr>> {
|
2019-08-11 11:14:30 +00:00
|
|
|
|
self.parse_assoc_expr_with(0, already_parsed_attrs.into())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Parses an associative expression with operators of at least `min_prec` precedence.
|
|
|
|
|
pub(super) fn parse_assoc_expr_with(
|
|
|
|
|
&mut self,
|
|
|
|
|
min_prec: usize,
|
|
|
|
|
lhs: LhsExpr,
|
|
|
|
|
) -> PResult<'a, P<Expr>> {
|
|
|
|
|
let mut lhs = if let LhsExpr::AlreadyParsed(expr) = lhs {
|
|
|
|
|
expr
|
|
|
|
|
} else {
|
|
|
|
|
let attrs = match lhs {
|
|
|
|
|
LhsExpr::AttributesParsed(attrs) => Some(attrs),
|
|
|
|
|
_ => None,
|
|
|
|
|
};
|
|
|
|
|
if [token::DotDot, token::DotDotDot, token::DotDotEq].contains(&self.token.kind) {
|
|
|
|
|
return self.parse_prefix_range_expr(attrs);
|
|
|
|
|
} else {
|
|
|
|
|
self.parse_prefix_expr(attrs)?
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
let last_type_ascription_set = self.last_type_ascription.is_some();
|
|
|
|
|
|
2019-12-03 08:04:36 +00:00
|
|
|
|
if !self.should_continue_as_assoc_expr(&lhs) {
|
|
|
|
|
self.last_type_ascription = None;
|
|
|
|
|
return Ok(lhs);
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 08:04:36 +00:00
|
|
|
|
self.expected_tokens.push(TokenType::Operator);
|
|
|
|
|
while let Some(op) = self.check_assoc_op() {
|
2019-08-11 11:14:30 +00:00
|
|
|
|
// Adjust the span for interpolated LHS to point to the `$lhs` token and not to what
|
|
|
|
|
// it refers to. Interpolated identifiers are unwrapped early and never show up here
|
|
|
|
|
// as `PrevTokenKind::Interpolated` so if LHS is a single identifier we always process
|
|
|
|
|
// it as "interpolated", it doesn't change the answer for non-interpolated idents.
|
2019-09-26 13:39:48 +00:00
|
|
|
|
let lhs_span = match (self.prev_token_kind, &lhs.kind) {
|
2019-08-11 11:14:30 +00:00
|
|
|
|
(PrevTokenKind::Interpolated, _) => self.prev_span,
|
|
|
|
|
(PrevTokenKind::Ident, &ExprKind::Path(None, ref path))
|
2019-12-22 22:42:04 +00:00
|
|
|
|
if path.segments.len() == 1 =>
|
|
|
|
|
{
|
|
|
|
|
self.prev_span
|
|
|
|
|
}
|
2019-08-11 11:14:30 +00:00
|
|
|
|
_ => lhs.span,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let cur_op_span = self.token.span;
|
|
|
|
|
let restrictions = if op.is_assign_like() {
|
|
|
|
|
self.restrictions & Restrictions::NO_STRUCT_LITERAL
|
|
|
|
|
} else {
|
|
|
|
|
self.restrictions
|
|
|
|
|
};
|
|
|
|
|
let prec = op.precedence();
|
|
|
|
|
if prec < min_prec {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
// Check for deprecated `...` syntax
|
|
|
|
|
if self.token == token::DotDotDot && op == AssocOp::DotDotEq {
|
|
|
|
|
self.err_dotdotdot_syntax(self.token.span);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-11 21:37:05 +00:00
|
|
|
|
if self.token == token::LArrow {
|
|
|
|
|
self.err_larrow_operator(self.token.span);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-11 11:14:30 +00:00
|
|
|
|
self.bump();
|
|
|
|
|
if op.is_comparison() {
|
2019-09-30 02:07:26 +00:00
|
|
|
|
if let Some(expr) = self.check_no_chained_comparison(&lhs, &op)? {
|
|
|
|
|
return Ok(expr);
|
|
|
|
|
}
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
// Special cases:
|
|
|
|
|
if op == AssocOp::As {
|
|
|
|
|
lhs = self.parse_assoc_op_cast(lhs, lhs_span, ExprKind::Cast)?;
|
2019-12-22 22:42:04 +00:00
|
|
|
|
continue;
|
2019-08-11 11:14:30 +00:00
|
|
|
|
} else if op == AssocOp::Colon {
|
2019-09-26 13:39:48 +00:00
|
|
|
|
let maybe_path = self.could_ascription_be_path(&lhs.kind);
|
2019-08-11 11:14:30 +00:00
|
|
|
|
self.last_type_ascription = Some((self.prev_span, maybe_path));
|
|
|
|
|
|
|
|
|
|
lhs = self.parse_assoc_op_cast(lhs, lhs_span, ExprKind::Type)?;
|
2019-10-30 15:38:16 +00:00
|
|
|
|
self.sess.gated_spans.gate(sym::type_ascription, lhs.span);
|
2019-12-22 22:42:04 +00:00
|
|
|
|
continue;
|
2019-08-11 11:14:30 +00:00
|
|
|
|
} else if op == AssocOp::DotDot || op == AssocOp::DotDotEq {
|
|
|
|
|
// If we didn’t have to handle `x..`/`x..=`, it would be pretty easy to
|
|
|
|
|
// generalise it to the Fixity::None code.
|
|
|
|
|
//
|
|
|
|
|
// We have 2 alternatives here: `x..y`/`x..=y` and `x..`/`x..=` The other
|
|
|
|
|
// two variants are handled with `parse_prefix_range_expr` call above.
|
|
|
|
|
let rhs = if self.is_at_start_of_range_notation_rhs() {
|
|
|
|
|
Some(self.parse_assoc_expr_with(prec + 1, LhsExpr::NotYetParsed)?)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
2019-12-22 22:42:04 +00:00
|
|
|
|
let (lhs_span, rhs_span) =
|
|
|
|
|
(lhs.span, if let Some(ref x) = rhs { x.span } else { cur_op_span });
|
|
|
|
|
let limits =
|
|
|
|
|
if op == AssocOp::DotDot { RangeLimits::HalfOpen } else { RangeLimits::Closed };
|
2019-08-11 11:14:30 +00:00
|
|
|
|
|
|
|
|
|
let r = self.mk_range(Some(lhs), rhs, limits)?;
|
2019-12-03 15:38:34 +00:00
|
|
|
|
lhs = self.mk_expr(lhs_span.to(rhs_span), r, AttrVec::new());
|
2019-12-22 22:42:04 +00:00
|
|
|
|
break;
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let fixity = op.fixity();
|
|
|
|
|
let prec_adjustment = match fixity {
|
|
|
|
|
Fixity::Right => 0,
|
|
|
|
|
Fixity::Left => 1,
|
|
|
|
|
// We currently have no non-associative operators that are not handled above by
|
|
|
|
|
// the special cases. The code is here only for future convenience.
|
|
|
|
|
Fixity::None => 1,
|
|
|
|
|
};
|
2019-12-22 22:42:04 +00:00
|
|
|
|
let rhs = self.with_res(restrictions - Restrictions::STMT_EXPR, |this| {
|
|
|
|
|
this.parse_assoc_expr_with(prec + prec_adjustment, LhsExpr::NotYetParsed)
|
|
|
|
|
})?;
|
2019-08-11 11:14:30 +00:00
|
|
|
|
|
|
|
|
|
// Make sure that the span of the parent node is larger than the span of lhs and rhs,
|
|
|
|
|
// including the attributes.
|
|
|
|
|
let lhs_span = lhs
|
|
|
|
|
.attrs
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|a| a.style == AttrStyle::Outer)
|
|
|
|
|
.next()
|
|
|
|
|
.map_or(lhs_span, |a| a.span);
|
|
|
|
|
let span = lhs_span.to(rhs.span);
|
|
|
|
|
lhs = match op {
|
2019-12-22 22:42:04 +00:00
|
|
|
|
AssocOp::Add
|
|
|
|
|
| AssocOp::Subtract
|
|
|
|
|
| AssocOp::Multiply
|
|
|
|
|
| AssocOp::Divide
|
|
|
|
|
| AssocOp::Modulus
|
|
|
|
|
| AssocOp::LAnd
|
|
|
|
|
| AssocOp::LOr
|
|
|
|
|
| AssocOp::BitXor
|
|
|
|
|
| AssocOp::BitAnd
|
|
|
|
|
| AssocOp::BitOr
|
|
|
|
|
| AssocOp::ShiftLeft
|
|
|
|
|
| AssocOp::ShiftRight
|
|
|
|
|
| AssocOp::Equal
|
|
|
|
|
| AssocOp::Less
|
|
|
|
|
| AssocOp::LessEqual
|
|
|
|
|
| AssocOp::NotEqual
|
|
|
|
|
| AssocOp::Greater
|
|
|
|
|
| AssocOp::GreaterEqual => {
|
2019-08-11 11:14:30 +00:00
|
|
|
|
let ast_op = op.to_ast_binop().unwrap();
|
|
|
|
|
let binary = self.mk_binary(source_map::respan(cur_op_span, ast_op), lhs, rhs);
|
2019-12-03 15:38:34 +00:00
|
|
|
|
self.mk_expr(span, binary, AttrVec::new())
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
2019-12-03 15:38:34 +00:00
|
|
|
|
AssocOp::Assign => self.mk_expr(span, ExprKind::Assign(lhs, rhs), AttrVec::new()),
|
2019-08-11 11:14:30 +00:00
|
|
|
|
AssocOp::AssignOp(k) => {
|
|
|
|
|
let aop = match k {
|
2019-12-22 22:42:04 +00:00
|
|
|
|
token::Plus => BinOpKind::Add,
|
|
|
|
|
token::Minus => BinOpKind::Sub,
|
|
|
|
|
token::Star => BinOpKind::Mul,
|
|
|
|
|
token::Slash => BinOpKind::Div,
|
2019-08-11 11:14:30 +00:00
|
|
|
|
token::Percent => BinOpKind::Rem,
|
2019-12-22 22:42:04 +00:00
|
|
|
|
token::Caret => BinOpKind::BitXor,
|
|
|
|
|
token::And => BinOpKind::BitAnd,
|
|
|
|
|
token::Or => BinOpKind::BitOr,
|
|
|
|
|
token::Shl => BinOpKind::Shl,
|
|
|
|
|
token::Shr => BinOpKind::Shr,
|
2019-08-11 11:14:30 +00:00
|
|
|
|
};
|
|
|
|
|
let aopexpr = self.mk_assign_op(source_map::respan(cur_op_span, aop), lhs, rhs);
|
2019-12-03 15:38:34 +00:00
|
|
|
|
self.mk_expr(span, aopexpr, AttrVec::new())
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
AssocOp::As | AssocOp::Colon | AssocOp::DotDot | AssocOp::DotDotEq => {
|
|
|
|
|
self.bug("AssocOp should have been handled by special case")
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
|
if let Fixity::None = fixity {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
if last_type_ascription_set {
|
|
|
|
|
self.last_type_ascription = None;
|
|
|
|
|
}
|
|
|
|
|
Ok(lhs)
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 08:04:36 +00:00
|
|
|
|
fn should_continue_as_assoc_expr(&mut self, lhs: &Expr) -> bool {
|
|
|
|
|
match (self.expr_is_complete(lhs), self.check_assoc_op()) {
|
|
|
|
|
// Semi-statement forms are odd:
|
|
|
|
|
// See https://github.com/rust-lang/rust/issues/29071
|
|
|
|
|
(true, None) => false,
|
|
|
|
|
(false, _) => true, // Continue parsing the expression.
|
|
|
|
|
// An exhaustive check is done in the following block, but these are checked first
|
|
|
|
|
// because they *are* ambiguous but also reasonable looking incorrect syntax, so we
|
|
|
|
|
// want to keep their span info to improve diagnostics in these cases in a later stage.
|
|
|
|
|
(true, Some(AssocOp::Multiply)) | // `{ 42 } *foo = bar;` or `{ 42 } * 3`
|
|
|
|
|
(true, Some(AssocOp::Subtract)) | // `{ 42 } -5`
|
|
|
|
|
(true, Some(AssocOp::LAnd)) | // `{ 42 } &&x` (#61475)
|
|
|
|
|
(true, Some(AssocOp::Add)) // `{ 42 } + 42
|
|
|
|
|
// If the next token is a keyword, then the tokens above *are* unambiguously incorrect:
|
|
|
|
|
// `if x { a } else { b } && if y { c } else { d }`
|
|
|
|
|
if !self.look_ahead(1, |t| t.is_reserved_ident()) => {
|
|
|
|
|
// These cases are ambiguous and can't be identified in the parser alone.
|
|
|
|
|
let sp = self.sess.source_map().start_point(self.token.span);
|
|
|
|
|
self.sess.ambiguous_block_expr_parse.borrow_mut().insert(sp, lhs.span);
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
(true, Some(ref op)) if !op.can_continue_expr_unambiguously() => false,
|
|
|
|
|
(true, Some(_)) => {
|
|
|
|
|
self.error_found_expr_would_be_stmt(lhs);
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// We've found an expression that would be parsed as a statement,
|
|
|
|
|
/// but the next token implies this should be parsed as an expression.
|
|
|
|
|
/// For example: `if let Some(x) = x { x } else { 0 } / 2`.
|
|
|
|
|
fn error_found_expr_would_be_stmt(&self, lhs: &Expr) {
|
2019-12-22 22:42:04 +00:00
|
|
|
|
let mut err = self.struct_span_err(
|
|
|
|
|
self.token.span,
|
|
|
|
|
&format!("expected expression, found `{}`", pprust::token_to_string(&self.token),),
|
|
|
|
|
);
|
2019-12-03 08:04:36 +00:00
|
|
|
|
err.span_label(self.token.span, "expected expression");
|
|
|
|
|
self.sess.expr_parentheses_needed(&mut err, lhs.span, Some(pprust::expr_to_string(&lhs)));
|
|
|
|
|
err.emit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Possibly translate the current token to an associative operator.
|
|
|
|
|
/// The method does not advance the current token.
|
|
|
|
|
///
|
|
|
|
|
/// Also performs recovery for `and` / `or` which are mistaken for `&&` and `||` respectively.
|
|
|
|
|
fn check_assoc_op(&self) -> Option<AssocOp> {
|
2019-12-03 09:19:58 +00:00
|
|
|
|
match (AssocOp::from_token(&self.token), &self.token.kind) {
|
|
|
|
|
(op @ Some(_), _) => op,
|
|
|
|
|
(None, token::Ident(sym::and, false)) => {
|
|
|
|
|
self.error_bad_logical_op("and", "&&", "conjunction");
|
|
|
|
|
Some(AssocOp::LAnd)
|
|
|
|
|
}
|
|
|
|
|
(None, token::Ident(sym::or, false)) => {
|
|
|
|
|
self.error_bad_logical_op("or", "||", "disjunction");
|
|
|
|
|
Some(AssocOp::LOr)
|
|
|
|
|
}
|
|
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Error on `and` and `or` suggesting `&&` and `||` respectively.
|
|
|
|
|
fn error_bad_logical_op(&self, bad: &str, good: &str, english: &str) {
|
|
|
|
|
self.struct_span_err(self.token.span, &format!("`{}` is not a logical operator", bad))
|
2019-12-04 10:27:11 +00:00
|
|
|
|
.span_suggestion_short(
|
2019-12-03 09:19:58 +00:00
|
|
|
|
self.token.span,
|
2019-12-04 10:27:11 +00:00
|
|
|
|
&format!("use `{}` to perform logical {}", good, english),
|
2019-12-03 09:19:58 +00:00
|
|
|
|
good.to_string(),
|
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
|
)
|
|
|
|
|
.note("unlike in e.g., python and PHP, `&&` and `||` are used for logical operators")
|
|
|
|
|
.emit();
|
2019-12-03 08:04:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-11 11:14:30 +00:00
|
|
|
|
/// Checks if this expression is a successfully parsed statement.
|
|
|
|
|
fn expr_is_complete(&self, e: &Expr) -> bool {
|
2019-12-22 22:42:04 +00:00
|
|
|
|
self.restrictions.contains(Restrictions::STMT_EXPR)
|
|
|
|
|
&& !classify::expr_requires_semi_to_be_stmt(e)
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_at_start_of_range_notation_rhs(&self) -> bool {
|
|
|
|
|
if self.token.can_begin_expr() {
|
2019-09-06 02:56:45 +00:00
|
|
|
|
// Parse `for i in 1.. { }` as infinite loop, not as `for i in (1..{})`.
|
2019-08-11 11:14:30 +00:00
|
|
|
|
if self.token == token::OpenDelim(token::Brace) {
|
|
|
|
|
return !self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL);
|
|
|
|
|
}
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 02:56:45 +00:00
|
|
|
|
/// Parses prefix-forms of range notation: `..expr`, `..`, `..=expr`.
|
2019-12-07 03:37:05 +00:00
|
|
|
|
fn parse_prefix_range_expr(&mut self, attrs: Option<AttrVec>) -> PResult<'a, P<Expr>> {
|
2019-09-06 02:56:45 +00:00
|
|
|
|
// Check for deprecated `...` syntax.
|
2019-08-11 11:14:30 +00:00
|
|
|
|
if self.token == token::DotDotDot {
|
|
|
|
|
self.err_dotdotdot_syntax(self.token.span);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
|
debug_assert!(
|
|
|
|
|
[token::DotDot, token::DotDotDot, token::DotDotEq].contains(&self.token.kind),
|
|
|
|
|
"parse_prefix_range_expr: token {:?} is not DotDot/DotDotEq",
|
|
|
|
|
self.token
|
|
|
|
|
);
|
2019-12-07 03:37:05 +00:00
|
|
|
|
|
|
|
|
|
let limits = match self.token.kind {
|
|
|
|
|
token::DotDot => RangeLimits::HalfOpen,
|
|
|
|
|
_ => RangeLimits::Closed,
|
|
|
|
|
};
|
|
|
|
|
let op = AssocOp::from_token(&self.token);
|
|
|
|
|
let attrs = self.parse_or_use_outer_attributes(attrs)?;
|
2019-08-11 11:14:30 +00:00
|
|
|
|
let lo = self.token.span;
|
|
|
|
|
self.bump();
|
2019-12-07 03:37:05 +00:00
|
|
|
|
let (span, opt_end) = if self.is_at_start_of_range_notation_rhs() {
|
2019-08-11 11:14:30 +00:00
|
|
|
|
// RHS must be parsed with more associativity than the dots.
|
2019-12-07 03:37:05 +00:00
|
|
|
|
self.parse_assoc_expr_with(op.unwrap().precedence() + 1, LhsExpr::NotYetParsed)
|
|
|
|
|
.map(|x| (lo.to(x.span), Some(x)))?
|
2019-08-11 11:14:30 +00:00
|
|
|
|
} else {
|
2019-12-07 03:37:05 +00:00
|
|
|
|
(lo, None)
|
2019-08-11 11:14:30 +00:00
|
|
|
|
};
|
2019-12-07 03:37:05 +00:00
|
|
|
|
Ok(self.mk_expr(span, self.mk_range(None, opt_end, limits)?, attrs))
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 02:56:45 +00:00
|
|
|
|
/// Parses a prefix-unary-operator expr.
|
2019-12-07 02:07:35 +00:00
|
|
|
|
fn parse_prefix_expr(&mut self, attrs: Option<AttrVec>) -> PResult<'a, P<Expr>> {
|
|
|
|
|
let attrs = self.parse_or_use_outer_attributes(attrs)?;
|
2019-08-11 11:14:30 +00:00
|
|
|
|
let lo = self.token.span;
|
|
|
|
|
// Note: when adding new unary operators, don't forget to adjust TokenKind::can_begin_expr()
|
|
|
|
|
let (hi, ex) = match self.token.kind {
|
2019-12-07 02:07:35 +00:00
|
|
|
|
token::Not => self.parse_unary_expr(lo, UnOp::Not), // `!expr`
|
|
|
|
|
token::Tilde => self.recover_tilde_expr(lo), // `~expr`
|
|
|
|
|
token::BinOp(token::Minus) => self.parse_unary_expr(lo, UnOp::Neg), // `-expr`
|
|
|
|
|
token::BinOp(token::Star) => self.parse_unary_expr(lo, UnOp::Deref), // `*expr`
|
|
|
|
|
token::BinOp(token::And) | token::AndAnd => self.parse_borrow_expr(lo),
|
|
|
|
|
token::Ident(..) if self.token.is_keyword(kw::Box) => self.parse_box_expr(lo),
|
|
|
|
|
token::Ident(..) if self.is_mistaken_not_ident_negation() => self.recover_not_expr(lo),
|
2019-12-07 01:50:22 +00:00
|
|
|
|
_ => return self.parse_dot_or_call_expr(Some(attrs)),
|
2019-12-07 02:07:35 +00:00
|
|
|
|
}?;
|
|
|
|
|
Ok(self.mk_expr(lo.to(hi), ex, attrs))
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-07 02:07:35 +00:00
|
|
|
|
fn parse_prefix_expr_common(&mut self, lo: Span) -> PResult<'a, (Span, P<Expr>)> {
|
2019-12-07 02:05:51 +00:00
|
|
|
|
self.bump();
|
|
|
|
|
let expr = self.parse_prefix_expr(None);
|
|
|
|
|
let (span, expr) = self.interpolated_or_expr_span(expr)?;
|
2019-12-07 02:07:35 +00:00
|
|
|
|
Ok((lo.to(span), expr))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_unary_expr(&mut self, lo: Span, op: UnOp) -> PResult<'a, (Span, ExprKind)> {
|
|
|
|
|
let (span, expr) = self.parse_prefix_expr_common(lo)?;
|
|
|
|
|
Ok((span, self.mk_unary(op, expr)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Recover on `!` suggesting for bitwise negation instead.
|
|
|
|
|
fn recover_tilde_expr(&mut self, lo: Span) -> PResult<'a, (Span, ExprKind)> {
|
2019-12-07 02:05:51 +00:00
|
|
|
|
self.struct_span_err(lo, "`~` cannot be used as a unary operator")
|
|
|
|
|
.span_suggestion_short(
|
|
|
|
|
lo,
|
|
|
|
|
"use `!` to perform bitwise not",
|
|
|
|
|
"!".to_owned(),
|
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
|
)
|
|
|
|
|
.emit();
|
|
|
|
|
|
2019-12-07 02:07:35 +00:00
|
|
|
|
self.parse_unary_expr(lo, UnOp::Not)
|
2019-12-07 02:00:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-07 01:55:12 +00:00
|
|
|
|
/// Parse `box expr`.
|
|
|
|
|
fn parse_box_expr(&mut self, lo: Span) -> PResult<'a, (Span, ExprKind)> {
|
2019-12-07 02:07:35 +00:00
|
|
|
|
let (span, expr) = self.parse_prefix_expr_common(lo)?;
|
2019-12-07 01:55:12 +00:00
|
|
|
|
self.sess.gated_spans.gate(sym::box_syntax, span);
|
2019-12-07 02:00:06 +00:00
|
|
|
|
Ok((span, ExprKind::Box(expr)))
|
2019-12-07 01:55:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_mistaken_not_ident_negation(&self) -> bool {
|
|
|
|
|
let token_cannot_continue_expr = |t: &Token| match t.kind {
|
|
|
|
|
// These tokens can start an expression after `!`, but
|
|
|
|
|
// can't continue an expression after an ident
|
|
|
|
|
token::Ident(name, is_raw) => token::ident_can_begin_expr(name, t.span, is_raw),
|
|
|
|
|
token::Literal(..) | token::Pound => true,
|
|
|
|
|
_ => t.is_whole_expr(),
|
|
|
|
|
};
|
|
|
|
|
self.token.is_ident_named(sym::not) && self.look_ahead(1, token_cannot_continue_expr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Recover on `not expr` in favor of `!expr`.
|
2019-12-07 01:50:22 +00:00
|
|
|
|
fn recover_not_expr(&mut self, lo: Span) -> PResult<'a, (Span, ExprKind)> {
|
2019-12-07 02:07:35 +00:00
|
|
|
|
// Emit the error...
|
|
|
|
|
let not_token = self.look_ahead(1, |t| t.clone());
|
2019-12-07 01:50:22 +00:00
|
|
|
|
self.struct_span_err(
|
2019-12-07 02:07:35 +00:00
|
|
|
|
not_token.span,
|
|
|
|
|
&format!("unexpected {} after identifier", super::token_descr(¬_token)),
|
2019-12-07 01:50:22 +00:00
|
|
|
|
)
|
|
|
|
|
.span_suggestion_short(
|
|
|
|
|
// Span the `not` plus trailing whitespace to avoid
|
|
|
|
|
// trailing whitespace after the `!` in our suggestion
|
2019-12-07 02:07:35 +00:00
|
|
|
|
self.sess.source_map().span_until_non_whitespace(lo.to(not_token.span)),
|
2019-12-07 01:50:22 +00:00
|
|
|
|
"use `!` to perform logical negation",
|
|
|
|
|
"!".to_owned(),
|
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
|
)
|
|
|
|
|
.emit();
|
2019-12-07 02:07:35 +00:00
|
|
|
|
|
|
|
|
|
// ...and recover!
|
|
|
|
|
self.parse_unary_expr(lo, UnOp::Not)
|
2019-12-07 01:50:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-11 11:14:30 +00:00
|
|
|
|
/// Returns the span of expr, if it was not interpolated or the span of the interpolated token.
|
|
|
|
|
fn interpolated_or_expr_span(
|
|
|
|
|
&self,
|
|
|
|
|
expr: PResult<'a, P<Expr>>,
|
|
|
|
|
) -> PResult<'a, (Span, P<Expr>)> {
|
|
|
|
|
expr.map(|e| {
|
|
|
|
|
if self.prev_token_kind == PrevTokenKind::Interpolated {
|
|
|
|
|
(self.prev_span, e)
|
|
|
|
|
} else {
|
|
|
|
|
(e.span, e)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
|
fn parse_assoc_op_cast(
|
|
|
|
|
&mut self,
|
|
|
|
|
lhs: P<Expr>,
|
|
|
|
|
lhs_span: Span,
|
|
|
|
|
expr_kind: fn(P<Expr>, P<Ty>) -> ExprKind,
|
|
|
|
|
) -> PResult<'a, P<Expr>> {
|
2019-08-11 11:14:30 +00:00
|
|
|
|
let mk_expr = |this: &mut Self, rhs: P<Ty>| {
|
2019-12-03 15:38:34 +00:00
|
|
|
|
this.mk_expr(lhs_span.to(rhs.span), expr_kind(lhs, rhs), AttrVec::new())
|
2019-08-11 11:14:30 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Save the state of the parser before parsing type normally, in case there is a
|
|
|
|
|
// LessThan comparison after this cast.
|
|
|
|
|
let parser_snapshot_before_type = self.clone();
|
|
|
|
|
match self.parse_ty_no_plus() {
|
2019-12-22 22:42:04 +00:00
|
|
|
|
Ok(rhs) => Ok(mk_expr(self, rhs)),
|
2019-08-11 11:14:30 +00:00
|
|
|
|
Err(mut type_err) => {
|
|
|
|
|
// Rewind to before attempting to parse the type with generics, to recover
|
|
|
|
|
// from situations like `x as usize < y` in which we first tried to parse
|
|
|
|
|
// `usize < y` as a type with generic arguments.
|
|
|
|
|
let parser_snapshot_after_type = self.clone();
|
|
|
|
|
mem::replace(self, parser_snapshot_before_type);
|
|
|
|
|
|
|
|
|
|
match self.parse_path(PathStyle::Expr) {
|
|
|
|
|
Ok(path) => {
|
|
|
|
|
let (op_noun, op_verb) = match self.token.kind {
|
|
|
|
|
token::Lt => ("comparison", "comparing"),
|
|
|
|
|
token::BinOp(token::Shl) => ("shift", "shifting"),
|
|
|
|
|
_ => {
|
|
|
|
|
// We can end up here even without `<` being the next token, for
|
|
|
|
|
// example because `parse_ty_no_plus` returns `Err` on keywords,
|
|
|
|
|
// but `parse_path` returns `Ok` on them due to error recovery.
|
|
|
|
|
// Return original error and parser state.
|
|
|
|
|
mem::replace(self, parser_snapshot_after_type);
|
|
|
|
|
return Err(type_err);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Successfully parsed the type path leaving a `<` yet to parse.
|
|
|
|
|
type_err.cancel();
|
|
|
|
|
|
|
|
|
|
// Report non-fatal diagnostics, keep `x as usize` as an expression
|
|
|
|
|
// in AST and continue parsing.
|
2019-10-08 20:17:46 +00:00
|
|
|
|
let msg = format!(
|
|
|
|
|
"`<` is interpreted as a start of generic arguments for `{}`, not a {}",
|
|
|
|
|
pprust::path_to_string(&path),
|
|
|
|
|
op_noun,
|
|
|
|
|
);
|
2019-08-11 11:14:30 +00:00
|
|
|
|
let span_after_type = parser_snapshot_after_type.token.span;
|
2019-12-07 01:40:48 +00:00
|
|
|
|
let expr = mk_expr(self, self.mk_ty(path.span, TyKind::Path(None, path)));
|
2019-08-11 11:14:30 +00:00
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
|
let expr_str = self
|
|
|
|
|
.span_to_snippet(expr.span)
|
2019-08-11 11:14:30 +00:00
|
|
|
|
.unwrap_or_else(|_| pprust::expr_to_string(&expr));
|
|
|
|
|
|
|
|
|
|
self.struct_span_err(self.token.span, &msg)
|
|
|
|
|
.span_label(
|
|
|
|
|
self.look_ahead(1, |t| t.span).to(span_after_type),
|
2019-12-22 22:42:04 +00:00
|
|
|
|
"interpreted as generic arguments",
|
2019-08-11 11:14:30 +00:00
|
|
|
|
)
|
|
|
|
|
.span_label(self.token.span, format!("not interpreted as {}", op_noun))
|
|
|
|
|
.span_suggestion(
|
|
|
|
|
expr.span,
|
|
|
|
|
&format!("try {} the cast value", op_verb),
|
|
|
|
|
format!("({})", expr_str),
|
2019-09-06 02:56:45 +00:00
|
|
|
|
Applicability::MachineApplicable,
|
2019-08-11 11:14:30 +00:00
|
|
|
|
)
|
|
|
|
|
.emit();
|
|
|
|
|
|
|
|
|
|
Ok(expr)
|
|
|
|
|
}
|
|
|
|
|
Err(mut path_err) => {
|
|
|
|
|
// Couldn't parse as a path, return original error and parser state.
|
|
|
|
|
path_err.cancel();
|
|
|
|
|
mem::replace(self, parser_snapshot_after_type);
|
|
|
|
|
Err(type_err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-23 14:22:00 +00:00
|
|
|
|
/// Parse `& mut? <expr>` or `& raw [ const | mut ] <expr>`.
|
2019-12-07 01:37:03 +00:00
|
|
|
|
fn parse_borrow_expr(&mut self, lo: Span) -> PResult<'a, (Span, ExprKind)> {
|
2019-11-23 14:15:49 +00:00
|
|
|
|
self.expect_and()?;
|
2019-12-07 01:37:03 +00:00
|
|
|
|
let (borrow_kind, mutbl) = self.parse_borrow_modifiers(lo);
|
|
|
|
|
let expr = self.parse_prefix_expr(None);
|
|
|
|
|
let (span, expr) = self.interpolated_or_expr_span(expr)?;
|
|
|
|
|
Ok((lo.to(span), ExprKind::AddrOf(borrow_kind, mutbl, expr)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Parse `mut?` or `raw [ const | mut ]`.
|
|
|
|
|
fn parse_borrow_modifiers(&mut self, lo: Span) -> (ast::BorrowKind, ast::Mutability) {
|
|
|
|
|
if self.check_keyword(kw::Raw) && self.look_ahead(1, Token::is_mutability) {
|
|
|
|
|
// `raw [ const | mut ]`.
|
2019-11-23 14:15:49 +00:00
|
|
|
|
let found_raw = self.eat_keyword(kw::Raw);
|
|
|
|
|
assert!(found_raw);
|
|
|
|
|
let mutability = self.parse_const_or_mut().unwrap();
|
|
|
|
|
self.sess.gated_spans.gate(sym::raw_ref_op, lo.to(self.prev_span));
|
|
|
|
|
(ast::BorrowKind::Raw, mutability)
|
|
|
|
|
} else {
|
2019-12-07 01:37:03 +00:00
|
|
|
|
// `mut?`
|
2019-11-23 14:15:49 +00:00
|
|
|
|
(ast::BorrowKind::Ref, self.parse_mutability())
|
2019-12-07 01:37:03 +00:00
|
|
|
|
}
|
2019-11-23 14:15:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-11 11:14:30 +00:00
|
|
|
|
/// Parses `a.b` or `a(13)` or `a[4]` or just `a`.
|
2019-12-07 01:30:54 +00:00
|
|
|
|
fn parse_dot_or_call_expr(&mut self, attrs: Option<AttrVec>) -> PResult<'a, P<Expr>> {
|
|
|
|
|
let attrs = self.parse_or_use_outer_attributes(attrs)?;
|
|
|
|
|
let base = self.parse_bottom_expr();
|
|
|
|
|
let (span, base) = self.interpolated_or_expr_span(base)?;
|
|
|
|
|
self.parse_dot_or_call_expr_with(base, span, attrs)
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn parse_dot_or_call_expr_with(
|
|
|
|
|
&mut self,
|
|
|
|
|
e0: P<Expr>,
|
|
|
|
|
lo: Span,
|
2019-12-03 15:38:34 +00:00
|
|
|
|
mut attrs: AttrVec,
|
2019-08-11 11:14:30 +00:00
|
|
|
|
) -> PResult<'a, P<Expr>> {
|
|
|
|
|
// Stitch the list of outer attributes onto the return value.
|
|
|
|
|
// A little bit ugly, but the best way given the current code
|
|
|
|
|
// structure
|
2019-12-22 22:42:04 +00:00
|
|
|
|
self.parse_dot_or_call_expr_with_(e0, lo).map(|expr| {
|
2019-08-11 11:14:30 +00:00
|
|
|
|
expr.map(|mut expr| {
|
|
|
|
|
attrs.extend::<Vec<_>>(expr.attrs.into());
|
|
|
|
|
expr.attrs = attrs;
|
2019-12-04 07:10:41 +00:00
|
|
|
|
self.error_attr_on_if_expr(&expr);
|
2019-08-11 11:14:30 +00:00
|
|
|
|
expr
|
|
|
|
|
})
|
2019-12-22 22:42:04 +00:00
|
|
|
|
})
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-04 07:10:41 +00:00
|
|
|
|
fn error_attr_on_if_expr(&self, expr: &Expr) {
|
|
|
|
|
if let (ExprKind::If(..), [a0, ..]) = (&expr.kind, &*expr.attrs) {
|
|
|
|
|
// Just point to the first attribute in there...
|
|
|
|
|
self.struct_span_err(a0.span, "attributes are not yet allowed on `if` expressions")
|
|
|
|
|
.emit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-07 01:01:58 +00:00
|
|
|
|
fn parse_dot_or_call_expr_with_(&mut self, mut e: P<Expr>, lo: Span) -> PResult<'a, P<Expr>> {
|
2019-08-11 11:14:30 +00:00
|
|
|
|
loop {
|
2019-12-07 01:01:58 +00:00
|
|
|
|
if self.eat(&token::Question) {
|
|
|
|
|
// `expr?`
|
|
|
|
|
e = self.mk_expr(lo.to(self.prev_span), ExprKind::Try(e), AttrVec::new());
|
|
|
|
|
continue;
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
if self.eat(&token::Dot) {
|
2019-12-07 01:01:58 +00:00
|
|
|
|
// expr.f
|
2019-12-07 00:52:53 +00:00
|
|
|
|
e = self.parse_dot_suffix_expr(lo, e)?;
|
2019-08-11 11:14:30 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
|
if self.expr_is_complete(&e) {
|
2019-12-07 01:01:58 +00:00
|
|
|
|
return Ok(e);
|
2019-12-22 22:42:04 +00:00
|
|
|
|
}
|
2019-12-07 01:01:58 +00:00
|
|
|
|
e = match self.token.kind {
|
|
|
|
|
token::OpenDelim(token::Paren) => self.parse_fn_call_expr(lo, e),
|
|
|
|
|
token::OpenDelim(token::Bracket) => self.parse_index_expr(lo, e)?,
|
2019-12-22 22:42:04 +00:00
|
|
|
|
_ => return Ok(e),
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-07 00:52:53 +00:00
|
|
|
|
fn parse_dot_suffix_expr(&mut self, lo: Span, base: P<Expr>) -> PResult<'a, P<Expr>> {
|
|
|
|
|
match self.token.kind {
|
|
|
|
|
token::Ident(..) => self.parse_dot_suffix(base, lo),
|
|
|
|
|
token::Literal(token::Lit { kind: token::Integer, symbol, suffix }) => {
|
|
|
|
|
Ok(self.parse_tuple_field_access_expr(lo, base, symbol, suffix))
|
|
|
|
|
}
|
|
|
|
|
token::Literal(token::Lit { kind: token::Float, symbol, .. }) => {
|
|
|
|
|
self.recover_field_access_by_float_lit(lo, base, symbol)
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
self.error_unexpected_after_dot();
|
|
|
|
|
Ok(base)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-06 23:59:56 +00:00
|
|
|
|
fn error_unexpected_after_dot(&self) {
|
|
|
|
|
// FIXME Could factor this out into non_fatal_unexpected or something.
|
2019-12-07 02:07:35 +00:00
|
|
|
|
let actual = pprust::token_to_string(&self.token);
|
2019-12-06 23:59:56 +00:00
|
|
|
|
self.struct_span_err(self.token.span, &format!("unexpected token: `{}`", actual)).emit();
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-06 23:34:32 +00:00
|
|
|
|
fn recover_field_access_by_float_lit(
|
|
|
|
|
&mut self,
|
|
|
|
|
lo: Span,
|
2019-12-07 00:52:53 +00:00
|
|
|
|
base: P<Expr>,
|
2019-12-06 23:34:32 +00:00
|
|
|
|
sym: Symbol,
|
2019-12-07 00:52:53 +00:00
|
|
|
|
) -> PResult<'a, P<Expr>> {
|
2019-12-06 23:34:32 +00:00
|
|
|
|
self.bump();
|
|
|
|
|
|
|
|
|
|
let fstr = sym.as_str();
|
|
|
|
|
let msg = format!("unexpected token: `{}`", sym);
|
|
|
|
|
|
|
|
|
|
let mut err = self.struct_span_err(self.prev_span, &msg);
|
|
|
|
|
err.span_label(self.prev_span, "unexpected token");
|
|
|
|
|
|
|
|
|
|
if fstr.chars().all(|x| "0123456789.".contains(x)) {
|
2019-12-07 00:52:53 +00:00
|
|
|
|
let float = match fstr.parse::<f64>() {
|
|
|
|
|
Ok(f) => f,
|
|
|
|
|
Err(_) => {
|
|
|
|
|
err.emit();
|
|
|
|
|
return Ok(base);
|
|
|
|
|
}
|
|
|
|
|
};
|
2019-12-06 23:34:32 +00:00
|
|
|
|
let sugg = pprust::to_string(|s| {
|
|
|
|
|
s.popen();
|
|
|
|
|
s.print_expr(&base);
|
|
|
|
|
s.s.word(".");
|
|
|
|
|
s.print_usize(float.trunc() as usize);
|
|
|
|
|
s.pclose();
|
|
|
|
|
s.s.word(".");
|
|
|
|
|
s.s.word(fstr.splitn(2, ".").last().unwrap().to_string())
|
|
|
|
|
});
|
|
|
|
|
err.span_suggestion(
|
|
|
|
|
lo.to(self.prev_span),
|
|
|
|
|
"try parenthesizing the first index",
|
|
|
|
|
sugg,
|
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-12-07 00:52:53 +00:00
|
|
|
|
Err(err)
|
2019-12-06 23:34:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-06 23:16:19 +00:00
|
|
|
|
fn parse_tuple_field_access_expr(
|
|
|
|
|
&mut self,
|
|
|
|
|
lo: Span,
|
|
|
|
|
base: P<Expr>,
|
|
|
|
|
field: Symbol,
|
|
|
|
|
suffix: Option<Symbol>,
|
|
|
|
|
) -> P<Expr> {
|
|
|
|
|
let span = self.token.span;
|
|
|
|
|
self.bump();
|
|
|
|
|
let field = ExprKind::Field(base, Ident::new(field, span));
|
|
|
|
|
self.expect_no_suffix(span, "a tuple index", suffix);
|
|
|
|
|
self.mk_expr(lo.to(span), field, AttrVec::new())
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-06 23:08:44 +00:00
|
|
|
|
/// Parse a function call expression, `expr(...)`.
|
|
|
|
|
fn parse_fn_call_expr(&mut self, lo: Span, fun: P<Expr>) -> P<Expr> {
|
|
|
|
|
let seq = self.parse_paren_expr_seq().map(|args| {
|
|
|
|
|
self.mk_expr(lo.to(self.prev_span), self.mk_call(fun, args), AttrVec::new())
|
|
|
|
|
});
|
|
|
|
|
self.recover_seq_parse_error(token::Paren, lo, seq)
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-06 23:04:46 +00:00
|
|
|
|
/// Parse an indexing expression `expr[...]`.
|
|
|
|
|
fn parse_index_expr(&mut self, lo: Span, base: P<Expr>) -> PResult<'a, P<Expr>> {
|
|
|
|
|
self.bump(); // `[`
|
|
|
|
|
let index = self.parse_expr()?;
|
|
|
|
|
self.expect(&token::CloseDelim(token::Bracket))?;
|
|
|
|
|
Ok(self.mk_expr(lo.to(self.prev_span), self.mk_index(base, index), AttrVec::new()))
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-11 11:14:30 +00:00
|
|
|
|
/// Assuming we have just parsed `.`, continue parsing into an expression.
|
|
|
|
|
fn parse_dot_suffix(&mut self, self_arg: P<Expr>, lo: Span) -> PResult<'a, P<Expr>> {
|
|
|
|
|
if self.token.span.rust_2018() && self.eat_keyword(kw::Await) {
|
|
|
|
|
return self.mk_await_expr(self_arg, lo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let segment = self.parse_path_segment(PathStyle::Expr)?;
|
|
|
|
|
self.check_trailing_angle_brackets(&segment, token::OpenDelim(token::Paren));
|
|
|
|
|
|
2019-12-06 23:04:46 +00:00
|
|
|
|
if self.check(&token::OpenDelim(token::Paren)) {
|
|
|
|
|
// Method call `expr.f()`
|
|
|
|
|
let mut args = self.parse_paren_expr_seq()?;
|
|
|
|
|
args.insert(0, self_arg);
|
2019-08-11 11:14:30 +00:00
|
|
|
|
|
2019-12-06 23:04:46 +00:00
|
|
|
|
let span = lo.to(self.prev_span);
|
|
|
|
|
Ok(self.mk_expr(span, ExprKind::MethodCall(segment, args), AttrVec::new()))
|
|
|
|
|
} else {
|
|
|
|
|
// Field access `expr.f`
|
|
|
|
|
if let Some(args) = segment.args {
|
|
|
|
|
self.span_err(args.span(), "field expressions may not have generic arguments");
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-06 23:04:46 +00:00
|
|
|
|
let span = lo.to(self.prev_span);
|
|
|
|
|
Ok(self.mk_expr(span, ExprKind::Field(self_arg, segment.ident), AttrVec::new()))
|
|
|
|
|
}
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// At the bottom (top?) of the precedence hierarchy,
|
|
|
|
|
/// Parses things like parenthesized exprs, macros, `return`, etc.
|
|
|
|
|
///
|
|
|
|
|
/// N.B., this does not parse outer attributes, and is private because it only works
|
|
|
|
|
/// correctly if called from `parse_dot_or_call_expr()`.
|
|
|
|
|
fn parse_bottom_expr(&mut self) -> PResult<'a, P<Expr>> {
|
|
|
|
|
maybe_recover_from_interpolated_ty_qpath!(self, true);
|
|
|
|
|
maybe_whole_expr!(self);
|
|
|
|
|
|
|
|
|
|
// Outer attributes are already parsed and will be
|
|
|
|
|
// added to the return value after the fact.
|
|
|
|
|
//
|
|
|
|
|
// Therefore, prevent sub-parser from parsing
|
2019-09-06 02:56:45 +00:00
|
|
|
|
// attributes by giving them a empty "already-parsed" list.
|
2019-12-03 15:38:34 +00:00
|
|
|
|
let attrs = AttrVec::new();
|
2019-08-11 11:14:30 +00:00
|
|
|
|
|
2019-09-06 02:56:45 +00:00
|
|
|
|
// Note: when adding new syntax here, don't forget to adjust `TokenKind::can_begin_expr()`.
|
2019-12-03 14:31:45 +00:00
|
|
|
|
let lo = self.token.span;
|
2019-12-03 15:38:08 +00:00
|
|
|
|
if let token::Literal(_) = self.token.kind {
|
2019-08-11 11:14:30 +00:00
|
|
|
|
// This match arm is a special-case of the `_` match arm below and
|
|
|
|
|
// could be removed without changing functionality, but it's faster
|
|
|
|
|
// to have it here, especially for programs with large constants.
|
2019-12-03 15:38:08 +00:00
|
|
|
|
self.parse_lit_expr(attrs)
|
|
|
|
|
} else if self.check(&token::OpenDelim(token::Paren)) {
|
|
|
|
|
self.parse_tuple_parens_expr(attrs)
|
|
|
|
|
} else if self.check(&token::OpenDelim(token::Brace)) {
|
|
|
|
|
self.parse_block_expr(None, lo, BlockCheckMode::Default, attrs)
|
|
|
|
|
} else if self.check(&token::BinOp(token::Or)) || self.check(&token::OrOr) {
|
|
|
|
|
self.parse_closure_expr(attrs)
|
|
|
|
|
} else if self.check(&token::OpenDelim(token::Bracket)) {
|
|
|
|
|
self.parse_array_or_repeat_expr(attrs)
|
|
|
|
|
} else if self.eat_lt() {
|
|
|
|
|
let (qself, path) = self.parse_qpath(PathStyle::Expr)?;
|
|
|
|
|
Ok(self.mk_expr(lo.to(path.span), ExprKind::Path(Some(qself), path), attrs))
|
|
|
|
|
} else if self.token.is_path_start() {
|
|
|
|
|
self.parse_path_start_expr(attrs)
|
|
|
|
|
} else if self.check_keyword(kw::Move) || self.check_keyword(kw::Static) {
|
|
|
|
|
self.parse_closure_expr(attrs)
|
|
|
|
|
} else if self.eat_keyword(kw::If) {
|
|
|
|
|
self.parse_if_expr(attrs)
|
|
|
|
|
} else if self.eat_keyword(kw::For) {
|
|
|
|
|
self.parse_for_expr(None, self.prev_span, attrs)
|
|
|
|
|
} else if self.eat_keyword(kw::While) {
|
|
|
|
|
self.parse_while_expr(None, self.prev_span, attrs)
|
|
|
|
|
} else if let Some(label) = self.eat_label() {
|
|
|
|
|
self.parse_labeled_expr(label, attrs)
|
|
|
|
|
} else if self.eat_keyword(kw::Loop) {
|
|
|
|
|
self.parse_loop_expr(None, self.prev_span, attrs)
|
|
|
|
|
} else if self.eat_keyword(kw::Continue) {
|
|
|
|
|
let kind = ExprKind::Continue(self.eat_label());
|
|
|
|
|
Ok(self.mk_expr(lo.to(self.prev_span), kind, attrs))
|
|
|
|
|
} else if self.eat_keyword(kw::Match) {
|
|
|
|
|
let match_sp = self.prev_span;
|
|
|
|
|
self.parse_match_expr(attrs).map_err(|mut err| {
|
|
|
|
|
err.span_label(match_sp, "while parsing this match expression");
|
|
|
|
|
err
|
|
|
|
|
})
|
|
|
|
|
} else if self.eat_keyword(kw::Unsafe) {
|
|
|
|
|
self.parse_block_expr(None, lo, BlockCheckMode::Unsafe(ast::UserProvided), attrs)
|
|
|
|
|
} else if self.is_do_catch_block() {
|
|
|
|
|
self.recover_do_catch(attrs)
|
|
|
|
|
} else if self.is_try_block() {
|
|
|
|
|
self.expect_keyword(kw::Try)?;
|
|
|
|
|
self.parse_try_block(lo, attrs)
|
|
|
|
|
} else if self.eat_keyword(kw::Return) {
|
|
|
|
|
self.parse_return_expr(attrs)
|
|
|
|
|
} else if self.eat_keyword(kw::Break) {
|
|
|
|
|
self.parse_break_expr(attrs)
|
|
|
|
|
} else if self.eat_keyword(kw::Yield) {
|
|
|
|
|
self.parse_yield_expr(attrs)
|
|
|
|
|
} else if self.eat_keyword(kw::Let) {
|
|
|
|
|
self.parse_let_expr(attrs)
|
|
|
|
|
} else if !self.unclosed_delims.is_empty() && self.check(&token::Semi) {
|
|
|
|
|
// Don't complain about bare semicolons after unclosed braces
|
|
|
|
|
// recovery in order to keep the error count down. Fixing the
|
|
|
|
|
// delimiters will possibly also fix the bare semicolon found in
|
|
|
|
|
// expression context. For example, silence the following error:
|
|
|
|
|
//
|
|
|
|
|
// error: expected expression, found `;`
|
|
|
|
|
// --> file.rs:2:13
|
|
|
|
|
// |
|
|
|
|
|
// 2 | foo(bar(;
|
|
|
|
|
// | ^ expected expression
|
|
|
|
|
self.bump();
|
|
|
|
|
Ok(self.mk_expr_err(self.token.span))
|
|
|
|
|
} else if self.token.span.rust_2018() {
|
|
|
|
|
// `Span::rust_2018()` is somewhat expensive; don't get it repeatedly.
|
|
|
|
|
if self.check_keyword(kw::Async) {
|
2019-12-22 22:42:04 +00:00
|
|
|
|
if self.is_async_block() {
|
|
|
|
|
// Check for `async {` and `async move {`.
|
2019-12-03 15:38:08 +00:00
|
|
|
|
self.parse_async_block(attrs)
|
2019-08-11 11:14:30 +00:00
|
|
|
|
} else {
|
2019-12-03 15:38:08 +00:00
|
|
|
|
self.parse_closure_expr(attrs)
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
2019-12-03 15:38:08 +00:00
|
|
|
|
} else if self.eat_keyword(kw::Await) {
|
|
|
|
|
self.recover_incorrect_await_syntax(lo, self.prev_span, attrs)
|
|
|
|
|
} else {
|
|
|
|
|
self.parse_lit_expr(attrs)
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
2019-12-03 15:38:08 +00:00
|
|
|
|
} else {
|
|
|
|
|
self.parse_lit_expr(attrs)
|
2019-12-03 14:31:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-11 11:14:30 +00:00
|
|
|
|
|
2019-12-03 15:38:34 +00:00
|
|
|
|
fn parse_lit_expr(&mut self, attrs: AttrVec) -> PResult<'a, P<Expr>> {
|
2019-12-03 14:31:45 +00:00
|
|
|
|
let lo = self.token.span;
|
|
|
|
|
match self.parse_opt_lit() {
|
|
|
|
|
Some(literal) => {
|
|
|
|
|
let expr = self.mk_expr(lo.to(self.prev_span), ExprKind::Lit(literal), attrs);
|
|
|
|
|
self.maybe_recover_from_bad_qpath(expr, true)
|
|
|
|
|
}
|
|
|
|
|
None => return Err(self.expected_expression_found()),
|
|
|
|
|
}
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 15:38:34 +00:00
|
|
|
|
fn parse_tuple_parens_expr(&mut self, mut attrs: AttrVec) -> PResult<'a, P<Expr>> {
|
2019-12-03 10:36:40 +00:00
|
|
|
|
let lo = self.token.span;
|
2019-12-04 09:13:29 +00:00
|
|
|
|
self.expect(&token::OpenDelim(token::Paren))?;
|
|
|
|
|
attrs.extend(self.parse_inner_attributes()?); // `(#![foo] a, b, ...)` is OK.
|
|
|
|
|
let (es, trailing_comma) = match self.parse_seq_to_end(
|
|
|
|
|
&token::CloseDelim(token::Paren),
|
|
|
|
|
SeqSep::trailing_allowed(token::Comma),
|
|
|
|
|
|p| p.parse_expr_catch_underscore(),
|
|
|
|
|
) {
|
2019-12-03 10:36:40 +00:00
|
|
|
|
Ok(x) => x,
|
|
|
|
|
Err(err) => return Ok(self.recover_seq_parse_error(token::Paren, lo, Err(err))),
|
|
|
|
|
};
|
|
|
|
|
let kind = if es.len() == 1 && !trailing_comma {
|
|
|
|
|
// `(e)` is parenthesized `e`.
|
|
|
|
|
ExprKind::Paren(es.into_iter().nth(0).unwrap())
|
|
|
|
|
} else {
|
|
|
|
|
// `(e,)` is a tuple with only one field, `e`.
|
|
|
|
|
ExprKind::Tup(es)
|
|
|
|
|
};
|
|
|
|
|
let expr = self.mk_expr(lo.to(self.prev_span), kind, attrs);
|
|
|
|
|
self.maybe_recover_from_bad_qpath(expr, true)
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 15:38:34 +00:00
|
|
|
|
fn parse_array_or_repeat_expr(&mut self, mut attrs: AttrVec) -> PResult<'a, P<Expr>> {
|
2019-12-03 10:49:56 +00:00
|
|
|
|
let lo = self.token.span;
|
|
|
|
|
self.bump(); // `[`
|
|
|
|
|
|
2019-12-03 12:14:50 +00:00
|
|
|
|
attrs.extend(self.parse_inner_attributes()?);
|
2019-12-03 10:49:56 +00:00
|
|
|
|
|
2019-12-04 09:13:29 +00:00
|
|
|
|
let close = &token::CloseDelim(token::Bracket);
|
|
|
|
|
let kind = if self.eat(close) {
|
2019-12-03 10:49:56 +00:00
|
|
|
|
// Empty vector
|
|
|
|
|
ExprKind::Array(Vec::new())
|
|
|
|
|
} else {
|
|
|
|
|
// Non-empty vector
|
|
|
|
|
let first_expr = self.parse_expr()?;
|
|
|
|
|
if self.eat(&token::Semi) {
|
|
|
|
|
// Repeating array syntax: `[ 0; 512 ]`
|
2019-12-08 07:19:53 +00:00
|
|
|
|
let count = self.parse_anon_const_expr()?;
|
2019-12-04 09:13:29 +00:00
|
|
|
|
self.expect(close)?;
|
2019-12-03 10:49:56 +00:00
|
|
|
|
ExprKind::Repeat(first_expr, count)
|
|
|
|
|
} else if self.eat(&token::Comma) {
|
|
|
|
|
// Vector with two or more elements.
|
2019-12-04 09:13:29 +00:00
|
|
|
|
let sep = SeqSep::trailing_allowed(token::Comma);
|
|
|
|
|
let (remaining_exprs, _) = self.parse_seq_to_end(close, sep, |p| p.parse_expr())?;
|
2019-12-03 10:49:56 +00:00
|
|
|
|
let mut exprs = vec![first_expr];
|
|
|
|
|
exprs.extend(remaining_exprs);
|
|
|
|
|
ExprKind::Array(exprs)
|
|
|
|
|
} else {
|
|
|
|
|
// Vector with one element
|
2019-12-04 09:13:29 +00:00
|
|
|
|
self.expect(close)?;
|
2019-12-03 10:49:56 +00:00
|
|
|
|
ExprKind::Array(vec![first_expr])
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
let expr = self.mk_expr(lo.to(self.prev_span), kind, attrs);
|
|
|
|
|
self.maybe_recover_from_bad_qpath(expr, true)
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 15:38:34 +00:00
|
|
|
|
fn parse_path_start_expr(&mut self, attrs: AttrVec) -> PResult<'a, P<Expr>> {
|
2019-12-03 11:43:45 +00:00
|
|
|
|
let lo = self.token.span;
|
|
|
|
|
let path = self.parse_path(PathStyle::Expr)?;
|
|
|
|
|
|
|
|
|
|
// `!`, as an operator, is prefix, so we know this isn't that.
|
|
|
|
|
let (hi, kind) = if self.eat(&token::Not) {
|
|
|
|
|
// MACRO INVOCATION expression
|
|
|
|
|
let mac = Mac {
|
|
|
|
|
path,
|
|
|
|
|
args: self.parse_mac_args()?,
|
|
|
|
|
prior_type_ascription: self.last_type_ascription,
|
|
|
|
|
};
|
|
|
|
|
(self.prev_span, ExprKind::Mac(mac))
|
|
|
|
|
} else if self.check(&token::OpenDelim(token::Brace)) {
|
|
|
|
|
if let Some(expr) = self.maybe_parse_struct_expr(lo, &path, &attrs) {
|
|
|
|
|
return expr;
|
|
|
|
|
} else {
|
|
|
|
|
(path.span, ExprKind::Path(None, path))
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
(path.span, ExprKind::Path(None, path))
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let expr = self.mk_expr(lo.to(hi), kind, attrs);
|
|
|
|
|
self.maybe_recover_from_bad_qpath(expr, true)
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 15:38:34 +00:00
|
|
|
|
fn parse_labeled_expr(&mut self, label: Label, attrs: AttrVec) -> PResult<'a, P<Expr>> {
|
2019-12-03 11:48:08 +00:00
|
|
|
|
let lo = label.ident.span;
|
|
|
|
|
self.expect(&token::Colon)?;
|
|
|
|
|
if self.eat_keyword(kw::While) {
|
2019-12-22 22:42:04 +00:00
|
|
|
|
return self.parse_while_expr(Some(label), lo, attrs);
|
2019-12-03 11:48:08 +00:00
|
|
|
|
}
|
|
|
|
|
if self.eat_keyword(kw::For) {
|
2019-12-22 22:42:04 +00:00
|
|
|
|
return self.parse_for_expr(Some(label), lo, attrs);
|
2019-12-03 11:48:08 +00:00
|
|
|
|
}
|
|
|
|
|
if self.eat_keyword(kw::Loop) {
|
2019-12-22 22:42:04 +00:00
|
|
|
|
return self.parse_loop_expr(Some(label), lo, attrs);
|
2019-12-03 11:48:08 +00:00
|
|
|
|
}
|
|
|
|
|
if self.token == token::OpenDelim(token::Brace) {
|
|
|
|
|
return self.parse_block_expr(Some(label), lo, BlockCheckMode::Default, attrs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let msg = "expected `while`, `for`, `loop` or `{` after a label";
|
2019-12-22 22:42:04 +00:00
|
|
|
|
self.struct_span_err(self.token.span, msg).span_label(self.token.span, msg).emit();
|
2019-12-03 12:11:34 +00:00
|
|
|
|
// Continue as an expression in an effort to recover on `'label: non_block_expr`.
|
|
|
|
|
self.parse_expr()
|
2019-12-03 11:48:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 12:35:05 +00:00
|
|
|
|
/// Recover on the syntax `do catch { ... }` suggesting `try { ... }` instead.
|
2019-12-03 15:38:34 +00:00
|
|
|
|
fn recover_do_catch(&mut self, attrs: AttrVec) -> PResult<'a, P<Expr>> {
|
2019-12-03 12:35:05 +00:00
|
|
|
|
let lo = self.token.span;
|
|
|
|
|
|
|
|
|
|
self.bump(); // `do`
|
|
|
|
|
self.bump(); // `catch`
|
|
|
|
|
|
|
|
|
|
let span_dc = lo.to(self.prev_span);
|
|
|
|
|
self.struct_span_err(span_dc, "found removed `do catch` syntax")
|
|
|
|
|
.span_suggestion(
|
|
|
|
|
span_dc,
|
|
|
|
|
"replace with the new syntax",
|
|
|
|
|
"try".to_string(),
|
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
|
)
|
|
|
|
|
.note("following RFC #2388, the new non-placeholder syntax is `try`")
|
|
|
|
|
.emit();
|
|
|
|
|
|
|
|
|
|
self.parse_try_block(lo, attrs)
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 13:01:24 +00:00
|
|
|
|
/// Parse an expression if the token can begin one.
|
|
|
|
|
fn parse_expr_opt(&mut self) -> PResult<'a, Option<P<Expr>>> {
|
2019-12-22 22:42:04 +00:00
|
|
|
|
Ok(if self.token.can_begin_expr() { Some(self.parse_expr()?) } else { None })
|
2019-12-03 13:01:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Parse `"return" expr?`.
|
2019-12-03 15:38:34 +00:00
|
|
|
|
fn parse_return_expr(&mut self, attrs: AttrVec) -> PResult<'a, P<Expr>> {
|
2019-12-03 13:01:24 +00:00
|
|
|
|
let lo = self.prev_span;
|
|
|
|
|
let kind = ExprKind::Ret(self.parse_expr_opt()?);
|
|
|
|
|
let expr = self.mk_expr(lo.to(self.prev_span), kind, attrs);
|
|
|
|
|
self.maybe_recover_from_bad_qpath(expr, true)
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 14:06:34 +00:00
|
|
|
|
/// Parse `"('label ":")? break expr?`.
|
2019-12-03 15:38:34 +00:00
|
|
|
|
fn parse_break_expr(&mut self, attrs: AttrVec) -> PResult<'a, P<Expr>> {
|
2019-12-03 14:06:34 +00:00
|
|
|
|
let lo = self.prev_span;
|
|
|
|
|
let label = self.eat_label();
|
|
|
|
|
let kind = if self.token != token::OpenDelim(token::Brace)
|
|
|
|
|
|| !self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL)
|
|
|
|
|
{
|
|
|
|
|
self.parse_expr_opt()?
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
let expr = self.mk_expr(lo.to(self.prev_span), ExprKind::Break(label, kind), attrs);
|
|
|
|
|
self.maybe_recover_from_bad_qpath(expr, true)
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 13:01:24 +00:00
|
|
|
|
/// Parse `"yield" expr?`.
|
2019-12-03 15:38:34 +00:00
|
|
|
|
fn parse_yield_expr(&mut self, attrs: AttrVec) -> PResult<'a, P<Expr>> {
|
2019-12-03 13:01:24 +00:00
|
|
|
|
let lo = self.prev_span;
|
|
|
|
|
let kind = ExprKind::Yield(self.parse_expr_opt()?);
|
|
|
|
|
let span = lo.to(self.prev_span);
|
|
|
|
|
self.sess.gated_spans.gate(sym::generators, span);
|
|
|
|
|
let expr = self.mk_expr(span, kind, attrs);
|
|
|
|
|
self.maybe_recover_from_bad_qpath(expr, true)
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-16 17:11:05 +00:00
|
|
|
|
/// Returns a string literal if the next token is a string literal.
|
|
|
|
|
/// In case of error returns `Some(lit)` if the next token is a literal with a wrong kind,
|
|
|
|
|
/// and returns `None` if the next token is not literal at all.
|
2019-11-10 14:04:12 +00:00
|
|
|
|
pub fn parse_str_lit(&mut self) -> Result<ast::StrLit, Option<Lit>> {
|
|
|
|
|
match self.parse_opt_lit() {
|
|
|
|
|
Some(lit) => match lit.kind {
|
|
|
|
|
ast::LitKind::Str(symbol_unescaped, style) => Ok(ast::StrLit {
|
|
|
|
|
style,
|
|
|
|
|
symbol: lit.token.symbol,
|
|
|
|
|
suffix: lit.token.suffix,
|
|
|
|
|
span: lit.span,
|
|
|
|
|
symbol_unescaped,
|
|
|
|
|
}),
|
|
|
|
|
_ => Err(Some(lit)),
|
2019-12-22 22:42:04 +00:00
|
|
|
|
},
|
2019-11-10 14:04:12 +00:00
|
|
|
|
None => Err(None),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 07:35:34 +00:00
|
|
|
|
pub(super) fn parse_lit(&mut self) -> PResult<'a, Lit> {
|
2019-11-10 12:32:41 +00:00
|
|
|
|
self.parse_opt_lit().ok_or_else(|| {
|
2019-12-07 02:07:35 +00:00
|
|
|
|
let msg = format!("unexpected token: {}", super::token_descr(&self.token));
|
2019-11-10 12:32:41 +00:00
|
|
|
|
self.span_fatal(self.token.span, &msg)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Matches `lit = true | false | token_lit`.
|
|
|
|
|
/// Returns `None` if the next token is not a literal.
|
|
|
|
|
pub(super) fn parse_opt_lit(&mut self) -> Option<Lit> {
|
2019-10-11 16:40:56 +00:00
|
|
|
|
let mut recovered = None;
|
|
|
|
|
if self.token == token::Dot {
|
2019-11-10 12:32:41 +00:00
|
|
|
|
// Attempt to recover `.4` as `0.4`. We don't currently have any syntax where
|
|
|
|
|
// dot would follow an optional literal, so we do this unconditionally.
|
2019-10-11 16:40:56 +00:00
|
|
|
|
recovered = self.look_ahead(1, |next_token| {
|
2019-12-22 22:42:04 +00:00
|
|
|
|
if let token::Literal(token::Lit { kind: token::Integer, symbol, suffix }) =
|
|
|
|
|
next_token.kind
|
|
|
|
|
{
|
2019-10-11 16:40:56 +00:00
|
|
|
|
if self.token.span.hi() == next_token.span.lo() {
|
|
|
|
|
let s = String::from("0.") + &symbol.as_str();
|
|
|
|
|
let kind = TokenKind::lit(token::Float, Symbol::intern(&s), suffix);
|
|
|
|
|
return Some(Token::new(kind, self.token.span.to(next_token.span)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
});
|
|
|
|
|
if let Some(token) = &recovered {
|
|
|
|
|
self.bump();
|
2019-12-06 22:51:18 +00:00
|
|
|
|
self.error_float_lits_must_have_int_part(&token);
|
2019-10-11 16:40:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let token = recovered.as_ref().unwrap_or(&self.token);
|
|
|
|
|
match Lit::from_token(token) {
|
|
|
|
|
Ok(lit) => {
|
|
|
|
|
self.bump();
|
2019-11-10 12:32:41 +00:00
|
|
|
|
Some(lit)
|
2019-10-11 16:40:56 +00:00
|
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
|
Err(LitError::NotLiteral) => None,
|
2019-10-11 16:40:56 +00:00
|
|
|
|
Err(err) => {
|
2019-10-27 23:29:23 +00:00
|
|
|
|
let span = token.span;
|
|
|
|
|
let lit = match token.kind {
|
|
|
|
|
token::Literal(lit) => lit,
|
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
};
|
2019-10-11 16:40:56 +00:00
|
|
|
|
self.bump();
|
2019-11-10 12:32:41 +00:00
|
|
|
|
self.report_lit_error(err, lit, span);
|
2019-10-11 16:40:56 +00:00
|
|
|
|
// Pack possible quotes and prefixes from the original literal into
|
|
|
|
|
// the error literal's symbol so they can be pretty-printed faithfully.
|
|
|
|
|
let suffixless_lit = token::Lit::new(lit.kind, lit.symbol, None);
|
|
|
|
|
let symbol = Symbol::intern(&suffixless_lit.to_string());
|
|
|
|
|
let lit = token::Lit::new(token::Err, symbol, lit.suffix);
|
2019-11-10 12:32:41 +00:00
|
|
|
|
Some(Lit::from_lit_token(lit, span).unwrap_or_else(|_| unreachable!()))
|
2019-10-11 16:40:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-06 22:51:18 +00:00
|
|
|
|
fn error_float_lits_must_have_int_part(&self, token: &Token) {
|
|
|
|
|
self.struct_span_err(token.span, "float literals must have an integer part")
|
|
|
|
|
.span_suggestion(
|
|
|
|
|
token.span,
|
|
|
|
|
"must have an integer part",
|
|
|
|
|
pprust::token_to_string(token),
|
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
|
)
|
|
|
|
|
.emit();
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-10 12:32:41 +00:00
|
|
|
|
fn report_lit_error(&self, err: LitError, lit: token::Lit, span: Span) {
|
2019-10-11 16:40:56 +00:00
|
|
|
|
// Checks if `s` looks like i32 or u1234 etc.
|
|
|
|
|
fn looks_like_width_suffix(first_chars: &[char], s: &str) -> bool {
|
2019-12-22 22:42:04 +00:00
|
|
|
|
s.len() > 1 && s.starts_with(first_chars) && s[1..].chars().all(|c| c.is_ascii_digit())
|
2019-10-11 16:40:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let token::Lit { kind, suffix, .. } = lit;
|
|
|
|
|
match err {
|
|
|
|
|
// `NotLiteral` is not an error by itself, so we don't report
|
|
|
|
|
// it and give the parser opportunity to try something else.
|
|
|
|
|
LitError::NotLiteral => {}
|
|
|
|
|
// `LexerError` *is* an error, but it was already reported
|
|
|
|
|
// by lexer, so here we don't report it the second time.
|
|
|
|
|
LitError::LexerError => {}
|
|
|
|
|
LitError::InvalidSuffix => {
|
|
|
|
|
self.expect_no_suffix(
|
|
|
|
|
span,
|
|
|
|
|
&format!("{} {} literal", kind.article(), kind.descr()),
|
|
|
|
|
suffix,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
LitError::InvalidIntSuffix => {
|
|
|
|
|
let suf = suffix.expect("suffix error with no suffix").as_str();
|
|
|
|
|
if looks_like_width_suffix(&['i', 'u'], &suf) {
|
|
|
|
|
// If it looks like a width, try to be helpful.
|
|
|
|
|
let msg = format!("invalid width `{}` for integer literal", &suf[1..]);
|
|
|
|
|
self.struct_span_err(span, &msg)
|
|
|
|
|
.help("valid widths are 8, 16, 32, 64 and 128")
|
|
|
|
|
.emit();
|
|
|
|
|
} else {
|
|
|
|
|
let msg = format!("invalid suffix `{}` for integer literal", suf);
|
|
|
|
|
self.struct_span_err(span, &msg)
|
|
|
|
|
.span_label(span, format!("invalid suffix `{}`", suf))
|
|
|
|
|
.help("the suffix must be one of the integral types (`u32`, `isize`, etc)")
|
|
|
|
|
.emit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
LitError::InvalidFloatSuffix => {
|
|
|
|
|
let suf = suffix.expect("suffix error with no suffix").as_str();
|
|
|
|
|
if looks_like_width_suffix(&['f'], &suf) {
|
|
|
|
|
// If it looks like a width, try to be helpful.
|
|
|
|
|
let msg = format!("invalid width `{}` for float literal", &suf[1..]);
|
2019-12-22 22:42:04 +00:00
|
|
|
|
self.struct_span_err(span, &msg).help("valid widths are 32 and 64").emit();
|
2019-10-11 16:40:56 +00:00
|
|
|
|
} else {
|
|
|
|
|
let msg = format!("invalid suffix `{}` for float literal", suf);
|
|
|
|
|
self.struct_span_err(span, &msg)
|
|
|
|
|
.span_label(span, format!("invalid suffix `{}`", suf))
|
|
|
|
|
.help("valid suffixes are `f32` and `f64`")
|
|
|
|
|
.emit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
LitError::NonDecimalFloat(base) => {
|
|
|
|
|
let descr = match base {
|
|
|
|
|
16 => "hexadecimal",
|
|
|
|
|
8 => "octal",
|
|
|
|
|
2 => "binary",
|
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
};
|
|
|
|
|
self.struct_span_err(span, &format!("{} float literal is not supported", descr))
|
|
|
|
|
.span_label(span, "not supported")
|
|
|
|
|
.emit();
|
|
|
|
|
}
|
|
|
|
|
LitError::IntTooLarge => {
|
2019-12-22 22:42:04 +00:00
|
|
|
|
self.struct_span_err(span, "integer literal is too large").emit();
|
2019-10-11 16:40:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn expect_no_suffix(&self, sp: Span, kind: &str, suffix: Option<Symbol>) {
|
|
|
|
|
if let Some(suf) = suffix {
|
|
|
|
|
let mut err = if kind == "a tuple index"
|
|
|
|
|
&& [sym::i32, sym::u32, sym::isize, sym::usize].contains(&suf)
|
|
|
|
|
{
|
|
|
|
|
// #59553: warn instead of reject out of hand to allow the fix to percolate
|
|
|
|
|
// through the ecosystem when people fix their macros
|
2019-12-22 22:42:04 +00:00
|
|
|
|
let mut err = self
|
|
|
|
|
.sess
|
|
|
|
|
.span_diagnostic
|
|
|
|
|
.struct_span_warn(sp, &format!("suffixes on {} are invalid", kind));
|
2019-10-11 16:40:56 +00:00
|
|
|
|
err.note(&format!(
|
|
|
|
|
"`{}` is *temporarily* accepted on tuple index fields as it was \
|
|
|
|
|
incorrectly accepted on stable for a few releases",
|
|
|
|
|
suf,
|
|
|
|
|
));
|
|
|
|
|
err.help(
|
|
|
|
|
"on proc macros, you'll want to use `syn::Index::from` or \
|
|
|
|
|
`proc_macro::Literal::*_unsuffixed` for code that will desugar \
|
|
|
|
|
to tuple field access",
|
|
|
|
|
);
|
2019-12-22 22:42:04 +00:00
|
|
|
|
err.note("for more context, see https://github.com/rust-lang/rust/issues/60210");
|
2019-10-11 16:40:56 +00:00
|
|
|
|
err
|
|
|
|
|
} else {
|
|
|
|
|
self.struct_span_err(sp, &format!("suffixes on {} are invalid", kind))
|
|
|
|
|
};
|
|
|
|
|
err.span_label(sp, format!("invalid suffix `{}`", suf));
|
|
|
|
|
err.emit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-11 11:14:30 +00:00
|
|
|
|
/// Matches `'-' lit | lit` (cf. `ast_validation::AstValidator::check_expr_within_pat`).
|
2019-10-16 08:59:30 +00:00
|
|
|
|
pub fn parse_literal_maybe_minus(&mut self) -> PResult<'a, P<Expr>> {
|
2019-08-11 11:14:30 +00:00
|
|
|
|
maybe_whole_expr!(self);
|
|
|
|
|
|
|
|
|
|
let lo = self.token.span;
|
2019-12-06 22:44:23 +00:00
|
|
|
|
let minus_present = self.eat(&token::BinOp(token::Minus));
|
|
|
|
|
let lit = self.parse_lit()?;
|
|
|
|
|
let expr = self.mk_expr(lit.span, ExprKind::Lit(lit), AttrVec::new());
|
2019-08-11 11:14:30 +00:00
|
|
|
|
|
|
|
|
|
if minus_present {
|
2019-12-06 22:44:23 +00:00
|
|
|
|
Ok(self.mk_expr(lo.to(self.prev_span), self.mk_unary(UnOp::Neg, expr), AttrVec::new()))
|
2019-08-11 11:14:30 +00:00
|
|
|
|
} else {
|
|
|
|
|
Ok(expr)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Parses a block or unsafe block.
|
2019-10-08 07:35:34 +00:00
|
|
|
|
pub(super) fn parse_block_expr(
|
2019-08-11 11:14:30 +00:00
|
|
|
|
&mut self,
|
|
|
|
|
opt_label: Option<Label>,
|
|
|
|
|
lo: Span,
|
|
|
|
|
blk_mode: BlockCheckMode,
|
2019-12-03 15:38:34 +00:00
|
|
|
|
outer_attrs: AttrVec,
|
2019-08-11 11:14:30 +00:00
|
|
|
|
) -> PResult<'a, P<Expr>> {
|
2019-09-21 21:54:05 +00:00
|
|
|
|
if let Some(label) = opt_label {
|
2019-10-30 15:38:16 +00:00
|
|
|
|
self.sess.gated_spans.gate(sym::label_break_value, label.ident.span);
|
2019-09-21 21:54:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-11 11:14:30 +00:00
|
|
|
|
self.expect(&token::OpenDelim(token::Brace))?;
|
|
|
|
|
|
|
|
|
|
let mut attrs = outer_attrs;
|
|
|
|
|
attrs.extend(self.parse_inner_attributes()?);
|
|
|
|
|
|
|
|
|
|
let blk = self.parse_block_tail(lo, blk_mode)?;
|
2019-09-06 02:56:45 +00:00
|
|
|
|
Ok(self.mk_expr(blk.span, ExprKind::Block(blk, opt_label), attrs))
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 21:38:07 +00:00
|
|
|
|
/// Parses a closure expression (e.g., `move |args| expr`).
|
2019-12-03 15:38:34 +00:00
|
|
|
|
fn parse_closure_expr(&mut self, attrs: AttrVec) -> PResult<'a, P<Expr>> {
|
2019-08-11 11:14:30 +00:00
|
|
|
|
let lo = self.token.span;
|
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
|
let movability =
|
|
|
|
|
if self.eat_keyword(kw::Static) { Movability::Static } else { Movability::Movable };
|
2019-08-11 11:14:30 +00:00
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
|
let asyncness =
|
|
|
|
|
if self.token.span.rust_2018() { self.parse_asyncness() } else { IsAsync::NotAsync };
|
2019-08-11 11:14:30 +00:00
|
|
|
|
if asyncness.is_async() {
|
2019-09-06 02:56:45 +00:00
|
|
|
|
// Feature-gate `async ||` closures.
|
2019-10-30 15:38:16 +00:00
|
|
|
|
self.sess.gated_spans.gate(sym::async_closure, self.prev_span);
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let capture_clause = self.parse_capture_clause();
|
|
|
|
|
let decl = self.parse_fn_block_decl()?;
|
|
|
|
|
let decl_hi = self.prev_span;
|
|
|
|
|
let body = match decl.output {
|
|
|
|
|
FunctionRetTy::Default(_) => {
|
|
|
|
|
let restrictions = self.restrictions - Restrictions::STMT_EXPR;
|
|
|
|
|
self.parse_expr_res(restrictions, None)?
|
2019-12-22 22:42:04 +00:00
|
|
|
|
}
|
2019-08-11 11:14:30 +00:00
|
|
|
|
_ => {
|
2019-09-06 02:56:45 +00:00
|
|
|
|
// If an explicit return type is given, require a block to appear (RFC 968).
|
2019-08-11 11:14:30 +00:00
|
|
|
|
let body_lo = self.token.span;
|
2019-12-03 15:38:34 +00:00
|
|
|
|
self.parse_block_expr(None, body_lo, BlockCheckMode::Default, AttrVec::new())?
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(self.mk_expr(
|
|
|
|
|
lo.to(body.span),
|
|
|
|
|
ExprKind::Closure(capture_clause, asyncness, movability, decl, body, lo.to(decl_hi)),
|
2019-12-22 22:42:04 +00:00
|
|
|
|
attrs,
|
|
|
|
|
))
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 02:56:45 +00:00
|
|
|
|
/// Parses an optional `move` prefix to a closure lke construct.
|
2019-08-11 11:14:30 +00:00
|
|
|
|
fn parse_capture_clause(&mut self) -> CaptureBy {
|
2019-12-22 22:42:04 +00:00
|
|
|
|
if self.eat_keyword(kw::Move) { CaptureBy::Value } else { CaptureBy::Ref }
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-11 18:04:09 +00:00
|
|
|
|
/// Parses the `|arg, arg|` header of a closure.
|
|
|
|
|
fn parse_fn_block_decl(&mut self) -> PResult<'a, P<FnDecl>> {
|
2019-12-06 22:35:48 +00:00
|
|
|
|
let inputs = if self.eat(&token::OrOr) {
|
|
|
|
|
Vec::new()
|
|
|
|
|
} else {
|
|
|
|
|
self.expect(&token::BinOp(token::Or))?;
|
|
|
|
|
let args = self
|
|
|
|
|
.parse_seq_to_before_tokens(
|
|
|
|
|
&[&token::BinOp(token::Or), &token::OrOr],
|
|
|
|
|
SeqSep::trailing_allowed(token::Comma),
|
|
|
|
|
TokenExpectType::NoExpect,
|
|
|
|
|
|p| p.parse_fn_block_param(),
|
|
|
|
|
)?
|
|
|
|
|
.0;
|
|
|
|
|
self.expect_or()?;
|
|
|
|
|
args
|
2019-08-11 18:04:09 +00:00
|
|
|
|
};
|
2019-12-01 15:00:08 +00:00
|
|
|
|
let output = self.parse_ret_ty(true, true)?;
|
2019-08-11 18:04:09 +00:00
|
|
|
|
|
2019-12-06 22:35:48 +00:00
|
|
|
|
Ok(P(FnDecl { inputs, output }))
|
2019-08-11 18:04:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 02:56:45 +00:00
|
|
|
|
/// Parses a parameter in a closure header (e.g., `|arg, arg|`).
|
2019-08-27 11:24:32 +00:00
|
|
|
|
fn parse_fn_block_param(&mut self) -> PResult<'a, Param> {
|
2019-08-11 18:04:09 +00:00
|
|
|
|
let lo = self.token.span;
|
2019-08-29 23:44:30 +00:00
|
|
|
|
let attrs = self.parse_outer_attributes()?;
|
2019-08-25 02:39:28 +00:00
|
|
|
|
let pat = self.parse_pat(PARAM_EXPECTED)?;
|
2019-12-06 22:34:33 +00:00
|
|
|
|
let ty = if self.eat(&token::Colon) {
|
2019-08-11 18:04:09 +00:00
|
|
|
|
self.parse_ty()?
|
|
|
|
|
} else {
|
2019-12-06 22:34:33 +00:00
|
|
|
|
self.mk_ty(self.prev_span, TyKind::Infer)
|
2019-08-11 18:04:09 +00:00
|
|
|
|
};
|
2019-08-27 11:24:32 +00:00
|
|
|
|
Ok(Param {
|
2019-08-11 18:04:09 +00:00
|
|
|
|
attrs: attrs.into(),
|
2019-12-06 22:34:33 +00:00
|
|
|
|
ty,
|
2019-08-11 18:04:09 +00:00
|
|
|
|
pat,
|
2019-12-06 22:34:33 +00:00
|
|
|
|
span: lo.to(self.token.span),
|
2019-09-09 12:26:25 +00:00
|
|
|
|
id: DUMMY_NODE_ID,
|
|
|
|
|
is_placeholder: false,
|
2019-08-11 18:04:09 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-11 11:14:30 +00:00
|
|
|
|
/// Parses an `if` expression (`if` token already eaten).
|
2019-12-03 15:38:34 +00:00
|
|
|
|
fn parse_if_expr(&mut self, attrs: AttrVec) -> PResult<'a, P<Expr>> {
|
2019-08-11 11:14:30 +00:00
|
|
|
|
let lo = self.prev_span;
|
|
|
|
|
let cond = self.parse_cond_expr()?;
|
|
|
|
|
|
|
|
|
|
// Verify that the parsed `if` condition makes sense as a condition. If it is a block, then
|
|
|
|
|
// verify that the last statement is either an implicit return (no `;`) or an explicit
|
|
|
|
|
// return. This won't catch blocks with an explicit `return`, but that would be caught by
|
|
|
|
|
// the dead code lint.
|
2019-12-06 22:23:30 +00:00
|
|
|
|
let thn = if self.eat_keyword(kw::Else) || !cond.returns() {
|
|
|
|
|
self.error_missing_if_cond(lo, cond.span)
|
|
|
|
|
} else {
|
|
|
|
|
let not_block = self.token != token::OpenDelim(token::Brace);
|
|
|
|
|
self.parse_block().map_err(|mut err| {
|
|
|
|
|
if not_block {
|
|
|
|
|
err.span_label(lo, "this `if` expression has a condition, but no block");
|
|
|
|
|
}
|
|
|
|
|
err
|
|
|
|
|
})?
|
|
|
|
|
};
|
2019-12-06 22:33:13 +00:00
|
|
|
|
let els = if self.eat_keyword(kw::Else) { Some(self.parse_else_expr()?) } else { None };
|
|
|
|
|
Ok(self.mk_expr(lo.to(self.prev_span), ExprKind::If(cond, thn, els), attrs))
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-06 22:23:30 +00:00
|
|
|
|
fn error_missing_if_cond(&self, lo: Span, span: Span) -> P<ast::Block> {
|
|
|
|
|
let sp = self.sess.source_map().next_point(lo);
|
|
|
|
|
self.struct_span_err(sp, "missing condition for `if` expression")
|
|
|
|
|
.span_label(sp, "expected if condition here")
|
|
|
|
|
.emit();
|
|
|
|
|
let expr = self.mk_expr_err(span);
|
|
|
|
|
let stmt = self.mk_stmt(span, ast::StmtKind::Expr(expr));
|
|
|
|
|
self.mk_block(vec![stmt], BlockCheckMode::Default, span)
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 02:56:45 +00:00
|
|
|
|
/// Parses the condition of a `if` or `while` expression.
|
2019-08-11 11:14:30 +00:00
|
|
|
|
fn parse_cond_expr(&mut self) -> PResult<'a, P<Expr>> {
|
|
|
|
|
let cond = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
|
|
|
|
|
|
2019-09-26 13:39:48 +00:00
|
|
|
|
if let ExprKind::Let(..) = cond.kind {
|
2019-08-11 11:14:30 +00:00
|
|
|
|
// Remove the last feature gating of a `let` expression since it's stable.
|
2019-10-30 15:38:16 +00:00
|
|
|
|
self.sess.gated_spans.ungate_last(sym::let_chains, cond.span);
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(cond)
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-18 20:04:28 +00:00
|
|
|
|
/// Parses a `let $pat = $expr` pseudo-expression.
|
2019-08-11 11:14:30 +00:00
|
|
|
|
/// The `let` token has already been eaten.
|
2019-12-03 15:38:34 +00:00
|
|
|
|
fn parse_let_expr(&mut self, attrs: AttrVec) -> PResult<'a, P<Expr>> {
|
2019-08-11 11:14:30 +00:00
|
|
|
|
let lo = self.prev_span;
|
2019-08-27 23:06:33 +00:00
|
|
|
|
let pat = self.parse_top_pat(GateOr::No)?;
|
2019-08-11 11:14:30 +00:00
|
|
|
|
self.expect(&token::Eq)?;
|
2019-12-22 22:42:04 +00:00
|
|
|
|
let expr = self.with_res(Restrictions::NO_STRUCT_LITERAL, |this| {
|
|
|
|
|
this.parse_assoc_expr_with(1 + prec_let_scrutinee_needs_par(), None.into())
|
|
|
|
|
})?;
|
2019-08-11 11:14:30 +00:00
|
|
|
|
let span = lo.to(expr.span);
|
2019-10-30 15:38:16 +00:00
|
|
|
|
self.sess.gated_spans.gate(sym::let_chains, span);
|
2019-08-18 20:04:28 +00:00
|
|
|
|
Ok(self.mk_expr(span, ExprKind::Let(pat, expr), attrs))
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 02:56:45 +00:00
|
|
|
|
/// Parses an `else { ... }` expression (`else` token already eaten).
|
2019-08-11 11:14:30 +00:00
|
|
|
|
fn parse_else_expr(&mut self) -> PResult<'a, P<Expr>> {
|
|
|
|
|
if self.eat_keyword(kw::If) {
|
2019-12-06 22:23:30 +00:00
|
|
|
|
self.parse_if_expr(AttrVec::new())
|
2019-08-11 11:14:30 +00:00
|
|
|
|
} else {
|
|
|
|
|
let blk = self.parse_block()?;
|
2019-12-06 22:23:30 +00:00
|
|
|
|
Ok(self.mk_expr(blk.span, ExprKind::Block(blk, None), AttrVec::new()))
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-06 21:41:10 +00:00
|
|
|
|
/// Parses `for <src_pat> in <src_expr> <src_loop_block>` (`for` token already eaten).
|
2019-08-11 11:14:30 +00:00
|
|
|
|
fn parse_for_expr(
|
|
|
|
|
&mut self,
|
|
|
|
|
opt_label: Option<Label>,
|
2019-12-06 21:41:10 +00:00
|
|
|
|
lo: Span,
|
2019-12-22 22:42:04 +00:00
|
|
|
|
mut attrs: AttrVec,
|
2019-08-11 11:14:30 +00:00
|
|
|
|
) -> PResult<'a, P<Expr>> {
|
|
|
|
|
// Record whether we are about to parse `for (`.
|
|
|
|
|
// This is used below for recovery in case of `for ( $stuff ) $block`
|
|
|
|
|
// in which case we will suggest `for $stuff $block`.
|
|
|
|
|
let begin_paren = match self.token.kind {
|
|
|
|
|
token::OpenDelim(token::Paren) => Some(self.token.span),
|
|
|
|
|
_ => None,
|
|
|
|
|
};
|
|
|
|
|
|
2019-08-24 19:43:28 +00:00
|
|
|
|
let pat = self.parse_top_pat(GateOr::Yes)?;
|
2019-08-11 11:14:30 +00:00
|
|
|
|
if !self.eat_keyword(kw::In) {
|
2019-12-06 21:41:10 +00:00
|
|
|
|
self.error_missing_in_for_loop();
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
2019-12-06 21:41:10 +00:00
|
|
|
|
self.check_for_for_in_in_typo(self.prev_span);
|
2019-08-11 11:14:30 +00:00
|
|
|
|
let expr = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
|
|
|
|
|
|
|
|
|
|
let pat = self.recover_parens_around_for_head(pat, &expr, begin_paren);
|
|
|
|
|
|
|
|
|
|
let (iattrs, loop_block) = self.parse_inner_attrs_and_block()?;
|
|
|
|
|
attrs.extend(iattrs);
|
|
|
|
|
|
2019-12-06 21:41:10 +00:00
|
|
|
|
let kind = ExprKind::ForLoop(pat, expr, loop_block, opt_label);
|
|
|
|
|
Ok(self.mk_expr(lo.to(self.prev_span), kind, attrs))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn error_missing_in_for_loop(&self) {
|
|
|
|
|
let in_span = self.prev_span.between(self.token.span);
|
|
|
|
|
self.struct_span_err(in_span, "missing `in` in `for` loop")
|
|
|
|
|
.span_suggestion_short(
|
|
|
|
|
in_span,
|
|
|
|
|
"try adding `in` here",
|
|
|
|
|
" in ".into(),
|
|
|
|
|
// Has been misleading, at least in the past (closed Issue #48492).
|
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
)
|
|
|
|
|
.emit();
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Parses a `while` or `while let` expression (`while` token already eaten).
|
|
|
|
|
fn parse_while_expr(
|
|
|
|
|
&mut self,
|
|
|
|
|
opt_label: Option<Label>,
|
2019-12-06 21:41:10 +00:00
|
|
|
|
lo: Span,
|
2019-12-22 22:42:04 +00:00
|
|
|
|
mut attrs: AttrVec,
|
2019-08-11 11:14:30 +00:00
|
|
|
|
) -> PResult<'a, P<Expr>> {
|
|
|
|
|
let cond = self.parse_cond_expr()?;
|
|
|
|
|
let (iattrs, body) = self.parse_inner_attrs_and_block()?;
|
|
|
|
|
attrs.extend(iattrs);
|
2019-12-06 21:41:10 +00:00
|
|
|
|
Ok(self.mk_expr(lo.to(self.prev_span), ExprKind::While(cond, body, opt_label), attrs))
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 02:56:45 +00:00
|
|
|
|
/// Parses `loop { ... }` (`loop` token already eaten).
|
2019-08-11 11:14:30 +00:00
|
|
|
|
fn parse_loop_expr(
|
|
|
|
|
&mut self,
|
|
|
|
|
opt_label: Option<Label>,
|
2019-12-06 21:41:10 +00:00
|
|
|
|
lo: Span,
|
2019-12-22 22:42:04 +00:00
|
|
|
|
mut attrs: AttrVec,
|
2019-08-11 11:14:30 +00:00
|
|
|
|
) -> PResult<'a, P<Expr>> {
|
|
|
|
|
let (iattrs, body) = self.parse_inner_attrs_and_block()?;
|
|
|
|
|
attrs.extend(iattrs);
|
2019-12-06 21:41:10 +00:00
|
|
|
|
Ok(self.mk_expr(lo.to(self.prev_span), ExprKind::Loop(body, opt_label), attrs))
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn eat_label(&mut self) -> Option<Label> {
|
2019-12-06 21:41:10 +00:00
|
|
|
|
self.token.lifetime().map(|ident| {
|
2019-08-11 11:14:30 +00:00
|
|
|
|
let span = self.token.span;
|
|
|
|
|
self.bump();
|
2019-12-06 21:41:10 +00:00
|
|
|
|
Label { ident: Ident::new(ident.name, span) }
|
|
|
|
|
})
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 02:56:45 +00:00
|
|
|
|
/// Parses a `match ... { ... }` expression (`match` token already eaten).
|
2019-12-03 15:38:34 +00:00
|
|
|
|
fn parse_match_expr(&mut self, mut attrs: AttrVec) -> PResult<'a, P<Expr>> {
|
2019-08-11 11:14:30 +00:00
|
|
|
|
let match_span = self.prev_span;
|
|
|
|
|
let lo = self.prev_span;
|
2019-12-06 21:05:47 +00:00
|
|
|
|
let scrutinee = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
|
2019-08-11 11:14:30 +00:00
|
|
|
|
if let Err(mut e) = self.expect(&token::OpenDelim(token::Brace)) {
|
|
|
|
|
if self.token == token::Semi {
|
|
|
|
|
e.span_suggestion_short(
|
|
|
|
|
match_span,
|
|
|
|
|
"try removing this `match`",
|
|
|
|
|
String::new(),
|
2019-12-22 22:42:04 +00:00
|
|
|
|
Applicability::MaybeIncorrect, // speculative
|
2019-08-11 11:14:30 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
|
return Err(e);
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
attrs.extend(self.parse_inner_attributes()?);
|
|
|
|
|
|
|
|
|
|
let mut arms: Vec<Arm> = Vec::new();
|
|
|
|
|
while self.token != token::CloseDelim(token::Brace) {
|
|
|
|
|
match self.parse_arm() {
|
|
|
|
|
Ok(arm) => arms.push(arm),
|
|
|
|
|
Err(mut e) => {
|
|
|
|
|
// Recover by skipping to the end of the block.
|
|
|
|
|
e.emit();
|
|
|
|
|
self.recover_stmt();
|
|
|
|
|
let span = lo.to(self.token.span);
|
|
|
|
|
if self.token == token::CloseDelim(token::Brace) {
|
|
|
|
|
self.bump();
|
|
|
|
|
}
|
2019-12-06 21:05:47 +00:00
|
|
|
|
return Ok(self.mk_expr(span, ExprKind::Match(scrutinee, arms), attrs));
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let hi = self.token.span;
|
|
|
|
|
self.bump();
|
2019-12-06 21:05:47 +00:00
|
|
|
|
return Ok(self.mk_expr(lo.to(hi), ExprKind::Match(scrutinee, arms), attrs));
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 07:35:34 +00:00
|
|
|
|
pub(super) fn parse_arm(&mut self) -> PResult<'a, Arm> {
|
2019-08-11 11:14:30 +00:00
|
|
|
|
let attrs = self.parse_outer_attributes()?;
|
|
|
|
|
let lo = self.token.span;
|
2019-08-27 23:06:33 +00:00
|
|
|
|
let pat = self.parse_top_pat(GateOr::No)?;
|
2019-12-22 22:42:04 +00:00
|
|
|
|
let guard = if self.eat_keyword(kw::If) { Some(self.parse_expr()?) } else { None };
|
2019-08-11 11:14:30 +00:00
|
|
|
|
let arrow_span = self.token.span;
|
|
|
|
|
self.expect(&token::FatArrow)?;
|
|
|
|
|
let arm_start_span = self.token.span;
|
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
|
let expr = self.parse_expr_res(Restrictions::STMT_EXPR, None).map_err(|mut err| {
|
|
|
|
|
err.span_label(arrow_span, "while parsing the `match` arm starting here");
|
|
|
|
|
err
|
|
|
|
|
})?;
|
2019-08-11 11:14:30 +00:00
|
|
|
|
|
|
|
|
|
let require_comma = classify::expr_requires_semi_to_be_stmt(&expr)
|
|
|
|
|
&& self.token != token::CloseDelim(token::Brace);
|
|
|
|
|
|
|
|
|
|
let hi = self.token.span;
|
|
|
|
|
|
|
|
|
|
if require_comma {
|
|
|
|
|
let cm = self.sess.source_map();
|
2019-12-22 22:42:04 +00:00
|
|
|
|
self.expect_one_of(&[token::Comma], &[token::CloseDelim(token::Brace)]).map_err(
|
|
|
|
|
|mut err| {
|
2019-08-11 11:14:30 +00:00
|
|
|
|
match (cm.span_to_lines(expr.span), cm.span_to_lines(arm_start_span)) {
|
|
|
|
|
(Ok(ref expr_lines), Ok(ref arm_start_lines))
|
2019-12-22 22:42:04 +00:00
|
|
|
|
if arm_start_lines.lines[0].end_col == expr_lines.lines[0].end_col
|
|
|
|
|
&& expr_lines.lines.len() == 2
|
|
|
|
|
&& self.token == token::FatArrow =>
|
|
|
|
|
{
|
2019-08-11 11:14:30 +00:00
|
|
|
|
// We check whether there's any trailing code in the parse span,
|
|
|
|
|
// if there isn't, we very likely have the following:
|
|
|
|
|
//
|
|
|
|
|
// X | &Y => "y"
|
|
|
|
|
// | -- - missing comma
|
|
|
|
|
// | |
|
|
|
|
|
// | arrow_span
|
|
|
|
|
// X | &X => "x"
|
|
|
|
|
// | - ^^ self.token.span
|
|
|
|
|
// | |
|
|
|
|
|
// | parsed until here as `"y" & X`
|
|
|
|
|
err.span_suggestion_short(
|
|
|
|
|
cm.next_point(arm_start_span),
|
|
|
|
|
"missing a comma here to end this `match` arm",
|
|
|
|
|
",".to_owned(),
|
2019-12-22 22:42:04 +00:00
|
|
|
|
Applicability::MachineApplicable,
|
2019-08-11 11:14:30 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
2019-12-22 22:42:04 +00:00
|
|
|
|
err.span_label(
|
|
|
|
|
arrow_span,
|
|
|
|
|
"while parsing the `match` arm starting here",
|
|
|
|
|
);
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
err
|
2019-12-22 22:42:04 +00:00
|
|
|
|
},
|
|
|
|
|
)?;
|
2019-08-11 11:14:30 +00:00
|
|
|
|
} else {
|
|
|
|
|
self.eat(&token::Comma);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(ast::Arm {
|
|
|
|
|
attrs,
|
2019-08-27 23:06:33 +00:00
|
|
|
|
pat,
|
2019-08-11 11:14:30 +00:00
|
|
|
|
guard,
|
|
|
|
|
body: expr,
|
|
|
|
|
span: lo.to(hi),
|
2019-09-06 02:56:45 +00:00
|
|
|
|
id: DUMMY_NODE_ID,
|
2019-09-09 12:26:25 +00:00
|
|
|
|
is_placeholder: false,
|
2019-08-11 11:14:30 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Parses a `try {...}` expression (`try` token already eaten).
|
2019-12-03 15:38:34 +00:00
|
|
|
|
fn parse_try_block(&mut self, span_lo: Span, mut attrs: AttrVec) -> PResult<'a, P<Expr>> {
|
2019-08-11 11:14:30 +00:00
|
|
|
|
let (iattrs, body) = self.parse_inner_attrs_and_block()?;
|
|
|
|
|
attrs.extend(iattrs);
|
|
|
|
|
if self.eat_keyword(kw::Catch) {
|
2019-12-22 22:42:04 +00:00
|
|
|
|
let mut error =
|
|
|
|
|
self.struct_span_err(self.prev_span, "keyword `catch` cannot follow a `try` block");
|
2019-08-11 11:14:30 +00:00
|
|
|
|
error.help("try using `match` on the result of the `try` block instead");
|
|
|
|
|
error.emit();
|
|
|
|
|
Err(error)
|
|
|
|
|
} else {
|
2019-09-21 21:09:17 +00:00
|
|
|
|
let span = span_lo.to(body.span);
|
2019-10-30 15:38:16 +00:00
|
|
|
|
self.sess.gated_spans.gate(sym::try_blocks, span);
|
2019-09-21 21:09:17 +00:00
|
|
|
|
Ok(self.mk_expr(span, ExprKind::TryBlock(body), attrs))
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_do_catch_block(&self) -> bool {
|
2019-12-22 22:42:04 +00:00
|
|
|
|
self.token.is_keyword(kw::Do)
|
|
|
|
|
&& self.is_keyword_ahead(1, &[kw::Catch])
|
|
|
|
|
&& self.look_ahead(2, |t| *t == token::OpenDelim(token::Brace))
|
|
|
|
|
&& !self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL)
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_try_block(&self) -> bool {
|
|
|
|
|
self.token.is_keyword(kw::Try) &&
|
|
|
|
|
self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace)) &&
|
|
|
|
|
self.token.span.rust_2018() &&
|
2019-09-06 02:56:45 +00:00
|
|
|
|
// Prevent `while try {} {}`, `if try {} {} else {}`, etc.
|
2019-08-11 11:14:30 +00:00
|
|
|
|
!self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Parses an `async move? {...}` expression.
|
2019-12-03 15:38:34 +00:00
|
|
|
|
fn parse_async_block(&mut self, mut attrs: AttrVec) -> PResult<'a, P<Expr>> {
|
2019-12-06 21:05:47 +00:00
|
|
|
|
let lo = self.token.span;
|
2019-08-11 11:14:30 +00:00
|
|
|
|
self.expect_keyword(kw::Async)?;
|
|
|
|
|
let capture_clause = self.parse_capture_clause();
|
|
|
|
|
let (iattrs, body) = self.parse_inner_attrs_and_block()?;
|
|
|
|
|
attrs.extend(iattrs);
|
2019-12-06 21:05:47 +00:00
|
|
|
|
let kind = ExprKind::Async(capture_clause, DUMMY_NODE_ID, body);
|
|
|
|
|
Ok(self.mk_expr(lo.to(self.prev_span), kind, attrs))
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_async_block(&self) -> bool {
|
2019-12-22 22:42:04 +00:00
|
|
|
|
self.token.is_keyword(kw::Async)
|
|
|
|
|
&& ((
|
|
|
|
|
// `async move {`
|
|
|
|
|
self.is_keyword_ahead(1, &[kw::Move])
|
|
|
|
|
&& self.look_ahead(2, |t| *t == token::OpenDelim(token::Brace))
|
|
|
|
|
) || (
|
|
|
|
|
// `async {`
|
2019-08-11 11:14:30 +00:00
|
|
|
|
self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace))
|
2019-12-22 22:42:04 +00:00
|
|
|
|
))
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-04 03:31:44 +00:00
|
|
|
|
fn is_certainly_not_a_block(&self) -> bool {
|
|
|
|
|
self.look_ahead(1, |t| t.is_ident())
|
|
|
|
|
&& (
|
|
|
|
|
// `{ ident, ` cannot start a block.
|
|
|
|
|
self.look_ahead(2, |t| t == &token::Comma)
|
|
|
|
|
|| self.look_ahead(2, |t| t == &token::Colon)
|
|
|
|
|
&& (
|
|
|
|
|
// `{ ident: token, ` cannot start a block.
|
|
|
|
|
self.look_ahead(4, |t| t == &token::Comma) ||
|
|
|
|
|
// `{ ident: ` cannot start a block unless it's a type ascription `ident: Type`.
|
|
|
|
|
self.look_ahead(3, |t| !t.can_begin_type())
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-11 11:14:30 +00:00
|
|
|
|
fn maybe_parse_struct_expr(
|
|
|
|
|
&mut self,
|
|
|
|
|
lo: Span,
|
|
|
|
|
path: &ast::Path,
|
2019-12-03 15:38:34 +00:00
|
|
|
|
attrs: &AttrVec,
|
2019-08-11 11:14:30 +00:00
|
|
|
|
) -> Option<PResult<'a, P<Expr>>> {
|
|
|
|
|
let struct_allowed = !self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL);
|
2019-12-04 03:31:44 +00:00
|
|
|
|
if struct_allowed || self.is_certainly_not_a_block() {
|
2019-09-06 02:56:45 +00:00
|
|
|
|
// This is a struct literal, but we don't can't accept them here.
|
2019-08-11 11:14:30 +00:00
|
|
|
|
let expr = self.parse_struct_expr(lo, path.clone(), attrs.clone());
|
|
|
|
|
if let (Ok(expr), false) = (&expr, struct_allowed) {
|
2019-12-04 03:24:53 +00:00
|
|
|
|
self.error_struct_lit_not_allowed_here(lo, expr.span);
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
return Some(expr);
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-04 03:24:53 +00:00
|
|
|
|
fn error_struct_lit_not_allowed_here(&self, lo: Span, sp: Span) {
|
|
|
|
|
self.struct_span_err(sp, "struct literals are not allowed here")
|
|
|
|
|
.multipart_suggestion(
|
|
|
|
|
"surround the struct literal with parentheses",
|
|
|
|
|
vec![(lo.shrink_to_lo(), "(".to_string()), (sp.shrink_to_hi(), ")".to_string())],
|
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
|
)
|
|
|
|
|
.emit();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-11 11:14:30 +00:00
|
|
|
|
pub(super) fn parse_struct_expr(
|
|
|
|
|
&mut self,
|
|
|
|
|
lo: Span,
|
|
|
|
|
pth: ast::Path,
|
2019-12-22 22:42:04 +00:00
|
|
|
|
mut attrs: AttrVec,
|
2019-08-11 11:14:30 +00:00
|
|
|
|
) -> PResult<'a, P<Expr>> {
|
|
|
|
|
let struct_sp = lo.to(self.prev_span);
|
|
|
|
|
self.bump();
|
|
|
|
|
let mut fields = Vec::new();
|
|
|
|
|
let mut base = None;
|
|
|
|
|
|
|
|
|
|
attrs.extend(self.parse_inner_attributes()?);
|
|
|
|
|
|
|
|
|
|
while self.token != token::CloseDelim(token::Brace) {
|
|
|
|
|
if self.eat(&token::DotDot) {
|
|
|
|
|
let exp_span = self.prev_span;
|
|
|
|
|
match self.parse_expr() {
|
2019-12-04 02:47:18 +00:00
|
|
|
|
Ok(e) => base = Some(e),
|
2019-08-11 11:14:30 +00:00
|
|
|
|
Err(mut e) => {
|
|
|
|
|
e.emit();
|
|
|
|
|
self.recover_stmt();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-04 02:31:32 +00:00
|
|
|
|
self.recover_struct_comma_after_dotdot(exp_span);
|
2019-08-11 11:14:30 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-04 02:47:18 +00:00
|
|
|
|
let recovery_field = self.find_struct_error_after_field_looking_code();
|
|
|
|
|
let parsed_field = match self.parse_field() {
|
|
|
|
|
Ok(f) => Some(f),
|
2019-08-11 11:14:30 +00:00
|
|
|
|
Err(mut e) => {
|
|
|
|
|
e.span_label(struct_sp, "while parsing this struct");
|
|
|
|
|
e.emit();
|
|
|
|
|
|
|
|
|
|
// If the next token is a comma, then try to parse
|
|
|
|
|
// what comes next as additional fields, rather than
|
|
|
|
|
// bailing out until next `}`.
|
|
|
|
|
if self.token != token::Comma {
|
|
|
|
|
self.recover_stmt_(SemiColonMode::Comma, BlockMode::Ignore);
|
|
|
|
|
if self.token != token::Comma {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-04 02:47:18 +00:00
|
|
|
|
None
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
2019-12-04 02:47:18 +00:00
|
|
|
|
};
|
2019-08-11 11:14:30 +00:00
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
|
match self.expect_one_of(&[token::Comma], &[token::CloseDelim(token::Brace)]) {
|
|
|
|
|
Ok(_) => {
|
|
|
|
|
if let Some(f) = parsed_field.or(recovery_field) {
|
|
|
|
|
// Only include the field if there's no parse error for the field name.
|
|
|
|
|
fields.push(f);
|
|
|
|
|
}
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
Err(mut e) => {
|
|
|
|
|
if let Some(f) = recovery_field {
|
|
|
|
|
fields.push(f);
|
|
|
|
|
}
|
|
|
|
|
e.span_label(struct_sp, "while parsing this struct");
|
|
|
|
|
e.emit();
|
|
|
|
|
self.recover_stmt_(SemiColonMode::Comma, BlockMode::Ignore);
|
|
|
|
|
self.eat(&token::Comma);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let span = lo.to(self.token.span);
|
|
|
|
|
self.expect(&token::CloseDelim(token::Brace))?;
|
2019-12-04 02:47:18 +00:00
|
|
|
|
Ok(self.mk_expr(span, ExprKind::Struct(pth, fields, base), attrs))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Use in case of error after field-looking code: `S { foo: () with a }`.
|
|
|
|
|
fn find_struct_error_after_field_looking_code(&self) -> Option<Field> {
|
|
|
|
|
if let token::Ident(name, _) = self.token.kind {
|
|
|
|
|
if !self.token.is_reserved_ident() && self.look_ahead(1, |t| *t == token::Colon) {
|
|
|
|
|
let span = self.token.span;
|
|
|
|
|
return Some(ast::Field {
|
|
|
|
|
ident: Ident::new(name, span),
|
|
|
|
|
span,
|
|
|
|
|
expr: self.mk_expr_err(span),
|
|
|
|
|
is_shorthand: false,
|
|
|
|
|
attrs: AttrVec::new(),
|
|
|
|
|
id: DUMMY_NODE_ID,
|
|
|
|
|
is_placeholder: false,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-04 02:31:32 +00:00
|
|
|
|
fn recover_struct_comma_after_dotdot(&mut self, span: Span) {
|
|
|
|
|
if self.token != token::Comma {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
self.struct_span_err(span.to(self.prev_span), "cannot use a comma after the base struct")
|
|
|
|
|
.span_suggestion_short(
|
|
|
|
|
self.token.span,
|
|
|
|
|
"remove this comma",
|
|
|
|
|
String::new(),
|
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
|
)
|
|
|
|
|
.note("the base struct must always be the last field")
|
|
|
|
|
.emit();
|
|
|
|
|
self.recover_stmt();
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 02:56:45 +00:00
|
|
|
|
/// Parses `ident (COLON expr)?`.
|
2019-08-11 11:14:30 +00:00
|
|
|
|
fn parse_field(&mut self) -> PResult<'a, Field> {
|
2019-12-04 02:23:20 +00:00
|
|
|
|
let attrs = self.parse_outer_attributes()?.into();
|
2019-08-11 11:14:30 +00:00
|
|
|
|
let lo = self.token.span;
|
|
|
|
|
|
|
|
|
|
// Check if a colon exists one ahead. This means we're parsing a fieldname.
|
2019-12-04 02:23:20 +00:00
|
|
|
|
let is_shorthand = !self.look_ahead(1, |t| t == &token::Colon || t == &token::Eq);
|
|
|
|
|
let (ident, expr) = if is_shorthand {
|
|
|
|
|
// Mimic `x: x` for the `x` field shorthand.
|
|
|
|
|
let ident = self.parse_ident_common(false)?;
|
|
|
|
|
let path = ast::Path::from_ident(ident);
|
|
|
|
|
(ident, self.mk_expr(ident.span, ExprKind::Path(None, path), AttrVec::new()))
|
|
|
|
|
} else {
|
|
|
|
|
let ident = self.parse_field_name()?;
|
|
|
|
|
self.error_on_eq_field_init(ident);
|
|
|
|
|
self.bump(); // `:`
|
|
|
|
|
(ident, self.parse_expr()?)
|
|
|
|
|
};
|
2019-08-11 11:14:30 +00:00
|
|
|
|
Ok(ast::Field {
|
2019-12-04 02:23:20 +00:00
|
|
|
|
ident,
|
2019-08-11 11:14:30 +00:00
|
|
|
|
span: lo.to(expr.span),
|
|
|
|
|
expr,
|
|
|
|
|
is_shorthand,
|
2019-12-04 02:23:20 +00:00
|
|
|
|
attrs,
|
2019-09-06 02:56:45 +00:00
|
|
|
|
id: DUMMY_NODE_ID,
|
2019-09-09 12:26:25 +00:00
|
|
|
|
is_placeholder: false,
|
2019-08-11 11:14:30 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-04 02:23:20 +00:00
|
|
|
|
/// Check for `=`. This means the source incorrectly attempts to
|
|
|
|
|
/// initialize a field with an eq rather than a colon.
|
|
|
|
|
fn error_on_eq_field_init(&self, field_name: Ident) {
|
|
|
|
|
if self.token != token::Eq {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.diagnostic()
|
|
|
|
|
.struct_span_err(self.token.span, "expected `:`, found `=`")
|
|
|
|
|
.span_suggestion(
|
|
|
|
|
field_name.span.shrink_to_hi().to(self.token.span),
|
|
|
|
|
"replace equals symbol with a colon",
|
|
|
|
|
":".to_string(),
|
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
|
)
|
|
|
|
|
.emit();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-11 11:14:30 +00:00
|
|
|
|
fn err_dotdotdot_syntax(&self, span: Span) {
|
|
|
|
|
self.struct_span_err(span, "unexpected token: `...`")
|
|
|
|
|
.span_suggestion(
|
|
|
|
|
span,
|
2019-12-22 22:42:04 +00:00
|
|
|
|
"use `..` for an exclusive range",
|
|
|
|
|
"..".to_owned(),
|
|
|
|
|
Applicability::MaybeIncorrect,
|
2019-08-11 11:14:30 +00:00
|
|
|
|
)
|
|
|
|
|
.span_suggestion(
|
|
|
|
|
span,
|
2019-12-22 22:42:04 +00:00
|
|
|
|
"or `..=` for an inclusive range",
|
|
|
|
|
"..=".to_owned(),
|
|
|
|
|
Applicability::MaybeIncorrect,
|
2019-08-11 11:14:30 +00:00
|
|
|
|
)
|
|
|
|
|
.emit();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-11 21:37:05 +00:00
|
|
|
|
fn err_larrow_operator(&self, span: Span) {
|
2019-12-22 22:42:04 +00:00
|
|
|
|
self.struct_span_err(span, "unexpected token: `<-`")
|
|
|
|
|
.span_suggestion(
|
|
|
|
|
span,
|
|
|
|
|
"if you meant to write a comparison against a negative value, add a \
|
2019-08-11 21:37:05 +00:00
|
|
|
|
space in between `<` and `-`",
|
2019-12-22 22:42:04 +00:00
|
|
|
|
"< -".to_string(),
|
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
)
|
|
|
|
|
.emit();
|
2019-08-11 21:37:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-11 11:14:30 +00:00
|
|
|
|
fn mk_assign_op(&self, binop: BinOp, lhs: P<Expr>, rhs: P<Expr>) -> ExprKind {
|
|
|
|
|
ExprKind::AssignOp(binop, lhs, rhs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn mk_range(
|
|
|
|
|
&self,
|
|
|
|
|
start: Option<P<Expr>>,
|
|
|
|
|
end: Option<P<Expr>>,
|
2019-12-22 22:42:04 +00:00
|
|
|
|
limits: RangeLimits,
|
2019-08-11 11:14:30 +00:00
|
|
|
|
) -> PResult<'a, ExprKind> {
|
|
|
|
|
if end.is_none() && limits == RangeLimits::Closed {
|
|
|
|
|
Err(self.span_fatal_err(self.token.span, Error::InclusiveRangeWithNoEnd))
|
|
|
|
|
} else {
|
|
|
|
|
Ok(ExprKind::Range(start, end, limits))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn mk_unary(&self, unop: UnOp, expr: P<Expr>) -> ExprKind {
|
|
|
|
|
ExprKind::Unary(unop, expr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn mk_binary(&self, binop: BinOp, lhs: P<Expr>, rhs: P<Expr>) -> ExprKind {
|
|
|
|
|
ExprKind::Binary(binop, lhs, rhs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn mk_index(&self, expr: P<Expr>, idx: P<Expr>) -> ExprKind {
|
|
|
|
|
ExprKind::Index(expr, idx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn mk_call(&self, f: P<Expr>, args: Vec<P<Expr>>) -> ExprKind {
|
|
|
|
|
ExprKind::Call(f, args)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn mk_await_expr(&mut self, self_arg: P<Expr>, lo: Span) -> PResult<'a, P<Expr>> {
|
|
|
|
|
let span = lo.to(self.prev_span);
|
2019-12-03 15:38:34 +00:00
|
|
|
|
let await_expr = self.mk_expr(span, ExprKind::Await(self_arg), AttrVec::new());
|
2019-08-11 11:14:30 +00:00
|
|
|
|
self.recover_from_await_method_call();
|
|
|
|
|
Ok(await_expr)
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 15:38:34 +00:00
|
|
|
|
crate fn mk_expr(&self, span: Span, kind: ExprKind, attrs: AttrVec) -> P<Expr> {
|
2019-09-26 13:39:48 +00:00
|
|
|
|
P(Expr { kind, span, attrs, id: DUMMY_NODE_ID })
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|
2019-10-08 12:39:58 +00:00
|
|
|
|
|
|
|
|
|
pub(super) fn mk_expr_err(&self, span: Span) -> P<Expr> {
|
2019-12-03 15:38:34 +00:00
|
|
|
|
self.mk_expr(span, ExprKind::Err, AttrVec::new())
|
2019-10-08 12:39:58 +00:00
|
|
|
|
}
|
2019-08-11 11:14:30 +00:00
|
|
|
|
}
|