1 use rustc_index::vec::IndexVec;
2 use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin};
3 use rustc_middle::mir::visit::{MutVisitor, TyContext};
4 use rustc_middle::mir::{Body, Location, Promoted};
5 use rustc_middle::ty::subst::SubstsRef;
6 use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable};
8 /// Replaces all free regions appearing in the MIR with fresh
9 /// inference variables, returning the number of variables created.
10 #[instrument(skip(infcx, body, promoted), level = "debug")]
11 pub fn renumber_mir<'tcx>(
12 infcx: &InferCtxt<'_, 'tcx>,
13 body: &mut Body<'tcx>,
14 promoted: &mut IndexVec<Promoted, Body<'tcx>>,
16 debug!(?body.arg_count);
18 let mut visitor = NllVisitor { infcx };
20 for body in promoted.iter_mut() {
21 visitor.visit_body(body);
24 visitor.visit_body(body);
27 /// Replaces all regions appearing in `value` with fresh inference
29 #[instrument(skip(infcx), level = "debug")]
30 pub fn renumber_regions<'tcx, T>(infcx: &InferCtxt<'_, 'tcx>, value: T) -> T
32 T: TypeFoldable<'tcx>,
34 infcx.tcx.fold_regions(value, |_region, _depth| {
35 let origin = NllRegionVariableOrigin::Existential { from_forall: false };
36 infcx.next_nll_region_var(origin)
40 struct NllVisitor<'a, 'tcx> {
41 infcx: &'a InferCtxt<'a, 'tcx>,
44 impl<'a, 'tcx> NllVisitor<'a, 'tcx> {
45 fn renumber_regions<T>(&mut self, value: T) -> T
47 T: TypeFoldable<'tcx>,
49 renumber_regions(self.infcx, value)
53 impl<'a, 'tcx> MutVisitor<'tcx> for NllVisitor<'a, 'tcx> {
54 fn tcx(&self) -> TyCtxt<'tcx> {
58 #[instrument(skip(self), level = "debug")]
59 fn visit_ty(&mut self, ty: &mut Ty<'tcx>, ty_context: TyContext) {
60 *ty = self.renumber_regions(*ty);
65 #[instrument(skip(self), level = "debug")]
66 fn visit_substs(&mut self, substs: &mut SubstsRef<'tcx>, location: Location) {
67 *substs = self.renumber_regions(*substs);
72 #[instrument(skip(self), level = "debug")]
73 fn visit_region(&mut self, region: &mut ty::Region<'tcx>, location: Location) {
74 let old_region = *region;
75 *region = self.renumber_regions(old_region);
80 fn visit_const(&mut self, constant: &mut ty::Const<'tcx>, _location: Location) {
81 *constant = self.renumber_regions(*constant);