X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=compiler%2Frustc_parse%2Fsrc%2Fparser%2Fstmt.rs;h=d083c379f77209fc316fb776cc7d7e83f285a79d;hb=89d2600d01dec0fa73e11edc9c5bb435e4ec1285;hp=85515bd2a631459c6cf10ba8e60deb78f4e0d0a7;hpb=6f388bb369ddb6fb64e547009e031598425f773c;p=rust.git diff --git a/compiler/rustc_parse/src/parser/stmt.rs b/compiler/rustc_parse/src/parser/stmt.rs index 85515bd2a63..d083c379f77 100644 --- a/compiler/rustc_parse/src/parser/stmt.rs +++ b/compiler/rustc_parse/src/parser/stmt.rs @@ -11,8 +11,9 @@ use rustc_ast::ptr::P; use rustc_ast::token::{self, TokenKind}; use rustc_ast::util::classify; -use rustc_ast::AstLike; -use rustc_ast::{AttrStyle, AttrVec, Attribute, MacCall, MacCallStmt, MacStmtStyle}; +use rustc_ast::{ + AstLike, AttrStyle, AttrVec, Attribute, LocalKind, MacCall, MacCallStmt, MacStmtStyle, +}; use rustc_ast::{Block, BlockCheckMode, Expr, ExprKind, Local, Stmt}; use rustc_ast::{StmtKind, DUMMY_NODE_ID}; use rustc_errors::{Applicability, PResult}; @@ -292,8 +293,19 @@ fn parse_local(&mut self, attrs: AttrVec) -> PResult<'a, P> { return Err(err); } }; + let kind = match init { + None => LocalKind::Decl, + Some(init) => { + if self.eat_keyword(kw::Else) { + let els = self.parse_block()?; + LocalKind::InitElse(init, els) + } else { + LocalKind::Init(init) + } + } + }; let hi = if self.token == token::Semi { self.token.span } else { self.prev_token.span }; - Ok(P(ast::Local { ty, pat, init, id: DUMMY_NODE_ID, span: lo.to(hi), attrs, tokens: None })) + Ok(P(ast::Local { ty, pat, kind, id: DUMMY_NODE_ID, span: lo.to(hi), attrs, tokens: None })) } /// Parses the RHS of a local variable declaration (e.g., `= 14;`). @@ -495,13 +507,13 @@ pub fn parse_full_stmt( StmtKind::Expr(_) | StmtKind::MacCall(_) => {} StmtKind::Local(ref mut local) if let Err(e) = self.expect_semi() => { // We might be at the `,` in `let x = foo;`. Try to recover. - match &mut local.init { - Some(ref mut expr) => { - self.check_mistyped_turbofish_with_multiple_type_params(e, expr)?; - // We found `foo`, have we fully recovered? - self.expect_semi()?; - } - None => return Err(e), + match &mut local.kind { + LocalKind::Init(expr) | LocalKind::InitElse(expr, _) => { + self.check_mistyped_turbofish_with_multiple_type_params(e, expr)?; + // We found `foo`, have we fully recovered? + self.expect_semi()?; + } + LocalKind::Decl => return Err(e), } eat_semi = false; }