]> git.proxmox.com Git - rustc.git/blame - src/librustc_mir/transform/erase_regions.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / librustc_mir / transform / erase_regions.rs
CommitLineData
92a42be0
SL
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.
4//
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.
10
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.
14
54a0048b
SL
15use rustc::ty::subst::Substs;
16use rustc::ty::{Ty, TyCtxt};
92a42be0 17use rustc::mir::repr::*;
7453a54e 18use rustc::mir::visit::MutVisitor;
54a0048b
SL
19use rustc::mir::transform::{MirPass, Pass};
20use syntax::ast::NodeId;
92a42be0 21
7453a54e 22struct EraseRegionsVisitor<'a, 'tcx: 'a> {
54a0048b 23 tcx: &'a TyCtxt<'tcx>,
92a42be0
SL
24}
25
7453a54e 26impl<'a, 'tcx> EraseRegionsVisitor<'a, 'tcx> {
54a0048b 27 pub fn new(tcx: &'a TyCtxt<'tcx>) -> Self {
7453a54e
SL
28 EraseRegionsVisitor {
29 tcx: tcx
92a42be0 30 }
92a42be0 31 }
7453a54e 32}
92a42be0 33
7453a54e 34impl<'a, 'tcx> MutVisitor<'tcx> for EraseRegionsVisitor<'a, 'tcx> {
54a0048b
SL
35 fn visit_ty(&mut self, ty: &mut Ty<'tcx>) {
36 let old_ty = *ty;
37 *ty = self.tcx.erase_regions(&old_ty);
92a42be0
SL
38 }
39
54a0048b
SL
40 fn visit_substs(&mut self, substs: &mut &'tcx Substs<'tcx>) {
41 *substs = self.tcx.mk_substs(self.tcx.erase_regions(*substs));
92a42be0 42 }
54a0048b 43}
92a42be0 44
54a0048b 45pub struct EraseRegions;
7453a54e 46
54a0048b 47impl Pass for EraseRegions {}
7453a54e 48
54a0048b
SL
49impl<'tcx> MirPass<'tcx> for EraseRegions {
50 fn run_pass(&mut self, tcx: &TyCtxt<'tcx>, _: NodeId, mir: &mut Mir<'tcx>) {
51 EraseRegionsVisitor::new(tcx).visit_mir(mir);
92a42be0
SL
52 }
53}