about summary refs log tree commit diff
path: root/src/libsyntax/parse
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-06-17 05:41:21 +0200
committerMazdak Farrokhzad <twingoow@gmail.com>2019-06-23 01:29:29 +0200
commiteb4f54a58d0247dbd830165fd2ff214e07890964 (patch)
tree59489d9d482d57bf95a33850bc6a605aa824c452 /src/libsyntax/parse
parentd5518802677d4701185b5cd3b9df4b263fc864b2 (diff)
downloadrust-eb4f54a58d0247dbd830165fd2ff214e07890964.tar.gz
rust-eb4f54a58d0247dbd830165fd2ff214e07890964.zip
let_chains: Move feature gating to pre-expansion.
Diffstat (limited to 'src/libsyntax/parse')
-rw-r--r--src/libsyntax/parse/mod.rs5
-rw-r--r--src/libsyntax/parse/parser.rs15
2 files changed, 17 insertions, 3 deletions
diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs
index ba5d1d009d1..e19eab371f4 100644
--- a/src/libsyntax/parse/mod.rs
+++ b/src/libsyntax/parse/mod.rs
@@ -54,7 +54,9 @@ pub struct ParseSess {
     /// operation token that followed it, but that the parser cannot identify without further
     /// analysis.
     pub ambiguous_block_expr_parse: Lock<FxHashMap<Span, Span>>,
-    pub param_attr_spans: Lock<Vec<Span>>
+    pub param_attr_spans: Lock<Vec<Span>>,
+    // Places where `let` exprs were used and should be feature gated according to `let_chains`.
+    pub let_chains_spans: Lock<Vec<Span>>,
 }
 
 impl ParseSess {
@@ -81,6 +83,7 @@ impl ParseSess {
             edition: Edition::from_session(),
             ambiguous_block_expr_parse: Lock::new(FxHashMap::default()),
             param_attr_spans: Lock::new(Vec::new()),
+            let_chains_spans: Lock::new(Vec::new()),
         }
     }
 
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index 56b363439d1..1e420855b67 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -3158,6 +3158,7 @@ impl<'a> Parser<'a> {
     fn parse_if_expr(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
         let lo = self.prev_span;
         let cond = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
+        self.ungate_prev_let_expr(&cond);
 
         // 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
@@ -3187,18 +3188,27 @@ impl<'a> Parser<'a> {
         Ok(self.mk_expr(lo.to(hi), ExprKind::If(cond, thn, els), attrs))
     }
 
+    /// Remove the last feature gating of a `let` expression that must the one provided.
+    fn ungate_prev_let_expr(&mut self, expr: &Expr) {
+        if let ExprKind::Let(..) = expr.node {
+            let last = self.sess.let_chains_spans.borrow_mut().pop();
+            debug_assert_eq!(expr.span, last.unwrap());
+        }
+    }
+
     /// Parses a `let $pats = $expr` pseudo-expression.
     /// The `let` token has already been eaten.
     fn parse_let_expr(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
         let lo = self.prev_span;
         let pats = self.parse_pats()?;
         self.expect(&token::Eq)?;
-
         let expr = self.with_res(
             Restrictions::NO_STRUCT_LITERAL,
             |this| this.parse_assoc_expr_with(1 + AssocOp::LAnd.precedence(), None.into())
         )?;
-        Ok(self.mk_expr(lo.to(expr.span), ExprKind::Let(pats, expr), attrs))
+        let span = lo.to(expr.span);
+        self.sess.let_chains_spans.borrow_mut().push(span);
+        Ok(self.mk_expr(span, ExprKind::Let(pats, expr), attrs))
     }
 
     /// Parses `move |args| expr`.
@@ -3286,6 +3296,7 @@ impl<'a> Parser<'a> {
                             span_lo: Span,
                             mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
         let cond = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
+        self.ungate_prev_let_expr(&cond);
         let (iattrs, body) = self.parse_inner_attrs_and_block()?;
         attrs.extend(iattrs);
         let span = span_lo.to(body.span);