@@ -467,15 +467,13 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
467
467
self.infcx.tcx.local_parent(self.mir_def),
468
468
|r| {
469
469
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());
479
477
},
480
478
);
481
479
@@ -484,21 +482,17 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
484
482
self.infcx.num_region_vars()
485
483
};
486
484
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.
489
491
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);
502
496
let region_vid = {
503
497
let name = r.get_name_or_anon();
504
498
self.infcx.next_nll_region_var(FR, || RegionCtxt::LateBound(name))
@@ -507,7 +501,12 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
507
501
debug!(?region_vid);
508
502
indices.insert_late_bound_region(r, region_vid.as_var());
509
503
}
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
+ );
511
510
512
511
let (unnormalized_output_ty, mut unnormalized_input_tys) =
513
512
inputs_and_output.split_last().unwrap();
@@ -832,10 +831,9 @@ impl<'tcx> BorrowckInferCtxt<'tcx> {
832
831
#[instrument(level = "debug", skip(self, indices))]
833
832
fn replace_bound_regions_with_nll_infer_vars<T>(
834
833
&self,
835
- origin: NllRegionVariableOrigin,
836
834
all_outlive_scope: LocalDefId,
837
835
value: ty::Binder<'tcx, T>,
838
- indices: &mut UniversalRegionIndices<'tcx>,
836
+ indices: &UniversalRegionIndices<'tcx>,
839
837
) -> T
840
838
where
841
839
T: TypeFoldable<TyCtxt<'tcx>>,
@@ -845,18 +843,7 @@ impl<'tcx> BorrowckInferCtxt<'tcx> {
845
843
let kind = ty::LateParamRegionKind::from_bound(br.var, br.kind);
846
844
let liberated_region =
847
845
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))
860
847
});
861
848
value
862
849
}
@@ -870,7 +857,7 @@ impl<'tcx> UniversalRegionIndices<'tcx> {
870
857
/// well. These are used for error reporting.
871
858
fn insert_late_bound_region(&mut self, r: ty::Region<'tcx>, vid: ty::RegionVid) {
872
859
debug!("insert_late_bound_region({:?}, {:?})", r, vid);
873
- self.indices.insert(r, vid);
860
+ assert_eq!( self.indices.insert(r, vid), None );
874
861
}
875
862
876
863
/// Converts `r` into a local inference variable: `r` can either
0 commit comments