]>
Commit | Line | Data |
---|---|---|
e74abb32 | 1 | use rustc_index::vec::IndexVec; |
5869c6ff | 2 | use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin}; |
ba9703b0 | 3 | use rustc_middle::mir::visit::{MutVisitor, TyContext}; |
a2a8927a | 4 | use rustc_middle::mir::{Body, Location, Promoted}; |
ba9703b0 XL |
5 | use rustc_middle::ty::subst::SubstsRef; |
6 | use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable}; | |
ff7c6d11 XL |
7 | |
8 | /// Replaces all free regions appearing in the MIR with fresh | |
9 | /// inference variables, returning the number of variables created. | |
c295e0f8 | 10 | #[instrument(skip(infcx, body, promoted), level = "debug")] |
e1599b0c XL |
11 | pub fn renumber_mir<'tcx>( |
12 | infcx: &InferCtxt<'_, 'tcx>, | |
f9f354fc XL |
13 | body: &mut Body<'tcx>, |
14 | promoted: &mut IndexVec<Promoted, Body<'tcx>>, | |
e1599b0c | 15 | ) { |
c295e0f8 | 16 | debug!(?body.arg_count); |
ff7c6d11 | 17 | |
5869c6ff | 18 | let mut visitor = NllVisitor { infcx }; |
e1599b0c XL |
19 | |
20 | for body in promoted.iter_mut() { | |
21 | visitor.visit_body(body); | |
22 | } | |
23 | ||
dc9dc135 | 24 | visitor.visit_body(body); |
ff7c6d11 XL |
25 | } |
26 | ||
27 | /// Replaces all regions appearing in `value` with fresh inference | |
28 | /// variables. | |
c295e0f8 | 29 | #[instrument(skip(infcx), level = "debug")] |
fc512014 | 30 | pub fn renumber_regions<'tcx, T>(infcx: &InferCtxt<'_, 'tcx>, value: T) -> T |
ff7c6d11 XL |
31 | where |
32 | T: TypeFoldable<'tcx>, | |
33 | { | |
064997fb | 34 | infcx.tcx.fold_regions(value, |_region, _depth| { |
5869c6ff | 35 | let origin = NllRegionVariableOrigin::Existential { from_forall: false }; |
dfeec247 XL |
36 | infcx.next_nll_region_var(origin) |
37 | }) | |
ff7c6d11 XL |
38 | } |
39 | ||
5869c6ff | 40 | struct NllVisitor<'a, 'tcx> { |
dc9dc135 | 41 | infcx: &'a InferCtxt<'a, 'tcx>, |
ff7c6d11 XL |
42 | } |
43 | ||
5869c6ff | 44 | impl<'a, 'tcx> NllVisitor<'a, 'tcx> { |
fc512014 | 45 | fn renumber_regions<T>(&mut self, value: T) -> T |
ff7c6d11 XL |
46 | where |
47 | T: TypeFoldable<'tcx>, | |
48 | { | |
8faf50e0 | 49 | renumber_regions(self.infcx, value) |
ff7c6d11 XL |
50 | } |
51 | } | |
52 | ||
5869c6ff | 53 | impl<'a, 'tcx> MutVisitor<'tcx> for NllVisitor<'a, 'tcx> { |
e74abb32 XL |
54 | fn tcx(&self) -> TyCtxt<'tcx> { |
55 | self.infcx.tcx | |
56 | } | |
57 | ||
c295e0f8 | 58 | #[instrument(skip(self), level = "debug")] |
ff7c6d11 | 59 | fn visit_ty(&mut self, ty: &mut Ty<'tcx>, ty_context: TyContext) { |
5099ac24 | 60 | *ty = self.renumber_regions(*ty); |
ff7c6d11 | 61 | |
c295e0f8 | 62 | debug!(?ty); |
ff7c6d11 XL |
63 | } |
64 | ||
c295e0f8 | 65 | #[instrument(skip(self), level = "debug")] |
532ac7d7 | 66 | fn visit_substs(&mut self, substs: &mut SubstsRef<'tcx>, location: Location) { |
fc512014 | 67 | *substs = self.renumber_regions(*substs); |
ff7c6d11 | 68 | |
c295e0f8 | 69 | debug!(?substs); |
ff7c6d11 XL |
70 | } |
71 | ||
c295e0f8 | 72 | #[instrument(skip(self), level = "debug")] |
ff7c6d11 | 73 | fn visit_region(&mut self, region: &mut ty::Region<'tcx>, location: Location) { |
ff7c6d11 | 74 | let old_region = *region; |
5099ac24 | 75 | *region = self.renumber_regions(old_region); |
ff7c6d11 | 76 | |
c295e0f8 | 77 | debug!(?region); |
ff7c6d11 XL |
78 | } |
79 | ||
5099ac24 FG |
80 | fn visit_const(&mut self, constant: &mut ty::Const<'tcx>, _location: Location) { |
81 | *constant = self.renumber_regions(*constant); | |
ff7c6d11 | 82 | } |
ff7c6d11 | 83 | } |