1
Fork 0

let_chains: Move feature gating to pre-expansion.

This commit is contained in:
Mazdak Farrokhzad 2019-06-17 05:41:21 +02:00
parent d551880267
commit eb4f54a58d
3 changed files with 28 additions and 28 deletions

View file

@ -1940,27 +1940,6 @@ impl<'a> PostExpansionVisitor<'a> {
Err(mut err) => err.emit(), Err(mut err) => err.emit(),
} }
} }
/// Recurse into all places where a `let` expression would be feature gated
/// and emit gate post errors for those.
fn find_and_gate_lets(&mut self, e: &'a ast::Expr) {
match &e.node {
ast::ExprKind::Paren(e) => {
self.find_and_gate_lets(e);
}
ast::ExprKind::Binary(op, lhs, rhs) if op.node == ast::BinOpKind::And => {
self.find_and_gate_lets(lhs);
self.find_and_gate_lets(rhs);
}
ast::ExprKind::Let(..) => {
gate_feature_post!(
&self, let_chains, e.span,
"`let` expressions in this position are experimental"
);
}
_ => {}
}
}
} }
impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
@ -2158,10 +2137,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
fn visit_expr(&mut self, e: &'a ast::Expr) { fn visit_expr(&mut self, e: &'a ast::Expr) {
match e.node { match e.node {
ast::ExprKind::If(ref e, ..) | ast::ExprKind::While(ref e, ..) => match e.node {
ast::ExprKind::Let(..) => {} // Stable!,
_ => self.find_and_gate_lets(e),
}
ast::ExprKind::Box(_) => { ast::ExprKind::Box(_) => {
gate_feature_post!(&self, box_syntax, e.span, EXPLAIN_BOX_SYNTAX); gate_feature_post!(&self, box_syntax, e.span, EXPLAIN_BOX_SYNTAX);
} }
@ -2546,6 +2521,17 @@ pub fn check_crate(krate: &ast::Crate,
"attributes on function parameters are unstable" "attributes on function parameters are unstable"
)); ));
sess
.let_chains_spans
.borrow()
.iter()
.for_each(|span| gate_feature!(
&ctx,
let_chains,
*span,
"`let` expressions in this position are experimental"
));
let visitor = &mut PostExpansionVisitor { let visitor = &mut PostExpansionVisitor {
context: &ctx, context: &ctx,
builtin_attributes: &*BUILTIN_ATTRIBUTE_MAP, builtin_attributes: &*BUILTIN_ATTRIBUTE_MAP,

View file

@ -54,7 +54,9 @@ pub struct ParseSess {
/// operation token that followed it, but that the parser cannot identify without further /// operation token that followed it, but that the parser cannot identify without further
/// analysis. /// analysis.
pub ambiguous_block_expr_parse: Lock<FxHashMap<Span, Span>>, 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 { impl ParseSess {
@ -81,6 +83,7 @@ impl ParseSess {
edition: Edition::from_session(), edition: Edition::from_session(),
ambiguous_block_expr_parse: Lock::new(FxHashMap::default()), ambiguous_block_expr_parse: Lock::new(FxHashMap::default()),
param_attr_spans: Lock::new(Vec::new()), param_attr_spans: Lock::new(Vec::new()),
let_chains_spans: Lock::new(Vec::new()),
} }
} }

View file

@ -3158,6 +3158,7 @@ impl<'a> Parser<'a> {
fn parse_if_expr(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> { fn parse_if_expr(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
let lo = self.prev_span; let lo = self.prev_span;
let cond = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?; 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 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 // 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)) 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. /// Parses a `let $pats = $expr` pseudo-expression.
/// The `let` token has already been eaten. /// The `let` token has already been eaten.
fn parse_let_expr(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> { fn parse_let_expr(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
let lo = self.prev_span; let lo = self.prev_span;
let pats = self.parse_pats()?; let pats = self.parse_pats()?;
self.expect(&token::Eq)?; self.expect(&token::Eq)?;
let expr = self.with_res( let expr = self.with_res(
Restrictions::NO_STRUCT_LITERAL, Restrictions::NO_STRUCT_LITERAL,
|this| this.parse_assoc_expr_with(1 + AssocOp::LAnd.precedence(), None.into()) |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`. /// Parses `move |args| expr`.
@ -3286,6 +3296,7 @@ impl<'a> Parser<'a> {
span_lo: Span, span_lo: Span,
mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> { mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
let cond = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?; 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()?; let (iattrs, body) = self.parse_inner_attrs_and_block()?;
attrs.extend(iattrs); attrs.extend(iattrs);
let span = span_lo.to(body.span); let span = span_lo.to(body.span);