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
|
2015-11-16 18:41:16 +01:00
|
|
|
//! 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`.
|
2015-11-16 18:41:16 +01:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
use crate::transform::{MirPass, MirSource};
|
|
|
|
use rustc::mir::visit::{MutVisitor, TyContext};
|
|
|
|
use rustc::mir::*;
|
2019-02-09 22:11:53 +08:00
|
|
|
use rustc::ty::subst::SubstsRef;
|
2017-08-04 11:25:13 +03:00
|
|
|
use rustc::ty::{self, Ty, TyCtxt};
|
2015-11-16 18:41:16 +01:00
|
|
|
|
2019-06-11 22:03:44 +03:00
|
|
|
struct EraseRegionsVisitor<'tcx> {
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2015-11-16 18:41:16 +01:00
|
|
|
}
|
|
|
|
|
2019-06-11 22:03:44 +03:00
|
|
|
impl EraseRegionsVisitor<'tcx> {
|
2019-06-14 00:48:52 +03:00
|
|
|
pub fn new(tcx: TyCtxt<'tcx>) -> Self {
|
2019-12-22 17:42:04 -05:00
|
|
|
EraseRegionsVisitor { tcx }
|
2015-11-16 18:41:16 +01:00
|
|
|
}
|
2016-01-31 20:25:17 +02:00
|
|
|
}
|
|
|
|
|
2019-06-11 22:03:44 +03:00
|
|
|
impl MutVisitor<'tcx> for EraseRegionsVisitor<'tcx> {
|
2019-10-20 16:11:04 -04:00
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
|
2017-10-26 19:53:31 -04:00
|
|
|
fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _: TyContext) {
|
2018-10-24 11:47:17 +02:00
|
|
|
*ty = self.tcx.erase_regions(ty);
|
2015-11-16 18:41:16 +01:00
|
|
|
}
|
|
|
|
|
2017-08-04 11:25:13 +03:00
|
|
|
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;
|
2015-11-16 18:41:16 +01:00
|
|
|
}
|
2017-03-20 15:22:24 +01:00
|
|
|
|
2019-03-14 10:19:31 +01:00
|
|
|
fn visit_const(&mut self, constant: &mut &'tcx ty::Const<'tcx>, _: Location) {
|
2017-08-04 11:25:13 +03:00
|
|
|
*constant = self.tcx.erase_regions(constant);
|
|
|
|
}
|
|
|
|
|
2019-02-09 22:11:53 +08:00
|
|
|
fn visit_substs(&mut self, substs: &mut SubstsRef<'tcx>, _: Location) {
|
2017-08-04 11:25:13 +03:00
|
|
|
*substs = self.tcx.erase_regions(substs);
|
2017-03-20 15:22:24 +01:00
|
|
|
}
|
2019-10-07 19:35:41 -03:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn process_projection_elem(&mut self, elem: &PlaceElem<'tcx>) -> Option<PlaceElem<'tcx>> {
|
2019-10-08 15:33:19 -03:00
|
|
|
if let PlaceElem::Field(field, ty) = elem {
|
2019-10-08 23:46:14 -03:00
|
|
|
let new_ty = self.tcx.erase_regions(ty);
|
|
|
|
|
|
|
|
if new_ty != *ty {
|
|
|
|
return Some(PlaceElem::Field(*field, new_ty));
|
|
|
|
}
|
2019-10-08 15:33:19 -03:00
|
|
|
}
|
2019-10-08 23:46:14 -03:00
|
|
|
|
|
|
|
None
|
2019-10-07 19:35:41 -03:00
|
|
|
}
|
2015-11-16 18:41:16 +01:00
|
|
|
}
|
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 {
|
2019-12-03 11:51:58 -05:00
|
|
|
fn run_pass(&self, tcx: TyCtxt<'tcx>, _: MirSource<'tcx>, body: &mut BodyAndCache<'tcx>) {
|
2019-11-06 12:00:46 -05:00
|
|
|
EraseRegionsVisitor::new(tcx).visit_body(body);
|
2016-02-26 18:05:50 +02:00
|
|
|
}
|
|
|
|
}
|