@@ -155,34 +155,33 @@ enum AdjustMode {
155
155
#[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
156
156
enum MutblCap {
157
157
/// Mutability restricted to immutable.
158
+ Not ,
159
+
160
+ /// Mutability restricted to immutable, but only because of the pattern
161
+ /// (not the scrutinee type).
158
162
///
159
163
/// The contained span, if present, points to an `&` pattern
160
164
/// that is the reason for the restriction,
161
165
/// and which will be reported in a diagnostic.
162
166
/// (Said diagnostic is shown only if
163
167
/// replacing the `&` pattern with `&mut` would allow the code to compile.)
164
- ///
165
- /// (Outer [`Option`] is for whether to show the diagnostic,
166
- /// inner [`Option`] is for whether we have a span we can report)
167
- Not ( Option < Option < Span > > ) ,
168
+ WeaklyNot ( Option < Span > ) ,
169
+
168
170
/// No restriction on mutability
169
171
Mut ,
170
172
}
171
173
172
174
impl MutblCap {
173
- fn cap_mutbl_to_not ( self , span : Option < Option < Span > > ) -> Self {
174
- if let Some ( s) = span
175
- && self != MutblCap :: Not ( None )
176
- {
177
- MutblCap :: Not ( Some ( s) )
178
- } else {
179
- MutblCap :: Not ( None )
175
+ fn cap_to_weakly_not ( self , span : Option < Span > ) -> Self {
176
+ match self {
177
+ MutblCap :: Not => MutblCap :: Not ,
178
+ _ => MutblCap :: WeaklyNot ( span) ,
180
179
}
181
180
}
182
181
183
182
fn as_mutbl ( self ) -> Mutability {
184
183
match self {
185
- MutblCap :: Not ( _) => Mutability :: Not ,
184
+ MutblCap :: Not | MutblCap :: WeaklyNot ( _) => Mutability :: Not ,
186
185
MutblCap :: Mut => Mutability :: Mut ,
187
186
}
188
187
}
@@ -357,7 +356,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
357
356
358
357
if pat. span . at_least_rust_2024 ( ) && self . tcx . features ( ) . ref_pat_eat_one_layer_2024 {
359
358
let max_ref_mutbl = if ref_pat_mutbl == Mutability :: Not {
360
- max_ref_mutbl. cap_mutbl_to_not ( Some ( ref_span) )
359
+ max_ref_mutbl. cap_to_weakly_not ( ref_span)
361
360
} else {
362
361
max_ref_mutbl
363
362
} ;
@@ -505,7 +504,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
505
504
if pat. span . at_least_rust_2024 ( ) && self . tcx . features ( ) . ref_pat_eat_one_layer_2024 {
506
505
def_br = def_br. cap_ref_mutability ( max_ref_mutability. as_mutbl ( ) ) ;
507
506
if def_br == ByRef :: Yes ( Mutability :: Not ) {
508
- max_ref_mutability = max_ref_mutability . cap_mutbl_to_not ( None ) ;
507
+ max_ref_mutability = MutblCap :: Not ;
509
508
}
510
509
}
511
510
@@ -752,7 +751,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
752
751
} ;
753
752
754
753
if bm. 0 == ByRef :: Yes ( Mutability :: Mut )
755
- && let MutblCap :: Not ( Some ( and_pat_span) ) = pat_info. max_ref_mutbl
754
+ && let MutblCap :: WeaklyNot ( and_pat_span) = pat_info. max_ref_mutbl
756
755
{
757
756
let mut err = struct_span_code_err ! (
758
757
self . tcx. dcx( ) ,
@@ -2215,10 +2214,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2215
2214
&& self . tcx . features ( ) . ref_pat_eat_one_layer_2024 )
2216
2215
|| self . tcx . features ( ) . ref_pat_everywhere )
2217
2216
{
2218
- PatInfo {
2219
- max_ref_mutbl : pat_info. max_ref_mutbl . cap_mutbl_to_not ( None ) ,
2220
- ..pat_info
2221
- }
2217
+ PatInfo { max_ref_mutbl : MutblCap :: Not , ..pat_info }
2222
2218
} else {
2223
2219
pat_info
2224
2220
} ;
0 commit comments