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::Constant;
5 use rustc_middle::mir::{Body, Location, Promoted};
6 use rustc_middle::ty::subst::SubstsRef;
7 use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable};
9 /// Replaces all free regions appearing in the MIR with fresh
10 /// inference variables, returning the number of variables created.
11 #[instrument(skip(infcx, body, promoted), level = "debug")]
12 pub fn renumber_mir<'tcx>(
13 infcx: &InferCtxt<'tcx>,
14 body: &mut Body<'tcx>,
15 promoted: &mut IndexVec<Promoted, Body<'tcx>>,
17 debug!(?body.arg_count);
19 let mut visitor = NllVisitor { infcx };
21 for body in promoted.iter_mut() {
22 visitor.visit_body(body);
25 visitor.visit_body(body);
28 /// Replaces all regions appearing in `value` with fresh inference
30 #[instrument(skip(infcx), level = "debug")]
31 pub fn renumber_regions<'tcx, T>(infcx: &InferCtxt<'tcx>, value: T) -> T
33 T: TypeFoldable<'tcx>,
35 infcx.tcx.fold_regions(value, |_region, _depth| {
36 let origin = NllRegionVariableOrigin::Existential { from_forall: false };
37 infcx.next_nll_region_var(origin)
41 struct NllVisitor<'a, 'tcx> {
42 infcx: &'a InferCtxt<'tcx>,
45 impl<'a, 'tcx> NllVisitor<'a, 'tcx> {
46 fn renumber_regions<T>(&mut self, value: T) -> T
48 T: TypeFoldable<'tcx>,
50 renumber_regions(self.infcx, value)
54 impl<'a, 'tcx> MutVisitor<'tcx> for NllVisitor<'a, 'tcx> {
55 fn tcx(&self) -> TyCtxt<'tcx> {
59 #[instrument(skip(self), level = "debug")]
60 fn visit_ty(&mut self, ty: &mut Ty<'tcx>, ty_context: TyContext) {
61 *ty = self.renumber_regions(*ty);
66 #[instrument(skip(self), level = "debug")]
67 fn visit_substs(&mut self, substs: &mut SubstsRef<'tcx>, location: Location) {
68 *substs = self.renumber_regions(*substs);
73 #[instrument(skip(self), level = "debug")]
74 fn visit_region(&mut self, region: &mut ty::Region<'tcx>, location: Location) {
75 let old_region = *region;
76 *region = self.renumber_regions(old_region);
81 #[instrument(skip(self), level = "debug")]
82 fn visit_constant(&mut self, constant: &mut Constant<'tcx>, _location: Location) {
83 let literal = constant.literal;
84 constant.literal = self.renumber_regions(literal);
85 debug!("constant: {:#?}", constant);