From e77b9d36ca5b33c7c76ca420b4021dadb8b2a05e Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Wed, 4 Dec 2019 03:23:20 +0100 Subject: [PATCH 01/32] refactor parse_field --- src/librustc_parse/parser/expr.rs | 63 ++++++++++++++++--------------- 1 file changed, 33 insertions(+), 30 deletions(-) diff --git a/src/librustc_parse/parser/expr.rs b/src/librustc_parse/parser/expr.rs index fa68ddf272a84..ce17b8fa54642 100644 --- a/src/librustc_parse/parser/expr.rs +++ b/src/librustc_parse/parser/expr.rs @@ -1866,48 +1866,51 @@ impl<'a> Parser<'a> { /// Parses `ident (COLON expr)?`. fn parse_field(&mut self) -> PResult<'a, Field> { - let attrs = self.parse_outer_attributes()?; + let attrs = self.parse_outer_attributes()?.into(); let lo = self.token.span; // Check if a colon exists one ahead. This means we're parsing a fieldname. - let (fieldname, expr, is_shorthand) = - if self.look_ahead(1, |t| t == &token::Colon || t == &token::Eq) { - let fieldname = self.parse_field_name()?; - - // Check for an equals token. This means the source incorrectly attempts to - // initialize a field with an eq rather than a colon. - if self.token == token::Eq { - self.diagnostic() - .struct_span_err(self.token.span, "expected `:`, found `=`") - .span_suggestion( - fieldname.span.shrink_to_hi().to(self.token.span), - "replace equals symbol with a colon", - ":".to_string(), - Applicability::MachineApplicable, - ) - .emit(); - } - self.bump(); // `:` - (fieldname, self.parse_expr()?, false) - } else { - let fieldname = self.parse_ident_common(false)?; - - // Mimic `x: x` for the `x` field shorthand. - let path = ast::Path::from_ident(fieldname); - let expr = self.mk_expr(fieldname.span, ExprKind::Path(None, path), AttrVec::new()); - (fieldname, expr, true) - }; + 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()?) + }; Ok(ast::Field { - ident: fieldname, + ident, span: lo.to(expr.span), expr, is_shorthand, - attrs: attrs.into(), + attrs, id: DUMMY_NODE_ID, is_placeholder: false, }) } + /// 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(); + } + fn err_dotdotdot_syntax(&self, span: Span) { self.struct_span_err(span, "unexpected token: `...`") .span_suggestion( From 8480b31ba9ca615d6c1e3e6a4a42d5757b447a0c Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Wed, 4 Dec 2019 03:31:32 +0100 Subject: [PATCH 02/32] extract recover_struct_comma_after_dotdot --- src/librustc_parse/parser/expr.rs | 32 ++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/librustc_parse/parser/expr.rs b/src/librustc_parse/parser/expr.rs index ce17b8fa54642..28b49d01b7ebd 100644 --- a/src/librustc_parse/parser/expr.rs +++ b/src/librustc_parse/parser/expr.rs @@ -1788,21 +1788,7 @@ impl<'a> Parser<'a> { self.recover_stmt(); } } - if self.token == token::Comma { - self.struct_span_err( - exp_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(); - } + self.recover_struct_comma_after_dotdot(exp_span); break; } @@ -1864,6 +1850,22 @@ impl<'a> Parser<'a> { return Ok(self.mk_expr(span, ExprKind::Struct(pth, fields, base), attrs)); } + 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(); + } + /// Parses `ident (COLON expr)?`. fn parse_field(&mut self) -> PResult<'a, Field> { let attrs = self.parse_outer_attributes()?.into(); From 701b974eb9df59de29a0cff86a2b6a9026e31b5a Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Wed, 4 Dec 2019 03:47:18 +0100 Subject: [PATCH 03/32] extract find_struct_error_after_field_looking_code --- src/librustc_parse/parser/expr.rs | 49 ++++++++++++++++--------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/src/librustc_parse/parser/expr.rs b/src/librustc_parse/parser/expr.rs index 28b49d01b7ebd..5170a0b9d0732 100644 --- a/src/librustc_parse/parser/expr.rs +++ b/src/librustc_parse/parser/expr.rs @@ -1780,9 +1780,7 @@ impl<'a> Parser<'a> { if self.eat(&token::DotDot) { let exp_span = self.prev_span; match self.parse_expr() { - Ok(e) => { - base = Some(e); - } + Ok(e) => base = Some(e), Err(mut e) => { e.emit(); self.recover_stmt(); @@ -1792,24 +1790,9 @@ impl<'a> Parser<'a> { break; } - let mut recovery_field = None; - if let token::Ident(name, _) = self.token.kind { - if !self.token.is_reserved_ident() && self.look_ahead(1, |t| *t == token::Colon) { - // Use in case of error after field-looking code: `S { foo: () with a }`. - recovery_field = Some(ast::Field { - ident: Ident::new(name, self.token.span), - span: self.token.span, - expr: self.mk_expr(self.token.span, ExprKind::Err, AttrVec::new()), - is_shorthand: false, - attrs: AttrVec::new(), - id: DUMMY_NODE_ID, - is_placeholder: false, - }); - } - } - let mut parsed_field = None; - match self.parse_field() { - Ok(f) => parsed_field = Some(f), + let recovery_field = self.find_struct_error_after_field_looking_code(); + let parsed_field = match self.parse_field() { + Ok(f) => Some(f), Err(mut e) => { e.span_label(struct_sp, "while parsing this struct"); e.emit(); @@ -1823,8 +1806,9 @@ impl<'a> Parser<'a> { break; } } + None } - } + }; match self.expect_one_of(&[token::Comma], &[token::CloseDelim(token::Brace)]) { Ok(_) => { @@ -1847,7 +1831,26 @@ impl<'a> Parser<'a> { let span = lo.to(self.token.span); self.expect(&token::CloseDelim(token::Brace))?; - return Ok(self.mk_expr(span, ExprKind::Struct(pth, fields, base), attrs)); + 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 { + 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 } fn recover_struct_comma_after_dotdot(&mut self, span: Span) { From 66b8ae4bce061907bb1fdb88ba6f0a9ad918c378 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Wed, 4 Dec 2019 04:24:53 +0100 Subject: [PATCH 04/32] extract error_struct_lit_not_allowed_here --- src/librustc_parse/parser/expr.rs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/librustc_parse/parser/expr.rs b/src/librustc_parse/parser/expr.rs index 5170a0b9d0732..10912c84efc83 100644 --- a/src/librustc_parse/parser/expr.rs +++ b/src/librustc_parse/parser/expr.rs @@ -1747,22 +1747,23 @@ impl<'a> Parser<'a> { // This is a struct literal, but we don't can't accept them here. let expr = self.parse_struct_expr(lo, path.clone(), attrs.clone()); if let (Ok(expr), false) = (&expr, struct_allowed) { - self.struct_span_err(expr.span, "struct literals are not allowed here") - .multipart_suggestion( - "surround the struct literal with parentheses", - vec![ - (lo.shrink_to_lo(), "(".to_string()), - (expr.span.shrink_to_hi(), ")".to_string()), - ], - Applicability::MachineApplicable, - ) - .emit(); + self.error_struct_lit_not_allowed_here(lo, expr.span); } return Some(expr); } None } + 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(); + } + pub(super) fn parse_struct_expr( &mut self, lo: Span, From f6e2bdc341a5f25da3f29b5f37150fd320e90e8c Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Wed, 4 Dec 2019 04:31:44 +0100 Subject: [PATCH 05/32] extract is_certainly_not_a_block --- src/librustc_parse/parser/expr.rs | 32 +++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/librustc_parse/parser/expr.rs b/src/librustc_parse/parser/expr.rs index 10912c84efc83..5a44b5edc533b 100644 --- a/src/librustc_parse/parser/expr.rs +++ b/src/librustc_parse/parser/expr.rs @@ -1721,6 +1721,21 @@ impl<'a> Parser<'a> { )) } + 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()) + ) + ) + } + fn maybe_parse_struct_expr( &mut self, lo: Span, @@ -1728,22 +1743,7 @@ impl<'a> Parser<'a> { attrs: &AttrVec, ) -> Option>> { let struct_allowed = !self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL); - let certainly_not_a_block = || { - 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()) - ) - ) - }; - - if struct_allowed || certainly_not_a_block() { + if struct_allowed || self.is_certainly_not_a_block() { // This is a struct literal, but we don't can't accept them here. let expr = self.parse_struct_expr(lo, path.clone(), attrs.clone()); if let (Ok(expr), false) = (&expr, struct_allowed) { From de2e443bc62a5e6ce003c850d8a2b5302950d6e3 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Fri, 6 Dec 2019 22:05:47 +0100 Subject: [PATCH 06/32] make parse_async_block conventional --- src/librustc_parse/parser/expr.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/librustc_parse/parser/expr.rs b/src/librustc_parse/parser/expr.rs index 5a44b5edc533b..c3cac8c646598 100644 --- a/src/librustc_parse/parser/expr.rs +++ b/src/librustc_parse/parser/expr.rs @@ -1556,7 +1556,7 @@ impl<'a> Parser<'a> { fn parse_match_expr(&mut self, mut attrs: AttrVec) -> PResult<'a, P> { let match_span = self.prev_span; let lo = self.prev_span; - let discriminant = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?; + let scrutinee = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?; if let Err(mut e) = self.expect(&token::OpenDelim(token::Brace)) { if self.token == token::Semi { e.span_suggestion_short( @@ -1582,13 +1582,13 @@ impl<'a> Parser<'a> { if self.token == token::CloseDelim(token::Brace) { self.bump(); } - return Ok(self.mk_expr(span, ExprKind::Match(discriminant, arms), attrs)); + return Ok(self.mk_expr(span, ExprKind::Match(scrutinee, arms), attrs)); } } } let hi = self.token.span; self.bump(); - return Ok(self.mk_expr(lo.to(hi), ExprKind::Match(discriminant, arms), attrs)); + return Ok(self.mk_expr(lo.to(hi), ExprKind::Match(scrutinee, arms), attrs)); } pub(super) fn parse_arm(&mut self) -> PResult<'a, Arm> { @@ -1697,16 +1697,13 @@ impl<'a> Parser<'a> { /// Parses an `async move? {...}` expression. fn parse_async_block(&mut self, mut attrs: AttrVec) -> PResult<'a, P> { - let span_lo = self.token.span; + let lo = self.token.span; self.expect_keyword(kw::Async)?; let capture_clause = self.parse_capture_clause(); let (iattrs, body) = self.parse_inner_attrs_and_block()?; attrs.extend(iattrs); - Ok(self.mk_expr( - span_lo.to(body.span), - ExprKind::Async(capture_clause, DUMMY_NODE_ID, body), - attrs, - )) + let kind = ExprKind::Async(capture_clause, DUMMY_NODE_ID, body); + Ok(self.mk_expr(lo.to(self.prev_span), kind, attrs)) } fn is_async_block(&self) -> bool { From 7262dcc4a78a4e63db29410365fe7d47f2b56fd0 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Fri, 6 Dec 2019 22:41:10 +0100 Subject: [PATCH 07/32] refactor loop parsing a bit --- src/librustc_parse/parser/expr.rs | 55 +++++++++++++++---------------- src/test/ui/while-let.stderr | 4 +-- 2 files changed, 28 insertions(+), 31 deletions(-) diff --git a/src/librustc_parse/parser/expr.rs b/src/librustc_parse/parser/expr.rs index c3cac8c646598..17198040d25b5 100644 --- a/src/librustc_parse/parser/expr.rs +++ b/src/librustc_parse/parser/expr.rs @@ -1472,15 +1472,13 @@ impl<'a> Parser<'a> { } } - /// Parses a `for ... in` expression (`for` token already eaten). + /// Parses `for in ` (`for` token already eaten). fn parse_for_expr( &mut self, opt_label: Option