]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_borrowck/src/renumber.rs
Auto merge of #101703 - nicholasbishop:bishop-add-uefi-ci-2, r=jyn514
[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::Constant;
5 use rustc_middle::mir::{Body, Location, Promoted};
6 use rustc_middle::ty::subst::SubstsRef;
7 use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable};
8
9 /// Replaces all free regions appearing in the MIR with fresh
10 /// inference variables, returning the number of variables created.
11 #[instrument(skip(infcx, body, promoted), level = "debug")]
12 pub fn renumber_mir<'tcx>(
13     infcx: &InferCtxt<'tcx>,
14     body: &mut Body<'tcx>,
15     promoted: &mut IndexVec<Promoted, Body<'tcx>>,
16 ) {
17     debug!(?body.arg_count);
18
19     let mut visitor = NllVisitor { infcx };
20
21     for body in promoted.iter_mut() {
22         visitor.visit_body(body);
23     }
24
25     visitor.visit_body(body);
26 }
27
28 /// Replaces all regions appearing in `value` with fresh inference
29 /// variables.
30 #[instrument(skip(infcx), level = "debug")]
31 pub fn renumber_regions<'tcx, T>(infcx: &InferCtxt<'tcx>, value: T) -> T
32 where
33     T: TypeFoldable<'tcx>,
34 {
35     infcx.tcx.fold_regions(value, |_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<'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     #[instrument(skip(self), level = "debug")]
60     fn visit_ty(&mut self, ty: &mut Ty<'tcx>, ty_context: TyContext) {
61         *ty = self.renumber_regions(*ty);
62
63         debug!(?ty);
64     }
65
66     #[instrument(skip(self), level = "debug")]
67     fn visit_substs(&mut self, substs: &mut SubstsRef<'tcx>, location: Location) {
68         *substs = self.renumber_regions(*substs);
69
70         debug!(?substs);
71     }
72
73     #[instrument(skip(self), level = "debug")]
74     fn visit_region(&mut self, region: &mut ty::Region<'tcx>, location: Location) {
75         let old_region = *region;
76         *region = self.renumber_regions(old_region);
77
78         debug!(?region);
79     }
80
81     #[instrument(skip(self), level = "debug")]
82     fn visit_constant(&mut self, constant: &mut Constant<'tcx>, _location: Location) {
83         let literal = constant.literal;
84         constant.literal = self.renumber_regions(literal);
85         debug!("constant: {:#?}", constant);
86     }
87 }