@@ -10,6 +10,7 @@ use rustc_hir::pat_util::EnumerateAndAdjustIterator;
10
10
use rustc_hir:: { self as hir, BindingAnnotation , ByRef , HirId , Mutability , Pat , PatKind } ;
11
11
use rustc_infer:: infer;
12
12
use rustc_infer:: infer:: type_variable:: TypeVariableOrigin ;
13
+ use rustc_lint as lint;
13
14
use rustc_middle:: mir:: interpret:: ErrorHandled ;
14
15
use rustc_middle:: ty:: { self , Adt , Ty , TypeVisitableExt } ;
15
16
use rustc_session:: lint:: builtin:: NON_EXHAUSTIVE_OMITTED_PATTERNS ;
@@ -629,12 +630,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
629
630
expected : Ty < ' tcx > ,
630
631
pat_info : PatInfo < ' tcx , ' _ > ,
631
632
) -> 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;
633
634
634
635
// Determine the binding mode...
635
636
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,
638
653
} ;
639
654
// ...and store it in a side table:
640
655
self . typeck_results . borrow_mut ( ) . pat_binding_modes_mut ( ) . insert ( pat. hir_id , bm) ;
@@ -743,7 +758,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
743
758
}
744
759
}
745
760
746
- // Precondition: pat is a Ref(_) pattern
761
+ /// Precondition: pat is a ` Ref(_)` pattern
747
762
fn borrow_pat_suggestion ( & self , err : & mut Diag < ' _ > , pat : & Pat < ' _ > ) {
748
763
let tcx = self . tcx ;
749
764
if let PatKind :: Ref ( inner, mutbl) = pat. kind
0 commit comments