@@ -467,15 +467,13 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
467467 self . infcx . tcx . local_parent ( self . mir_def ) ,
468468 |r| {
469469 debug ! ( ?r) ;
470- if !indices. indices . contains_key ( & r) {
471- let region_vid = {
472- let name = r. get_name_or_anon ( ) ;
473- self . infcx . next_nll_region_var ( FR , || RegionCtxt :: LateBound ( name) )
474- } ;
475-
476- debug ! ( ?region_vid) ;
477- indices. insert_late_bound_region ( r, region_vid. as_var ( ) ) ;
478- }
470+ let region_vid = {
471+ let name = r. get_name_or_anon ( ) ;
472+ self . infcx . next_nll_region_var ( FR , || RegionCtxt :: LateBound ( name) )
473+ } ;
474+
475+ debug ! ( ?region_vid) ;
476+ indices. insert_late_bound_region ( r, region_vid. as_var ( ) ) ;
479477 } ,
480478 ) ;
481479
@@ -484,21 +482,17 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
484482 self . infcx . num_region_vars ( )
485483 } ;
486484
487- // "Liberate" the late-bound regions. These correspond to
488- // "local" free regions.
485+ // Converse of above, if this is a function/closure then the late-bound regions declared
486+ // on its signature are local.
487+ //
488+ // We manually loop over `bound_inputs_and_output` instead of using
489+ // `for_each_late_bound_region_in_item` as we may need to add the otherwise
490+ // implicit `ClosureEnv` region.
489491 let bound_inputs_and_output = self . compute_inputs_and_output ( & indices, defining_ty) ;
490-
491- let inputs_and_output = self . infcx . replace_bound_regions_with_nll_infer_vars (
492- FR ,
493- self . mir_def ,
494- bound_inputs_and_output,
495- & mut indices,
496- ) ;
497- // Converse of above, if this is a function/closure then the late-bound regions declared on its
498- // signature are local.
499- for_each_late_bound_region_in_item ( self . infcx . tcx , self . mir_def , |r| {
500- debug ! ( ?r) ;
501- if !indices. indices . contains_key ( & r) {
492+ for ( idx, bound_var) in bound_inputs_and_output. bound_vars ( ) . iter ( ) . enumerate ( ) {
493+ if let ty:: BoundVariableKind :: Region ( kind) = bound_var {
494+ let kind = ty:: LateParamRegionKind :: from_bound ( ty:: BoundVar :: from_usize ( idx) , kind) ;
495+ let r = ty:: Region :: new_late_param ( self . infcx . tcx , self . mir_def . to_def_id ( ) , kind) ;
502496 let region_vid = {
503497 let name = r. get_name_or_anon ( ) ;
504498 self . infcx . next_nll_region_var ( FR , || RegionCtxt :: LateBound ( name) )
@@ -507,7 +501,12 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
507501 debug ! ( ?region_vid) ;
508502 indices. insert_late_bound_region ( r, region_vid. as_var ( ) ) ;
509503 }
510- } ) ;
504+ }
505+ let inputs_and_output = self . infcx . replace_bound_regions_with_nll_infer_vars (
506+ self . mir_def ,
507+ bound_inputs_and_output,
508+ & indices,
509+ ) ;
511510
512511 let ( unnormalized_output_ty, mut unnormalized_input_tys) =
513512 inputs_and_output. split_last ( ) . unwrap ( ) ;
@@ -832,10 +831,9 @@ impl<'tcx> BorrowckInferCtxt<'tcx> {
832831 #[ instrument( level = "debug" , skip( self , indices) ) ]
833832 fn replace_bound_regions_with_nll_infer_vars < T > (
834833 & self ,
835- origin : NllRegionVariableOrigin ,
836834 all_outlive_scope : LocalDefId ,
837835 value : ty:: Binder < ' tcx , T > ,
838- indices : & mut UniversalRegionIndices < ' tcx > ,
836+ indices : & UniversalRegionIndices < ' tcx > ,
839837 ) -> T
840838 where
841839 T : TypeFoldable < TyCtxt < ' tcx > > ,
@@ -845,18 +843,7 @@ impl<'tcx> BorrowckInferCtxt<'tcx> {
845843 let kind = ty:: LateParamRegionKind :: from_bound ( br. var , br. kind ) ;
846844 let liberated_region =
847845 ty:: Region :: new_late_param ( self . tcx , all_outlive_scope. to_def_id ( ) , kind) ;
848- let region_vid = {
849- let name = match br. kind . get_name ( ) {
850- Some ( name) => name,
851- _ => sym:: anon,
852- } ;
853-
854- self . next_nll_region_var ( origin, || RegionCtxt :: Bound ( name) )
855- } ;
856-
857- indices. insert_late_bound_region ( liberated_region, region_vid. as_var ( ) ) ;
858- debug ! ( ?liberated_region, ?region_vid) ;
859- region_vid
846+ ty:: Region :: new_var ( self . tcx , indices. to_region_vid ( liberated_region) )
860847 } ) ;
861848 value
862849 }
@@ -870,7 +857,7 @@ impl<'tcx> UniversalRegionIndices<'tcx> {
870857 /// well. These are used for error reporting.
871858 fn insert_late_bound_region ( & mut self , r : ty:: Region < ' tcx > , vid : ty:: RegionVid ) {
872859 debug ! ( "insert_late_bound_region({:?}, {:?})" , r, vid) ;
873- self . indices . insert ( r, vid) ;
860+ assert_eq ! ( self . indices. insert( r, vid) , None ) ;
874861 }
875862
876863 /// Converts `r` into a local inference variable: `r` can either
0 commit comments