2015-11-16 11:41:16 -06:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! This pass erases all early-bound regions from the types occuring in the MIR.
|
|
|
|
//! We want to do this once just before trans, so trans does not have to take
|
|
|
|
//! care erasing regions all over the place.
|
|
|
|
|
2016-03-22 10:30:57 -05:00
|
|
|
use rustc::ty::subst::Substs;
|
2017-04-19 17:58:12 -05:00
|
|
|
use rustc::ty::{Ty, TyCtxt, ClosureSubsts};
|
2016-09-19 15:50:00 -05:00
|
|
|
use rustc::mir::*;
|
2016-01-31 12:25:17 -06:00
|
|
|
use rustc::mir::visit::MutVisitor;
|
2017-04-25 17:23:33 -05:00
|
|
|
use rustc::mir::transform::{MirPass, MirSource};
|
2015-11-16 11:41:16 -06:00
|
|
|
|
2016-02-05 02:35:00 -06:00
|
|
|
struct EraseRegionsVisitor<'a, 'tcx: 'a> {
|
2016-05-02 21:23:22 -05:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2015-11-16 11:41:16 -06:00
|
|
|
}
|
|
|
|
|
2016-02-05 02:35:00 -06:00
|
|
|
impl<'a, 'tcx> EraseRegionsVisitor<'a, 'tcx> {
|
2016-05-02 21:23:22 -05:00
|
|
|
pub fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Self {
|
2016-02-05 02:35:00 -06:00
|
|
|
EraseRegionsVisitor {
|
2015-11-16 11:41:16 -06:00
|
|
|
tcx: tcx
|
|
|
|
}
|
|
|
|
}
|
2016-01-31 12:25:17 -06:00
|
|
|
}
|
|
|
|
|
2016-02-05 02:35:00 -06:00
|
|
|
impl<'a, 'tcx> MutVisitor<'tcx> for EraseRegionsVisitor<'a, 'tcx> {
|
2016-03-25 12:10:32 -05:00
|
|
|
fn visit_ty(&mut self, ty: &mut Ty<'tcx>) {
|
|
|
|
let old_ty = *ty;
|
|
|
|
*ty = self.tcx.erase_regions(&old_ty);
|
2015-11-16 11:41:16 -06:00
|
|
|
}
|
|
|
|
|
2016-03-25 12:10:32 -05:00
|
|
|
fn visit_substs(&mut self, substs: &mut &'tcx Substs<'tcx>) {
|
2016-04-29 00:30:54 -05:00
|
|
|
*substs = self.tcx.erase_regions(&{*substs});
|
2015-11-16 11:41:16 -06:00
|
|
|
}
|
2017-03-20 09:22:24 -05:00
|
|
|
|
|
|
|
fn visit_rvalue(&mut self, rvalue: &mut Rvalue<'tcx>, location: Location) {
|
|
|
|
match *rvalue {
|
|
|
|
Rvalue::Ref(ref mut r, _, _) => {
|
2017-04-19 17:58:12 -05:00
|
|
|
*r = self.tcx.types.re_erased;
|
2017-03-20 09:22:24 -05:00
|
|
|
}
|
2017-03-21 04:42:51 -05:00
|
|
|
Rvalue::Use(..) |
|
|
|
|
Rvalue::Repeat(..) |
|
|
|
|
Rvalue::Len(..) |
|
|
|
|
Rvalue::Cast(..) |
|
|
|
|
Rvalue::BinaryOp(..) |
|
|
|
|
Rvalue::CheckedBinaryOp(..) |
|
|
|
|
Rvalue::UnaryOp(..) |
|
|
|
|
Rvalue::Discriminant(..) |
|
2017-05-18 10:43:52 -05:00
|
|
|
Rvalue::NullaryOp(..) |
|
2017-03-21 04:42:51 -05:00
|
|
|
Rvalue::Aggregate(..) => {
|
|
|
|
// These variants don't contain regions.
|
2017-03-20 09:22:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
self.super_rvalue(rvalue, location);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_closure_substs(&mut self,
|
|
|
|
substs: &mut ClosureSubsts<'tcx>) {
|
|
|
|
*substs = self.tcx.erase_regions(substs);
|
|
|
|
}
|
2015-11-16 11:41:16 -06:00
|
|
|
}
|
2016-02-26 10:05:50 -06:00
|
|
|
|
|
|
|
pub struct EraseRegions;
|
|
|
|
|
2017-04-25 17:23:33 -05:00
|
|
|
impl MirPass for EraseRegions {
|
|
|
|
fn run_pass<'a, 'tcx>(&self,
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
_: MirSource,
|
|
|
|
mir: &mut Mir<'tcx>) {
|
2016-02-26 10:05:50 -06:00
|
|
|
EraseRegionsVisitor::new(tcx).visit_mir(mir);
|
|
|
|
}
|
|
|
|
}
|