@@ -2,7 +2,7 @@ use rustc_ast as ast;
2
2
use rustc_ast:: visit:: { self , AssocCtxt , FnCtxt , FnKind , Visitor } ;
3
3
use rustc_ast:: { AssocConstraint , AssocConstraintKind , NodeId } ;
4
4
use rustc_ast:: { PatKind , RangeEnd , VariantData } ;
5
- use rustc_errors:: struct_span_err;
5
+ use rustc_errors:: { struct_span_err, Applicability } ;
6
6
use rustc_feature:: { AttributeGate , BuiltinAttribute , BUILTIN_ATTRIBUTE_MAP } ;
7
7
use rustc_feature:: { Features , GateIssue } ;
8
8
use rustc_session:: parse:: { feature_err, feature_err_issue} ;
@@ -577,6 +577,32 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
577
577
}
578
578
}
579
579
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
+
580
606
fn visit_expr ( & mut self , e : & ' a ast:: Expr ) {
581
607
match e. kind {
582
608
ast:: ExprKind :: Box ( _) => {
@@ -795,8 +821,6 @@ fn maybe_stage_features(sess: &Session, krate: &ast::Crate) {
795
821
// checks if `#![feature]` has been used to enable any lang feature
796
822
// does not check the same for lib features unless there's at least one
797
823
// declared lang feature
798
- use rustc_errors:: Applicability ;
799
-
800
824
if !sess. opts . unstable_features . is_nightly_build ( ) {
801
825
let lang_features = & sess. features_untracked ( ) . declared_lang_features ;
802
826
if lang_features. len ( ) == 0 {
0 commit comments