1 //! This pass erases all early-bound regions from the types occurring in the MIR.
2 //! We want to do this once just before codegen, so codegen does not have to take
3 //! care erasing regions all over the place.
4 //! N.B., we do _not_ erase regions of statements that are relevant for
5 //! "types-as-contracts"-validation, namely, `AcquireValid` and `ReleaseValid`.
7 use rustc::ty::subst::SubstsRef;
8 use rustc::ty::{self, Ty, TyCtxt};
10 use rustc::mir::visit::{MutVisitor, TyContext};
11 use crate::transform::{MirPass, MirSource};
13 struct EraseRegionsVisitor<'a, 'tcx: 'a> {
14 tcx: TyCtxt<'a, 'tcx, 'tcx>,
17 impl<'a, 'tcx> EraseRegionsVisitor<'a, 'tcx> {
18 pub fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Self {
25 impl<'a, 'tcx> MutVisitor<'tcx> for EraseRegionsVisitor<'a, 'tcx> {
26 fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _: TyContext) {
27 *ty = self.tcx.erase_regions(ty);
31 fn visit_region(&mut self, region: &mut ty::Region<'tcx>, _: Location) {
32 *region = self.tcx.lifetimes.re_erased;
35 fn visit_const(&mut self, constant: &mut &'tcx ty::Const<'tcx>, _: Location) {
36 *constant = self.tcx.erase_regions(constant);
39 fn visit_substs(&mut self, substs: &mut SubstsRef<'tcx>, _: Location) {
40 *substs = self.tcx.erase_regions(substs);
43 fn visit_statement(&mut self,
44 statement: &mut Statement<'tcx>,
46 self.super_statement(statement, location);
50 pub struct EraseRegions;
52 impl MirPass for EraseRegions {
53 fn run_pass<'a, 'tcx>(&self,
54 tcx: TyCtxt<'a, 'tcx, 'tcx>,
56 mir: &mut Body<'tcx>) {
57 EraseRegionsVisitor::new(tcx).visit_body(mir);