]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/transform/erase_regions.rs
Auto merge of #31782 - pitdicker:clean_out_windows_c, r=alexcrichton
[rust.git] / src / librustc_mir / transform / erase_regions.rs
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
15 use rustc::middle::ty;
16 use rustc::mir::repr::*;
17 use rustc::mir::visit::MutVisitor;
18 use rustc::mir::mir_map::MirMap;
19
20 pub fn erase_regions<'tcx>(tcx: &ty::ctxt<'tcx>, mir_map: &mut MirMap<'tcx>) {
21     for (_, mir) in &mut mir_map.map {
22         EraseRegionsVisitor::new(tcx).visit_mir(mir);
23     }
24 }
25
26 struct EraseRegionsVisitor<'a, 'tcx: 'a> {
27     tcx: &'a ty::ctxt<'tcx>,
28 }
29
30 impl<'a, 'tcx> EraseRegionsVisitor<'a, 'tcx> {
31     pub fn new(tcx: &'a ty::ctxt<'tcx>) -> Self {
32         EraseRegionsVisitor {
33             tcx: tcx
34         }
35     }
36
37     fn erase_regions_return_ty(&mut self, fn_output: &mut ty::FnOutput<'tcx>) {
38         match *fn_output {
39             ty::FnConverging(ref mut ty) => {
40                 *ty = self.tcx.erase_regions(ty);
41             },
42             ty::FnDiverging => {}
43         }
44     }
45
46     fn erase_regions_tys<'b, T>(&mut self, tys: T)
47         where T: Iterator<Item = &'b mut ty::Ty<'tcx>>,
48               'tcx: 'b
49     {
50         for ty in tys {
51             *ty = self.tcx.erase_regions(ty);
52         }
53     }
54 }
55
56 impl<'a, 'tcx> MutVisitor<'tcx> for EraseRegionsVisitor<'a, 'tcx> {
57     fn visit_mir(&mut self, mir: &mut Mir<'tcx>) {
58         self.erase_regions_return_ty(&mut mir.return_ty);
59         self.erase_regions_tys(mir.var_decls.iter_mut().map(|d| &mut d.ty));
60         self.erase_regions_tys(mir.arg_decls.iter_mut().map(|d| &mut d.ty));
61         self.erase_regions_tys(mir.temp_decls.iter_mut().map(|d| &mut d.ty));
62         self.super_mir(mir);
63     }
64
65     fn visit_terminator(&mut self, bb: BasicBlock, terminator: &mut Terminator<'tcx>) {
66         match *terminator {
67             Terminator::Goto { .. } |
68             Terminator::Resume |
69             Terminator::Return |
70             Terminator::If { .. } |
71             Terminator::Switch { .. } |
72             Terminator::Drop { .. } |
73             Terminator::Call { .. } => {
74                 /* nothing to do */
75             },
76             Terminator::SwitchInt { ref mut switch_ty, .. } => {
77                 *switch_ty = self.tcx.erase_regions(switch_ty);
78             },
79         }
80         self.super_terminator(bb, terminator);
81     }
82
83     fn visit_rvalue(&mut self, rvalue: &mut Rvalue<'tcx>) {
84         match *rvalue {
85             Rvalue::Use(_) |
86             Rvalue::Len(_) |
87             Rvalue::BinaryOp(_, _, _) |
88             Rvalue::UnaryOp(_, _) |
89             Rvalue::Slice { input: _, from_start: _, from_end: _ } |
90             Rvalue::InlineAsm(_) => {},
91
92             Rvalue::Repeat(_, ref mut value) => value.ty = self.tcx.erase_regions(&value.ty),
93             Rvalue::Ref(ref mut region, _, _) => *region = ty::ReStatic,
94             Rvalue::Cast(_, _, ref mut ty) => *ty = self.tcx.erase_regions(ty),
95             Rvalue::Box(ref mut ty) => *ty = self.tcx.erase_regions(ty),
96
97
98             Rvalue::Aggregate(AggregateKind::Vec, _) |
99             Rvalue::Aggregate(AggregateKind::Tuple, _) => {},
100             Rvalue::Aggregate(AggregateKind::Adt(_, _, ref mut substs), _) =>
101                 *substs = self.tcx.mk_substs(self.tcx.erase_regions(*substs)),
102             Rvalue::Aggregate(AggregateKind::Closure(def_id, ref mut closure_substs), _) => {
103                 let cloned = Box::new(closure_substs.clone());
104                 let ty = self.tcx.mk_closure_from_closure_substs(def_id, cloned);
105                 let erased = self.tcx.erase_regions(&ty);
106                 *closure_substs = match erased.sty {
107                     ty::TyClosure(_, ref closure_substs) => &*closure_substs,
108                     _ => unreachable!()
109                 };
110             }
111         }
112         self.super_rvalue(rvalue);
113     }
114
115     fn visit_constant(&mut self, constant: &mut Constant<'tcx>) {
116         constant.ty = self.tcx.erase_regions(&constant.ty);
117         match constant.literal {
118             Literal::Item { ref mut substs, .. } => {
119                 *substs = self.tcx.mk_substs(self.tcx.erase_regions(substs));
120             }
121             Literal::Value { .. } => { /* nothing to do */ }
122         }
123         self.super_constant(constant);
124     }
125 }