1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! This pass erases all early-bound regions from the types occuring in the MIR.
12 //! We want to do this once just before trans, so trans does not have to take
13 //! care erasing regions all over the place.
15 use rustc::ty::subst::Substs;
16 use rustc::ty::{Ty, TyCtxt, ClosureSubsts};
18 use rustc::mir::visit::MutVisitor;
19 use rustc::mir::transform::{MirPass, MirSource, Pass};
21 struct EraseRegionsVisitor<'a, 'tcx: 'a> {
22 tcx: TyCtxt<'a, 'tcx, 'tcx>,
25 impl<'a, 'tcx> EraseRegionsVisitor<'a, 'tcx> {
26 pub fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Self {
33 impl<'a, 'tcx> MutVisitor<'tcx> for EraseRegionsVisitor<'a, 'tcx> {
34 fn visit_ty(&mut self, ty: &mut Ty<'tcx>) {
36 *ty = self.tcx.erase_regions(&old_ty);
39 fn visit_substs(&mut self, substs: &mut &'tcx Substs<'tcx>) {
40 *substs = self.tcx.erase_regions(&{*substs});
43 fn visit_rvalue(&mut self, rvalue: &mut Rvalue<'tcx>, location: Location) {
45 Rvalue::Ref(ref mut r, _, _) => {
46 *r = self.tcx.types.re_erased;
52 Rvalue::BinaryOp(..) |
53 Rvalue::CheckedBinaryOp(..) |
55 Rvalue::Discriminant(..) |
57 Rvalue::Aggregate(..) => {
58 // These variants don't contain regions.
61 self.super_rvalue(rvalue, location);
64 fn visit_closure_substs(&mut self,
65 substs: &mut ClosureSubsts<'tcx>) {
66 *substs = self.tcx.erase_regions(substs);
70 pub struct EraseRegions;
72 impl Pass for EraseRegions {}
74 impl<'tcx> MirPass<'tcx> for EraseRegions {
75 fn run_pass<'a>(&mut self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
76 _: MirSource, mir: &mut Mir<'tcx>) {
77 EraseRegionsVisitor::new(tcx).visit_mir(mir);