@@ -10,6 +10,7 @@ use rustc_hir::pat_util::EnumerateAndAdjustIterator;
1010use rustc_hir:: { self as hir, BindingAnnotation , ByRef , HirId , Mutability , Pat , PatKind } ;
1111use rustc_infer:: infer;
1212use rustc_infer:: infer:: type_variable:: TypeVariableOrigin ;
13+ use rustc_lint as lint;
1314use rustc_middle:: mir:: interpret:: ErrorHandled ;
1415use rustc_middle:: ty:: { self , Adt , Ty , TypeVisitableExt } ;
1516use rustc_session:: lint:: builtin:: NON_EXHAUSTIVE_OMITTED_PATTERNS ;
@@ -629,12 +630,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
629630 expected : Ty < ' tcx > ,
630631 pat_info : PatInfo < ' tcx , ' _ > ,
631632 ) -> Ty < ' tcx > {
632- let PatInfo { binding_mode : def_bm , top_info : ti, .. } = pat_info;
633+ let PatInfo { binding_mode : BindingAnnotation ( def_br , _ ) , top_info : ti, .. } = pat_info;
633634
634635 // Determine the binding mode...
635636 let bm = match ba {
636- BindingAnnotation ( ByRef :: No , Mutability :: Not ) => def_bm,
637- _ => ba,
637+ BindingAnnotation ( ByRef :: No , Mutability :: Mut )
638+ if !( pat. span . at_least_rust_2024 ( )
639+ && self . tcx . features ( ) . mut_preserve_binding_mode_2024 )
640+ && matches ! ( def_br, ByRef :: Yes ( _) ) =>
641+ {
642+ // `mut x` resets the binding mode in edition <= 2021.
643+ self . tcx . emit_node_span_lint (
644+ lint:: builtin:: DEREFERENCING_MUT_BINDING ,
645+ pat. hir_id ,
646+ pat. span ,
647+ errors:: DereferencingMutBinding { span : pat. span } ,
648+ ) ;
649+ BindingAnnotation ( ByRef :: No , Mutability :: Mut )
650+ }
651+ BindingAnnotation ( ByRef :: No , mutbl) => BindingAnnotation ( def_br, mutbl) ,
652+ BindingAnnotation ( ByRef :: Yes ( _) , _) => ba,
638653 } ;
639654 // ...and store it in a side table:
640655 self . typeck_results . borrow_mut ( ) . pat_binding_modes_mut ( ) . insert ( pat. hir_id , bm) ;
@@ -743,7 +758,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
743758 }
744759 }
745760
746- // Precondition: pat is a Ref(_) pattern
761+ /// Precondition: pat is a ` Ref(_)` pattern
747762 fn borrow_pat_suggestion ( & self , err : & mut Diag < ' _ > , pat : & Pat < ' _ > ) {
748763 let tcx = self . tcx ;
749764 if let PatKind :: Ref ( inner, mutbl) = pat. kind
0 commit comments