]> git.proxmox.com Git - rustc.git/blame - src/librustc_mir/transform/erase_regions.rs
New upstream version 1.43.0+dfsg1
[rustc.git] / src / librustc_mir / transform / erase_regions.rs
CommitLineData
3b2f2976 1//! This pass erases all early-bound regions from the types occurring in the MIR.
94b46f34 2//! We want to do this once just before codegen, so codegen does not have to take
92a42be0 3//! care erasing regions all over the place.
9fa01778
XL
4//! N.B., we do _not_ erase regions of statements that are relevant for
5//! "types-as-contracts"-validation, namely, `AcquireValid` and `ReleaseValid`.
92a42be0 6
dfeec247
XL
7use crate::transform::{MirPass, MirSource};
8use rustc::mir::visit::{MutVisitor, TyContext};
9use rustc::mir::*;
532ac7d7 10use rustc::ty::subst::SubstsRef;
ea8adc8c 11use rustc::ty::{self, Ty, TyCtxt};
92a42be0 12
dc9dc135
XL
13struct EraseRegionsVisitor<'tcx> {
14 tcx: TyCtxt<'tcx>,
92a42be0
SL
15}
16
dc9dc135
XL
17impl EraseRegionsVisitor<'tcx> {
18 pub fn new(tcx: TyCtxt<'tcx>) -> Self {
dfeec247 19 EraseRegionsVisitor { tcx }
92a42be0 20 }
7453a54e 21}
92a42be0 22
dc9dc135 23impl MutVisitor<'tcx> for EraseRegionsVisitor<'tcx> {
e74abb32
XL
24 fn tcx(&self) -> TyCtxt<'tcx> {
25 self.tcx
26 }
27
abe05a73 28 fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _: TyContext) {
a1dfa0c6 29 *ty = self.tcx.erase_regions(ty);
92a42be0
SL
30 }
31
ea8adc8c 32 fn visit_region(&mut self, region: &mut ty::Region<'tcx>, _: Location) {
48663c56 33 *region = self.tcx.lifetimes.re_erased;
92a42be0 34 }
cc61c64b 35
532ac7d7 36 fn visit_const(&mut self, constant: &mut &'tcx ty::Const<'tcx>, _: Location) {
ea8adc8c
XL
37 *constant = self.tcx.erase_regions(constant);
38 }
39
532ac7d7 40 fn visit_substs(&mut self, substs: &mut SubstsRef<'tcx>, _: Location) {
ea8adc8c 41 *substs = self.tcx.erase_regions(substs);
cc61c64b
XL
42 }
43
dfeec247 44 fn process_projection_elem(&mut self, elem: &PlaceElem<'tcx>) -> Option<PlaceElem<'tcx>> {
e74abb32
XL
45 if let PlaceElem::Field(field, ty) = elem {
46 let new_ty = self.tcx.erase_regions(ty);
47
48 if new_ty != *ty {
49 return Some(PlaceElem::Field(*field, new_ty));
50 }
51 }
52
53 None
041b39d2 54 }
54a0048b 55}
92a42be0 56
54a0048b 57pub struct EraseRegions;
7453a54e 58
e1599b0c 59impl<'tcx> MirPass<'tcx> for EraseRegions {
60c5eb7d 60 fn run_pass(&self, tcx: TyCtxt<'tcx>, _: MirSource<'tcx>, body: &mut BodyAndCache<'tcx>) {
dc9dc135 61 EraseRegionsVisitor::new(tcx).visit_body(body);
92a42be0
SL
62 }
63}