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