@@ -24,6 +24,7 @@ use std::ops::{ControlFlow, Deref};
24
24
use rustc_abi:: FieldIdx ;
25
25
use rustc_data_structures:: fx:: { FxIndexMap , FxIndexSet } ;
26
26
use rustc_data_structures:: graph:: dominators:: Dominators ;
27
+ use rustc_data_structures:: unord:: UnordMap ;
27
28
use rustc_errors:: LintDiagnostic ;
28
29
use rustc_hir as hir;
29
30
use rustc_hir:: CRATE_HIR_ID ;
@@ -261,6 +262,7 @@ fn do_mir_borrowck<'tcx>(
261
262
regioncx : & regioncx,
262
263
used_mut : Default :: default ( ) ,
263
264
used_mut_upvars : SmallVec :: new ( ) ,
265
+ local_from_upvars : UnordMap :: default ( ) ,
264
266
borrow_set : & borrow_set,
265
267
upvars : & [ ] ,
266
268
local_names : IndexVec :: from_elem ( None , & promoted_body. local_decls ) ,
@@ -286,6 +288,11 @@ fn do_mir_borrowck<'tcx>(
286
288
}
287
289
}
288
290
291
+ let mut local_from_upvars = UnordMap :: default ( ) ;
292
+ for ( field, & local) in body. local_upvar_map . iter_enumerated ( ) {
293
+ let Some ( local) = local else { continue } ;
294
+ local_from_upvars. insert ( local, field) ;
295
+ }
289
296
let mut mbcx = MirBorrowckCtxt {
290
297
infcx : & infcx,
291
298
body,
@@ -300,6 +307,7 @@ fn do_mir_borrowck<'tcx>(
300
307
regioncx : & regioncx,
301
308
used_mut : Default :: default ( ) ,
302
309
used_mut_upvars : SmallVec :: new ( ) ,
310
+ local_from_upvars,
303
311
borrow_set : & borrow_set,
304
312
upvars : tcx. closure_captures ( def) ,
305
313
local_names,
@@ -554,6 +562,9 @@ struct MirBorrowckCtxt<'a, 'infcx, 'tcx> {
554
562
/// If the function we're checking is a closure, then we'll need to report back the list of
555
563
/// mutable upvars that have been used. This field keeps track of them.
556
564
used_mut_upvars : SmallVec < [ FieldIdx ; 8 ] > ,
565
+ /// Since upvars are moved to real locals, we need to map mutations to the locals back to
566
+ /// the upvars, so that used_mut_upvars is up-to-date.
567
+ local_from_upvars : UnordMap < Local , FieldIdx > ,
557
568
/// Region inference context. This contains the results from region inference and lets us e.g.
558
569
/// find out which CFG points are contained in each borrow region.
559
570
regioncx : & ' a RegionInferenceContext < ' tcx > ,
@@ -2261,10 +2272,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
2261
2272
// If the local may have been initialized, and it is now currently being
2262
2273
// mutated, then it is justified to be annotated with the `mut`
2263
2274
// keyword, since the mutation may be a possible reassignment.
2264
- if is_local_mutation_allowed != LocalMutationIsAllowed :: Yes
2265
- && self . is_local_ever_initialized ( local, state) . is_some ( )
2266
- {
2267
- self . used_mut . insert ( local) ;
2275
+ if !matches ! ( is_local_mutation_allowed, LocalMutationIsAllowed :: Yes ) {
2276
+ if self . is_local_ever_initialized ( local, state) . is_some ( ) {
2277
+ self . used_mut . insert ( local) ;
2278
+ } else if let Some ( & field) = self . local_from_upvars . get ( & local) {
2279
+ self . used_mut_upvars . push ( field) ;
2280
+ }
2268
2281
}
2269
2282
}
2270
2283
RootPlace {
@@ -2282,6 +2295,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
2282
2295
projection : place_projection,
2283
2296
} ) {
2284
2297
self . used_mut_upvars . push ( field) ;
2298
+ } else if let Some ( & field) = self . local_from_upvars . get ( & place_local) {
2299
+ self . used_mut_upvars . push ( field) ;
2285
2300
}
2286
2301
}
2287
2302
}
0 commit comments