@@ -699,7 +699,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
699699
700700 // Determine the binding mode...
701701 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 => {
703703 if pat. span . at_least_rust_2024 ( )
704704 && ( self . tcx . features ( ) . ref_pat_eat_one_layer_2024 ( )
705705 || self . tcx . features ( ) . ref_pat_eat_one_layer_2024_structural ( ) )
@@ -721,18 +721,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
721721 pat_info. top_info . hir_id ,
722722 pat,
723723 ident. span ,
724+ def_br_mutbl,
724725 ) ;
725726 BindingMode ( ByRef :: No , Mutability :: Mut )
726727 }
727728 }
728729 BindingMode ( ByRef :: No , mutbl) => BindingMode ( def_br, mutbl) ,
729730 BindingMode ( ByRef :: Yes ( _) , _) => {
730- if matches ! ( def_br , ByRef :: Yes ( _ ) ) {
731+ if let ByRef :: Yes ( def_br_mutbl ) = def_br {
731732 // `ref`/`ref mut` overrides the binding mode on edition <= 2021
732733 self . add_rust_2024_migration_desugared_pat (
733734 pat_info. top_info . hir_id ,
734735 pat,
735736 ident. span ,
737+ def_br_mutbl,
736738 ) ;
737739 }
738740 user_bind_annot
@@ -2261,12 +2263,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22612263 }
22622264 } else {
22632265 // 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 {
22652267 pat_info. binding_mode = ByRef :: No ;
22662268 self . add_rust_2024_migration_desugared_pat (
22672269 pat_info. top_info . hir_id ,
22682270 pat,
22692271 inner. span ,
2272+ inh_mut,
22702273 )
22712274 }
22722275 }
@@ -2634,6 +2637,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
26342637 pat_id : HirId ,
26352638 subpat : & ' tcx Pat < ' tcx > ,
26362639 cutoff_span : Span ,
2640+ def_br_mutbl : Mutability ,
26372641 ) {
26382642 // Try to trim the span we're labeling to just the `&` or binding mode that's an issue.
26392643 // 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> {
26562660 // NB: This wording assumes the only expansions that can produce problematic reference
26572661 // patterns and bindings are macros. If a desugaring or AST pass is added that can do
26582662 // 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 ( )
26602664 } else {
2661- if matches ! ( subpat. kind, PatKind :: Binding ( _, _, _, _) ) {
2665+ let pat_kind = if matches ! ( subpat. kind, PatKind :: Binding ( _, _, _, _) ) {
26622666 info. bad_modifiers |= true ;
2663- "this binding modifier"
2667+ "binding modifier"
26642668 } else {
26652669 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" )
26682677 } ;
2669- info. primary_labels . push ( ( trimmed_span, primary_label. to_owned ( ) ) ) ;
2678+ info. primary_labels . push ( ( trimmed_span, primary_label) ) ;
26702679 }
26712680}
0 commit comments