]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/transform/erase_regions.rs
Fix rebase fail
[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 //! NOTE:  We do NOT erase regions of statements that are relevant for
5 //! "types-as-contracts"-validation, namely, AcquireValid, ReleaseValid
6
7 use rustc::ty::subst::Substs;
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.types.re_erased;
33     }
34
35     fn visit_const(&mut self, constant: &mut &'tcx ty::LazyConst<'tcx>, _: Location) {
36         *constant = self.tcx.erase_regions(constant);
37     }
38
39     fn visit_substs(&mut self, substs: &mut &'tcx Substs<'tcx>, _: Location) {
40         *substs = self.tcx.erase_regions(substs);
41     }
42
43     fn visit_statement(&mut self,
44                        block: BasicBlock,
45                        statement: &mut Statement<'tcx>,
46                        location: Location) {
47         self.super_statement(block, statement, location);
48     }
49 }
50
51 pub struct EraseRegions;
52
53 impl MirPass for EraseRegions {
54     fn run_pass<'a, 'tcx>(&self,
55                           tcx: TyCtxt<'a, 'tcx, 'tcx>,
56                           _: MirSource<'tcx>,
57                           mir: &mut Mir<'tcx>) {
58         EraseRegionsVisitor::new(tcx).visit_mir(mir);
59     }
60 }