@@ -2,7 +2,7 @@ use rustc_ast as ast;
22use rustc_ast:: visit:: { self , AssocCtxt , FnCtxt , FnKind , Visitor } ;
33use rustc_ast:: { AssocConstraint , AssocConstraintKind , NodeId } ;
44use rustc_ast:: { PatKind , RangeEnd , VariantData } ;
5- use rustc_errors:: struct_span_err;
5+ use rustc_errors:: { struct_span_err, Applicability } ;
66use rustc_feature:: { AttributeGate , BuiltinAttribute , BUILTIN_ATTRIBUTE_MAP } ;
77use rustc_feature:: { Features , GateIssue } ;
88use rustc_session:: parse:: { feature_err, feature_err_issue} ;
@@ -577,6 +577,32 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
577577 }
578578 }
579579
580+ fn visit_stmt ( & mut self , stmt : & ' a ast:: Stmt ) {
581+ if let ast:: StmtKind :: Semi ( expr) = & stmt. kind
582+ && let ast:: ExprKind :: Assign ( lhs, _, _) = & expr. kind
583+ && let ast:: ExprKind :: Type ( ..) = lhs. kind
584+ && self . sess . parse_sess . span_diagnostic . err_count ( ) == 0
585+ && !self . features . type_ascription
586+ && !lhs. span . allows_unstable ( sym:: type_ascription)
587+ {
588+ // When we encounter a statement of the form `foo: Ty = val;`, this will emit a type
589+ // ascription error, but the likely intention was to write a `let` statement. (#78907).
590+ feature_err_issue (
591+ & self . sess . parse_sess ,
592+ sym:: type_ascription,
593+ lhs. span ,
594+ GateIssue :: Language ,
595+ "type ascription is experimental" ,
596+ ) . span_suggestion_verbose (
597+ lhs. span . shrink_to_lo ( ) ,
598+ "you might have meant to introduce a new binding" ,
599+ "let " . to_string ( ) ,
600+ Applicability :: MachineApplicable ,
601+ ) . emit ( ) ;
602+ }
603+ visit:: walk_stmt ( self , stmt) ;
604+ }
605+
580606 fn visit_expr ( & mut self , e : & ' a ast:: Expr ) {
581607 match e. kind {
582608 ast:: ExprKind :: Box ( _) => {
@@ -795,8 +821,6 @@ fn maybe_stage_features(sess: &Session, krate: &ast::Crate) {
795821 // checks if `#![feature]` has been used to enable any lang feature
796822 // does not check the same for lib features unless there's at least one
797823 // declared lang feature
798- use rustc_errors:: Applicability ;
799-
800824 if !sess. opts . unstable_features . is_nightly_build ( ) {
801825 let lang_features = & sess. features_untracked ( ) . declared_lang_features ;
802826 if lang_features. len ( ) == 0 {
0 commit comments