2017-11-06 05:21:48 -05:00
|
|
|
use rustc::ty::subst::Substs;
|
2018-10-10 17:07:10 -04:00
|
|
|
use rustc::ty::{self, ClosureSubsts, GeneratorSubsts, Ty, TypeFoldable};
|
2018-11-10 23:02:13 +00:00
|
|
|
use rustc::mir::{Location, Mir, UserTypeAnnotation};
|
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.
|
2017-12-10 10:23:45 -05:00
|
|
|
pub fn renumber_mir<'tcx>(infcx: &InferCtxt<'_, '_, 'tcx>, mir: &mut Mir<'tcx>) {
|
2017-11-07 04:30:43 -05:00
|
|
|
debug!("renumber_mir()");
|
|
|
|
debug!("renumber_mir: mir.arg_count={:?}", mir.arg_count);
|
|
|
|
|
2017-12-06 04:30:58 -05:00
|
|
|
let mut visitor = NLLVisitor { infcx };
|
2017-10-24 14:20:47 -04:00
|
|
|
visitor.visit_mir(mir);
|
|
|
|
}
|
|
|
|
|
2017-12-10 10:23:45 -05:00
|
|
|
/// Replaces all regions appearing in `value` with fresh inference
|
|
|
|
/// variables.
|
|
|
|
pub fn renumber_regions<'tcx, T>(
|
|
|
|
infcx: &InferCtxt<'_, '_, 'tcx>,
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-24 14:20:47 -04:00
|
|
|
struct NLLVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
|
|
|
|
infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'gcx, 'tcx> NLLVisitor<'a, 'gcx, '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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'gcx, 'tcx> MutVisitor<'tcx> for NLLVisitor<'a, 'gcx, 'tcx> {
|
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
|
|
|
}
|
|
|
|
|
2018-10-10 17:07:10 -04:00
|
|
|
fn visit_user_type_annotation(&mut self, _ty: &mut UserTypeAnnotation<'tcx>) {
|
|
|
|
// User type annotations represent the types that the user
|
2018-10-08 18:03:43 -04:00
|
|
|
// wrote in the progarm. We don't want to erase the regions
|
|
|
|
// from these types: rather, we want to add them as
|
|
|
|
// constraints at type-check time.
|
2018-10-10 17:07:10 -04:00
|
|
|
debug!("visit_user_type_annotation: skipping renumber");
|
2018-10-08 18:03:43 -04:00
|
|
|
}
|
|
|
|
|
2017-10-24 14:20:47 -04:00
|
|
|
fn visit_substs(&mut self, substs: &mut &'tcx Substs<'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
|
|
|
}
|
|
|
|
|
2018-07-22 19:42:40 +03:00
|
|
|
fn visit_const(&mut self, constant: &mut &'tcx ty::Const<'tcx>, _location: Location) {
|
|
|
|
*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
|
|
|
}
|
|
|
|
}
|