]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/borrow_check/renumber.rs
Auto merge of #68551 - Marwes:allocations_mir, r=ecstatic-morse
[rust.git] / src / librustc_mir / borrow_check / renumber.rs
1 use rustc::mir::visit::{MutVisitor, TyContext};
2 use rustc::mir::{BodyAndCache, Location, PlaceElem, Promoted};
3 use rustc::ty::subst::SubstsRef;
4 use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
5 use rustc_index::vec::IndexVec;
6 use rustc_infer::infer::{InferCtxt, NLLRegionVariableOrigin};
7
8 /// Replaces all free regions appearing in the MIR with fresh
9 /// inference variables, returning the number of variables created.
10 pub fn renumber_mir<'tcx>(
11     infcx: &InferCtxt<'_, 'tcx>,
12     body: &mut BodyAndCache<'tcx>,
13     promoted: &mut IndexVec<Promoted, BodyAndCache<'tcx>>,
14 ) {
15     debug!("renumber_mir()");
16     debug!("renumber_mir: body.arg_count={:?}", body.arg_count);
17
18     let mut visitor = NLLVisitor { infcx };
19
20     for body in promoted.iter_mut() {
21         visitor.visit_body(body);
22     }
23
24     visitor.visit_body(body);
25 }
26
27 /// Replaces all regions appearing in `value` with fresh inference
28 /// variables.
29 pub fn renumber_regions<'tcx, T>(infcx: &InferCtxt<'_, 'tcx>, value: &T) -> T
30 where
31     T: TypeFoldable<'tcx>,
32 {
33     debug!("renumber_regions(value={:?})", value);
34
35     infcx.tcx.fold_regions(value, &mut false, |_region, _depth| {
36         let origin = NLLRegionVariableOrigin::Existential { from_forall: false };
37         infcx.next_nll_region_var(origin)
38     })
39 }
40
41 struct NLLVisitor<'a, 'tcx> {
42     infcx: &'a InferCtxt<'a, 'tcx>,
43 }
44
45 impl<'a, 'tcx> NLLVisitor<'a, 'tcx> {
46     fn renumber_regions<T>(&mut self, value: &T) -> T
47     where
48         T: TypeFoldable<'tcx>,
49     {
50         renumber_regions(self.infcx, value)
51     }
52 }
53
54 impl<'a, 'tcx> MutVisitor<'tcx> for NLLVisitor<'a, 'tcx> {
55     fn tcx(&self) -> TyCtxt<'tcx> {
56         self.infcx.tcx
57     }
58
59     fn visit_ty(&mut self, ty: &mut Ty<'tcx>, ty_context: TyContext) {
60         debug!("visit_ty(ty={:?}, ty_context={:?})", ty, ty_context);
61
62         *ty = self.renumber_regions(ty);
63
64         debug!("visit_ty: ty={:?}", ty);
65     }
66
67     fn process_projection_elem(&mut self, elem: &PlaceElem<'tcx>) -> Option<PlaceElem<'tcx>> {
68         if let PlaceElem::Field(field, ty) = elem {
69             let new_ty = self.renumber_regions(ty);
70
71             if new_ty != *ty {
72                 return Some(PlaceElem::Field(*field, new_ty));
73             }
74         }
75
76         None
77     }
78
79     fn visit_substs(&mut self, substs: &mut SubstsRef<'tcx>, location: Location) {
80         debug!("visit_substs(substs={:?}, location={:?})", substs, location);
81
82         *substs = self.renumber_regions(&{ *substs });
83
84         debug!("visit_substs: substs={:?}", substs);
85     }
86
87     fn visit_region(&mut self, region: &mut ty::Region<'tcx>, location: Location) {
88         debug!("visit_region(region={:?}, location={:?})", region, location);
89
90         let old_region = *region;
91         *region = self.renumber_regions(&old_region);
92
93         debug!("visit_region: region={:?}", region);
94     }
95
96     fn visit_const(&mut self, constant: &mut &'tcx ty::Const<'tcx>, _location: Location) {
97         *constant = self.renumber_regions(&*constant);
98     }
99 }