Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

84 lines
2.4 KiB
Rust
Raw Normal View History

2020-03-29 17:19:48 +02:00
use rustc_index::vec::IndexVec;
use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin};
2020-03-29 16:41:09 +02:00
use rustc_middle::mir::visit::{MutVisitor, TyContext};
2021-10-16 14:03:30 +02:00
use rustc_middle::mir::{Body, Location, Promoted};
2020-03-29 16:41:09 +02:00
use rustc_middle::ty::subst::SubstsRef;
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable};
/// Replaces all free regions appearing in the MIR with fresh
/// inference variables, returning the number of variables created.
2021-08-20 13:36:04 +00:00
#[instrument(skip(infcx, body, promoted), level = "debug")]
2019-08-14 08:08:17 -04:00
pub fn renumber_mir<'tcx>(
infcx: &InferCtxt<'_, 'tcx>,
2020-04-12 10:31:00 -07:00
body: &mut Body<'tcx>,
promoted: &mut IndexVec<Promoted, Body<'tcx>>,
2019-08-14 08:08:17 -04:00
) {
2021-08-20 13:36:04 +00:00
debug!(?body.arg_count);
let mut visitor = NllVisitor { infcx };
2019-08-15 06:39:31 -04:00
for body in promoted.iter_mut() {
visitor.visit_body(body);
}
visitor.visit_body(body);
}
/// Replaces all regions appearing in `value` with fresh inference
/// variables.
2021-08-20 13:36:04 +00:00
#[instrument(skip(infcx), level = "debug")]
2020-10-24 02:21:18 +02:00
pub fn renumber_regions<'tcx, T>(infcx: &InferCtxt<'_, 'tcx>, value: T) -> T
where
T: TypeFoldable<'tcx>,
{
2022-06-27 15:55:03 +02:00
infcx.tcx.fold_regions(value, |_region, _depth| {
let origin = NllRegionVariableOrigin::Existential { from_forall: false };
infcx.next_nll_region_var(origin)
})
}
struct NllVisitor<'a, 'tcx> {
2019-06-14 00:48:52 +03:00
infcx: &'a InferCtxt<'a, 'tcx>,
}
impl<'a, 'tcx> NllVisitor<'a, 'tcx> {
2020-10-24 02:21:18 +02:00
fn renumber_regions<T>(&mut self, value: T) -> T
where
T: TypeFoldable<'tcx>,
{
2018-07-22 19:42:40 +03:00
renumber_regions(self.infcx, value)
}
2019-08-04 16:20:21 -04:00
}
impl<'a, 'tcx> MutVisitor<'tcx> for NllVisitor<'a, 'tcx> {
2019-10-20 16:11:04 -04:00
fn tcx(&self) -> TyCtxt<'tcx> {
self.infcx.tcx
}
2021-08-20 13:36:04 +00:00
#[instrument(skip(self), level = "debug")]
fn visit_ty(&mut self, ty: &mut Ty<'tcx>, ty_context: TyContext) {
2022-01-25 14:13:38 +11:00
*ty = self.renumber_regions(*ty);
2021-08-20 13:36:04 +00:00
debug!(?ty);
}
2021-08-20 13:36:04 +00:00
#[instrument(skip(self), level = "debug")]
2019-02-09 22:11:53 +08:00
fn visit_substs(&mut self, substs: &mut SubstsRef<'tcx>, location: Location) {
2020-10-24 02:21:18 +02:00
*substs = self.renumber_regions(*substs);
2021-08-20 13:36:04 +00:00
debug!(?substs);
}
2021-08-20 13:36:04 +00:00
#[instrument(skip(self), level = "debug")]
fn visit_region(&mut self, region: &mut ty::Region<'tcx>, location: Location) {
let old_region = *region;
*region = self.renumber_regions(old_region);
2021-08-20 13:36:04 +00:00
debug!(?region);
}
fn visit_const(&mut self, constant: &mut ty::Const<'tcx>, _location: Location) {
*constant = self.renumber_regions(*constant);
}
}