@@ -25,7 +25,9 @@ use rustc_hir as hir;
2525use rustc_hir:: def_id:: LocalDefId ;
2626use rustc_index:: bit_set:: ChunkedBitSet ;
2727use rustc_index:: vec:: IndexVec ;
28- use rustc_infer:: infer:: { DefiningAnchor , InferCtxt , NllRegionVariableOrigin , TyCtxtInferExt } ;
28+ use rustc_infer:: infer:: {
29+ DefiningAnchor , InferCtxt , NllRegionVariableOrigin , RegionVariableOrigin , TyCtxtInferExt ,
30+ } ;
2931use rustc_middle:: mir:: {
3032 traversal, Body , ClearCrossCrate , Local , Location , Mutability , NonDivergingIntrinsic , Operand ,
3133 Place , PlaceElem , PlaceRef , VarDebugInfoContents ,
@@ -95,6 +97,7 @@ use nll::{PoloniusOutput, ToRegionVid};
9597use place_ext:: PlaceExt ;
9698use places_conflict:: { places_conflict, PlaceConflictBias } ;
9799use region_infer:: RegionInferenceContext ;
100+ use renumber:: RegionCtxt ;
98101
99102// FIXME(eddyb) perhaps move this somewhere more centrally.
100103#[ derive( Debug ) ]
@@ -168,10 +171,10 @@ fn do_mir_borrowck<'tcx>(
168171 return_body_with_facts : bool ,
169172) -> ( BorrowCheckResult < ' tcx > , Option < Box < BodyWithBorrowckFacts < ' tcx > > > ) {
170173 let def = input_body. source . with_opt_param ( ) . as_local ( ) . unwrap ( ) ;
171-
172174 debug ! ( ?def) ;
173175
174176 let tcx = infcx. tcx ;
177+ let infcx = BorrowckInferCtxt :: new ( infcx) ;
175178 let param_env = tcx. param_env ( def. did ) ;
176179
177180 let mut local_names = IndexVec :: from_elem ( None , & input_body. local_decls ) ;
@@ -219,7 +222,7 @@ fn do_mir_borrowck<'tcx>(
219222 let mut body_owned = input_body. clone ( ) ;
220223 let mut promoted = input_promoted. clone ( ) ;
221224 let free_regions =
222- nll:: replace_regions_in_mir ( infcx, param_env, & mut body_owned, & mut promoted) ;
225+ nll:: replace_regions_in_mir ( & infcx, param_env, & mut body_owned, & mut promoted) ;
223226 let body = & body_owned; // no further changes
224227
225228 let location_table_owned = LocationTable :: new ( body) ;
@@ -257,7 +260,7 @@ fn do_mir_borrowck<'tcx>(
257260 opt_closure_req,
258261 nll_errors,
259262 } = nll:: compute_regions (
260- infcx,
263+ & infcx,
261264 free_regions,
262265 body,
263266 & promoted,
@@ -272,12 +275,12 @@ fn do_mir_borrowck<'tcx>(
272275
273276 // Dump MIR results into a file, if that is enabled. This let us
274277 // write unit-tests, as well as helping with debugging.
275- nll:: dump_mir_results ( infcx, & body, & regioncx, & opt_closure_req) ;
278+ nll:: dump_mir_results ( & infcx, & body, & regioncx, & opt_closure_req) ;
276279
277280 // We also have a `#[rustc_regions]` annotation that causes us to dump
278281 // information.
279282 nll:: dump_annotation (
280- infcx,
283+ & infcx,
281284 & body,
282285 & regioncx,
283286 & opt_closure_req,
@@ -321,7 +324,7 @@ fn do_mir_borrowck<'tcx>(
321324
322325 if let Err ( ( move_data, move_errors) ) = move_data_results {
323326 let mut promoted_mbcx = MirBorrowckCtxt {
324- infcx,
327+ infcx : & infcx ,
325328 param_env,
326329 body : promoted_body,
327330 move_data : & move_data,
@@ -350,7 +353,7 @@ fn do_mir_borrowck<'tcx>(
350353 }
351354
352355 let mut mbcx = MirBorrowckCtxt {
353- infcx,
356+ infcx : & infcx ,
354357 param_env,
355358 body,
356359 move_data : & mdpe. move_data ,
@@ -482,22 +485,81 @@ pub struct BodyWithBorrowckFacts<'tcx> {
482485 pub location_table : LocationTable ,
483486}
484487
485- struct BorrowckInferCtxt < ' cx , ' tcx > {
488+ pub struct BorrowckInferCtxt < ' cx , ' tcx > {
486489 pub ( crate ) infcx : & ' cx InferCtxt < ' tcx > ,
487490
488491 #[ cfg( debug_assertions) ]
489- pub ( crate ) _reg_var_to_origin : RefCell < FxHashMap < ty:: Region < ' tcx > , NllRegionVariableOrigin > > ,
492+ pub ( crate ) reg_var_to_origin : RefCell < FxHashMap < ty:: RegionVid , RegionCtxt > > ,
490493}
491494
492495impl < ' cx , ' tcx > BorrowckInferCtxt < ' cx , ' tcx > {
493496 #[ cfg( not( debug_assertions) ) ]
494- pub ( crate ) fn _new ( infcx : & ' cx InferCtxt < ' tcx > ) -> Self {
497+ pub ( crate ) fn new ( infcx : & ' cx InferCtxt < ' tcx > ) -> Self {
495498 BorrowckInferCtxt { infcx }
496499 }
497500
498501 #[ cfg( debug_assertions) ]
499- pub ( crate ) fn _new ( infcx : & ' cx InferCtxt < ' tcx > ) -> Self {
500- BorrowckInferCtxt { infcx, _reg_var_to_origin : RefCell :: new ( Default :: default ( ) ) }
502+ pub ( crate ) fn new ( infcx : & ' cx InferCtxt < ' tcx > ) -> Self {
503+ BorrowckInferCtxt { infcx, reg_var_to_origin : RefCell :: new ( Default :: default ( ) ) }
504+ }
505+
506+ #[ cfg( not( debug_assertions) ) ]
507+ pub ( crate ) fn next_region_var ( & self , origin : RegionVariableOrigin ) -> ty:: Region < ' tcx > {
508+ self . infcx . next_region_var ( origin)
509+ }
510+
511+ #[ cfg( debug_assertions) ]
512+ pub ( crate ) fn next_region_var (
513+ & self ,
514+ origin : RegionVariableOrigin ,
515+ ctxt : RegionCtxt ,
516+ ) -> ty:: Region < ' tcx > {
517+ let next_region = self . infcx . next_region_var ( origin) ;
518+ let vid = next_region
519+ . try_get_var ( )
520+ . unwrap_or_else ( || bug ! ( "expected RegionKind::RegionVar on {:?}" , next_region) ) ;
521+
522+ debug ! ( "inserting vid {:?} with origin {:?} into var_to_origin" , vid, origin) ;
523+ let mut var_to_origin = self . reg_var_to_origin . borrow_mut ( ) ;
524+ let prev = var_to_origin. insert ( vid, ctxt) ;
525+ debug ! ( "var_to_origin after insertion: {:?}" , var_to_origin) ;
526+
527+ // This only makes sense if not called in a canonicalization context. If this
528+ // ever changes we either want to get rid of `BorrowckInferContext::reg_var_to_origin`
529+ // or modify how we track nll region vars for that map.
530+ assert ! ( matches!( prev, None ) ) ;
531+
532+ next_region
533+ }
534+
535+ #[ cfg( not( debug_assertions) ) ]
536+ pub ( crate ) fn next_nll_region_var ( & self , origin : NllRegionVariableOrigin ) -> ty:: Region < ' tcx > {
537+ self . infcx . next_nll_region_var ( origin)
538+ }
539+
540+ #[ cfg( debug_assertions) ]
541+ #[ instrument( skip( self ) , level = "debug" ) ]
542+ pub ( crate ) fn next_nll_region_var (
543+ & self ,
544+ origin : NllRegionVariableOrigin ,
545+ ctxt : RegionCtxt ,
546+ ) -> ty:: Region < ' tcx > {
547+ let next_region = self . infcx . next_nll_region_var ( origin. clone ( ) ) ;
548+ let vid = next_region
549+ . try_get_var ( )
550+ . unwrap_or_else ( || bug ! ( "expected RegionKind::RegionVar on {:?}" , next_region) ) ;
551+
552+ debug ! ( "inserting vid {:?} with origin {:?} into var_to_origin" , vid, origin) ;
553+ let mut var_to_origin = self . reg_var_to_origin . borrow_mut ( ) ;
554+ let prev = var_to_origin. insert ( vid, ctxt) ;
555+ debug ! ( "var_to_origin after insertion: {:?}" , var_to_origin) ;
556+
557+ // This only makes sense if not called in a canonicalization context. If this
558+ // ever changes we either want to get rid of `BorrowckInferContext::reg_var_to_origin`
559+ // or modify how we track nll region vars for that map.
560+ assert ! ( matches!( prev, None ) ) ;
561+
562+ next_region
501563 }
502564}
503565
@@ -510,7 +572,7 @@ impl<'cx, 'tcx> Deref for BorrowckInferCtxt<'cx, 'tcx> {
510572}
511573
512574struct MirBorrowckCtxt < ' cx , ' tcx > {
513- infcx : & ' cx InferCtxt < ' tcx > ,
575+ infcx : & ' cx BorrowckInferCtxt < ' cx , ' tcx > ,
514576 param_env : ParamEnv < ' tcx > ,
515577 body : & ' cx Body < ' tcx > ,
516578 move_data : & ' cx MoveData < ' tcx > ,
0 commit comments