]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_borrowck/src/renumber.rs
New upstream version 1.59.0+dfsg1
[rustc.git] / compiler / rustc_borrowck / src / renumber.rs
CommitLineData
e74abb32 1use rustc_index::vec::IndexVec;
5869c6ff 2use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin};
ba9703b0 3use rustc_middle::mir::visit::{MutVisitor, TyContext};
a2a8927a 4use rustc_middle::mir::{Body, Location, Promoted};
ba9703b0
XL
5use rustc_middle::ty::subst::SubstsRef;
6use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable};
ff7c6d11
XL
7
8/// Replaces all free regions appearing in the MIR with fresh
9/// inference variables, returning the number of variables created.
c295e0f8 10#[instrument(skip(infcx, body, promoted), level = "debug")]
e1599b0c
XL
11pub fn renumber_mir<'tcx>(
12 infcx: &InferCtxt<'_, 'tcx>,
f9f354fc
XL
13 body: &mut Body<'tcx>,
14 promoted: &mut IndexVec<Promoted, Body<'tcx>>,
e1599b0c 15) {
c295e0f8 16 debug!(?body.arg_count);
ff7c6d11 17
5869c6ff 18 let mut visitor = NllVisitor { infcx };
e1599b0c
XL
19
20 for body in promoted.iter_mut() {
21 visitor.visit_body(body);
22 }
23
dc9dc135 24 visitor.visit_body(body);
ff7c6d11
XL
25}
26
27/// Replaces all regions appearing in `value` with fresh inference
28/// variables.
c295e0f8 29#[instrument(skip(infcx), level = "debug")]
fc512014 30pub fn renumber_regions<'tcx, T>(infcx: &InferCtxt<'_, 'tcx>, value: T) -> T
ff7c6d11
XL
31where
32 T: TypeFoldable<'tcx>,
33{
dfeec247 34 infcx.tcx.fold_regions(value, &mut false, |_region, _depth| {
5869c6ff 35 let origin = NllRegionVariableOrigin::Existential { from_forall: false };
dfeec247
XL
36 infcx.next_nll_region_var(origin)
37 })
ff7c6d11
XL
38}
39
5869c6ff 40struct NllVisitor<'a, 'tcx> {
dc9dc135 41 infcx: &'a InferCtxt<'a, 'tcx>,
ff7c6d11
XL
42}
43
5869c6ff 44impl<'a, 'tcx> NllVisitor<'a, 'tcx> {
fc512014 45 fn renumber_regions<T>(&mut self, value: T) -> T
ff7c6d11
XL
46 where
47 T: TypeFoldable<'tcx>,
48 {
8faf50e0 49 renumber_regions(self.infcx, value)
ff7c6d11
XL
50 }
51}
52
5869c6ff 53impl<'a, 'tcx> MutVisitor<'tcx> for NllVisitor<'a, 'tcx> {
e74abb32
XL
54 fn tcx(&self) -> TyCtxt<'tcx> {
55 self.infcx.tcx
56 }
57
c295e0f8 58 #[instrument(skip(self), level = "debug")]
ff7c6d11 59 fn visit_ty(&mut self, ty: &mut Ty<'tcx>, ty_context: TyContext) {
8faf50e0 60 *ty = self.renumber_regions(ty);
ff7c6d11 61
c295e0f8 62 debug!(?ty);
ff7c6d11
XL
63 }
64
c295e0f8 65 #[instrument(skip(self), level = "debug")]
532ac7d7 66 fn visit_substs(&mut self, substs: &mut SubstsRef<'tcx>, location: Location) {
fc512014 67 *substs = self.renumber_regions(*substs);
ff7c6d11 68
c295e0f8 69 debug!(?substs);
ff7c6d11
XL
70 }
71
c295e0f8 72 #[instrument(skip(self), level = "debug")]
ff7c6d11 73 fn visit_region(&mut self, region: &mut ty::Region<'tcx>, location: Location) {
ff7c6d11 74 let old_region = *region;
8faf50e0 75 *region = self.renumber_regions(&old_region);
ff7c6d11 76
c295e0f8 77 debug!(?region);
ff7c6d11
XL
78 }
79
532ac7d7 80 fn visit_const(&mut self, constant: &mut &'tcx ty::Const<'tcx>, _location: Location) {
8faf50e0 81 *constant = self.renumber_regions(&*constant);
ff7c6d11 82 }
ff7c6d11 83}