]> git.proxmox.com Git - rustc.git/blob - src/librustc_mir/borrow_check/nll/renumber.rs
New upstream version 1.40.0+dfsg1
[rustc.git] / src / librustc_mir / borrow_check / nll / renumber.rs
1 use rustc::ty::subst::SubstsRef;
2 use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
3 use rustc::mir::{Body, Location, PlaceElem, Promoted};
4 use rustc::mir::visit::{MutVisitor, TyContext};
5 use rustc::infer::{InferCtxt, NLLRegionVariableOrigin};
6 use rustc_index::vec::IndexVec;
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 Body<'tcx>,
13 promoted: &mut IndexVec<Promoted, Body<'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
36 .tcx
37 .fold_regions(value, &mut false, |_region, _depth| {
38 let origin = NLLRegionVariableOrigin::Existential { from_forall: false };
39 infcx.next_nll_region_var(origin)
40 })
41 }
42
43 struct NLLVisitor<'a, 'tcx> {
44 infcx: &'a InferCtxt<'a, 'tcx>,
45 }
46
47 impl<'a, 'tcx> NLLVisitor<'a, 'tcx> {
48 fn renumber_regions<T>(&mut self, value: &T) -> T
49 where
50 T: TypeFoldable<'tcx>,
51 {
52 renumber_regions(self.infcx, value)
53 }
54 }
55
56 impl<'a, 'tcx> MutVisitor<'tcx> for NLLVisitor<'a, 'tcx> {
57 fn tcx(&self) -> TyCtxt<'tcx> {
58 self.infcx.tcx
59 }
60
61 fn visit_ty(&mut self, ty: &mut Ty<'tcx>, ty_context: TyContext) {
62 debug!("visit_ty(ty={:?}, ty_context={:?})", ty, ty_context);
63
64 *ty = self.renumber_regions(ty);
65
66 debug!("visit_ty: ty={:?}", ty);
67 }
68
69 fn process_projection_elem(
70 &mut self,
71 elem: &PlaceElem<'tcx>,
72 ) -> Option<PlaceElem<'tcx>> {
73 if let PlaceElem::Field(field, ty) = elem {
74 let new_ty = self.renumber_regions(ty);
75
76 if new_ty != *ty {
77 return Some(PlaceElem::Field(*field, new_ty));
78 }
79 }
80
81 None
82 }
83
84 fn visit_substs(&mut self, substs: &mut SubstsRef<'tcx>, location: Location) {
85 debug!("visit_substs(substs={:?}, location={:?})", substs, location);
86
87 *substs = self.renumber_regions(&{ *substs });
88
89 debug!("visit_substs: substs={:?}", substs);
90 }
91
92 fn visit_region(&mut self, region: &mut ty::Region<'tcx>, location: Location) {
93 debug!("visit_region(region={:?}, location={:?})", region, location);
94
95 let old_region = *region;
96 *region = self.renumber_regions(&old_region);
97
98 debug!("visit_region: region={:?}", region);
99 }
100
101 fn visit_const(&mut self, constant: &mut &'tcx ty::Const<'tcx>, _location: Location) {
102 *constant = self.renumber_regions(&*constant);
103 }
104 }