]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/erase_regions.rs
f53e634a044e7814d857f6b2a275e3af04ca97b0
[rust.git] / src / librustc / ty / erase_regions.rs
1 // Copyright 2017 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 use ty::{self, Ty, TyCtxt};
12 use ty::fold::{TypeFolder, TypeFoldable};
13
14 pub(super) fn provide(providers: &mut ty::query::Providers<'_>) {
15     *providers = ty::query::Providers {
16         erase_regions_ty,
17         ..*providers
18     };
19 }
20
21 fn erase_regions_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
22     // NB: use `super_fold_with` here. If we used `fold_with`, it
23     // could invoke the `erase_regions_ty` query recursively.
24     ty.super_fold_with(&mut RegionEraserVisitor { tcx })
25 }
26
27 impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
28     /// Returns an equivalent value with all free regions removed (note
29     /// that late-bound regions remain, because they are important for
30     /// subtyping, but they are anonymized and normalized as well)..
31     pub fn erase_regions<T>(self, value: &T) -> T
32         where T : TypeFoldable<'tcx>
33     {
34         let value1 = value.fold_with(&mut RegionEraserVisitor { tcx: self });
35         debug!("erase_regions({:?}) = {:?}", value, value1);
36         value1
37     }
38 }
39
40 struct RegionEraserVisitor<'a, 'gcx: 'tcx, 'tcx: 'a> {
41     tcx: TyCtxt<'a, 'gcx, 'tcx>,
42 }
43
44 impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for RegionEraserVisitor<'a, 'gcx, 'tcx> {
45     fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> {
46         self.tcx
47     }
48
49     fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
50         if let Some(ty_lifted) = self.tcx.lift_to_global(&ty) {
51             self.tcx.erase_regions_ty(ty_lifted)
52         } else {
53             ty.super_fold_with(self)
54         }
55     }
56
57     fn fold_binder<T>(&mut self, t: &ty::Binder<T>) -> ty::Binder<T>
58         where T : TypeFoldable<'tcx>
59     {
60         let u = self.tcx.anonymize_late_bound_regions(t);
61         u.super_fold_with(self)
62     }
63
64     fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
65         // because late-bound regions affect subtyping, we can't
66         // erase the bound/free distinction, but we can replace
67         // all free regions with 'erased.
68         //
69         // Note that we *CAN* replace early-bound regions -- the
70         // type system never "sees" those, they get substituted
71         // away. In codegen, they will always be erased to 'erased
72         // whenever a substitution occurs.
73         match *r {
74             ty::ReLateBound(..) => r,
75             _ => self.tcx.types.re_erased
76         }
77     }
78 }