rust/src/librustc_mir/transform/erase_regions.rs

69 lines
2.1 KiB
Rust
Raw Normal View History

2017-08-11 20:34:14 +02:00
//! This pass erases all early-bound regions from the types occurring in the MIR.
2018-05-08 16:10:16 +03:00
//! We want to do this once just before codegen, so codegen does not have to take
//! care erasing regions all over the place.
2019-02-08 14:53:55 +01:00
//! N.B., we do _not_ erase regions of statements that are relevant for
//! "types-as-contracts"-validation, namely, `AcquireValid` and `ReleaseValid`.
2019-02-09 22:11:53 +08:00
use rustc::ty::subst::SubstsRef;
use rustc::ty::{self, Ty, TyCtxt};
2016-09-19 23:50:00 +03:00
use rustc::mir::*;
use rustc::mir::visit::{MutVisitor, PlaceContext, TyContext};
2019-02-08 06:28:15 +09:00
use crate::transform::{MirPass, MirSource};
struct EraseRegionsVisitor<'tcx> {
2019-06-14 00:48:52 +03:00
tcx: TyCtxt<'tcx>,
}
impl EraseRegionsVisitor<'tcx> {
2019-06-14 00:48:52 +03:00
pub fn new(tcx: TyCtxt<'tcx>) -> Self {
EraseRegionsVisitor {
tcx,
}
}
}
impl MutVisitor<'tcx> for EraseRegionsVisitor<'tcx> {
fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _: TyContext) {
*ty = self.tcx.erase_regions(ty);
}
fn visit_region(&mut self, region: &mut ty::Region<'tcx>, _: Location) {
2019-04-25 22:05:04 +01:00
*region = self.tcx.lifetimes.re_erased;
}
2019-03-14 10:19:31 +01:00
fn visit_const(&mut self, constant: &mut &'tcx ty::Const<'tcx>, _: Location) {
*constant = self.tcx.erase_regions(constant);
}
2019-02-09 22:11:53 +08:00
fn visit_substs(&mut self, substs: &mut SubstsRef<'tcx>, _: Location) {
*substs = self.tcx.erase_regions(substs);
}
fn visit_place(
&mut self,
place: &mut Place<'tcx>,
context: PlaceContext,
location: Location,
) {
self.visit_place_base(&mut place.base, context, location);
let new_projection: Vec<_> = place.projection.iter().map(|elem|
if let PlaceElem::Field(field, ty) = elem {
PlaceElem::Field(*field, self.tcx.erase_regions(ty))
} else {
elem.clone()
}
).collect();
place.projection = new_projection.into_boxed_slice();
}
}
2016-02-26 18:05:50 +02:00
pub struct EraseRegions;
2019-08-04 16:20:00 -04:00
impl<'tcx> MirPass<'tcx> for EraseRegions {
fn run_pass(&self, tcx: TyCtxt<'tcx>, _: MirSource<'tcx>, body: &mut Body<'tcx>) {
EraseRegionsVisitor::new(tcx).visit_body(body);
2016-02-26 18:05:50 +02:00
}
}