From b002c9ff11805e681d3f5cd9aa33f14d15543bd8 Mon Sep 17 00:00:00 2001 From: yukang Date: Mon, 8 May 2023 06:49:50 +0800 Subject: [PATCH 1/3] remove type ascription feature gate --- compiler/rustc_ast_passes/src/feature_gate.rs | 35 ++----------------- compiler/rustc_feature/src/active.rs | 2 -- compiler/rustc_hir_typeck/src/cast.rs | 4 +-- ...ial-casts-featuring-type-ascription.stderr | 4 +-- 4 files changed, 5 insertions(+), 40 deletions(-) diff --git a/compiler/rustc_ast_passes/src/feature_gate.rs b/compiler/rustc_ast_passes/src/feature_gate.rs index 274f931e43f..a6fd3b5c943 100644 --- a/compiler/rustc_ast_passes/src/feature_gate.rs +++ b/compiler/rustc_ast_passes/src/feature_gate.rs @@ -2,7 +2,7 @@ use rustc_ast as ast; use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor}; use rustc_ast::{attr, AssocConstraint, AssocConstraintKind, NodeId}; use rustc_ast::{PatKind, RangeEnd}; -use rustc_errors::{Applicability, StashKey}; +use rustc_errors::StashKey; use rustc_feature::{AttributeGate, BuiltinAttribute, Features, GateIssue, BUILTIN_ATTRIBUTE_MAP}; use rustc_session::parse::{feature_err, feature_err_issue, feature_warn}; use rustc_session::Session; @@ -375,43 +375,13 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { } fn visit_stmt(&mut self, stmt: &'a ast::Stmt) { - if let ast::StmtKind::Semi(expr) = &stmt.kind - && let ast::ExprKind::Assign(lhs, _, _) = &expr.kind - && let ast::ExprKind::Type(..) = lhs.kind - && self.sess.parse_sess.span_diagnostic.err_count() == 0 - && !self.features.type_ascription - && !lhs.span.allows_unstable(sym::type_ascription) - { - // When we encounter a statement of the form `foo: Ty = val;`, this will emit a type - // ascription error, but the likely intention was to write a `let` statement. (#78907). - feature_err( - &self.sess.parse_sess, - sym::type_ascription, - lhs.span, - "type ascription is experimental", - ).span_suggestion_verbose( - lhs.span.shrink_to_lo(), - "you might have meant to introduce a new binding", - "let ", - Applicability::MachineApplicable, - ).emit(); - } visit::walk_stmt(self, stmt); } fn visit_expr(&mut self, e: &'a ast::Expr) { match e.kind { ast::ExprKind::Type(..) => { - if self.sess.parse_sess.span_diagnostic.err_count() == 0 { - // To avoid noise about type ascription in common syntax errors, - // only emit if it is the *only* error. - gate_feature_post!( - &self, - type_ascription, - e.span, - "type ascription is experimental" - ); - } else { + if self.sess.parse_sess.span_diagnostic.err_count() > 0 { // And if it isn't, cancel the early-pass warning. if let Some(err) = self .sess @@ -629,7 +599,6 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session) { gate_all!(box_patterns, "box pattern syntax is experimental"); gate_all!(exclusive_range_pattern, "exclusive range pattern syntax is experimental"); gate_all!(try_blocks, "`try` blocks are unstable"); - gate_all!(type_ascription, "type ascription is experimental"); visit::walk_crate(&mut visitor, krate); } diff --git a/compiler/rustc_feature/src/active.rs b/compiler/rustc_feature/src/active.rs index 4c53f9d8369..549662a72fa 100644 --- a/compiler/rustc_feature/src/active.rs +++ b/compiler/rustc_feature/src/active.rs @@ -534,8 +534,6 @@ declare_features! ( (active, try_blocks, "1.29.0", Some(31436), None), /// Allows `impl Trait` to be used inside type aliases (RFC 2515). (active, type_alias_impl_trait, "1.38.0", Some(63063), None), - /// Allows the use of type ascription in expressions. - (active, type_ascription, "1.6.0", Some(23416), None), /// Allows creation of instances of a struct by moving fields that have /// not changed from prior instances of the same struct (RFC #2528) (active, type_changing_struct_update, "1.58.0", Some(86555), None), diff --git a/compiler/rustc_hir_typeck/src/cast.rs b/compiler/rustc_hir_typeck/src/cast.rs index 98c683f0200..7a1e830073a 100644 --- a/compiler/rustc_hir_typeck/src/cast.rs +++ b/compiler/rustc_hir_typeck/src/cast.rs @@ -689,8 +689,6 @@ impl<'a, 'tcx> CastCheck<'tcx> { fn trivial_cast_lint(&self, fcx: &FnCtxt<'a, 'tcx>) { let t_cast = self.cast_ty; let t_expr = self.expr_ty; - let type_asc_or = - if fcx.tcx.features().type_ascription { "type ascription or " } else { "" }; let (adjective, lint) = if t_cast.is_numeric() && t_expr.is_numeric() { ("numeric ", lint::builtin::TRIVIAL_NUMERIC_CASTS) } else { @@ -711,7 +709,7 @@ impl<'a, 'tcx> CastCheck<'tcx> { |lint| { lint.help(format!( "cast can be replaced by coercion; this might \ - require {type_asc_or}a temporary variable" + require a temporary variable" )) }, ); diff --git a/tests/ui/lint/trivial-casts-featuring-type-ascription.stderr b/tests/ui/lint/trivial-casts-featuring-type-ascription.stderr index 5087807b6c7..159a54873cc 100644 --- a/tests/ui/lint/trivial-casts-featuring-type-ascription.stderr +++ b/tests/ui/lint/trivial-casts-featuring-type-ascription.stderr @@ -4,7 +4,7 @@ error: trivial numeric cast: `i32` as `i32` LL | let lugubrious = 12i32 as i32; | ^^^^^^^^^^^^ | - = help: cast can be replaced by coercion; this might require type ascription or a temporary variable + = help: cast can be replaced by coercion; this might require a temporary variable note: the lint level is defined here --> $DIR/trivial-casts-featuring-type-ascription.rs:1:24 | @@ -17,7 +17,7 @@ error: trivial cast: `&u32` as `*const u32` LL | let _ = haunted as *const u32; | ^^^^^^^^^^^^^^^^^^^^^ | - = help: cast can be replaced by coercion; this might require type ascription or a temporary variable + = help: cast can be replaced by coercion; this might require a temporary variable note: the lint level is defined here --> $DIR/trivial-casts-featuring-type-ascription.rs:1:9 | From 2d27932b6c935b1678af3fc76c8e40ac266b4b41 Mon Sep 17 00:00:00 2001 From: yukang Date: Mon, 8 May 2023 21:00:26 +0800 Subject: [PATCH 2/3] remove EarlySyntaxWarning for type ascription --- compiler/rustc_ast_passes/src/feature_gate.rs | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/compiler/rustc_ast_passes/src/feature_gate.rs b/compiler/rustc_ast_passes/src/feature_gate.rs index a6fd3b5c943..99e2340e8d8 100644 --- a/compiler/rustc_ast_passes/src/feature_gate.rs +++ b/compiler/rustc_ast_passes/src/feature_gate.rs @@ -2,7 +2,6 @@ use rustc_ast as ast; use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor}; use rustc_ast::{attr, AssocConstraint, AssocConstraintKind, NodeId}; use rustc_ast::{PatKind, RangeEnd}; -use rustc_errors::StashKey; use rustc_feature::{AttributeGate, BuiltinAttribute, Features, GateIssue, BUILTIN_ATTRIBUTE_MAP}; use rustc_session::parse::{feature_err, feature_err_issue, feature_warn}; use rustc_session::Session; @@ -380,19 +379,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { fn visit_expr(&mut self, e: &'a ast::Expr) { match e.kind { - ast::ExprKind::Type(..) => { - if self.sess.parse_sess.span_diagnostic.err_count() > 0 { - // And if it isn't, cancel the early-pass warning. - if let Some(err) = self - .sess - .parse_sess - .span_diagnostic - .steal_diagnostic(e.span, StashKey::EarlySyntaxWarning) - { - err.cancel() - } - } - } ast::ExprKind::TryBlock(_) => { gate_feature_post!(&self, try_blocks, e.span, "`try` expression is experimental"); } From 8baa32ff95539ee80136a662a0305d0c8dab9800 Mon Sep 17 00:00:00 2001 From: yukang Date: Mon, 8 May 2023 23:35:04 +0800 Subject: [PATCH 3/3] cleanup --- compiler/rustc_ast_passes/src/feature_gate.rs | 4 ---- compiler/rustc_feature/src/active.rs | 2 ++ 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_ast_passes/src/feature_gate.rs b/compiler/rustc_ast_passes/src/feature_gate.rs index 99e2340e8d8..2125349909e 100644 --- a/compiler/rustc_ast_passes/src/feature_gate.rs +++ b/compiler/rustc_ast_passes/src/feature_gate.rs @@ -373,10 +373,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { } } - fn visit_stmt(&mut self, stmt: &'a ast::Stmt) { - visit::walk_stmt(self, stmt); - } - fn visit_expr(&mut self, e: &'a ast::Expr) { match e.kind { ast::ExprKind::TryBlock(_) => { diff --git a/compiler/rustc_feature/src/active.rs b/compiler/rustc_feature/src/active.rs index 549662a72fa..4c53f9d8369 100644 --- a/compiler/rustc_feature/src/active.rs +++ b/compiler/rustc_feature/src/active.rs @@ -534,6 +534,8 @@ declare_features! ( (active, try_blocks, "1.29.0", Some(31436), None), /// Allows `impl Trait` to be used inside type aliases (RFC 2515). (active, type_alias_impl_trait, "1.38.0", Some(63063), None), + /// Allows the use of type ascription in expressions. + (active, type_ascription, "1.6.0", Some(23416), None), /// Allows creation of instances of a struct by moving fields that have /// not changed from prior instances of the same struct (RFC #2528) (active, type_changing_struct_update, "1.58.0", Some(86555), None),