]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/borrow_check/nll/renumber.rs
Changes the type `mir::Mir` into `mir::Body`
[rust.git] / src / librustc_mir / borrow_check / nll / renumber.rs
1 use rustc::ty::subst::SubstsRef;
2 use rustc::ty::{self, ClosureSubsts, GeneratorSubsts, Ty, TypeFoldable};
3 use rustc::mir::{Location, Body};
4 use rustc::mir::visit::{MutVisitor, TyContext};
5 use rustc::infer::{InferCtxt, NLLRegionVariableOrigin};
6
7 /// Replaces all free regions appearing in the MIR with fresh
8 /// inference variables, returning the number of variables created.
9 pub fn renumber_mir<'tcx>(infcx: &InferCtxt<'_, '_, 'tcx>, mir: &mut Body<'tcx>) {
10     debug!("renumber_mir()");
11     debug!("renumber_mir: mir.arg_count={:?}", mir.arg_count);
12
13     let mut visitor = NLLVisitor { infcx };
14     visitor.visit_body(mir);
15 }
16
17 /// Replaces all regions appearing in `value` with fresh inference
18 /// variables.
19 pub fn renumber_regions<'tcx, T>(
20     infcx: &InferCtxt<'_, '_, 'tcx>,
21     value: &T,
22 ) -> T
23 where
24     T: TypeFoldable<'tcx>,
25 {
26     debug!("renumber_regions(value={:?})", value);
27
28     infcx
29         .tcx
30         .fold_regions(value, &mut false, |_region, _depth| {
31             let origin = NLLRegionVariableOrigin::Existential;
32             infcx.next_nll_region_var(origin)
33         })
34 }
35
36 struct NLLVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
37     infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
38 }
39
40 impl<'a, 'gcx, 'tcx> NLLVisitor<'a, 'gcx, 'tcx> {
41     fn renumber_regions<T>(&mut self, value: &T) -> T
42     where
43         T: TypeFoldable<'tcx>,
44     {
45         renumber_regions(self.infcx, value)
46     }
47 }
48
49 impl<'a, 'gcx, 'tcx> MutVisitor<'tcx> for NLLVisitor<'a, 'gcx, 'tcx> {
50     fn visit_body(&mut self, mir: &mut Body<'tcx>) {
51         for promoted in mir.promoted.iter_mut() {
52             self.visit_body(promoted);
53         }
54
55         self.super_body(mir);
56     }
57
58     fn visit_ty(&mut self, ty: &mut Ty<'tcx>, ty_context: TyContext) {
59         debug!("visit_ty(ty={:?}, ty_context={:?})", ty, ty_context);
60
61         *ty = self.renumber_regions(ty);
62
63         debug!("visit_ty: ty={:?}", ty);
64     }
65
66     fn visit_substs(&mut self, substs: &mut SubstsRef<'tcx>, location: Location) {
67         debug!("visit_substs(substs={:?}, location={:?})", substs, location);
68
69         *substs = self.renumber_regions(&{ *substs });
70
71         debug!("visit_substs: substs={:?}", substs);
72     }
73
74     fn visit_region(&mut self, region: &mut ty::Region<'tcx>, location: Location) {
75         debug!("visit_region(region={:?}, location={:?})", region, location);
76
77         let old_region = *region;
78         *region = self.renumber_regions(&old_region);
79
80         debug!("visit_region: region={:?}", region);
81     }
82
83     fn visit_const(&mut self, constant: &mut &'tcx ty::Const<'tcx>, _location: Location) {
84         *constant = self.renumber_regions(&*constant);
85     }
86
87     fn visit_generator_substs(&mut self,
88                               substs: &mut GeneratorSubsts<'tcx>,
89                               location: Location) {
90         debug!(
91             "visit_generator_substs(substs={:?}, location={:?})",
92             substs,
93             location,
94         );
95
96         *substs = self.renumber_regions(substs);
97
98         debug!("visit_generator_substs: substs={:?}", substs);
99     }
100
101     fn visit_closure_substs(&mut self, substs: &mut ClosureSubsts<'tcx>, location: Location) {
102         debug!(
103             "visit_closure_substs(substs={:?}, location={:?})",
104             substs,
105             location
106         );
107
108         *substs = self.renumber_regions(substs);
109
110         debug!("visit_closure_substs: substs={:?}", substs);
111     }
112 }