@@ -699,7 +699,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
699
699
700
700
// Determine the binding mode...
701
701
let bm = match user_bind_annot {
702
- BindingMode ( ByRef :: No , Mutability :: Mut ) if matches ! ( def_br , ByRef :: Yes ( _ ) ) => {
702
+ BindingMode ( ByRef :: No , Mutability :: Mut ) if let ByRef :: Yes ( def_br_mutbl ) = def_br => {
703
703
if pat. span . at_least_rust_2024 ( )
704
704
&& ( self . tcx . features ( ) . ref_pat_eat_one_layer_2024 ( )
705
705
|| self . tcx . features ( ) . ref_pat_eat_one_layer_2024_structural ( ) )
@@ -721,18 +721,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
721
721
pat_info. top_info . hir_id ,
722
722
pat,
723
723
ident. span ,
724
+ def_br_mutbl,
724
725
) ;
725
726
BindingMode ( ByRef :: No , Mutability :: Mut )
726
727
}
727
728
}
728
729
BindingMode ( ByRef :: No , mutbl) => BindingMode ( def_br, mutbl) ,
729
730
BindingMode ( ByRef :: Yes ( _) , _) => {
730
- if matches ! ( def_br , ByRef :: Yes ( _ ) ) {
731
+ if let ByRef :: Yes ( def_br_mutbl ) = def_br {
731
732
// `ref`/`ref mut` overrides the binding mode on edition <= 2021
732
733
self . add_rust_2024_migration_desugared_pat (
733
734
pat_info. top_info . hir_id ,
734
735
pat,
735
736
ident. span ,
737
+ def_br_mutbl,
736
738
) ;
737
739
}
738
740
user_bind_annot
@@ -2261,12 +2263,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2261
2263
}
2262
2264
} else {
2263
2265
// Reset binding mode on old editions
2264
- if pat_info . binding_mode != ByRef :: No {
2266
+ if let ByRef :: Yes ( inh_mut ) = pat_info . binding_mode {
2265
2267
pat_info. binding_mode = ByRef :: No ;
2266
2268
self . add_rust_2024_migration_desugared_pat (
2267
2269
pat_info. top_info . hir_id ,
2268
2270
pat,
2269
2271
inner. span ,
2272
+ inh_mut,
2270
2273
)
2271
2274
}
2272
2275
}
@@ -2634,6 +2637,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2634
2637
pat_id : HirId ,
2635
2638
subpat : & ' tcx Pat < ' tcx > ,
2636
2639
cutoff_span : Span ,
2640
+ def_br_mutbl : Mutability ,
2637
2641
) {
2638
2642
// Try to trim the span we're labeling to just the `&` or binding mode that's an issue.
2639
2643
// If the subpattern's span is is from an expansion, the emitted label will not be trimmed.
@@ -2656,16 +2660,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2656
2660
// NB: This wording assumes the only expansions that can produce problematic reference
2657
2661
// patterns and bindings are macros. If a desugaring or AST pass is added that can do
2658
2662
// so, we may want to inspect the span's source callee or macro backtrace.
2659
- "occurs within macro expansion"
2663
+ "occurs within macro expansion" . to_owned ( )
2660
2664
} else {
2661
- if matches ! ( subpat. kind, PatKind :: Binding ( _, _, _, _) ) {
2665
+ let pat_kind = if matches ! ( subpat. kind, PatKind :: Binding ( _, _, _, _) ) {
2662
2666
info. bad_modifiers |= true ;
2663
- "this binding modifier"
2667
+ "binding modifier"
2664
2668
} else {
2665
2669
info. bad_ref_pats |= true ;
2666
- "this reference pattern"
2667
- }
2670
+ "reference pattern"
2671
+ } ;
2672
+ let dbm_str = match def_br_mutbl {
2673
+ Mutability :: Not => "ref" ,
2674
+ Mutability :: Mut => "ref mut" ,
2675
+ } ;
2676
+ format ! ( "{pat_kind} not allowed under `{dbm_str}` default binding mode" )
2668
2677
} ;
2669
- info. primary_labels . push ( ( trimmed_span, primary_label. to_owned ( ) ) ) ;
2678
+ info. primary_labels . push ( ( trimmed_span, primary_label) ) ;
2670
2679
}
2671
2680
}
0 commit comments