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