@@ -23,6 +23,7 @@ use std::ops::Deref;
2323use rustc_abi:: FieldIdx ;
2424use rustc_data_structures:: fx:: { FxIndexMap , FxIndexSet } ;
2525use rustc_data_structures:: graph:: dominators:: Dominators ;
26+ use rustc_data_structures:: unord:: UnordMap ;
2627use rustc_errors:: Diag ;
2728use rustc_hir as hir;
2829use rustc_hir:: def_id:: LocalDefId ;
@@ -281,6 +282,7 @@ fn do_mir_borrowck<'tcx>(
281282 regioncx : & regioncx,
282283 used_mut : Default :: default ( ) ,
283284 used_mut_upvars : SmallVec :: new ( ) ,
285+ local_from_upvars : UnordMap :: default ( ) ,
284286 borrow_set : & borrow_set,
285287 upvars : & [ ] ,
286288 local_names : IndexVec :: from_elem ( None , & promoted_body. local_decls ) ,
@@ -306,6 +308,11 @@ fn do_mir_borrowck<'tcx>(
306308 }
307309 }
308310
311+ let mut local_from_upvars = UnordMap :: default ( ) ;
312+ for ( field, & local) in body. local_upvar_map . iter_enumerated ( ) {
313+ let Some ( local) = local else { continue } ;
314+ local_from_upvars. insert ( local, field) ;
315+ }
309316 let mut mbcx = MirBorrowckCtxt {
310317 infcx : & infcx,
311318 param_env,
@@ -321,6 +328,7 @@ fn do_mir_borrowck<'tcx>(
321328 regioncx : & regioncx,
322329 used_mut : Default :: default ( ) ,
323330 used_mut_upvars : SmallVec :: new ( ) ,
331+ local_from_upvars,
324332 borrow_set : & borrow_set,
325333 upvars : tcx. closure_captures ( def) ,
326334 local_names,
@@ -556,6 +564,9 @@ struct MirBorrowckCtxt<'a, 'infcx, 'tcx> {
556564 /// If the function we're checking is a closure, then we'll need to report back the list of
557565 /// mutable upvars that have been used. This field keeps track of them.
558566 used_mut_upvars : SmallVec < [ FieldIdx ; 8 ] > ,
567+ /// Since upvars are moved to real locals, we need to map mutations to the locals back to
568+ /// the upvars, so that used_mut_upvars is up-to-date.
569+ local_from_upvars : UnordMap < Local , FieldIdx > ,
559570 /// Region inference context. This contains the results from region inference and lets us e.g.
560571 /// find out which CFG points are contained in each borrow region.
561572 regioncx : & ' a RegionInferenceContext < ' tcx > ,
@@ -2214,10 +2225,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
22142225 // If the local may have been initialized, and it is now currently being
22152226 // mutated, then it is justified to be annotated with the `mut`
22162227 // keyword, since the mutation may be a possible reassignment.
2217- if is_local_mutation_allowed != LocalMutationIsAllowed :: Yes
2218- && self . is_local_ever_initialized ( local, state) . is_some ( )
2219- {
2220- self . used_mut . insert ( local) ;
2228+ if !matches ! ( is_local_mutation_allowed, LocalMutationIsAllowed :: Yes ) {
2229+ if self . is_local_ever_initialized ( local, state) . is_some ( ) {
2230+ self . used_mut . insert ( local) ;
2231+ } else if let Some ( & field) = self . local_from_upvars . get ( & local) {
2232+ self . used_mut_upvars . push ( field) ;
2233+ }
22212234 }
22222235 }
22232236 RootPlace {
@@ -2235,6 +2248,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
22352248 projection : place_projection,
22362249 } ) {
22372250 self . used_mut_upvars . push ( field) ;
2251+ } else if let Some ( & field) = self . local_from_upvars . get ( & place_local) {
2252+ self . used_mut_upvars . push ( field) ;
22382253 }
22392254 }
22402255 }
0 commit comments