about summary refs log tree commit diff
path: root/src/librustc_parse/parser/expr.rs
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2020-05-01 18:24:14 -0700
committerEsteban Küber <esteban@kuber.com.ar>2020-05-05 19:35:20 -0700
commit3cf556939e4f5fcce686ad9c157ac331bd05aabc (patch)
treecd83bd426c7021aaef20743110ea1d6457cf595b /src/librustc_parse/parser/expr.rs
parent6318d24ad8440fa30428b405be1174478e9536e3 (diff)
downloadrust-3cf556939e4f5fcce686ad9c157ac331bd05aabc.tar.gz
rust-3cf556939e4f5fcce686ad9c157ac331bd05aabc.zip
Detect errors caused by `async` block in 2015 edition
Diffstat (limited to 'src/librustc_parse/parser/expr.rs')
-rw-r--r--src/librustc_parse/parser/expr.rs54
1 files changed, 34 insertions, 20 deletions
diff --git a/src/librustc_parse/parser/expr.rs b/src/librustc_parse/parser/expr.rs
index 55c9f26999b..b3bb72554e9 100644
--- a/src/librustc_parse/parser/expr.rs
+++ b/src/librustc_parse/parser/expr.rs
@@ -13,7 +13,7 @@ use rustc_ast::util::classify;
 use rustc_ast::util::literal::LitError;
 use rustc_ast::util::parser::{prec_let_scrutinee_needs_par, AssocOp, Fixity};
 use rustc_ast_pretty::pprust;
-use rustc_errors::{Applicability, PResult};
+use rustc_errors::{Applicability, DiagnosticBuilder, PResult};
 use rustc_span::source_map::{self, Span, Spanned};
 use rustc_span::symbol::{kw, sym, Symbol};
 use std::mem;
@@ -1068,8 +1068,8 @@ impl<'a> Parser<'a> {
     }
 
     fn parse_path_start_expr(&mut self, attrs: AttrVec) -> PResult<'a, P<Expr>> {
-        let lo = self.token.span;
         let path = self.parse_path(PathStyle::Expr)?;
+        let lo = path.span;
 
         // `!`, as an operator, is prefix, so we know this isn't that.
         let (hi, kind) = if self.eat(&token::Not) {
@@ -1081,7 +1081,7 @@ impl<'a> Parser<'a> {
             };
             (self.prev_token.span, ExprKind::MacCall(mac))
         } else if self.check(&token::OpenDelim(token::Brace)) {
-            if let Some(expr) = self.maybe_parse_struct_expr(lo, &path, &attrs) {
+            if let Some(expr) = self.maybe_parse_struct_expr(&path, &attrs) {
                 return expr;
             } else {
                 (path.span, ExprKind::Path(None, path))
@@ -1895,16 +1895,15 @@ impl<'a> Parser<'a> {
 
     fn maybe_parse_struct_expr(
         &mut self,
-        lo: Span,
         path: &ast::Path,
         attrs: &AttrVec,
     ) -> Option<PResult<'a, P<Expr>>> {
         let struct_allowed = !self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL);
         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());
+            let expr = self.parse_struct_expr(path.clone(), attrs.clone());
             if let (Ok(expr), false) = (&expr, struct_allowed) {
-                self.error_struct_lit_not_allowed_here(lo, expr.span);
+                self.error_struct_lit_not_allowed_here(path.span, expr.span);
             }
             return Some(expr);
         }
@@ -1923,17 +1922,23 @@ impl<'a> Parser<'a> {
 
     pub(super) fn parse_struct_expr(
         &mut self,
-        lo: Span,
         pth: ast::Path,
         mut attrs: AttrVec,
     ) -> PResult<'a, P<Expr>> {
-        let struct_sp = lo.to(self.prev_token.span);
         self.bump();
         let mut fields = Vec::new();
         let mut base = None;
+        let mut recover_async = false;
 
         attrs.extend(self.parse_inner_attributes()?);
 
+        let mut async_block_err = |e: &mut DiagnosticBuilder<'_>, span: Span| {
+            recover_async = true;
+            e.span_label(span, "`async` blocks are only allowed in the 2018 edition");
+            e.help("set `edition = \"2018\"` in `Cargo.toml`");
+            e.note("for more on editions, read https://doc.rust-lang.org/edition-guide");
+        };
+
         while self.token != token::CloseDelim(token::Brace) {
             if self.eat(&token::DotDot) {
                 let exp_span = self.prev_token.span;
@@ -1952,7 +1957,11 @@ impl<'a> Parser<'a> {
             let parsed_field = match self.parse_field() {
                 Ok(f) => Some(f),
                 Err(mut e) => {
-                    e.span_label(struct_sp, "while parsing this struct");
+                    if pth == kw::Async {
+                        async_block_err(&mut e, pth.span);
+                    } else {
+                        e.span_label(pth.span, "while parsing this struct");
+                    }
                     e.emit();
 
                     // If the next token is a comma, then try to parse
@@ -1976,15 +1985,19 @@ impl<'a> Parser<'a> {
                     }
                 }
                 Err(mut e) => {
-                    e.span_label(struct_sp, "while parsing this struct");
-                    if let Some(f) = recovery_field {
-                        fields.push(f);
-                        e.span_suggestion(
-                            self.prev_token.span.shrink_to_hi(),
-                            "try adding a comma",
-                            ",".into(),
-                            Applicability::MachineApplicable,
-                        );
+                    if pth == kw::Async {
+                        async_block_err(&mut e, pth.span);
+                    } else {
+                        e.span_label(pth.span, "while parsing this struct");
+                        if let Some(f) = recovery_field {
+                            fields.push(f);
+                            e.span_suggestion(
+                                self.prev_token.span.shrink_to_hi(),
+                                "try adding a comma",
+                                ",".into(),
+                                Applicability::MachineApplicable,
+                            );
+                        }
                     }
                     e.emit();
                     self.recover_stmt_(SemiColonMode::Comma, BlockMode::Ignore);
@@ -1993,9 +2006,10 @@ impl<'a> Parser<'a> {
             }
         }
 
-        let span = lo.to(self.token.span);
+        let span = pth.span.to(self.token.span);
         self.expect(&token::CloseDelim(token::Brace))?;
-        Ok(self.mk_expr(span, ExprKind::Struct(pth, fields, base), attrs))
+        let expr = if recover_async { ExprKind::Err } else { ExprKind::Struct(pth, fields, base) };
+        Ok(self.mk_expr(span, expr, attrs))
     }
 
     /// Use in case of error after field-looking code: `S { foo: () with a }`.