@@ -847,20 +847,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
847
847
self . add_rust_2024_migration_desugared_pat (
848
848
pat_info. top_info . hir_id ,
849
849
pat,
850
- ident . span ,
850
+ 't' , // last char of `mut`
851
851
def_br_mutbl,
852
852
) ;
853
853
BindingMode ( ByRef :: No , Mutability :: Mut )
854
854
}
855
855
}
856
856
BindingMode ( ByRef :: No , mutbl) => BindingMode ( def_br, mutbl) ,
857
- BindingMode ( ByRef :: Yes ( _ ) , _) => {
857
+ BindingMode ( ByRef :: Yes ( user_br_mutbl ) , _) => {
858
858
if let ByRef :: Yes ( def_br_mutbl) = def_br {
859
859
// `ref`/`ref mut` overrides the binding mode on edition <= 2021
860
860
self . add_rust_2024_migration_desugared_pat (
861
861
pat_info. top_info . hir_id ,
862
862
pat,
863
- ident. span ,
863
+ match user_br_mutbl {
864
+ Mutability :: Not => 'f' , // last char of `ref`
865
+ Mutability :: Mut => 't' , // last char of `ref mut`
866
+ } ,
864
867
def_br_mutbl,
865
868
) ;
866
869
}
@@ -2440,7 +2443,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2440
2443
self . add_rust_2024_migration_desugared_pat (
2441
2444
pat_info. top_info . hir_id ,
2442
2445
pat,
2443
- inner. span ,
2446
+ match pat_mutbl {
2447
+ Mutability :: Not => '&' , // last char of `&`
2448
+ Mutability :: Mut => 't' , // last char of `&mut`
2449
+ } ,
2444
2450
inh_mut,
2445
2451
)
2446
2452
}
@@ -2832,18 +2838,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2832
2838
& self ,
2833
2839
pat_id : HirId ,
2834
2840
subpat : & ' tcx Pat < ' tcx > ,
2835
- cutoff_span : Span ,
2841
+ final_char : char ,
2836
2842
def_br_mutbl : Mutability ,
2837
2843
) {
2838
2844
// Try to trim the span we're labeling to just the `&` or binding mode that's an issue.
2839
- // If the subpattern's span is is from an expansion, the emitted label will not be trimmed.
2840
- let source_map = self . tcx . sess . source_map ( ) ;
2841
- let cutoff_span = source_map
2842
- . span_extend_prev_while ( cutoff_span, |c| c. is_whitespace ( ) || c == '(' )
2843
- . unwrap_or ( cutoff_span) ;
2844
- // Ensure we use the syntax context and thus edition of `subpat.span`; this will be a hard
2845
- // error if the subpattern is of edition >= 2024.
2846
- let trimmed_span = subpat. span . until ( cutoff_span) . with_ctxt ( subpat. span . ctxt ( ) ) ;
2845
+ let from_expansion = subpat. span . from_expansion ( ) ;
2846
+ let trimmed_span = if from_expansion {
2847
+ // If the subpattern is from an expansion, highlight the whole macro call instead.
2848
+ subpat. span
2849
+ } else {
2850
+ let trimmed = self . tcx . sess . source_map ( ) . span_through_char ( subpat. span , final_char) ;
2851
+ // The edition of the trimmed span should be the same as `subpat.span`; this will be a
2852
+ // a hard error if the subpattern is of edition >= 2024. We set it manually to be sure:
2853
+ trimmed. with_ctxt ( subpat. span . ctxt ( ) )
2854
+ } ;
2847
2855
2848
2856
let mut typeck_results = self . typeck_results . borrow_mut ( ) ;
2849
2857
let mut table = typeck_results. rust_2024_migration_desugared_pats_mut ( ) ;
@@ -2877,7 +2885,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2877
2885
} ;
2878
2886
// Only provide a detailed label if the problematic subpattern isn't from an expansion.
2879
2887
// In the case that it's from a macro, we'll add a more detailed note in the emitter.
2880
- let from_expansion = subpat. span . from_expansion ( ) ;
2881
2888
let primary_label = if from_expansion {
2882
2889
// We can't suggest eliding modifiers within expansions.
2883
2890
info. suggest_eliding_modes = false ;
0 commit comments