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, PlaceElem, Promoted}
;
5 use rustc_middle
::ty
::subst
::SubstsRef
;
6 use rustc_middle
::ty
::{self, Ty, TyCtxt, TypeFoldable}
;
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
>>,
15 debug
!("renumber_mir()");
16 debug
!("renumber_mir: body.arg_count={:?}", body
.arg_count
);
18 let mut visitor
= NLLVisitor { infcx }
;
20 for body
in promoted
.iter_mut() {
21 visitor
.visit_body(body
);
24 visitor
.visit_body(body
);
27 /// Replaces all regions appearing in `value` with fresh inference
29 pub fn renumber_regions
<'tcx
, T
>(infcx
: &InferCtxt
<'_
, 'tcx
>, value
: &T
) -> T
31 T
: TypeFoldable
<'tcx
>,
33 debug
!("renumber_regions(value={:?})", value
);
35 infcx
.tcx
.fold_regions(value
, &mut false, |_region
, _depth
| {
36 let origin
= NLLRegionVariableOrigin
::Existential { from_forall: false }
;
37 infcx
.next_nll_region_var(origin
)
41 struct NLLVisitor
<'a
, 'tcx
> {
42 infcx
: &'a InferCtxt
<'a
, 'tcx
>,
45 impl<'a
, 'tcx
> NLLVisitor
<'a
, 'tcx
> {
46 fn renumber_regions
<T
>(&mut self, value
: &T
) -> T
48 T
: TypeFoldable
<'tcx
>,
50 renumber_regions(self.infcx
, value
)
54 impl<'a
, 'tcx
> MutVisitor
<'tcx
> for NLLVisitor
<'a
, 'tcx
> {
55 fn tcx(&self) -> TyCtxt
<'tcx
> {
59 fn visit_ty(&mut self, ty
: &mut Ty
<'tcx
>, ty_context
: TyContext
) {
60 debug
!("visit_ty(ty={:?}, ty_context={:?})", ty
, ty_context
);
62 *ty
= self.renumber_regions(ty
);
64 debug
!("visit_ty: ty={:?}", ty
);
67 fn process_projection_elem(
69 elem
: PlaceElem
<'tcx
>,
71 ) -> Option
<PlaceElem
<'tcx
>> {
72 if let PlaceElem
::Field(field
, ty
) = elem
{
73 let new_ty
= self.renumber_regions(&ty
);
76 return Some(PlaceElem
::Field(field
, new_ty
));
83 fn visit_substs(&mut self, substs
: &mut SubstsRef
<'tcx
>, location
: Location
) {
84 debug
!("visit_substs(substs={:?}, location={:?})", substs
, location
);
86 *substs
= self.renumber_regions(&{ *substs }
);
88 debug
!("visit_substs: substs={:?}", substs
);
91 fn visit_region(&mut self, region
: &mut ty
::Region
<'tcx
>, location
: Location
) {
92 debug
!("visit_region(region={:?}, location={:?})", region
, location
);
94 let old_region
= *region
;
95 *region
= self.renumber_regions(&old_region
);
97 debug
!("visit_region: region={:?}", region
);
100 fn visit_const(&mut self, constant
: &mut &'tcx ty
::Const
<'tcx
>, _location
: Location
) {
101 *constant
= self.renumber_regions(&*constant
);