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