2013-10-29 04:25:18 -05:00
|
|
|
// Copyright 2012-2013 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.
|
|
|
|
|
2014-11-25 20:17:11 -06:00
|
|
|
//! Generalized type folding mechanism. The setup is a bit convoluted
|
|
|
|
//! but allows for convenient usage. Let T be an instance of some
|
|
|
|
//! "foldable type" (one which implements `TypeFoldable`) and F be an
|
|
|
|
//! instance of a "folder" (a type which implements `TypeFolder`). Then
|
|
|
|
//! the setup is intended to be:
|
|
|
|
//!
|
|
|
|
//! T.fold_with(F) --calls--> F.fold_T(T) --calls--> super_fold_T(F, T)
|
|
|
|
//!
|
|
|
|
//! This way, when you define a new folder F, you can override
|
|
|
|
//! `fold_T()` to customize the behavior, and invoke `super_fold_T()`
|
|
|
|
//! to get the original behavior. Meanwhile, to actually fold
|
|
|
|
//! something, you can just write `T.fold_with(F)`, which is
|
|
|
|
//! convenient. (Note that `fold_with` will also transparently handle
|
|
|
|
//! things like a `Vec<T>` where T is foldable and so on.)
|
|
|
|
//!
|
|
|
|
//! In this ideal setup, the only function that actually *does*
|
|
|
|
//! anything is `super_fold_T`, which traverses the type `T`. Moreover,
|
|
|
|
//! `super_fold_T` should only ever call `T.fold_with()`.
|
|
|
|
//!
|
|
|
|
//! In some cases, we follow a degenerate pattern where we do not have
|
|
|
|
//! a `fold_T` nor `super_fold_T` method. Instead, `T.fold_with`
|
|
|
|
//! traverses the structure directly. This is suboptimal because the
|
2015-01-06 19:53:18 -06:00
|
|
|
//! behavior cannot be overridden, but it's much less work to implement.
|
2014-11-25 20:17:11 -06:00
|
|
|
//! If you ever *do* need an override that doesn't exist, it's not hard
|
|
|
|
//! to convert the degenerate pattern into the proper thing.
|
2013-10-29 04:25:18 -05:00
|
|
|
|
2014-05-13 10:35:42 -05:00
|
|
|
use middle::subst;
|
2014-05-31 17:53:13 -05:00
|
|
|
use middle::subst::VecPerParamSpace;
|
2015-01-03 21:42:21 -06:00
|
|
|
use middle::ty::{self, Ty};
|
2014-09-12 09:53:35 -05:00
|
|
|
use middle::traits;
|
2015-06-18 00:51:23 -05:00
|
|
|
|
|
|
|
use std::fmt;
|
2014-05-12 16:12:51 -05:00
|
|
|
use std::rc::Rc;
|
2015-03-22 14:11:56 -05:00
|
|
|
use syntax::abi;
|
|
|
|
use syntax::ast;
|
2014-05-12 16:12:51 -05:00
|
|
|
use syntax::owned_slice::OwnedSlice;
|
2015-06-05 18:06:14 -05:00
|
|
|
use util::nodemap::FnvHashMap;
|
2013-10-29 04:25:18 -05:00
|
|
|
|
2014-05-12 16:12:51 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Two generic traits
|
|
|
|
|
|
|
|
/// The TypeFoldable trait is implemented for every type that can be folded.
|
|
|
|
/// Basically, every type that has a corresponding method in TypeFolder.
|
2015-06-18 00:51:23 -05:00
|
|
|
pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
|
2014-09-29 14:11:30 -05:00
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self;
|
2014-05-12 16:12:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The TypeFolder trait defines the actual *folding*. There is a
|
|
|
|
/// method defined for every foldable type. Each of these has a
|
|
|
|
/// default implementation that does an "identity" fold. Within each
|
|
|
|
/// identity fold, it should invoke `foo.fold_with(self)` to fold each
|
|
|
|
/// sub-item.
|
2014-12-18 14:27:41 -06:00
|
|
|
pub trait TypeFolder<'tcx> : Sized {
|
2014-04-22 07:56:37 -05:00
|
|
|
fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx>;
|
2013-10-29 04:25:18 -05:00
|
|
|
|
2014-11-15 15:47:59 -06:00
|
|
|
/// Invoked by the `super_*` routines when we enter a region
|
|
|
|
/// binding level (for example, when entering a function
|
|
|
|
/// signature). This is used by clients that want to track the
|
|
|
|
/// Debruijn index nesting level.
|
|
|
|
fn enter_region_binder(&mut self) { }
|
|
|
|
|
|
|
|
/// Invoked by the `super_*` routines when we exit a region
|
|
|
|
/// binding level. This is used by clients that want to
|
|
|
|
/// track the Debruijn index nesting level.
|
|
|
|
fn exit_region_binder(&mut self) { }
|
|
|
|
|
2015-01-04 05:07:36 -06:00
|
|
|
fn fold_binder<T>(&mut self, t: &ty::Binder<T>) -> ty::Binder<T>
|
2015-06-18 00:51:23 -05:00
|
|
|
where T : TypeFoldable<'tcx>
|
2015-01-04 05:07:36 -06:00
|
|
|
{
|
2015-01-04 11:00:13 -06:00
|
|
|
// FIXME(#20526) this should replace `enter_region_binder`/`exit_region_binder`.
|
2015-01-04 05:07:36 -06:00
|
|
|
super_fold_binder(self, t)
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
|
2013-10-29 04:25:18 -05:00
|
|
|
super_fold_ty(self, t)
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
fn fold_mt(&mut self, t: &ty::mt<'tcx>) -> ty::mt<'tcx> {
|
2013-10-29 04:25:18 -05:00
|
|
|
super_fold_mt(self, t)
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
fn fold_trait_ref(&mut self, t: &ty::TraitRef<'tcx>) -> ty::TraitRef<'tcx> {
|
2013-10-29 04:25:18 -05:00
|
|
|
super_fold_trait_ref(self, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_substs(&mut self,
|
2014-09-29 14:11:30 -05:00
|
|
|
substs: &subst::Substs<'tcx>)
|
|
|
|
-> subst::Substs<'tcx> {
|
2013-10-29 04:25:18 -05:00
|
|
|
super_fold_substs(self, substs)
|
|
|
|
}
|
|
|
|
|
2014-11-15 15:47:59 -06:00
|
|
|
fn fold_fn_sig(&mut self,
|
2014-12-12 10:28:35 -06:00
|
|
|
sig: &ty::FnSig<'tcx>)
|
|
|
|
-> ty::FnSig<'tcx> {
|
2014-11-15 15:47:59 -06:00
|
|
|
super_fold_fn_sig(self, sig)
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
|
|
|
|
2014-10-24 14:14:37 -05:00
|
|
|
fn fold_output(&mut self,
|
2014-09-29 14:11:30 -05:00
|
|
|
output: &ty::FnOutput<'tcx>)
|
|
|
|
-> ty::FnOutput<'tcx> {
|
2014-10-24 14:14:37 -05:00
|
|
|
super_fold_output(self, output)
|
|
|
|
}
|
|
|
|
|
2013-10-29 04:25:18 -05:00
|
|
|
fn fold_bare_fn_ty(&mut self,
|
2014-09-29 14:11:30 -05:00
|
|
|
fty: &ty::BareFnTy<'tcx>)
|
|
|
|
-> ty::BareFnTy<'tcx>
|
2014-05-12 16:12:51 -05:00
|
|
|
{
|
|
|
|
super_fold_bare_fn_ty(self, fty)
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_closure_ty(&mut self,
|
2014-09-29 14:11:30 -05:00
|
|
|
fty: &ty::ClosureTy<'tcx>)
|
|
|
|
-> ty::ClosureTy<'tcx> {
|
2014-05-12 16:12:51 -05:00
|
|
|
super_fold_closure_ty(self, fty)
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_region(&mut self, r: ty::Region) -> ty::Region {
|
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2014-12-26 03:36:04 -06:00
|
|
|
fn fold_existential_bounds(&mut self, s: &ty::ExistentialBounds<'tcx>)
|
|
|
|
-> ty::ExistentialBounds<'tcx> {
|
2014-08-27 20:46:52 -05:00
|
|
|
super_fold_existential_bounds(self, s)
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
fn fold_autoref(&mut self, ar: &ty::AutoRef<'tcx>) -> ty::AutoRef<'tcx> {
|
2014-05-06 14:16:11 -05:00
|
|
|
super_fold_autoref(self, ar)
|
|
|
|
}
|
2014-05-12 16:12:51 -05:00
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
fn fold_item_substs(&mut self, i: ty::ItemSubsts<'tcx>) -> ty::ItemSubsts<'tcx> {
|
2014-05-12 16:12:51 -05:00
|
|
|
super_fold_item_substs(self, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// TypeFoldable implementations.
|
|
|
|
//
|
|
|
|
// Ideally, each type should invoke `folder.fold_foo(self)` and
|
|
|
|
// nothing else. In some cases, though, we haven't gotten around to
|
|
|
|
// adding methods on the `folder` yet, and thus the folding is
|
|
|
|
// hard-coded here. This is less-flexible, because folders cannot
|
|
|
|
// override the behavior, but there are a lot of random types and one
|
|
|
|
// can easily refactor the folding into the TypeFolder trait as
|
|
|
|
// needed.
|
|
|
|
|
2015-03-22 14:11:56 -05:00
|
|
|
macro_rules! CopyImpls {
|
|
|
|
($($ty:ty),+) => {
|
|
|
|
$(
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for $ty {
|
|
|
|
fn fold_with<F:TypeFolder<'tcx>>(&self, _: &mut F) -> $ty {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)+
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-22 14:11:56 -05:00
|
|
|
CopyImpls! { (), ast::Unsafety, abi::Abi }
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx, T:TypeFoldable<'tcx>, U:TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
|
|
|
|
fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> (T, U) {
|
2014-11-15 15:47:59 -06:00
|
|
|
(self.0.fold_with(folder), self.1.fold_with(folder))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Option<T> {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Option<T> {
|
2014-05-12 16:12:51 -05:00
|
|
|
self.as_ref().map(|t| t.fold_with(folder))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Rc<T> {
|
2014-05-12 16:12:51 -05:00
|
|
|
Rc::new((**self).fold_with(folder))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-17 13:16:28 -06:00
|
|
|
impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Box<T> {
|
|
|
|
let content: T = (**self).fold_with(folder);
|
|
|
|
box content
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Vec<T> {
|
2014-05-12 16:12:51 -05:00
|
|
|
self.iter().map(|t| t.fold_with(folder)).collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 00:51:23 -05:00
|
|
|
impl<'tcx, T:TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<T> {
|
2014-09-29 14:11:30 -05:00
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::Binder<T> {
|
2015-01-04 05:07:36 -06:00
|
|
|
folder.fold_binder(self)
|
2014-11-15 15:47:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for OwnedSlice<T> {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> OwnedSlice<T> {
|
2014-05-12 16:12:51 -05:00
|
|
|
self.iter().map(|t| t.fold_with(folder)).collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for VecPerParamSpace<T> {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> VecPerParamSpace<T> {
|
2014-11-15 15:47:59 -06:00
|
|
|
|
|
|
|
// Things in the Fn space take place under an additional level
|
|
|
|
// of region binding relative to the other spaces. This is
|
|
|
|
// because those entries are attached to a method, and methods
|
|
|
|
// always introduce a level of region binding.
|
|
|
|
|
|
|
|
let result = self.map_enumerated(|(space, index, elem)| {
|
|
|
|
if space == subst::FnSpace && index == 0 {
|
|
|
|
// enter new level when/if we reach the first thing in fn space
|
|
|
|
folder.enter_region_binder();
|
|
|
|
}
|
|
|
|
elem.fold_with(folder)
|
|
|
|
});
|
|
|
|
if result.len(subst::FnSpace) > 0 {
|
|
|
|
// if there was anything in fn space, exit the region binding level
|
|
|
|
folder.exit_region_binder();
|
|
|
|
}
|
|
|
|
result
|
2014-05-31 17:53:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Ty<'tcx> {
|
2014-05-12 16:12:51 -05:00
|
|
|
folder.fold_ty(*self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx> TypeFoldable<'tcx> for ty::BareFnTy<'tcx> {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::BareFnTy<'tcx> {
|
2014-05-12 16:12:51 -05:00
|
|
|
folder.fold_bare_fn_ty(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx> TypeFoldable<'tcx> for ty::ClosureTy<'tcx> {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::ClosureTy<'tcx> {
|
2014-05-12 16:12:51 -05:00
|
|
|
folder.fold_closure_ty(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx> TypeFoldable<'tcx> for ty::mt<'tcx> {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::mt<'tcx> {
|
2014-05-12 16:12:51 -05:00
|
|
|
folder.fold_mt(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx> TypeFoldable<'tcx> for ty::FnOutput<'tcx> {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::FnOutput<'tcx> {
|
2014-10-24 14:14:37 -05:00
|
|
|
folder.fold_output(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx> TypeFoldable<'tcx> for ty::FnSig<'tcx> {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::FnSig<'tcx> {
|
2014-11-15 15:47:59 -06:00
|
|
|
folder.fold_fn_sig(self)
|
2014-05-12 16:12:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx> TypeFoldable<'tcx> for ty::TraitRef<'tcx> {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::TraitRef<'tcx> {
|
2014-05-12 16:12:51 -05:00
|
|
|
folder.fold_trait_ref(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-07 12:53:54 -06:00
|
|
|
impl<'tcx> TypeFoldable<'tcx> for ty::field<'tcx> {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::field<'tcx> {
|
|
|
|
ty::field {
|
|
|
|
name: self.name,
|
|
|
|
mt: self.mt.fold_with(folder),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx> TypeFoldable<'tcx> for ty::Region {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::Region {
|
2014-05-12 16:12:51 -05:00
|
|
|
folder.fold_region(*self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx> TypeFoldable<'tcx> for subst::Substs<'tcx> {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> subst::Substs<'tcx> {
|
2014-05-12 16:12:51 -05:00
|
|
|
folder.fold_substs(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx> TypeFoldable<'tcx> for ty::ItemSubsts<'tcx> {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::ItemSubsts<'tcx> {
|
2014-05-12 16:12:51 -05:00
|
|
|
ty::ItemSubsts {
|
|
|
|
substs: self.substs.fold_with(folder),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx> TypeFoldable<'tcx> for ty::AutoRef<'tcx> {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::AutoRef<'tcx> {
|
2014-05-12 16:12:51 -05:00
|
|
|
folder.fold_autoref(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-25 13:21:20 -06:00
|
|
|
impl<'tcx> TypeFoldable<'tcx> for ty::MethodOrigin<'tcx> {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::MethodOrigin<'tcx> {
|
2014-09-30 15:53:03 -05:00
|
|
|
match *self {
|
2014-11-25 13:21:20 -06:00
|
|
|
ty::MethodStatic(def_id) => {
|
|
|
|
ty::MethodStatic(def_id)
|
2014-09-30 15:53:03 -05:00
|
|
|
}
|
2015-01-24 14:00:03 -06:00
|
|
|
ty::MethodStaticClosure(def_id) => {
|
|
|
|
ty::MethodStaticClosure(def_id)
|
2014-09-30 15:53:03 -05:00
|
|
|
}
|
2014-11-25 13:21:20 -06:00
|
|
|
ty::MethodTypeParam(ref param) => {
|
|
|
|
ty::MethodTypeParam(ty::MethodParam {
|
2014-09-30 15:53:03 -05:00
|
|
|
trait_ref: param.trait_ref.fold_with(folder),
|
2014-12-31 10:03:14 -06:00
|
|
|
method_num: param.method_num,
|
|
|
|
impl_def_id: param.impl_def_id,
|
2014-09-30 15:53:03 -05:00
|
|
|
})
|
|
|
|
}
|
2014-11-25 13:21:20 -06:00
|
|
|
ty::MethodTraitObject(ref object) => {
|
|
|
|
ty::MethodTraitObject(ty::MethodObject {
|
2014-09-30 15:53:03 -05:00
|
|
|
trait_ref: object.trait_ref.fold_with(folder),
|
|
|
|
object_trait_id: object.object_trait_id,
|
|
|
|
method_num: object.method_num,
|
2015-01-11 14:18:06 -06:00
|
|
|
vtable_index: object.vtable_index,
|
2014-09-30 15:53:03 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx> TypeFoldable<'tcx> for ty::BuiltinBounds {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> ty::BuiltinBounds {
|
2014-05-12 16:12:51 -05:00
|
|
|
*self
|
|
|
|
}
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
|
|
|
|
2014-12-26 03:36:04 -06:00
|
|
|
impl<'tcx> TypeFoldable<'tcx> for ty::ExistentialBounds<'tcx> {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::ExistentialBounds<'tcx> {
|
|
|
|
folder.fold_existential_bounds(self)
|
2014-08-27 20:46:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx> TypeFoldable<'tcx> for ty::TypeParameterDef<'tcx> {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::TypeParameterDef<'tcx> {
|
2014-05-12 16:12:51 -05:00
|
|
|
ty::TypeParameterDef {
|
2014-09-30 19:11:34 -05:00
|
|
|
name: self.name,
|
2014-05-12 16:12:51 -05:00
|
|
|
def_id: self.def_id,
|
2014-05-31 17:53:13 -05:00
|
|
|
space: self.space,
|
|
|
|
index: self.index,
|
2014-05-12 16:12:51 -05:00
|
|
|
default: self.default.fold_with(folder),
|
2015-02-11 15:37:36 -06:00
|
|
|
object_lifetime_default: self.object_lifetime_default.fold_with(folder),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for ty::ObjectLifetimeDefault {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::ObjectLifetimeDefault {
|
|
|
|
match *self {
|
|
|
|
ty::ObjectLifetimeDefault::Ambiguous =>
|
|
|
|
ty::ObjectLifetimeDefault::Ambiguous,
|
|
|
|
|
|
|
|
ty::ObjectLifetimeDefault::Specific(r) =>
|
|
|
|
ty::ObjectLifetimeDefault::Specific(r.fold_with(folder)),
|
2014-05-12 16:12:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx> TypeFoldable<'tcx> for ty::RegionParameterDef {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::RegionParameterDef {
|
2014-08-27 20:46:52 -05:00
|
|
|
ty::RegionParameterDef {
|
|
|
|
name: self.name,
|
|
|
|
def_id: self.def_id,
|
|
|
|
space: self.space,
|
|
|
|
index: self.index,
|
|
|
|
bounds: self.bounds.fold_with(folder)
|
|
|
|
}
|
2014-05-12 16:12:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx> TypeFoldable<'tcx> for ty::Generics<'tcx> {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::Generics<'tcx> {
|
2014-05-12 16:12:51 -05:00
|
|
|
ty::Generics {
|
2014-05-31 17:53:13 -05:00
|
|
|
types: self.types.fold_with(folder),
|
|
|
|
regions: self.regions.fold_with(folder),
|
2015-02-11 09:28:52 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for ty::GenericPredicates<'tcx> {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::GenericPredicates<'tcx> {
|
|
|
|
ty::GenericPredicates {
|
2014-12-05 10:04:49 -06:00
|
|
|
predicates: self.predicates.fold_with(folder),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::Predicate<'tcx> {
|
|
|
|
match *self {
|
|
|
|
ty::Predicate::Trait(ref a) =>
|
|
|
|
ty::Predicate::Trait(a.fold_with(folder)),
|
2014-12-13 04:34:34 -06:00
|
|
|
ty::Predicate::Equate(ref binder) =>
|
|
|
|
ty::Predicate::Equate(binder.fold_with(folder)),
|
|
|
|
ty::Predicate::RegionOutlives(ref binder) =>
|
|
|
|
ty::Predicate::RegionOutlives(binder.fold_with(folder)),
|
|
|
|
ty::Predicate::TypeOutlives(ref binder) =>
|
|
|
|
ty::Predicate::TypeOutlives(binder.fold_with(folder)),
|
2014-12-17 13:16:28 -06:00
|
|
|
ty::Predicate::Projection(ref binder) =>
|
|
|
|
ty::Predicate::Projection(binder.fold_with(folder)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for ty::ProjectionPredicate<'tcx> {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::ProjectionPredicate<'tcx> {
|
|
|
|
ty::ProjectionPredicate {
|
|
|
|
projection_ty: self.projection_ty.fold_with(folder),
|
|
|
|
ty: self.ty.fold_with(folder),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for ty::ProjectionTy<'tcx> {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::ProjectionTy<'tcx> {
|
|
|
|
ty::ProjectionTy {
|
|
|
|
trait_ref: self.trait_ref.fold_with(folder),
|
|
|
|
item_name: self.item_name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-11 09:28:52 -06:00
|
|
|
impl<'tcx> TypeFoldable<'tcx> for ty::InstantiatedPredicates<'tcx> {
|
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::InstantiatedPredicates<'tcx> {
|
|
|
|
ty::InstantiatedPredicates {
|
2014-12-07 10:10:48 -06:00
|
|
|
predicates: self.predicates.fold_with(folder),
|
2014-11-15 16:25:05 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-04 23:03:03 -06:00
|
|
|
impl<'tcx,O> TypeFoldable<'tcx> for traits::Obligation<'tcx,O>
|
|
|
|
where O : TypeFoldable<'tcx>
|
|
|
|
{
|
|
|
|
fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> traits::Obligation<'tcx, O> {
|
|
|
|
traits::Obligation {
|
2014-12-06 10:39:25 -06:00
|
|
|
cause: self.cause.clone(),
|
2014-12-04 23:03:03 -06:00
|
|
|
recursion_depth: self.recursion_depth,
|
2014-12-17 15:00:34 -06:00
|
|
|
predicate: self.predicate.fold_with(folder),
|
2014-12-04 23:03:03 -06:00
|
|
|
}
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx, N: TypeFoldable<'tcx>> TypeFoldable<'tcx> for traits::VtableImplData<'tcx, N> {
|
|
|
|
fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> traits::VtableImplData<'tcx, N> {
|
2014-09-11 00:07:49 -05:00
|
|
|
traits::VtableImplData {
|
2014-09-12 09:53:35 -05:00
|
|
|
impl_def_id: self.impl_def_id,
|
|
|
|
substs: self.substs.fold_with(folder),
|
|
|
|
nested: self.nested.fold_with(folder),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-09 16:09:37 -05:00
|
|
|
impl<'tcx, N: TypeFoldable<'tcx>> TypeFoldable<'tcx> for traits::VtableClosureData<'tcx, N> {
|
|
|
|
fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> traits::VtableClosureData<'tcx, N> {
|
|
|
|
traits::VtableClosureData {
|
|
|
|
closure_def_id: self.closure_def_id,
|
|
|
|
substs: self.substs.fold_with(folder),
|
|
|
|
nested: self.nested.fold_with(folder),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-07 07:24:34 -06:00
|
|
|
impl<'tcx, N: TypeFoldable<'tcx>> TypeFoldable<'tcx> for traits::VtableDefaultImplData<N> {
|
|
|
|
fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> traits::VtableDefaultImplData<N> {
|
|
|
|
traits::VtableDefaultImplData {
|
2015-02-02 05:14:01 -06:00
|
|
|
trait_def_id: self.trait_def_id,
|
|
|
|
nested: self.nested.fold_with(folder),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx, N: TypeFoldable<'tcx>> TypeFoldable<'tcx> for traits::VtableBuiltinData<N> {
|
|
|
|
fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> traits::VtableBuiltinData<N> {
|
2014-10-09 16:19:50 -05:00
|
|
|
traits::VtableBuiltinData {
|
|
|
|
nested: self.nested.fold_with(folder),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx, N: TypeFoldable<'tcx>> TypeFoldable<'tcx> for traits::Vtable<'tcx, N> {
|
|
|
|
fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> traits::Vtable<'tcx, N> {
|
2014-09-12 09:53:35 -05:00
|
|
|
match *self {
|
|
|
|
traits::VtableImpl(ref v) => traits::VtableImpl(v.fold_with(folder)),
|
2015-02-07 07:24:34 -06:00
|
|
|
traits::VtableDefaultImpl(ref t) => traits::VtableDefaultImpl(t.fold_with(folder)),
|
2015-06-09 16:09:37 -05:00
|
|
|
traits::VtableClosure(ref d) => {
|
|
|
|
traits::VtableClosure(d.fold_with(folder))
|
2014-11-06 01:50:10 -06:00
|
|
|
}
|
2014-12-01 08:23:40 -06:00
|
|
|
traits::VtableFnPointer(ref d) => {
|
|
|
|
traits::VtableFnPointer(d.fold_with(folder))
|
|
|
|
}
|
2015-01-08 20:41:42 -06:00
|
|
|
traits::VtableParam(ref n) => traits::VtableParam(n.fold_with(folder)),
|
2014-10-09 16:19:50 -05:00
|
|
|
traits::VtableBuiltin(ref d) => traits::VtableBuiltin(d.fold_with(folder)),
|
2014-12-23 04:26:34 -06:00
|
|
|
traits::VtableObject(ref d) => traits::VtableObject(d.fold_with(folder)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for traits::VtableObjectData<'tcx> {
|
|
|
|
fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> traits::VtableObjectData<'tcx> {
|
|
|
|
traits::VtableObjectData {
|
2015-03-03 07:01:13 -06:00
|
|
|
object_ty: self.object_ty.fold_with(folder),
|
|
|
|
upcast_trait_ref: self.upcast_trait_ref.fold_with(folder),
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-13 04:34:34 -06:00
|
|
|
impl<'tcx> TypeFoldable<'tcx> for ty::EquatePredicate<'tcx> {
|
|
|
|
fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::EquatePredicate<'tcx> {
|
|
|
|
ty::EquatePredicate(self.0.fold_with(folder),
|
|
|
|
self.1.fold_with(folder))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-17 13:16:28 -06:00
|
|
|
impl<'tcx> TypeFoldable<'tcx> for ty::TraitPredicate<'tcx> {
|
|
|
|
fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::TraitPredicate<'tcx> {
|
|
|
|
ty::TraitPredicate {
|
|
|
|
trait_ref: self.trait_ref.fold_with(folder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-13 04:34:34 -06:00
|
|
|
impl<'tcx,T,U> TypeFoldable<'tcx> for ty::OutlivesPredicate<T,U>
|
|
|
|
where T : TypeFoldable<'tcx>,
|
|
|
|
U : TypeFoldable<'tcx>,
|
|
|
|
{
|
|
|
|
fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::OutlivesPredicate<T,U> {
|
|
|
|
ty::OutlivesPredicate(self.0.fold_with(folder),
|
|
|
|
self.1.fold_with(folder))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-24 14:00:03 -06:00
|
|
|
impl<'tcx> TypeFoldable<'tcx> for ty::ClosureUpvar<'tcx> {
|
|
|
|
fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::ClosureUpvar<'tcx> {
|
|
|
|
ty::ClosureUpvar {
|
2015-01-01 16:58:57 -06:00
|
|
|
def: self.def,
|
|
|
|
span: self.span,
|
|
|
|
ty: self.ty.fold_with(folder),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-14 15:43:17 -06:00
|
|
|
impl<'a, 'tcx> TypeFoldable<'tcx> for ty::ParameterEnvironment<'a, 'tcx> where 'tcx: 'a {
|
|
|
|
fn fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::ParameterEnvironment<'a, 'tcx> {
|
|
|
|
ty::ParameterEnvironment {
|
|
|
|
tcx: self.tcx,
|
|
|
|
free_substs: self.free_substs.fold_with(folder),
|
|
|
|
implicit_region_bound: self.implicit_region_bound.fold_with(folder),
|
|
|
|
caller_bounds: self.caller_bounds.fold_with(folder),
|
|
|
|
selection_cache: traits::SelectionCache::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-12 16:12:51 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// "super" routines: these are the default implementations for TypeFolder.
|
|
|
|
//
|
|
|
|
// They should invoke `foo.fold_with()` to do recursive folding.
|
2014-11-15 16:09:51 -06:00
|
|
|
|
2015-01-04 05:07:36 -06:00
|
|
|
pub fn super_fold_binder<'tcx, T, U>(this: &mut T,
|
|
|
|
binder: &ty::Binder<U>)
|
|
|
|
-> ty::Binder<U>
|
|
|
|
where T : TypeFolder<'tcx>, U : TypeFoldable<'tcx>
|
|
|
|
{
|
|
|
|
this.enter_region_binder();
|
|
|
|
let result = ty::Binder(binder.0.fold_with(this));
|
|
|
|
this.exit_region_binder();
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
pub fn super_fold_ty<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
|
2014-12-16 10:38:06 -06:00
|
|
|
ty: Ty<'tcx>)
|
2014-09-29 14:11:30 -05:00
|
|
|
-> Ty<'tcx> {
|
2014-12-16 10:38:06 -06:00
|
|
|
let sty = match ty.sty {
|
2015-06-11 18:21:46 -05:00
|
|
|
ty::TyBox(typ) => {
|
|
|
|
ty::TyBox(typ.fold_with(this))
|
2014-12-16 10:38:06 -06:00
|
|
|
}
|
2015-06-11 18:21:46 -05:00
|
|
|
ty::TyRawPtr(ref tm) => {
|
|
|
|
ty::TyRawPtr(tm.fold_with(this))
|
2014-12-16 10:38:06 -06:00
|
|
|
}
|
2015-06-11 18:21:46 -05:00
|
|
|
ty::TyArray(typ, sz) => {
|
|
|
|
ty::TyArray(typ.fold_with(this), sz)
|
2014-12-16 10:38:06 -06:00
|
|
|
}
|
2015-06-12 18:50:13 -05:00
|
|
|
ty::TySlice(typ) => {
|
|
|
|
ty::TySlice(typ.fold_with(this))
|
|
|
|
}
|
2015-06-11 18:21:46 -05:00
|
|
|
ty::TyEnum(tid, ref substs) => {
|
2014-12-26 05:33:56 -06:00
|
|
|
let substs = substs.fold_with(this);
|
2015-06-11 18:21:46 -05:00
|
|
|
ty::TyEnum(tid, this.tcx().mk_substs(substs))
|
2014-12-16 10:38:06 -06:00
|
|
|
}
|
2015-06-11 18:21:46 -05:00
|
|
|
ty::TyTrait(box ty::TraitTy { ref principal, ref bounds }) => {
|
|
|
|
ty::TyTrait(box ty::TraitTy {
|
2014-12-26 03:36:04 -06:00
|
|
|
principal: principal.fold_with(this),
|
2014-12-16 10:38:06 -06:00
|
|
|
bounds: bounds.fold_with(this),
|
|
|
|
})
|
|
|
|
}
|
2015-06-11 18:21:46 -05:00
|
|
|
ty::TyTuple(ref ts) => {
|
|
|
|
ty::TyTuple(ts.fold_with(this))
|
2014-12-16 10:38:06 -06:00
|
|
|
}
|
2015-06-11 18:21:46 -05:00
|
|
|
ty::TyBareFn(opt_def_id, ref f) => {
|
2014-12-26 05:33:56 -06:00
|
|
|
let bfn = f.fold_with(this);
|
2015-06-11 18:21:46 -05:00
|
|
|
ty::TyBareFn(opt_def_id, this.tcx().mk_bare_fn(bfn))
|
2014-12-16 10:38:06 -06:00
|
|
|
}
|
2015-06-11 18:21:46 -05:00
|
|
|
ty::TyRef(r, ref tm) => {
|
2014-12-26 05:33:56 -06:00
|
|
|
let r = r.fold_with(this);
|
2015-06-11 18:21:46 -05:00
|
|
|
ty::TyRef(this.tcx().mk_region(r), tm.fold_with(this))
|
2014-12-16 10:38:06 -06:00
|
|
|
}
|
2015-06-11 18:21:46 -05:00
|
|
|
ty::TyStruct(did, ref substs) => {
|
2014-12-26 05:33:56 -06:00
|
|
|
let substs = substs.fold_with(this);
|
2015-06-11 18:21:46 -05:00
|
|
|
ty::TyStruct(did, this.tcx().mk_substs(substs))
|
2014-12-16 10:38:06 -06:00
|
|
|
}
|
2015-06-11 18:21:46 -05:00
|
|
|
ty::TyClosure(did, ref substs) => {
|
2014-12-26 05:33:56 -06:00
|
|
|
let s = substs.fold_with(this);
|
2015-06-11 18:21:46 -05:00
|
|
|
ty::TyClosure(did, this.tcx().mk_substs(s))
|
2014-12-16 10:38:06 -06:00
|
|
|
}
|
2015-06-11 18:21:46 -05:00
|
|
|
ty::TyProjection(ref data) => {
|
|
|
|
ty::TyProjection(data.fold_with(this))
|
2014-12-17 13:16:28 -06:00
|
|
|
}
|
2015-06-11 18:21:46 -05:00
|
|
|
ty::TyBool | ty::TyChar | ty::TyStr |
|
|
|
|
ty::TyInt(_) | ty::TyUint(_) | ty::TyFloat(_) |
|
|
|
|
ty::TyError | ty::TyInfer(_) |
|
|
|
|
ty::TyParam(..) => {
|
2014-12-16 10:38:06 -06:00
|
|
|
ty.sty.clone()
|
|
|
|
}
|
|
|
|
};
|
2014-03-05 21:07:47 -06:00
|
|
|
ty::mk_t(this.tcx(), sty)
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
pub fn super_fold_substs<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
|
2014-09-29 14:11:30 -05:00
|
|
|
substs: &subst::Substs<'tcx>)
|
|
|
|
-> subst::Substs<'tcx> {
|
2013-10-29 04:25:18 -05:00
|
|
|
let regions = match substs.regions {
|
2014-05-13 10:35:42 -05:00
|
|
|
subst::ErasedRegions => {
|
|
|
|
subst::ErasedRegions
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
2014-05-13 10:35:42 -05:00
|
|
|
subst::NonerasedRegions(ref regions) => {
|
|
|
|
subst::NonerasedRegions(regions.fold_with(this))
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-05-13 10:35:42 -05:00
|
|
|
subst::Substs { regions: regions,
|
2014-05-31 17:53:13 -05:00
|
|
|
types: substs.types.fold_with(this) }
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
|
|
|
|
2014-11-15 15:47:59 -06:00
|
|
|
pub fn super_fold_fn_sig<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
|
2014-09-29 14:11:30 -05:00
|
|
|
sig: &ty::FnSig<'tcx>)
|
|
|
|
-> ty::FnSig<'tcx>
|
2014-11-15 15:47:59 -06:00
|
|
|
{
|
|
|
|
ty::FnSig { inputs: sig.inputs.fold_with(this),
|
2014-05-12 16:12:51 -05:00
|
|
|
output: sig.output.fold_with(this),
|
2013-10-29 04:25:18 -05:00
|
|
|
variadic: sig.variadic }
|
|
|
|
}
|
|
|
|
|
2014-10-24 14:14:37 -05:00
|
|
|
pub fn super_fold_output<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
|
2014-09-29 14:11:30 -05:00
|
|
|
output: &ty::FnOutput<'tcx>)
|
|
|
|
-> ty::FnOutput<'tcx> {
|
2014-10-24 14:14:37 -05:00
|
|
|
match *output {
|
|
|
|
ty::FnConverging(ref ty) => ty::FnConverging(ty.fold_with(this)),
|
|
|
|
ty::FnDiverging => ty::FnDiverging
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
pub fn super_fold_bare_fn_ty<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
|
2014-09-29 14:11:30 -05:00
|
|
|
fty: &ty::BareFnTy<'tcx>)
|
|
|
|
-> ty::BareFnTy<'tcx>
|
2014-05-12 16:12:51 -05:00
|
|
|
{
|
|
|
|
ty::BareFnTy { sig: fty.sig.fold_with(this),
|
|
|
|
abi: fty.abi,
|
2014-12-09 09:36:46 -06:00
|
|
|
unsafety: fty.unsafety }
|
2014-05-12 16:12:51 -05:00
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
pub fn super_fold_closure_ty<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
|
2014-09-29 14:11:30 -05:00
|
|
|
fty: &ty::ClosureTy<'tcx>)
|
|
|
|
-> ty::ClosureTy<'tcx>
|
2014-05-12 16:12:51 -05:00
|
|
|
{
|
|
|
|
ty::ClosureTy {
|
|
|
|
sig: fty.sig.fold_with(this),
|
2014-12-09 09:36:46 -06:00
|
|
|
unsafety: fty.unsafety,
|
2014-05-29 00:26:56 -05:00
|
|
|
abi: fty.abi,
|
2014-05-12 16:12:51 -05:00
|
|
|
}
|
|
|
|
}
|
2014-11-15 16:09:51 -06:00
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
pub fn super_fold_trait_ref<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
|
2014-09-29 14:11:30 -05:00
|
|
|
t: &ty::TraitRef<'tcx>)
|
|
|
|
-> ty::TraitRef<'tcx>
|
2014-11-15 16:09:51 -06:00
|
|
|
{
|
2014-12-26 05:33:56 -06:00
|
|
|
let substs = t.substs.fold_with(this);
|
2013-10-29 04:25:18 -05:00
|
|
|
ty::TraitRef {
|
|
|
|
def_id: t.def_id,
|
2014-12-03 18:01:29 -06:00
|
|
|
substs: this.tcx().mk_substs(substs),
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
pub fn super_fold_mt<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
|
2014-09-29 14:11:30 -05:00
|
|
|
mt: &ty::mt<'tcx>)
|
|
|
|
-> ty::mt<'tcx> {
|
2014-05-12 16:12:51 -05:00
|
|
|
ty::mt {ty: mt.ty.fold_with(this),
|
2013-10-29 04:25:18 -05:00
|
|
|
mutbl: mt.mutbl}
|
|
|
|
}
|
|
|
|
|
2014-12-26 03:36:04 -06:00
|
|
|
pub fn super_fold_existential_bounds<'tcx, T: TypeFolder<'tcx>>(
|
|
|
|
this: &mut T,
|
|
|
|
bounds: &ty::ExistentialBounds<'tcx>)
|
|
|
|
-> ty::ExistentialBounds<'tcx>
|
|
|
|
{
|
2014-08-27 20:46:52 -05:00
|
|
|
ty::ExistentialBounds {
|
|
|
|
region_bound: bounds.region_bound.fold_with(this),
|
|
|
|
builtin_bounds: bounds.builtin_bounds,
|
2014-12-26 03:36:04 -06:00
|
|
|
projection_bounds: bounds.projection_bounds.fold_with(this),
|
2014-08-27 20:46:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
pub fn super_fold_autoref<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
|
2014-09-29 14:11:30 -05:00
|
|
|
autoref: &ty::AutoRef<'tcx>)
|
|
|
|
-> ty::AutoRef<'tcx>
|
2014-05-06 14:16:11 -05:00
|
|
|
{
|
|
|
|
match *autoref {
|
2015-03-16 11:45:01 -05:00
|
|
|
ty::AutoPtr(r, m) => {
|
|
|
|
let r = r.fold_with(this);
|
|
|
|
ty::AutoPtr(this.tcx().mk_region(r), m)
|
2014-08-27 00:07:28 -05:00
|
|
|
}
|
2015-03-16 11:45:01 -05:00
|
|
|
ty::AutoUnsafe(m) => ty::AutoUnsafe(m)
|
2014-05-12 16:12:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
pub fn super_fold_item_substs<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
|
2014-09-29 14:11:30 -05:00
|
|
|
substs: ty::ItemSubsts<'tcx>)
|
|
|
|
-> ty::ItemSubsts<'tcx>
|
2014-05-12 16:12:51 -05:00
|
|
|
{
|
|
|
|
ty::ItemSubsts {
|
|
|
|
substs: substs.substs.fold_with(this),
|
2014-05-06 14:16:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-15 16:09:51 -06:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2013-10-29 04:25:18 -05:00
|
|
|
// Some sample folders
|
|
|
|
|
2014-12-08 19:26:43 -06:00
|
|
|
pub struct BottomUpFolder<'a, 'tcx: 'a, F> where F: FnMut(Ty<'tcx>) -> Ty<'tcx> {
|
2014-04-22 07:56:37 -05:00
|
|
|
pub tcx: &'a ty::ctxt<'tcx>,
|
2014-12-08 19:26:43 -06:00
|
|
|
pub fldop: F,
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
|
|
|
|
2014-12-08 19:26:43 -06:00
|
|
|
impl<'a, 'tcx, F> TypeFolder<'tcx> for BottomUpFolder<'a, 'tcx, F> where
|
|
|
|
F: FnMut(Ty<'tcx>) -> Ty<'tcx>,
|
|
|
|
{
|
2014-12-16 16:34:47 -06:00
|
|
|
fn tcx(&self) -> &ty::ctxt<'tcx> { self.tcx }
|
2013-10-29 04:25:18 -05:00
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
|
2013-10-29 04:25:18 -05:00
|
|
|
let t1 = super_fold_ty(self, ty);
|
|
|
|
(self.fldop)(t1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Region folder
|
|
|
|
|
2014-06-20 15:26:14 -05:00
|
|
|
/// Folds over the substructure of a type, visiting its component
|
|
|
|
/// types and all regions that occur *free* within it.
|
|
|
|
///
|
2014-09-13 13:09:25 -05:00
|
|
|
/// That is, `Ty` can contain function or method types that bind
|
2014-06-20 15:26:14 -05:00
|
|
|
/// regions at the call site (`ReLateBound`), and occurrences of
|
|
|
|
/// regions (aka "lifetimes") that are bound within a type are not
|
|
|
|
/// visited by this folder; only regions that occur free will be
|
|
|
|
/// visited by `fld_r`.
|
2014-12-14 06:17:23 -06:00
|
|
|
|
|
|
|
pub struct RegionFolder<'a, 'tcx: 'a> {
|
2014-04-22 07:56:37 -05:00
|
|
|
tcx: &'a ty::ctxt<'tcx>,
|
2014-12-04 14:06:42 -06:00
|
|
|
current_depth: u32,
|
|
|
|
fld_r: &'a mut (FnMut(ty::Region, u32) -> ty::Region + 'a),
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
|
|
|
|
2014-12-14 06:17:23 -06:00
|
|
|
impl<'a, 'tcx> RegionFolder<'a, 'tcx> {
|
|
|
|
pub fn new<F>(tcx: &'a ty::ctxt<'tcx>, fld_r: &'a mut F) -> RegionFolder<'a, 'tcx>
|
2014-12-04 14:06:42 -06:00
|
|
|
where F : FnMut(ty::Region, u32) -> ty::Region
|
2014-12-14 06:17:23 -06:00
|
|
|
{
|
2013-10-29 04:25:18 -05:00
|
|
|
RegionFolder {
|
|
|
|
tcx: tcx,
|
2014-11-15 15:47:59 -06:00
|
|
|
current_depth: 1,
|
2014-06-20 15:26:14 -05:00
|
|
|
fld_r: fld_r,
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
|
|
|
}
|
2014-06-20 15:26:14 -05:00
|
|
|
}
|
|
|
|
|
2014-12-01 09:11:59 -06:00
|
|
|
pub fn collect_regions<'tcx,T>(tcx: &ty::ctxt<'tcx>, value: &T) -> Vec<ty::Region>
|
|
|
|
where T : TypeFoldable<'tcx>
|
|
|
|
{
|
|
|
|
let mut vec = Vec::new();
|
2014-12-14 06:17:23 -06:00
|
|
|
fold_regions(tcx, value, |r, _| { vec.push(r); r });
|
2014-12-01 09:11:59 -06:00
|
|
|
vec
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:17:23 -06:00
|
|
|
pub fn fold_regions<'tcx,T,F>(tcx: &ty::ctxt<'tcx>,
|
|
|
|
value: &T,
|
|
|
|
mut f: F)
|
|
|
|
-> T
|
2014-12-26 05:33:56 -06:00
|
|
|
where F : FnMut(ty::Region, u32) -> ty::Region,
|
2014-12-14 06:17:23 -06:00
|
|
|
T : TypeFoldable<'tcx>,
|
|
|
|
{
|
|
|
|
value.fold_with(&mut RegionFolder::new(tcx, &mut f))
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> TypeFolder<'tcx> for RegionFolder<'a, 'tcx>
|
2014-12-08 19:26:43 -06:00
|
|
|
{
|
2014-12-16 16:34:47 -06:00
|
|
|
fn tcx(&self) -> &ty::ctxt<'tcx> { self.tcx }
|
2013-10-29 04:25:18 -05:00
|
|
|
|
2014-11-15 15:47:59 -06:00
|
|
|
fn enter_region_binder(&mut self) {
|
|
|
|
self.current_depth += 1;
|
|
|
|
}
|
2014-06-20 15:26:14 -05:00
|
|
|
|
2014-11-15 15:47:59 -06:00
|
|
|
fn exit_region_binder(&mut self) {
|
|
|
|
self.current_depth -= 1;
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_region(&mut self, r: ty::Region) -> ty::Region {
|
2014-06-20 15:26:14 -05:00
|
|
|
match r {
|
2014-11-15 15:47:59 -06:00
|
|
|
ty::ReLateBound(debruijn, _) if debruijn.depth < self.current_depth => {
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("RegionFolder.fold_region({:?}) skipped bound region (current depth={})",
|
|
|
|
r, self.current_depth);
|
2014-06-20 15:26:14 -05:00
|
|
|
r
|
|
|
|
}
|
|
|
|
_ => {
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("RegionFolder.fold_region({:?}) folding free region (current_depth={})",
|
|
|
|
r, self.current_depth);
|
2015-01-06 11:24:46 -06:00
|
|
|
(self.fld_r)(r, self.current_depth)
|
2014-06-20 15:26:14 -05:00
|
|
|
}
|
|
|
|
}
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
|
|
|
}
|
2014-09-12 10:42:58 -05:00
|
|
|
|
2015-06-05 18:06:14 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Late-bound region replacer
|
|
|
|
|
|
|
|
// Replaces the escaping regions in a type.
|
|
|
|
|
|
|
|
struct RegionReplacer<'a, 'tcx: 'a> {
|
|
|
|
tcx: &'a ty::ctxt<'tcx>,
|
|
|
|
current_depth: u32,
|
|
|
|
fld_r: &'a mut (FnMut(ty::BoundRegion) -> ty::Region + 'a),
|
|
|
|
map: FnvHashMap<ty::BoundRegion, ty::Region>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> RegionReplacer<'a, 'tcx> {
|
|
|
|
fn new<F>(tcx: &'a ty::ctxt<'tcx>, fld_r: &'a mut F) -> RegionReplacer<'a, 'tcx>
|
|
|
|
where F : FnMut(ty::BoundRegion) -> ty::Region
|
|
|
|
{
|
|
|
|
RegionReplacer {
|
|
|
|
tcx: tcx,
|
|
|
|
current_depth: 1,
|
|
|
|
fld_r: fld_r,
|
|
|
|
map: FnvHashMap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn replace_late_bound_regions<'tcx,T,F>(tcx: &ty::ctxt<'tcx>,
|
|
|
|
value: &ty::Binder<T>,
|
|
|
|
mut f: F)
|
|
|
|
-> (T, FnvHashMap<ty::BoundRegion, ty::Region>)
|
|
|
|
where F : FnMut(ty::BoundRegion) -> ty::Region,
|
2015-06-18 00:51:23 -05:00
|
|
|
T : TypeFoldable<'tcx>,
|
2015-06-05 18:06:14 -05:00
|
|
|
{
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("replace_late_bound_regions({:?})", value);
|
2015-06-05 18:06:14 -05:00
|
|
|
let mut replacer = RegionReplacer::new(tcx, &mut f);
|
|
|
|
let result = value.skip_binder().fold_with(&mut replacer);
|
|
|
|
(result, replacer.map)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> TypeFolder<'tcx> for RegionReplacer<'a, 'tcx>
|
|
|
|
{
|
|
|
|
fn tcx(&self) -> &ty::ctxt<'tcx> { self.tcx }
|
|
|
|
|
|
|
|
fn enter_region_binder(&mut self) {
|
|
|
|
self.current_depth += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exit_region_binder(&mut self) {
|
|
|
|
self.current_depth -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
|
|
|
|
if !ty::type_escapes_depth(t, self.current_depth-1) {
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
super_fold_ty(self, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_region(&mut self, r: ty::Region) -> ty::Region {
|
|
|
|
match r {
|
|
|
|
ty::ReLateBound(debruijn, br) if debruijn.depth == self.current_depth => {
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("RegionReplacer.fold_region({:?}) folding region (current_depth={})",
|
|
|
|
r, self.current_depth);
|
2015-06-05 18:06:14 -05:00
|
|
|
let fld_r = &mut self.fld_r;
|
|
|
|
let region = *self.map.entry(br).or_insert_with(|| fld_r(br));
|
|
|
|
if let ty::ReLateBound(debruijn1, br) = region {
|
|
|
|
// If the callback returns a late-bound region,
|
|
|
|
// that region should always use depth 1. Then we
|
|
|
|
// adjust it to the correct depth.
|
|
|
|
assert_eq!(debruijn1.depth, 1);
|
|
|
|
ty::ReLateBound(debruijn, br)
|
|
|
|
} else {
|
|
|
|
region
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r => r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-12 10:42:58 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Region eraser
|
|
|
|
//
|
2015-01-07 08:36:11 -06:00
|
|
|
// Replaces all free regions with 'static. Useful in contexts, such as
|
|
|
|
// method probing, where precise region relationships are not
|
|
|
|
// important. Note that in trans you should use
|
|
|
|
// `common::erase_regions` instead.
|
2014-09-12 10:42:58 -05:00
|
|
|
|
|
|
|
pub struct RegionEraser<'a, 'tcx: 'a> {
|
|
|
|
tcx: &'a ty::ctxt<'tcx>,
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn erase_regions<'tcx, T: TypeFoldable<'tcx>>(tcx: &ty::ctxt<'tcx>, t: T) -> T {
|
2014-09-12 10:42:58 -05:00
|
|
|
let mut eraser = RegionEraser { tcx: tcx };
|
|
|
|
t.fold_with(&mut eraser)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> TypeFolder<'tcx> for RegionEraser<'a, 'tcx> {
|
2014-12-12 10:09:32 -06:00
|
|
|
fn tcx(&self) -> &ty::ctxt<'tcx> { self.tcx }
|
2014-09-12 10:42:58 -05:00
|
|
|
|
2015-06-05 18:06:14 -05:00
|
|
|
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
|
|
|
|
if !ty::type_has_erasable_regions(t) {
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
super_fold_ty(self, t)
|
|
|
|
}
|
|
|
|
|
2014-09-12 10:42:58 -05:00
|
|
|
fn fold_region(&mut self, r: ty::Region) -> ty::Region {
|
2015-01-06 04:03:42 -06:00
|
|
|
// because whether or not a region is bound affects subtyping,
|
|
|
|
// we can't erase the bound/free distinction, but we can
|
|
|
|
// replace all free regions with 'static
|
2014-09-12 10:42:58 -05:00
|
|
|
match r {
|
|
|
|
ty::ReLateBound(..) | ty::ReEarlyBound(..) => r,
|
|
|
|
_ => ty::ReStatic
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-15 15:47:59 -06:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Region shifter
|
|
|
|
//
|
|
|
|
// Shifts the De Bruijn indices on all escaping bound regions by a
|
|
|
|
// fixed amount. Useful in substitution or when otherwise introducing
|
|
|
|
// a binding level that is not intended to capture the existing bound
|
|
|
|
// regions. See comment on `shift_regions_through_binders` method in
|
|
|
|
// `subst.rs` for more details.
|
|
|
|
|
2014-12-04 14:06:42 -06:00
|
|
|
pub fn shift_region(region: ty::Region, amount: u32) -> ty::Region {
|
2014-11-15 15:47:59 -06:00
|
|
|
match region {
|
|
|
|
ty::ReLateBound(debruijn, br) => {
|
|
|
|
ty::ReLateBound(debruijn.shifted(amount), br)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
region
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 00:51:23 -05:00
|
|
|
pub fn shift_regions<'tcx, T:TypeFoldable<'tcx>>(tcx: &ty::ctxt<'tcx>,
|
|
|
|
amount: u32, value: &T) -> T {
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("shift_regions(value={:?}, amount={})",
|
|
|
|
value, amount);
|
2014-11-15 15:47:59 -06:00
|
|
|
|
2014-12-14 06:17:23 -06:00
|
|
|
value.fold_with(&mut RegionFolder::new(tcx, &mut |region, _current_depth| {
|
2014-11-15 15:47:59 -06:00
|
|
|
shift_region(region, amount)
|
|
|
|
}))
|
|
|
|
}
|