]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/transform/erase_regions.rs
Changes the type `mir::Mir` into `mir::Body`
[rust.git] / src / librustc_mir / transform / erase_regions.rs
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`.
6
7 use rustc::ty::subst::SubstsRef;
8 use rustc::ty::{self, Ty, TyCtxt};
9 use rustc::mir::*;
10 use rustc::mir::visit::{MutVisitor, TyContext};
11 use crate::transform::{MirPass, MirSource};
12
13 struct EraseRegionsVisitor<'a, 'tcx: 'a> {
14     tcx: TyCtxt<'a, 'tcx, 'tcx>,
15 }
16
17 impl<'a, 'tcx> EraseRegionsVisitor<'a, 'tcx> {
18     pub fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Self {
19         EraseRegionsVisitor {
20             tcx,
21         }
22     }
23 }
24
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);
28         self.super_ty(ty);
29     }
30
31     fn visit_region(&mut self, region: &mut ty::Region<'tcx>, _: Location) {
32         *region = self.tcx.lifetimes.re_erased;
33     }
34
35     fn visit_const(&mut self, constant: &mut &'tcx ty::Const<'tcx>, _: Location) {
36         *constant = self.tcx.erase_regions(constant);
37     }
38
39     fn visit_substs(&mut self, substs: &mut SubstsRef<'tcx>, _: Location) {
40         *substs = self.tcx.erase_regions(substs);
41     }
42
43     fn visit_statement(&mut self,
44                        statement: &mut Statement<'tcx>,
45                        location: Location) {
46         self.super_statement(statement, location);
47     }
48 }
49
50 pub struct EraseRegions;
51
52 impl MirPass for EraseRegions {
53     fn run_pass<'a, 'tcx>(&self,
54                           tcx: TyCtxt<'a, 'tcx, 'tcx>,
55                           _: MirSource<'tcx>,
56                           mir: &mut Body<'tcx>) {
57         EraseRegionsVisitor::new(tcx).visit_body(mir);
58     }
59 }