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.
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.
11 use ty::{self, Ty, TyCtxt};
12 use ty::fold::{TypeFolder, TypeFoldable};
14 pub(super) fn provide(providers: &mut ty::maps::Providers) {
15 *providers = ty::maps::Providers {
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 })
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>
34 let value1 = value.fold_with(&mut RegionEraserVisitor { tcx: self });
35 debug!("erase_regions({:?}) = {:?}", value, value1);
40 struct RegionEraserVisitor<'a, 'gcx: 'tcx, 'tcx: 'a> {
41 tcx: TyCtxt<'a, 'gcx, 'tcx>,
44 impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for RegionEraserVisitor<'a, 'gcx, 'tcx> {
45 fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> {
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)
53 ty.super_fold_with(self)
57 fn fold_binder<T>(&mut self, t: &ty::Binder<T>) -> ty::Binder<T>
58 where T : TypeFoldable<'tcx>
60 let u = self.tcx.anonymize_late_bound_regions(t);
61 u.super_fold_with(self)
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.
69 // Note that we *CAN* replace early-bound regions -- the
70 // type system never "sees" those, they get substituted
71 // away. In trans, they will always be erased to 'erased
72 // whenever a substitution occurs.
74 ty::ReLateBound(..) => r,
75 _ => self.tcx.types.re_erased