]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_borrowck/src/renumber.rs
fold_region: remove unused parameter
[rust.git] / compiler / rustc_borrowck / src / renumber.rs
1 use rustc_index::vec::IndexVec;
2 use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin};
3 use rustc_middle::mir::visit::{MutVisitor, TyContext};
4 use rustc_middle::mir::{Body, Location, Promoted};
5 use rustc_middle::ty::subst::SubstsRef;
6 use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable};
7
8 /// Replaces all free regions appearing in the MIR with fresh
9 /// inference variables, returning the number of variables created.
10 #[instrument(skip(infcx, body, promoted), level = "debug")]
11 pub fn renumber_mir<'tcx>(
12     infcx: &InferCtxt<'_, 'tcx>,
13     body: &mut Body<'tcx>,
14     promoted: &mut IndexVec<Promoted, Body<'tcx>>,
15 ) {
16     debug!(?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 #[instrument(skip(infcx), level = "debug")]
30 pub fn renumber_regions<'tcx, T>(infcx: &InferCtxt<'_, 'tcx>, value: T) -> T
31 where
32     T: TypeFoldable<'tcx>,
33 {
34     infcx.tcx.fold_regions(value, |_region, _depth| {
35         let origin = NllRegionVariableOrigin::Existential { from_forall: false };
36         infcx.next_nll_region_var(origin)
37     })
38 }
39
40 struct NllVisitor<'a, 'tcx> {
41     infcx: &'a InferCtxt<'a, 'tcx>,
42 }
43
44 impl<'a, 'tcx> NllVisitor<'a, 'tcx> {
45     fn renumber_regions<T>(&mut self, value: T) -> T
46     where
47         T: TypeFoldable<'tcx>,
48     {
49         renumber_regions(self.infcx, value)
50     }
51 }
52
53 impl<'a, 'tcx> MutVisitor<'tcx> for NllVisitor<'a, 'tcx> {
54     fn tcx(&self) -> TyCtxt<'tcx> {
55         self.infcx.tcx
56     }
57
58     #[instrument(skip(self), level = "debug")]
59     fn visit_ty(&mut self, ty: &mut Ty<'tcx>, ty_context: TyContext) {
60         *ty = self.renumber_regions(*ty);
61
62         debug!(?ty);
63     }
64
65     #[instrument(skip(self), level = "debug")]
66     fn visit_substs(&mut self, substs: &mut SubstsRef<'tcx>, location: Location) {
67         *substs = self.renumber_regions(*substs);
68
69         debug!(?substs);
70     }
71
72     #[instrument(skip(self), level = "debug")]
73     fn visit_region(&mut self, region: &mut ty::Region<'tcx>, location: Location) {
74         let old_region = *region;
75         *region = self.renumber_regions(old_region);
76
77         debug!(?region);
78     }
79
80     fn visit_const(&mut self, constant: &mut ty::Const<'tcx>, _location: Location) {
81         *constant = self.renumber_regions(*constant);
82     }
83 }