1 #![deny(rustc::untranslatable_diagnostic)]
2 #![deny(rustc::diagnostic_outside_of_impl)]
3 use rustc_index::vec::IndexVec;
4 use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin};
5 use rustc_middle::mir::visit::{MutVisitor, TyContext};
6 use rustc_middle::mir::Constant;
7 use rustc_middle::mir::{Body, Location, Promoted};
8 use rustc_middle::ty::subst::SubstsRef;
9 use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable};
11 /// Replaces all free regions appearing in the MIR with fresh
12 /// inference variables, returning the number of variables created.
13 #[instrument(skip(infcx, body, promoted), level = "debug")]
14 pub fn renumber_mir<'tcx>(
15 infcx: &InferCtxt<'tcx>,
16 body: &mut Body<'tcx>,
17 promoted: &mut IndexVec<Promoted, Body<'tcx>>,
19 debug!(?body.arg_count);
21 let mut visitor = NllVisitor { infcx };
23 for body in promoted.iter_mut() {
24 visitor.visit_body(body);
27 visitor.visit_body(body);
30 /// Replaces all regions appearing in `value` with fresh inference
32 #[instrument(skip(infcx), level = "debug")]
33 pub fn renumber_regions<'tcx, T>(infcx: &InferCtxt<'tcx>, value: T) -> T
35 T: TypeFoldable<'tcx>,
37 infcx.tcx.fold_regions(value, |_region, _depth| {
38 let origin = NllRegionVariableOrigin::Existential { from_forall: false };
39 infcx.next_nll_region_var(origin)
43 struct NllVisitor<'a, 'tcx> {
44 infcx: &'a InferCtxt<'tcx>,
47 impl<'a, 'tcx> NllVisitor<'a, 'tcx> {
48 fn renumber_regions<T>(&mut self, value: T) -> T
50 T: TypeFoldable<'tcx>,
52 renumber_regions(self.infcx, value)
56 impl<'a, 'tcx> MutVisitor<'tcx> for NllVisitor<'a, 'tcx> {
57 fn tcx(&self) -> TyCtxt<'tcx> {
61 #[instrument(skip(self), level = "debug")]
62 fn visit_ty(&mut self, ty: &mut Ty<'tcx>, ty_context: TyContext) {
63 *ty = self.renumber_regions(*ty);
68 #[instrument(skip(self), level = "debug")]
69 fn visit_substs(&mut self, substs: &mut SubstsRef<'tcx>, location: Location) {
70 *substs = self.renumber_regions(*substs);
75 #[instrument(skip(self), level = "debug")]
76 fn visit_region(&mut self, region: &mut ty::Region<'tcx>, location: Location) {
77 let old_region = *region;
78 *region = self.renumber_regions(old_region);
83 #[instrument(skip(self), level = "debug")]
84 fn visit_constant(&mut self, constant: &mut Constant<'tcx>, _location: Location) {
85 let literal = constant.literal;
86 constant.literal = self.renumber_regions(literal);
87 debug!("constant: {:#?}", constant);