2019-02-09 22:11:53 +08:00
|
|
|
use rustc::ty::subst::SubstsRef;
|
2019-01-12 14:55:23 +00:00
|
|
|
use rustc::ty::{self, ClosureSubsts, GeneratorSubsts, Ty, TypeFoldable};
|
2019-05-17 23:55:04 +02:00
|
|
|
use rustc::mir::{Location, Body};
|
2017-10-26 19:53:31 -04:00
|
|
|
use rustc::mir::visit::{MutVisitor, TyContext};
|
2017-11-06 05:21:48 -05:00
|
|
|
use rustc::infer::{InferCtxt, NLLRegionVariableOrigin};
|
2017-10-24 14:20:47 -04:00
|
|
|
|
|
|
|
/// Replaces all free regions appearing in the MIR with fresh
|
|
|
|
/// inference variables, returning the number of variables created.
|
2019-06-14 00:48:52 +03:00
|
|
|
pub fn renumber_mir<'tcx>(infcx: &InferCtxt<'_, 'tcx>, body: &mut Body<'tcx>) {
|
2017-11-07 04:30:43 -05:00
|
|
|
debug!("renumber_mir()");
|
2019-06-03 18:26:48 -04:00
|
|
|
debug!("renumber_mir: body.arg_count={:?}", body.arg_count);
|
2017-11-07 04:30:43 -05:00
|
|
|
|
2017-12-06 04:30:58 -05:00
|
|
|
let mut visitor = NLLVisitor { infcx };
|
2019-06-03 18:26:48 -04:00
|
|
|
visitor.visit_body(body);
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2017-12-10 10:23:45 -05:00
|
|
|
/// Replaces all regions appearing in `value` with fresh inference
|
|
|
|
/// variables.
|
|
|
|
pub fn renumber_regions<'tcx, T>(
|
2019-06-14 00:48:52 +03:00
|
|
|
infcx: &InferCtxt<'_, 'tcx>,
|
2017-12-10 10:23:45 -05:00
|
|
|
value: &T,
|
|
|
|
) -> T
|
|
|
|
where
|
|
|
|
T: TypeFoldable<'tcx>,
|
|
|
|
{
|
|
|
|
debug!("renumber_regions(value={:?})", value);
|
|
|
|
|
|
|
|
infcx
|
|
|
|
.tcx
|
|
|
|
.fold_regions(value, &mut false, |_region, _depth| {
|
2018-07-22 19:42:40 +03:00
|
|
|
let origin = NLLRegionVariableOrigin::Existential;
|
2017-12-10 10:23:45 -05:00
|
|
|
infcx.next_nll_region_var(origin)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
struct NLLVisitor<'a, 'tcx> {
|
|
|
|
infcx: &'a InferCtxt<'a, 'tcx>,
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'a, 'tcx> NLLVisitor<'a, 'tcx> {
|
2018-07-22 19:42:40 +03:00
|
|
|
fn renumber_regions<T>(&mut self, value: &T) -> T
|
2017-10-30 04:51:10 -04:00
|
|
|
where
|
|
|
|
T: TypeFoldable<'tcx>,
|
|
|
|
{
|
2018-07-22 19:42:40 +03:00
|
|
|
renumber_regions(self.infcx, value)
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'a, 'tcx> MutVisitor<'tcx> for NLLVisitor<'a, 'tcx> {
|
2019-06-03 18:26:48 -04:00
|
|
|
fn visit_body(&mut self, body: &mut Body<'tcx>) {
|
|
|
|
for promoted in body.promoted.iter_mut() {
|
2019-05-17 23:55:04 +02:00
|
|
|
self.visit_body(promoted);
|
2019-01-17 20:03:58 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 18:26:48 -04:00
|
|
|
self.super_body(body);
|
2019-01-17 20:03:58 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 19:53:31 -04:00
|
|
|
fn visit_ty(&mut self, ty: &mut Ty<'tcx>, ty_context: TyContext) {
|
2017-12-06 04:30:58 -05:00
|
|
|
debug!("visit_ty(ty={:?}, ty_context={:?})", ty, ty_context);
|
2017-10-30 04:51:10 -04:00
|
|
|
|
2018-07-22 19:42:40 +03:00
|
|
|
*ty = self.renumber_regions(ty);
|
2017-11-22 17:38:51 -05:00
|
|
|
|
2017-11-07 04:30:06 -05:00
|
|
|
debug!("visit_ty: ty={:?}", ty);
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2019-02-09 22:11:53 +08:00
|
|
|
fn visit_substs(&mut self, substs: &mut SubstsRef<'tcx>, location: Location) {
|
2017-11-07 04:30:43 -05:00
|
|
|
debug!("visit_substs(substs={:?}, location={:?})", substs, location);
|
|
|
|
|
2018-07-22 19:42:40 +03:00
|
|
|
*substs = self.renumber_regions(&{ *substs });
|
2017-11-07 04:30:43 -05:00
|
|
|
|
|
|
|
debug!("visit_substs: substs={:?}", substs);
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2017-11-07 04:30:43 -05:00
|
|
|
fn visit_region(&mut self, region: &mut ty::Region<'tcx>, location: Location) {
|
|
|
|
debug!("visit_region(region={:?}, location={:?})", region, location);
|
|
|
|
|
|
|
|
let old_region = *region;
|
2018-07-22 19:42:40 +03:00
|
|
|
*region = self.renumber_regions(&old_region);
|
2017-11-07 04:30:43 -05:00
|
|
|
|
|
|
|
debug!("visit_region: region={:?}", region);
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2019-03-14 10:19:31 +01:00
|
|
|
fn visit_const(&mut self, constant: &mut &'tcx ty::Const<'tcx>, _location: Location) {
|
2018-07-22 19:42:40 +03:00
|
|
|
*constant = self.renumber_regions(&*constant);
|
2017-11-22 17:29:55 -05:00
|
|
|
}
|
|
|
|
|
2018-05-02 13:14:30 +02:00
|
|
|
fn visit_generator_substs(&mut self,
|
|
|
|
substs: &mut GeneratorSubsts<'tcx>,
|
|
|
|
location: Location) {
|
2018-01-11 06:04:18 -05:00
|
|
|
debug!(
|
2018-05-02 13:14:30 +02:00
|
|
|
"visit_generator_substs(substs={:?}, location={:?})",
|
|
|
|
substs,
|
2018-01-11 06:04:18 -05:00
|
|
|
location,
|
|
|
|
);
|
|
|
|
|
2018-07-22 19:42:40 +03:00
|
|
|
*substs = self.renumber_regions(substs);
|
2018-01-11 06:04:18 -05:00
|
|
|
|
2018-05-02 13:14:30 +02:00
|
|
|
debug!("visit_generator_substs: substs={:?}", substs);
|
2018-01-11 06:04:18 -05:00
|
|
|
}
|
|
|
|
|
2017-10-30 04:51:10 -04:00
|
|
|
fn visit_closure_substs(&mut self, substs: &mut ClosureSubsts<'tcx>, location: Location) {
|
2017-11-07 04:30:43 -05:00
|
|
|
debug!(
|
|
|
|
"visit_closure_substs(substs={:?}, location={:?})",
|
|
|
|
substs,
|
|
|
|
location
|
|
|
|
);
|
|
|
|
|
2018-07-22 19:42:40 +03:00
|
|
|
*substs = self.renumber_regions(substs);
|
2017-11-07 04:30:43 -05:00
|
|
|
|
|
|
|
debug!("visit_closure_substs: substs={:?}", substs);
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
|
|
|
}
|