From 15a6c09b6e8a977f2c6f5a73de01a20d00b37930 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sun, 22 Sep 2019 00:19:02 +0200 Subject: [PATCH] pre-expansion gate type_ascription --- src/libsyntax/feature_gate/check.rs | 21 ++++++------------- src/libsyntax/parse/parser/expr.rs | 1 + src/libsyntax/sess.rs | 2 ++ .../feature-gate-type_ascription.rs | 5 ++++- .../feature-gate-type_ascription.stderr | 2 +- 5 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/libsyntax/feature_gate/check.rs b/src/libsyntax/feature_gate/check.rs index 7243f5c0320..502b1c0f743 100644 --- a/src/libsyntax/feature_gate/check.rs +++ b/src/libsyntax/feature_gate/check.rs @@ -498,21 +498,6 @@ fn visit_fn_ret_ty(&mut self, ret_ty: &'a ast::FunctionRetTy) { } } - fn visit_expr(&mut self, e: &'a ast::Expr) { - match e.kind { - ast::ExprKind::Type(..) => { - // To avoid noise about type ascription in common syntax errors, only emit if it - // is the *only* error. - if self.parse_sess.span_diagnostic.err_count() == 0 { - gate_feature_post!(&self, type_ascription, e.span, - "type ascription is experimental"); - } - } - _ => {} - } - visit::walk_expr(self, e) - } - fn visit_pat(&mut self, pattern: &'a ast::Pat) { match &pattern.kind { PatKind::Slice(pats) => { @@ -805,6 +790,12 @@ macro_rules! gate_all { gate_all!(label_break_value, "labels on blocks are unstable"); gate_all!(box_syntax, "box expression syntax is experimental; you can call `Box::new` instead"); + // To avoid noise about type ascription in common syntax errors, + // only emit if it is the *only* error. (Also check it last.) + if parse_sess.span_diagnostic.err_count() == 0 { + gate_all!(type_ascription, "type ascription is experimental"); + } + visit::walk_crate(&mut visitor, krate); } diff --git a/src/libsyntax/parse/parser/expr.rs b/src/libsyntax/parse/parser/expr.rs index e7dd15654d8..97b1092452a 100644 --- a/src/libsyntax/parse/parser/expr.rs +++ b/src/libsyntax/parse/parser/expr.rs @@ -252,6 +252,7 @@ pub(super) fn parse_assoc_expr_with( self.last_type_ascription = Some((self.prev_span, maybe_path)); lhs = self.parse_assoc_op_cast(lhs, lhs_span, ExprKind::Type)?; + self.sess.gated_spans.type_ascription.borrow_mut().push(lhs.span); continue } else if op == AssocOp::DotDot || op == AssocOp::DotDotEq { // If we didn’t have to handle `x..`/`x..=`, it would be pretty easy to diff --git a/src/libsyntax/sess.rs b/src/libsyntax/sess.rs index febaa714775..28a0868d5dd 100644 --- a/src/libsyntax/sess.rs +++ b/src/libsyntax/sess.rs @@ -50,6 +50,8 @@ pub label_break_value: Lock>, /// Spans collected for gating `box_syntax`, e.g. `box $expr`. pub box_syntax: Lock>, + /// Spans collected for gating `type_ascription`, e.g. `42: usize`. + pub type_ascription: Lock>, } /// Info about a parsing session. diff --git a/src/test/ui/feature-gates/feature-gate-type_ascription.rs b/src/test/ui/feature-gates/feature-gate-type_ascription.rs index 7a597157300..655891d802c 100644 --- a/src/test/ui/feature-gates/feature-gate-type_ascription.rs +++ b/src/test/ui/feature-gates/feature-gate-type_ascription.rs @@ -1,5 +1,8 @@ // Type ascription is unstable -fn main() { +#[cfg(FALSE)] +fn foo() { let a = 10: u8; //~ ERROR type ascription is experimental } + +fn main() {} diff --git a/src/test/ui/feature-gates/feature-gate-type_ascription.stderr b/src/test/ui/feature-gates/feature-gate-type_ascription.stderr index 83f95529f0d..d63d624c6c1 100644 --- a/src/test/ui/feature-gates/feature-gate-type_ascription.stderr +++ b/src/test/ui/feature-gates/feature-gate-type_ascription.stderr @@ -1,5 +1,5 @@ error[E0658]: type ascription is experimental - --> $DIR/feature-gate-type_ascription.rs:4:13 + --> $DIR/feature-gate-type_ascription.rs:5:13 | LL | let a = 10: u8; | ^^^^^^ -- 2.44.0