X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Flibsyntax%2Fparse%2Fparser%2Fexpr.rs;h=b383498b7b64d7fa72b0f80282df2a2ae048e0fc;hb=299d696b91e833f01f37e97b69767fcf6f5cccf0;hp=3db9c899dba40f12696cf01649ad28721d55d17d;hpb=4894123d21ed4b153a2e5c32c0870cb2d97f9b46;p=rust.git diff --git a/src/libsyntax/parse/parser/expr.rs b/src/libsyntax/parse/parser/expr.rs index 3db9c899dba..b383498b7b6 100644 --- a/src/libsyntax/parse/parser/expr.rs +++ b/src/libsyntax/parse/parser/expr.rs @@ -1,26 +1,26 @@ -use super::{Parser, PResult, Restrictions, PrevTokenKind, TokenType, PathStyle}; -use super::{BlockMode, SemiColonMode}; -use super::{SeqSep, TokenExpectType}; +use super::{ + Parser, PResult, Restrictions, PrevTokenKind, TokenType, PathStyle, BlockMode, SemiColonMode, + SeqSep, TokenExpectType, +}; use super::pat::{GateOr, PARAM_EXPECTED}; +use crate::ast::{ + self, DUMMY_NODE_ID, Attribute, AttrStyle, Ident, CaptureBy, BlockCheckMode, + Expr, ExprKind, RangeLimits, Label, Movability, IsAsync, Arm, Ty, TyKind, + FunctionRetTy, Param, FnDecl, BinOpKind, BinOp, UnOp, Mac, AnonConst, Field, +}; use crate::maybe_recover_from_interpolated_ty_qpath; -use crate::ptr::P; -use crate::ast::{self, Attribute, AttrStyle, Ident, CaptureBy, BlockCheckMode}; -use crate::ast::{Expr, ExprKind, RangeLimits, Label, Movability, IsAsync, Arm}; -use crate::ast::{Ty, TyKind, FunctionRetTy, Param, FnDecl}; -use crate::ast::{BinOpKind, BinOp, UnOp}; -use crate::ast::{Mac, AnonConst, Field}; - use crate::parse::classify; use crate::parse::token::{self, Token}; -use crate::parse::diagnostics::{Error}; +use crate::parse::diagnostics::Error; use crate::print::pprust; +use crate::ptr::P; use crate::source_map::{self, Span}; use crate::symbol::{kw, sym}; use crate::util::parser::{AssocOp, Fixity, prec_let_scrutinee_needs_par}; -use std::mem; use errors::Applicability; +use std::mem; use rustc_data_structures::thin_vec::ThinVec; /// Possibly accepts an `token::Interpolated` expression (a pre-parsed expression @@ -51,7 +51,7 @@ macro_rules! maybe_whole_expr { $p.token.span, ExprKind::Block(block, None), ThinVec::new() )); } - // N.B: `NtIdent(ident)` is normalized to `Ident` in `fn bump`. + // N.B., `NtIdent(ident)` is normalized to `Ident` in `fn bump`. _ => {}, }; } @@ -231,7 +231,7 @@ pub(super) fn parse_assoc_expr_with( self.bump(); if op.is_comparison() { - self.check_no_chained_comparison(&lhs, &op); + self.check_no_chained_comparison(&lhs, &op)?; } // Special cases: if op == AssocOp::As { @@ -340,7 +340,7 @@ fn expr_is_complete(&self, e: &Expr) -> bool { fn is_at_start_of_range_notation_rhs(&self) -> bool { if self.token.can_begin_expr() { - // parse `for i in 1.. { }` as infinite loop, not as `for i in (1..{})`. + // Parse `for i in 1.. { }` as infinite loop, not as `for i in (1..{})`. if self.token == token::OpenDelim(token::Brace) { return !self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL); } @@ -350,12 +350,12 @@ fn is_at_start_of_range_notation_rhs(&self) -> bool { } } - /// Parse prefix-forms of range notation: `..expr`, `..`, `..=expr` + /// Parses prefix-forms of range notation: `..expr`, `..`, `..=expr`. fn parse_prefix_range_expr( &mut self, already_parsed_attrs: Option> ) -> PResult<'a, P> { - // Check for deprecated `...` syntax + // Check for deprecated `...` syntax. if self.token == token::DotDotDot { self.err_dotdotdot_syntax(self.token.span); } @@ -389,7 +389,7 @@ fn parse_prefix_range_expr( Ok(self.mk_expr(lo.to(hi), r, attrs)) } - /// Parse a prefix-unary-operator expr + /// Parses a prefix-unary-operator expr. fn parse_prefix_expr( &mut self, already_parsed_attrs: Option> @@ -549,7 +549,7 @@ fn parse_assoc_op_cast(&mut self, lhs: P, lhs_span: Span, let expr = mk_expr(self, P(Ty { span: path.span, node: TyKind::Path(None, path), - id: ast::DUMMY_NODE_ID + id: DUMMY_NODE_ID, })); let expr_str = self.span_to_snippet(expr.span) @@ -565,7 +565,7 @@ fn parse_assoc_op_cast(&mut self, lhs: P, lhs_span: Span, expr.span, &format!("try {} the cast value", op_verb), format!("({})", expr_str), - Applicability::MachineApplicable + Applicability::MachineApplicable, ) .emit(); @@ -741,7 +741,6 @@ fn parse_dot_suffix(&mut self, self_arg: P, lo: Span) -> PResult<'a, P PResult<'a, P> { // added to the return value after the fact. // // Therefore, prevent sub-parser from parsing - // attributes by giving them a empty "already parsed" list. + // attributes by giving them a empty "already-parsed" list. let mut attrs = ThinVec::new(); let lo = self.token.span; @@ -778,7 +777,7 @@ macro_rules! parse_lit { } } - // Note: when adding new syntax here, don't forget to adjust TokenKind::can_begin_expr(). + // Note: when adding new syntax here, don't forget to adjust `TokenKind::can_begin_expr()`. match self.token.kind { // This match arm is a special-case of the `_` match arm below and // could be removed without changing functionality, but it's faster @@ -791,8 +790,8 @@ macro_rules! parse_lit { attrs.extend(self.parse_inner_attributes()?); - // (e) is parenthesized e - // (e,) is a tuple with only one field, e + // `(e)` is parenthesized `e`. + // `(e,)` is a tuple with only one field, `e`. let mut es = vec![]; let mut trailing_comma = false; let mut recovered = false; @@ -800,7 +799,7 @@ macro_rules! parse_lit { es.push(match self.parse_expr() { Ok(es) => es, Err(mut err) => { - // recover from parse error in tuple list + // Recover from parse error in tuple list. match self.token.kind { token::Ident(name, false) if name == kw::Underscore && self.look_ahead(1, |t| { @@ -844,7 +843,7 @@ macro_rules! parse_lit { return self.parse_block_expr(None, lo, BlockCheckMode::Default, attrs); } token::BinOp(token::Or) | token::OrOr => { - return self.parse_lambda_expr(attrs); + return self.parse_closure_expr(attrs); } token::OpenDelim(token::Bracket) => { self.bump(); @@ -852,21 +851,21 @@ macro_rules! parse_lit { attrs.extend(self.parse_inner_attributes()?); if self.eat(&token::CloseDelim(token::Bracket)) { - // Empty vector. + // Empty vector ex = ExprKind::Array(Vec::new()); } else { - // Nonempty vector. + // Non-empty vector let first_expr = self.parse_expr()?; if self.eat(&token::Semi) { - // Repeating array syntax: [ 0; 512 ] + // Repeating array syntax: `[ 0; 512 ]` let count = AnonConst { - id: ast::DUMMY_NODE_ID, + id: DUMMY_NODE_ID, value: self.parse_expr()?, }; self.expect(&token::CloseDelim(token::Bracket))?; ex = ExprKind::Repeat(first_expr, count); } else if self.eat(&token::Comma) { - // Vector with two or more elements. + // Vector with two or more elements let remaining_exprs = self.parse_seq_to_end( &token::CloseDelim(token::Bracket), SeqSep::trailing_allowed(token::Comma), @@ -876,7 +875,7 @@ macro_rules! parse_lit { exprs.extend(remaining_exprs); ex = ExprKind::Array(exprs); } else { - // Vector with one element. + // Vector with one element self.expect(&token::CloseDelim(token::Bracket))?; ex = ExprKind::Array(vec![first_expr]); } @@ -892,7 +891,7 @@ macro_rules! parse_lit { if self.token.is_path_start() { let path = self.parse_path(PathStyle::Expr)?; - // `!`, as an operator, is prefix, so we know this isn't that + // `!`, as an operator, is prefix, so we know this isn't that. if self.eat(&token::Not) { // MACRO INVOCATION expression let (delim, tts) = self.expect_delimited_token_tree()?; @@ -920,7 +919,7 @@ macro_rules! parse_lit { return self.maybe_recover_from_bad_qpath(expr, true); } if self.check_keyword(kw::Move) || self.check_keyword(kw::Static) { - return self.parse_lambda_expr(attrs); + return self.parse_closure_expr(attrs); } if self.eat_keyword(kw::If) { return self.parse_if_expr(attrs); @@ -982,7 +981,7 @@ macro_rules! parse_lit { } if self.is_do_catch_block() { let mut db = self.fatal("found removed `do catch` syntax"); - db.help("Following RFC #2388, the new non-placeholder syntax is `try`"); + db.help("following RFC #2388, the new non-placeholder syntax is `try`"); return Err(db); } if self.is_try_block() { @@ -991,13 +990,13 @@ macro_rules! parse_lit { return self.parse_try_block(lo, attrs); } - // Span::rust_2018() is somewhat expensive; don't get it repeatedly. + // `Span::rust_2018()` is somewhat expensive; don't get it repeatedly. let is_span_rust_2018 = self.token.span.rust_2018(); if is_span_rust_2018 && self.check_keyword(kw::Async) { - return if self.is_async_block() { // check for `async {` and `async move {` + return if self.is_async_block() { // Check for `async {` and `async move {`. self.parse_async_block(attrs) } else { - self.parse_lambda_expr(attrs) + self.parse_closure_expr(attrs) }; } if self.eat_keyword(kw::Return) { @@ -1043,13 +1042,12 @@ macro_rules! parse_lit { // 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 - // ``` + // + // error: expected expression, found `;` + // --> file.rs:2:13 + // | + // 2 | foo(bar(; + // | ^ expected expression self.bump(); return Ok(self.mk_expr(self.token.span, ExprKind::Err, ThinVec::new())); } @@ -1096,11 +1094,11 @@ macro_rules! parse_lit { attrs.extend(self.parse_inner_attributes()?); let blk = self.parse_block_tail(lo, blk_mode)?; - return Ok(self.mk_expr(blk.span, ExprKind::Block(blk, opt_label), attrs)); + Ok(self.mk_expr(blk.span, ExprKind::Block(blk, opt_label), attrs)) } - /// Parses `move |args| expr`. - fn parse_lambda_expr(&mut self, attrs: ThinVec) -> PResult<'a, P> { + /// Parses a closure expression (e.g., `move |args| expr`). + fn parse_closure_expr(&mut self, attrs: ThinVec) -> PResult<'a, P> { let lo = self.token.span; let movability = if self.eat_keyword(kw::Static) { @@ -1115,7 +1113,7 @@ fn parse_lambda_expr(&mut self, attrs: ThinVec) -> PResult<'a, P) -> PResult<'a, P { - // If an explicit return type is given, require a - // block to appear (RFC 968). + // If an explicit return type is given, require a block to appear (RFC 968). let body_lo = self.token.span; self.parse_block_expr(None, body_lo, BlockCheckMode::Default, ThinVec::new())? } @@ -1141,7 +1138,7 @@ fn parse_lambda_expr(&mut self, attrs: ThinVec) -> PResult<'a, P CaptureBy { if self.eat_keyword(kw::Move) { CaptureBy::Value @@ -1176,16 +1173,16 @@ fn parse_fn_block_decl(&mut self) -> PResult<'a, P> { })) } - /// Parses a parameter in a lambda header (e.g., `|arg, arg|`). + /// Parses a parameter in a closure header (e.g., `|arg, arg|`). fn parse_fn_block_param(&mut self) -> PResult<'a, Param> { let lo = self.token.span; - let attrs = self.parse_param_attributes()?; + let attrs = self.parse_outer_attributes()?; let pat = self.parse_pat(PARAM_EXPECTED)?; let t = if self.eat(&token::Colon) { self.parse_ty()? } else { P(Ty { - id: ast::DUMMY_NODE_ID, + id: DUMMY_NODE_ID, node: TyKind::Infer, span: self.prev_span, }) @@ -1196,7 +1193,8 @@ fn parse_fn_block_param(&mut self) -> PResult<'a, Param> { ty: t, pat, span, - id: ast::DUMMY_NODE_ID + id: DUMMY_NODE_ID, + is_placeholder: false, }) } @@ -1233,7 +1231,7 @@ fn parse_if_expr(&mut self, attrs: ThinVec) -> PResult<'a, P> { Ok(self.mk_expr(lo.to(hi), ExprKind::If(cond, thn, els), attrs)) } - /// Parse the condition of a `if`- or `while`-expression + /// Parses the condition of a `if` or `while` expression. fn parse_cond_expr(&mut self) -> PResult<'a, P> { let cond = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?; @@ -1261,7 +1259,7 @@ fn parse_let_expr(&mut self, attrs: ThinVec) -> PResult<'a, P> Ok(self.mk_expr(span, ExprKind::Let(pat, expr), attrs)) } - /// `else` token already eaten + /// Parses an `else { ... }` expression (`else` token already eaten). fn parse_else_expr(&mut self) -> PResult<'a, P> { if self.eat_keyword(kw::If) { return self.parse_if_expr(ThinVec::new()); @@ -1271,7 +1269,7 @@ fn parse_else_expr(&mut self) -> PResult<'a, P> { } } - /// Parse a 'for' .. 'in' expression ('for' token already eaten) + /// Parses a `for ... in` expression (`for` token already eaten). fn parse_for_expr( &mut self, opt_label: Option