2022-06-17 10:15:24 +01:00
|
|
|
//! A folding traversal mechanism for complex data structures that contain type
|
|
|
|
|
//! information.
|
2014-11-25 21:17:11 -05:00
|
|
|
//!
|
2022-06-17 10:15:24 +01:00
|
|
|
//! This is a modifying traversal. It consumes the data structure, producing a
|
|
|
|
|
//! (possibly) modified version of it. Both fallible and infallible versions are
|
|
|
|
|
//! available. The name is potentially confusing, because this traversal is more
|
|
|
|
|
//! like `Iterator::map` than `Iterator::fold`.
|
2014-11-25 21:17:11 -05:00
|
|
|
//!
|
2022-06-17 10:15:24 +01:00
|
|
|
//! This traversal has limited flexibility. Only a small number of "types of
|
2022-02-08 11:46:53 +11:00
|
|
|
//! interest" within the complex data structures can receive custom
|
2022-06-17 10:15:24 +01:00
|
|
|
//! modification. These are the ones containing the most important type-related
|
|
|
|
|
//! information, such as `Ty`, `Predicate`, `Region`, and `Const`.
|
2014-11-25 21:17:11 -05:00
|
|
|
//!
|
2022-06-17 10:15:24 +01:00
|
|
|
//! There are three groups of traits involved in each traversal.
|
|
|
|
|
//! - `TypeFoldable`. This is implemented once for many types, including:
|
Folding revamp.
This commit makes type folding more like the way chalk does it.
Currently, `TypeFoldable` has `fold_with` and `super_fold_with` methods.
- `fold_with` is the standard entry point, and defaults to calling
`super_fold_with`.
- `super_fold_with` does the actual work of traversing a type.
- For a few types of interest (`Ty`, `Region`, etc.) `fold_with` instead
calls into a `TypeFolder`, which can then call back into
`super_fold_with`.
With the new approach, `TypeFoldable` has `fold_with` and
`TypeSuperFoldable` has `super_fold_with`.
- `fold_with` is still the standard entry point, *and* it does the
actual work of traversing a type, for all types except types of
interest.
- `super_fold_with` is only implemented for the types of interest.
Benefits of the new model.
- I find it easier to understand. The distinction between types of
interest and other types is clearer, and `super_fold_with` doesn't
exist for most types.
- With the current model is easy to get confused and implement a
`super_fold_with` method that should be left defaulted. (Some of the
precursor commits fixed such cases.)
- With the current model it's easy to call `super_fold_with` within
`TypeFolder` impls where `fold_with` should be called. The new
approach makes this mistake impossible, and this commit fixes a number
of such cases.
- It's potentially faster, because it avoids the `fold_with` ->
`super_fold_with` call in all cases except types of interest. A lot of
the time the compile would inline those away, but not necessarily
always.
2022-06-02 11:38:15 +10:00
|
|
|
//! - Types of interest, for which the the methods delegate to the
|
2022-06-17 10:15:24 +01:00
|
|
|
//! folder.
|
Folding revamp.
This commit makes type folding more like the way chalk does it.
Currently, `TypeFoldable` has `fold_with` and `super_fold_with` methods.
- `fold_with` is the standard entry point, and defaults to calling
`super_fold_with`.
- `super_fold_with` does the actual work of traversing a type.
- For a few types of interest (`Ty`, `Region`, etc.) `fold_with` instead
calls into a `TypeFolder`, which can then call back into
`super_fold_with`.
With the new approach, `TypeFoldable` has `fold_with` and
`TypeSuperFoldable` has `super_fold_with`.
- `fold_with` is still the standard entry point, *and* it does the
actual work of traversing a type, for all types except types of
interest.
- `super_fold_with` is only implemented for the types of interest.
Benefits of the new model.
- I find it easier to understand. The distinction between types of
interest and other types is clearer, and `super_fold_with` doesn't
exist for most types.
- With the current model is easy to get confused and implement a
`super_fold_with` method that should be left defaulted. (Some of the
precursor commits fixed such cases.)
- With the current model it's easy to call `super_fold_with` within
`TypeFolder` impls where `fold_with` should be called. The new
approach makes this mistake impossible, and this commit fixes a number
of such cases.
- It's potentially faster, because it avoids the `fold_with` ->
`super_fold_with` call in all cases except types of interest. A lot of
the time the compile would inline those away, but not necessarily
always.
2022-06-02 11:38:15 +10:00
|
|
|
//! - All other types, including generic containers like `Vec` and `Option`.
|
2022-06-17 10:15:24 +01:00
|
|
|
//! It defines a "skeleton" of how they should be folded.
|
Folding revamp.
This commit makes type folding more like the way chalk does it.
Currently, `TypeFoldable` has `fold_with` and `super_fold_with` methods.
- `fold_with` is the standard entry point, and defaults to calling
`super_fold_with`.
- `super_fold_with` does the actual work of traversing a type.
- For a few types of interest (`Ty`, `Region`, etc.) `fold_with` instead
calls into a `TypeFolder`, which can then call back into
`super_fold_with`.
With the new approach, `TypeFoldable` has `fold_with` and
`TypeSuperFoldable` has `super_fold_with`.
- `fold_with` is still the standard entry point, *and* it does the
actual work of traversing a type, for all types except types of
interest.
- `super_fold_with` is only implemented for the types of interest.
Benefits of the new model.
- I find it easier to understand. The distinction between types of
interest and other types is clearer, and `super_fold_with` doesn't
exist for most types.
- With the current model is easy to get confused and implement a
`super_fold_with` method that should be left defaulted. (Some of the
precursor commits fixed such cases.)
- With the current model it's easy to call `super_fold_with` within
`TypeFolder` impls where `fold_with` should be called. The new
approach makes this mistake impossible, and this commit fixes a number
of such cases.
- It's potentially faster, because it avoids the `fold_with` ->
`super_fold_with` call in all cases except types of interest. A lot of
the time the compile would inline those away, but not necessarily
always.
2022-06-02 11:38:15 +10:00
|
|
|
//! - `TypeSuperFoldable`. This is implemented only for each type of interest,
|
2022-06-17 10:15:24 +01:00
|
|
|
//! and defines the folding "skeleton" for these types.
|
|
|
|
|
//! - `TypeFolder`/`FallibleTypeFolder. One of these is implemented for each
|
|
|
|
|
//! folder. This defines how types of interest are folded.
|
2014-11-25 21:17:11 -05:00
|
|
|
//!
|
2022-06-17 10:15:24 +01:00
|
|
|
//! This means each fold is a mixture of (a) generic folding operations, and (b)
|
|
|
|
|
//! custom fold operations that are specific to the folder.
|
2022-02-08 11:46:53 +11:00
|
|
|
//! - The `TypeFoldable` impls handle most of the traversal, and call into
|
2022-06-17 10:15:24 +01:00
|
|
|
//! `TypeFolder`/`FallibleTypeFolder` when they encounter a type of interest.
|
|
|
|
|
//! - A `TypeFolder`/`FallibleTypeFolder` may call into another `TypeFoldable`
|
|
|
|
|
//! impl, because some of the types of interest are recursive and can contain
|
|
|
|
|
//! other types of interest.
|
|
|
|
|
//! - A `TypeFolder`/`FallibleTypeFolder` may also call into a `TypeSuperFoldable`
|
|
|
|
|
//! impl, because each folder might provide custom handling only for some types
|
|
|
|
|
//! of interest, or only for some variants of each type of interest, and then
|
|
|
|
|
//! use default traversal for the remaining cases.
|
2015-11-18 09:38:57 +00:00
|
|
|
//!
|
2022-02-08 11:46:53 +11:00
|
|
|
//! For example, if you have `struct S(Ty, U)` where `S: TypeFoldable` and `U:
|
2022-06-17 10:15:24 +01:00
|
|
|
//! TypeFoldable`, and an instance `s = S(ty, u)`, it would be folded like so:
|
2022-04-15 15:04:34 -07:00
|
|
|
//! ```text
|
2022-06-17 10:15:24 +01:00
|
|
|
//! s.fold_with(folder) calls
|
|
|
|
|
//! - ty.fold_with(folder) calls
|
|
|
|
|
//! - folder.fold_ty(ty) may call
|
|
|
|
|
//! - ty.super_fold_with(folder)
|
|
|
|
|
//! - u.fold_with(folder)
|
2022-02-08 11:46:53 +11:00
|
|
|
//! ```
|
2021-03-30 14:26:40 +00:00
|
|
|
use crate::mir;
|
2022-06-17 10:15:24 +01:00
|
|
|
use crate::ty::{self, Binder, Ty, TyCtxt, TypeVisitable};
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir::def_id::DefId;
|
2015-06-18 08:51:23 +03:00
|
|
|
|
2018-05-14 22:27:45 +10:00
|
|
|
use std::collections::BTreeMap;
|
2013-10-29 05:25:18 -04:00
|
|
|
|
2022-06-17 10:15:24 +01:00
|
|
|
/// This trait is implemented for every type that can be folded,
|
2022-02-08 11:46:53 +11:00
|
|
|
/// providing the skeleton of the traversal.
|
2018-02-09 10:34:23 -05:00
|
|
|
///
|
2022-02-08 11:46:53 +11:00
|
|
|
/// To implement this conveniently, use the derive macro located in
|
|
|
|
|
/// `rustc_macros`.
|
2022-06-17 10:15:24 +01:00
|
|
|
pub trait TypeFoldable<'tcx>: TypeVisitable<'tcx> {
|
Folding revamp.
This commit makes type folding more like the way chalk does it.
Currently, `TypeFoldable` has `fold_with` and `super_fold_with` methods.
- `fold_with` is the standard entry point, and defaults to calling
`super_fold_with`.
- `super_fold_with` does the actual work of traversing a type.
- For a few types of interest (`Ty`, `Region`, etc.) `fold_with` instead
calls into a `TypeFolder`, which can then call back into
`super_fold_with`.
With the new approach, `TypeFoldable` has `fold_with` and
`TypeSuperFoldable` has `super_fold_with`.
- `fold_with` is still the standard entry point, *and* it does the
actual work of traversing a type, for all types except types of
interest.
- `super_fold_with` is only implemented for the types of interest.
Benefits of the new model.
- I find it easier to understand. The distinction between types of
interest and other types is clearer, and `super_fold_with` doesn't
exist for most types.
- With the current model is easy to get confused and implement a
`super_fold_with` method that should be left defaulted. (Some of the
precursor commits fixed such cases.)
- With the current model it's easy to call `super_fold_with` within
`TypeFolder` impls where `fold_with` should be called. The new
approach makes this mistake impossible, and this commit fixes a number
of such cases.
- It's potentially faster, because it avoids the `fold_with` ->
`super_fold_with` call in all cases except types of interest. A lot of
the time the compile would inline those away, but not necessarily
always.
2022-06-02 11:38:15 +10:00
|
|
|
/// The entry point for folding. To fold a value `t` with a folder `f`
|
2022-02-08 11:46:53 +11:00
|
|
|
/// call: `t.try_fold_with(f)`.
|
|
|
|
|
///
|
Folding revamp.
This commit makes type folding more like the way chalk does it.
Currently, `TypeFoldable` has `fold_with` and `super_fold_with` methods.
- `fold_with` is the standard entry point, and defaults to calling
`super_fold_with`.
- `super_fold_with` does the actual work of traversing a type.
- For a few types of interest (`Ty`, `Region`, etc.) `fold_with` instead
calls into a `TypeFolder`, which can then call back into
`super_fold_with`.
With the new approach, `TypeFoldable` has `fold_with` and
`TypeSuperFoldable` has `super_fold_with`.
- `fold_with` is still the standard entry point, *and* it does the
actual work of traversing a type, for all types except types of
interest.
- `super_fold_with` is only implemented for the types of interest.
Benefits of the new model.
- I find it easier to understand. The distinction between types of
interest and other types is clearer, and `super_fold_with` doesn't
exist for most types.
- With the current model is easy to get confused and implement a
`super_fold_with` method that should be left defaulted. (Some of the
precursor commits fixed such cases.)
- With the current model it's easy to call `super_fold_with` within
`TypeFolder` impls where `fold_with` should be called. The new
approach makes this mistake impossible, and this commit fixes a number
of such cases.
- It's potentially faster, because it avoids the `fold_with` ->
`super_fold_with` call in all cases except types of interest. A lot of
the time the compile would inline those away, but not necessarily
always.
2022-06-02 11:38:15 +10:00
|
|
|
/// For most types, this just traverses the value, calling `try_fold_with`
|
|
|
|
|
/// on each field/element.
|
|
|
|
|
///
|
|
|
|
|
/// For types of interest (such as `Ty`), the implementation of method
|
|
|
|
|
/// calls a folder method specifically for that type (such as
|
2022-02-08 11:46:53 +11:00
|
|
|
/// `F::try_fold_ty`). This is where control transfers from `TypeFoldable`
|
|
|
|
|
/// to `TypeFolder`.
|
Folding revamp.
This commit makes type folding more like the way chalk does it.
Currently, `TypeFoldable` has `fold_with` and `super_fold_with` methods.
- `fold_with` is the standard entry point, and defaults to calling
`super_fold_with`.
- `super_fold_with` does the actual work of traversing a type.
- For a few types of interest (`Ty`, `Region`, etc.) `fold_with` instead
calls into a `TypeFolder`, which can then call back into
`super_fold_with`.
With the new approach, `TypeFoldable` has `fold_with` and
`TypeSuperFoldable` has `super_fold_with`.
- `fold_with` is still the standard entry point, *and* it does the
actual work of traversing a type, for all types except types of
interest.
- `super_fold_with` is only implemented for the types of interest.
Benefits of the new model.
- I find it easier to understand. The distinction between types of
interest and other types is clearer, and `super_fold_with` doesn't
exist for most types.
- With the current model is easy to get confused and implement a
`super_fold_with` method that should be left defaulted. (Some of the
precursor commits fixed such cases.)
- With the current model it's easy to call `super_fold_with` within
`TypeFolder` impls where `fold_with` should be called. The new
approach makes this mistake impossible, and this commit fixes a number
of such cases.
- It's potentially faster, because it avoids the `fold_with` ->
`super_fold_with` call in all cases except types of interest. A lot of
the time the compile would inline those away, but not necessarily
always.
2022-06-02 11:38:15 +10:00
|
|
|
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error>;
|
2022-02-08 11:46:53 +11:00
|
|
|
|
|
|
|
|
/// A convenient alternative to `try_fold_with` for use with infallible
|
|
|
|
|
/// folders. Do not override this method, to ensure coherence with
|
|
|
|
|
/// `try_fold_with`.
|
2022-06-21 12:15:05 +01:00
|
|
|
fn fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
|
2021-12-01 00:55:57 +00:00
|
|
|
self.try_fold_with(folder).into_ok()
|
|
|
|
|
}
|
2014-05-12 17:12:51 -04:00
|
|
|
}
|
|
|
|
|
|
Folding revamp.
This commit makes type folding more like the way chalk does it.
Currently, `TypeFoldable` has `fold_with` and `super_fold_with` methods.
- `fold_with` is the standard entry point, and defaults to calling
`super_fold_with`.
- `super_fold_with` does the actual work of traversing a type.
- For a few types of interest (`Ty`, `Region`, etc.) `fold_with` instead
calls into a `TypeFolder`, which can then call back into
`super_fold_with`.
With the new approach, `TypeFoldable` has `fold_with` and
`TypeSuperFoldable` has `super_fold_with`.
- `fold_with` is still the standard entry point, *and* it does the
actual work of traversing a type, for all types except types of
interest.
- `super_fold_with` is only implemented for the types of interest.
Benefits of the new model.
- I find it easier to understand. The distinction between types of
interest and other types is clearer, and `super_fold_with` doesn't
exist for most types.
- With the current model is easy to get confused and implement a
`super_fold_with` method that should be left defaulted. (Some of the
precursor commits fixed such cases.)
- With the current model it's easy to call `super_fold_with` within
`TypeFolder` impls where `fold_with` should be called. The new
approach makes this mistake impossible, and this commit fixes a number
of such cases.
- It's potentially faster, because it avoids the `fold_with` ->
`super_fold_with` call in all cases except types of interest. A lot of
the time the compile would inline those away, but not necessarily
always.
2022-06-02 11:38:15 +10:00
|
|
|
// This trait is implemented for types of interest.
|
|
|
|
|
pub trait TypeSuperFoldable<'tcx>: TypeFoldable<'tcx> {
|
|
|
|
|
/// Provides a default fold for a type of interest. This should only be
|
|
|
|
|
/// called within `TypeFolder` methods, when a non-custom traversal is
|
|
|
|
|
/// desired for the value of the type of interest passed to that method.
|
|
|
|
|
/// For example, in `MyFolder::try_fold_ty(ty)`, it is valid to call
|
|
|
|
|
/// `ty.try_super_fold_with(self)`, but any other folding should be done
|
|
|
|
|
/// with `xyz.try_fold_with(self)`.
|
|
|
|
|
fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
|
|
|
|
|
self,
|
|
|
|
|
folder: &mut F,
|
|
|
|
|
) -> Result<Self, F::Error>;
|
|
|
|
|
|
|
|
|
|
/// A convenient alternative to `try_super_fold_with` for use with
|
|
|
|
|
/// infallible folders. Do not override this method, to ensure coherence
|
|
|
|
|
/// with `try_super_fold_with`.
|
2022-06-21 12:15:05 +01:00
|
|
|
fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
|
Folding revamp.
This commit makes type folding more like the way chalk does it.
Currently, `TypeFoldable` has `fold_with` and `super_fold_with` methods.
- `fold_with` is the standard entry point, and defaults to calling
`super_fold_with`.
- `super_fold_with` does the actual work of traversing a type.
- For a few types of interest (`Ty`, `Region`, etc.) `fold_with` instead
calls into a `TypeFolder`, which can then call back into
`super_fold_with`.
With the new approach, `TypeFoldable` has `fold_with` and
`TypeSuperFoldable` has `super_fold_with`.
- `fold_with` is still the standard entry point, *and* it does the
actual work of traversing a type, for all types except types of
interest.
- `super_fold_with` is only implemented for the types of interest.
Benefits of the new model.
- I find it easier to understand. The distinction between types of
interest and other types is clearer, and `super_fold_with` doesn't
exist for most types.
- With the current model is easy to get confused and implement a
`super_fold_with` method that should be left defaulted. (Some of the
precursor commits fixed such cases.)
- With the current model it's easy to call `super_fold_with` within
`TypeFolder` impls where `fold_with` should be called. The new
approach makes this mistake impossible, and this commit fixes a number
of such cases.
- It's potentially faster, because it avoids the `fold_with` ->
`super_fold_with` call in all cases except types of interest. A lot of
the time the compile would inline those away, but not necessarily
always.
2022-06-02 11:38:15 +10:00
|
|
|
self.try_super_fold_with(folder).into_ok()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-21 12:15:05 +01:00
|
|
|
/// This trait is implemented for every infallible folding traversal. There is
|
|
|
|
|
/// a fold method defined for every type of interest. Each such method has a
|
|
|
|
|
/// default that does an "identity" fold. Implementations of these methods
|
|
|
|
|
/// often fall back to a `super_fold_with` method if the primary argument
|
|
|
|
|
/// doesn't satisfy a particular condition.
|
2021-12-01 00:55:57 +00:00
|
|
|
///
|
2022-06-21 12:15:05 +01:00
|
|
|
/// A blanket implementation of [`FallibleTypeFolder`] will defer to
|
2021-12-01 00:55:57 +00:00
|
|
|
/// the infallible methods of this trait to ensure that the two APIs
|
|
|
|
|
/// are coherent.
|
2022-06-20 21:10:43 +01:00
|
|
|
pub trait TypeFolder<'tcx>: FallibleTypeFolder<'tcx, Error = !> {
|
2019-06-14 00:48:52 +03:00
|
|
|
fn tcx<'a>(&'a self) -> TyCtxt<'tcx>;
|
2013-10-29 05:25:18 -04:00
|
|
|
|
2021-12-01 00:55:57 +00:00
|
|
|
fn fold_binder<T>(&mut self, t: Binder<'tcx, T>) -> Binder<'tcx, T>
|
2019-12-22 17:42:04 -05:00
|
|
|
where
|
|
|
|
|
T: TypeFoldable<'tcx>,
|
2015-01-04 06:07:36 -05:00
|
|
|
{
|
2016-01-06 02:01:28 +00:00
|
|
|
t.super_fold_with(self)
|
2015-01-04 06:07:36 -05:00
|
|
|
}
|
|
|
|
|
|
2022-06-20 21:10:43 +01:00
|
|
|
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
|
2016-01-06 02:01:28 +00:00
|
|
|
t.super_fold_with(self)
|
2013-10-29 05:25:18 -04:00
|
|
|
}
|
|
|
|
|
|
2022-06-20 21:10:43 +01:00
|
|
|
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
|
2016-01-06 02:01:28 +00:00
|
|
|
r.super_fold_with(self)
|
2013-10-29 05:25:18 -04:00
|
|
|
}
|
2017-08-07 08:08:53 +03:00
|
|
|
|
2022-06-20 21:10:43 +01:00
|
|
|
fn fold_const(&mut self, c: ty::Const<'tcx>) -> ty::Const<'tcx> {
|
2021-12-01 00:55:57 +00:00
|
|
|
c.super_fold_with(self)
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-20 21:10:43 +01:00
|
|
|
fn fold_unevaluated(&mut self, uv: ty::Unevaluated<'tcx>) -> ty::Unevaluated<'tcx> {
|
2022-06-01 17:20:56 +10:00
|
|
|
uv.super_fold_with(self)
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-20 21:10:43 +01:00
|
|
|
fn fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> {
|
2021-12-01 00:55:57 +00:00
|
|
|
p.super_fold_with(self)
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-20 21:10:43 +01:00
|
|
|
fn fold_mir_const(&mut self, c: mir::ConstantKind<'tcx>) -> mir::ConstantKind<'tcx> {
|
2021-12-01 00:55:57 +00:00
|
|
|
bug!("most type folders should not be folding MIR datastructures: {:?}", c)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-08 11:46:53 +11:00
|
|
|
/// This trait is implemented for every folding traversal. There is a fold
|
|
|
|
|
/// method defined for every type of interest. Each such method has a default
|
|
|
|
|
/// that does an "identity" fold.
|
2021-12-01 00:55:57 +00:00
|
|
|
///
|
|
|
|
|
/// A blanket implementation of this trait (that defers to the relevant
|
|
|
|
|
/// method of [`TypeFolder`]) is provided for all infallible folders in
|
|
|
|
|
/// order to ensure the two APIs are coherent.
|
2022-06-20 21:10:43 +01:00
|
|
|
pub trait FallibleTypeFolder<'tcx>: Sized {
|
|
|
|
|
type Error;
|
|
|
|
|
|
|
|
|
|
fn tcx<'a>(&'a self) -> TyCtxt<'tcx>;
|
|
|
|
|
|
2021-12-01 00:55:57 +00:00
|
|
|
fn try_fold_binder<T>(&mut self, t: Binder<'tcx, T>) -> Result<Binder<'tcx, T>, Self::Error>
|
|
|
|
|
where
|
|
|
|
|
T: TypeFoldable<'tcx>,
|
|
|
|
|
{
|
|
|
|
|
t.try_super_fold_with(self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn try_fold_ty(&mut self, t: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
|
|
|
|
|
t.try_super_fold_with(self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn try_fold_region(&mut self, r: ty::Region<'tcx>) -> Result<ty::Region<'tcx>, Self::Error> {
|
|
|
|
|
r.try_super_fold_with(self)
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-02 14:24:45 +11:00
|
|
|
fn try_fold_const(&mut self, c: ty::Const<'tcx>) -> Result<ty::Const<'tcx>, Self::Error> {
|
2021-12-01 00:55:57 +00:00
|
|
|
c.try_super_fold_with(self)
|
2017-08-07 08:08:53 +03:00
|
|
|
}
|
2021-03-30 14:26:40 +00:00
|
|
|
|
2022-06-01 17:20:56 +10:00
|
|
|
fn try_fold_unevaluated(
|
|
|
|
|
&mut self,
|
|
|
|
|
c: ty::Unevaluated<'tcx>,
|
|
|
|
|
) -> Result<ty::Unevaluated<'tcx>, Self::Error> {
|
|
|
|
|
c.try_super_fold_with(self)
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-01 00:55:57 +00:00
|
|
|
fn try_fold_predicate(
|
2021-05-19 13:20:39 +02:00
|
|
|
&mut self,
|
|
|
|
|
p: ty::Predicate<'tcx>,
|
|
|
|
|
) -> Result<ty::Predicate<'tcx>, Self::Error> {
|
2021-12-01 00:55:57 +00:00
|
|
|
p.try_super_fold_with(self)
|
2021-07-19 12:13:25 +02:00
|
|
|
}
|
|
|
|
|
|
2021-12-01 00:55:57 +00:00
|
|
|
fn try_fold_mir_const(
|
2021-05-19 13:20:39 +02:00
|
|
|
&mut self,
|
|
|
|
|
c: mir::ConstantKind<'tcx>,
|
|
|
|
|
) -> Result<mir::ConstantKind<'tcx>, Self::Error> {
|
2021-03-30 14:26:40 +00:00
|
|
|
bug!("most type folders should not be folding MIR datastructures: {:?}", c)
|
|
|
|
|
}
|
2014-05-12 17:12:51 -04:00
|
|
|
}
|
|
|
|
|
|
2022-02-08 11:46:53 +11:00
|
|
|
// This blanket implementation of the fallible trait for infallible folders
|
|
|
|
|
// delegates to infallible methods to ensure coherence.
|
2021-12-01 15:11:24 +00:00
|
|
|
impl<'tcx, F> FallibleTypeFolder<'tcx> for F
|
2021-12-01 00:55:57 +00:00
|
|
|
where
|
2022-06-20 21:10:43 +01:00
|
|
|
F: TypeFolder<'tcx>,
|
2021-12-01 00:55:57 +00:00
|
|
|
{
|
2022-06-20 21:10:43 +01:00
|
|
|
type Error = !;
|
|
|
|
|
|
|
|
|
|
fn tcx<'a>(&'a self) -> TyCtxt<'tcx> {
|
|
|
|
|
TypeFolder::tcx(self)
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-21 12:15:05 +01:00
|
|
|
fn try_fold_binder<T>(&mut self, t: Binder<'tcx, T>) -> Result<Binder<'tcx, T>, !>
|
2021-12-01 00:55:57 +00:00
|
|
|
where
|
|
|
|
|
T: TypeFoldable<'tcx>,
|
|
|
|
|
{
|
|
|
|
|
Ok(self.fold_binder(t))
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-21 12:15:05 +01:00
|
|
|
fn try_fold_ty(&mut self, t: Ty<'tcx>) -> Result<Ty<'tcx>, !> {
|
2021-12-01 00:55:57 +00:00
|
|
|
Ok(self.fold_ty(t))
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-21 12:15:05 +01:00
|
|
|
fn try_fold_region(&mut self, r: ty::Region<'tcx>) -> Result<ty::Region<'tcx>, !> {
|
2021-12-01 00:55:57 +00:00
|
|
|
Ok(self.fold_region(r))
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-21 12:15:05 +01:00
|
|
|
fn try_fold_const(&mut self, c: ty::Const<'tcx>) -> Result<ty::Const<'tcx>, !> {
|
2021-12-01 00:55:57 +00:00
|
|
|
Ok(self.fold_const(c))
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-20 19:03:37 +01:00
|
|
|
fn try_fold_unevaluated(
|
|
|
|
|
&mut self,
|
|
|
|
|
c: ty::Unevaluated<'tcx>,
|
2022-06-21 12:15:05 +01:00
|
|
|
) -> Result<ty::Unevaluated<'tcx>, !> {
|
2022-06-20 19:03:37 +01:00
|
|
|
Ok(self.fold_unevaluated(c))
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-21 12:15:05 +01:00
|
|
|
fn try_fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> Result<ty::Predicate<'tcx>, !> {
|
2021-12-01 00:55:57 +00:00
|
|
|
Ok(self.fold_predicate(p))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn try_fold_mir_const(
|
|
|
|
|
&mut self,
|
|
|
|
|
c: mir::ConstantKind<'tcx>,
|
2022-06-21 12:15:05 +01:00
|
|
|
) -> Result<mir::ConstantKind<'tcx>, !> {
|
2021-12-01 00:55:57 +00:00
|
|
|
Ok(self.fold_mir_const(c))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-15 17:09:51 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2013-10-29 05:25:18 -04:00
|
|
|
// Some sample folders
|
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
pub struct BottomUpFolder<'tcx, F, G, H>
|
2019-06-12 00:11:55 +03:00
|
|
|
where
|
|
|
|
|
F: FnMut(Ty<'tcx>) -> Ty<'tcx>,
|
|
|
|
|
G: FnMut(ty::Region<'tcx>) -> ty::Region<'tcx>,
|
2022-02-02 14:24:45 +11:00
|
|
|
H: FnMut(ty::Const<'tcx>) -> ty::Const<'tcx>,
|
2016-04-29 06:00:23 +03:00
|
|
|
{
|
2019-06-14 00:48:52 +03:00
|
|
|
pub tcx: TyCtxt<'tcx>,
|
2019-03-08 01:15:23 +00:00
|
|
|
pub ty_op: F,
|
|
|
|
|
pub lt_op: G,
|
|
|
|
|
pub ct_op: H,
|
2013-10-29 05:25:18 -04:00
|
|
|
}
|
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'tcx, F, G, H> TypeFolder<'tcx> for BottomUpFolder<'tcx, F, G, H>
|
2019-06-12 00:11:55 +03:00
|
|
|
where
|
|
|
|
|
F: FnMut(Ty<'tcx>) -> Ty<'tcx>,
|
|
|
|
|
G: FnMut(ty::Region<'tcx>) -> ty::Region<'tcx>,
|
2022-02-02 14:24:45 +11:00
|
|
|
H: FnMut(ty::Const<'tcx>) -> ty::Const<'tcx>,
|
2014-12-08 20:26:43 -05:00
|
|
|
{
|
2019-06-14 00:48:52 +03:00
|
|
|
fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
|
2019-06-12 00:11:55 +03:00
|
|
|
self.tcx
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2013-10-29 05:25:18 -04:00
|
|
|
|
2021-12-01 00:55:57 +00:00
|
|
|
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
|
|
|
|
|
let t = ty.super_fold_with(self);
|
|
|
|
|
(self.ty_op)(t)
|
2013-10-29 05:25:18 -04:00
|
|
|
}
|
2018-07-17 11:21:54 +02:00
|
|
|
|
2021-12-01 00:55:57 +00:00
|
|
|
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
|
|
|
|
|
let r = r.super_fold_with(self);
|
|
|
|
|
(self.lt_op)(r)
|
2019-03-08 01:15:23 +00:00
|
|
|
}
|
|
|
|
|
|
2022-02-02 14:24:45 +11:00
|
|
|
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
|
2021-12-01 00:55:57 +00:00
|
|
|
let ct = ct.super_fold_with(self);
|
|
|
|
|
(self.ct_op)(ct)
|
2018-07-17 11:21:54 +02:00
|
|
|
}
|
2013-10-29 05:25:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Region folder
|
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'tcx> TyCtxt<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
/// Folds the escaping and free regions in `value` using `f`, and
|
|
|
|
|
/// sets `skipped_regions` to true if any late-bound region was found
|
|
|
|
|
/// and skipped.
|
2018-05-25 09:58:29 -04:00
|
|
|
pub fn fold_regions<T>(
|
|
|
|
|
self,
|
2020-10-24 02:21:18 +02:00
|
|
|
value: T,
|
2018-05-25 09:58:29 -04:00
|
|
|
mut f: impl FnMut(ty::Region<'tcx>, ty::DebruijnIndex) -> ty::Region<'tcx>,
|
|
|
|
|
) -> T
|
|
|
|
|
where
|
2019-12-22 17:42:04 -05:00
|
|
|
T: TypeFoldable<'tcx>,
|
2015-09-06 21:51:58 +03:00
|
|
|
{
|
2022-06-27 15:55:03 +02:00
|
|
|
value.fold_with(&mut RegionFolder::new(self, &mut f))
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-20 22:26:14 +02:00
|
|
|
/// Folds over the substructure of a type, visiting its component
|
|
|
|
|
/// types and all regions that occur *free* within it.
|
|
|
|
|
///
|
2014-09-13 21:09:25 +03:00
|
|
|
/// That is, `Ty` can contain function or method types that bind
|
2014-06-20 22:26:14 +02: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 07:17:23 -05:00
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
pub struct RegionFolder<'a, 'tcx> {
|
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2018-05-25 09:58:29 -04:00
|
|
|
|
|
|
|
|
/// Stores the index of a binder *just outside* the stuff we have
|
|
|
|
|
/// visited. So this begins as INNERMOST; when we pass through a
|
|
|
|
|
/// binder, it is incremented (via `shift_in`).
|
|
|
|
|
current_index: ty::DebruijnIndex,
|
|
|
|
|
|
|
|
|
|
/// Callback invokes for each free region. The `DebruijnIndex`
|
|
|
|
|
/// points to the binder *just outside* the ones we have passed
|
|
|
|
|
/// through.
|
2019-06-12 00:11:55 +03:00
|
|
|
fold_region_fn:
|
|
|
|
|
&'a mut (dyn FnMut(ty::Region<'tcx>, ty::DebruijnIndex) -> ty::Region<'tcx> + 'a),
|
2013-10-29 05:25:18 -04:00
|
|
|
}
|
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'a, 'tcx> RegionFolder<'a, 'tcx> {
|
2018-11-29 21:13:04 +01:00
|
|
|
#[inline]
|
2018-05-25 09:58:29 -04:00
|
|
|
pub fn new(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2018-05-25 09:58:29 -04:00
|
|
|
fold_region_fn: &'a mut dyn FnMut(ty::Region<'tcx>, ty::DebruijnIndex) -> ty::Region<'tcx>,
|
2019-06-14 00:48:52 +03:00
|
|
|
) -> RegionFolder<'a, 'tcx> {
|
2022-06-27 15:55:03 +02:00
|
|
|
RegionFolder { tcx, current_index: ty::INNERMOST, fold_region_fn }
|
2013-10-29 05:25:18 -04:00
|
|
|
}
|
2014-06-20 22:26:14 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'a, 'tcx> TypeFolder<'tcx> for RegionFolder<'a, 'tcx> {
|
|
|
|
|
fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
|
2019-06-12 00:11:55 +03:00
|
|
|
self.tcx
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2013-10-29 05:25:18 -04:00
|
|
|
|
2020-10-05 16:51:33 -04:00
|
|
|
fn fold_binder<T: TypeFoldable<'tcx>>(
|
|
|
|
|
&mut self,
|
|
|
|
|
t: ty::Binder<'tcx, T>,
|
2021-12-01 00:55:57 +00:00
|
|
|
) -> ty::Binder<'tcx, T> {
|
2018-05-25 09:58:29 -04:00
|
|
|
self.current_index.shift_in(1);
|
2016-01-08 23:34:05 +00:00
|
|
|
let t = t.super_fold_with(self);
|
2018-05-25 09:58:29 -04:00
|
|
|
self.current_index.shift_out(1);
|
2016-01-08 23:34:05 +00:00
|
|
|
t
|
2013-10-29 05:25:18 -04:00
|
|
|
}
|
|
|
|
|
|
2021-08-20 13:36:04 +00:00
|
|
|
#[instrument(skip(self), level = "debug")]
|
2021-12-01 00:55:57 +00:00
|
|
|
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
|
2016-08-25 23:58:52 +03:00
|
|
|
match *r {
|
2018-05-25 09:58:29 -04:00
|
|
|
ty::ReLateBound(debruijn, _) if debruijn < self.current_index => {
|
2021-08-20 13:36:04 +00:00
|
|
|
debug!(?self.current_index, "skipped bound region");
|
2021-12-01 00:55:57 +00:00
|
|
|
r
|
2014-06-20 22:26:14 +02:00
|
|
|
}
|
|
|
|
|
_ => {
|
2021-08-20 13:36:04 +00:00
|
|
|
debug!(?self.current_index, "folding free region");
|
2021-12-01 00:55:57 +00:00
|
|
|
(self.fold_region_fn)(r, self.current_index)
|
2014-06-20 22:26:14 +02:00
|
|
|
}
|
|
|
|
|
}
|
2013-10-29 05:25:18 -04:00
|
|
|
}
|
|
|
|
|
}
|
2014-09-12 11:42:58 -04:00
|
|
|
|
2015-06-06 02:06:14 +03:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-10-24 23:41:40 +02:00
|
|
|
// Bound vars replacer
|
2015-06-06 02:06:14 +03:00
|
|
|
|
2018-10-24 23:41:40 +02:00
|
|
|
/// Replaces the escaping bound vars (late bound regions or bound types) in a type.
|
2019-06-14 00:48:52 +03:00
|
|
|
struct BoundVarReplacer<'a, 'tcx> {
|
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2018-05-25 09:58:29 -04:00
|
|
|
|
|
|
|
|
/// As with `RegionFolder`, represents the index of a binder *just outside*
|
|
|
|
|
/// the ones we have visited.
|
|
|
|
|
current_index: ty::DebruijnIndex,
|
|
|
|
|
|
2022-06-02 12:02:30 +02:00
|
|
|
fld_r: &'a mut (dyn FnMut(ty::BoundRegion) -> ty::Region<'tcx> + 'a),
|
|
|
|
|
fld_t: &'a mut (dyn FnMut(ty::BoundTy) -> Ty<'tcx> + 'a),
|
|
|
|
|
fld_c: &'a mut (dyn FnMut(ty::BoundVar, Ty<'tcx>) -> ty::Const<'tcx> + 'a),
|
2018-10-24 23:41:40 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'a, 'tcx> BoundVarReplacer<'a, 'tcx> {
|
2021-03-13 13:44:00 -05:00
|
|
|
fn new(
|
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2022-06-02 12:02:30 +02:00
|
|
|
fld_r: &'a mut (dyn FnMut(ty::BoundRegion) -> ty::Region<'tcx> + 'a),
|
|
|
|
|
fld_t: &'a mut (dyn FnMut(ty::BoundTy) -> Ty<'tcx> + 'a),
|
|
|
|
|
fld_c: &'a mut (dyn FnMut(ty::BoundVar, Ty<'tcx>) -> ty::Const<'tcx> + 'a),
|
2021-03-13 13:44:00 -05:00
|
|
|
) -> Self {
|
2019-12-22 17:42:04 -05:00
|
|
|
BoundVarReplacer { tcx, current_index: ty::INNERMOST, fld_r, fld_t, fld_c }
|
2018-10-24 23:41:40 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'a, 'tcx> TypeFolder<'tcx> for BoundVarReplacer<'a, 'tcx> {
|
|
|
|
|
fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
|
2019-06-12 00:11:55 +03:00
|
|
|
self.tcx
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2018-10-24 23:41:40 +02:00
|
|
|
|
2020-10-05 16:51:33 -04:00
|
|
|
fn fold_binder<T: TypeFoldable<'tcx>>(
|
|
|
|
|
&mut self,
|
|
|
|
|
t: ty::Binder<'tcx, T>,
|
2021-12-01 00:55:57 +00:00
|
|
|
) -> ty::Binder<'tcx, T> {
|
2018-10-24 23:41:40 +02:00
|
|
|
self.current_index.shift_in(1);
|
|
|
|
|
let t = t.super_fold_with(self);
|
|
|
|
|
self.current_index.shift_out(1);
|
|
|
|
|
t
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-01 00:55:57 +00:00
|
|
|
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
|
2020-08-03 00:49:11 +02:00
|
|
|
match *t.kind() {
|
2021-03-13 13:44:00 -05:00
|
|
|
ty::Bound(debruijn, bound_ty) if debruijn == self.current_index => {
|
2022-06-02 12:02:30 +02:00
|
|
|
let ty = (self.fld_t)(bound_ty);
|
|
|
|
|
ty::fold::shift_vars(self.tcx, ty, self.current_index.as_u32())
|
2018-10-24 23:41:40 +02:00
|
|
|
}
|
2022-06-02 12:02:30 +02:00
|
|
|
_ if t.has_vars_bound_at_or_above(self.current_index) => t.super_fold_with(self),
|
|
|
|
|
_ => t,
|
2018-10-24 23:41:40 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-01 00:55:57 +00:00
|
|
|
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
|
2018-10-24 23:41:40 +02:00
|
|
|
match *r {
|
|
|
|
|
ty::ReLateBound(debruijn, br) if debruijn == self.current_index => {
|
2022-06-02 12:02:30 +02:00
|
|
|
let region = (self.fld_r)(br);
|
|
|
|
|
if let ty::ReLateBound(debruijn1, br) = *region {
|
|
|
|
|
// If the callback returns a late-bound region,
|
|
|
|
|
// that region should always use the INNERMOST
|
|
|
|
|
// debruijn index. Then we adjust it to the
|
|
|
|
|
// correct depth.
|
|
|
|
|
assert_eq!(debruijn1, ty::INNERMOST);
|
|
|
|
|
self.tcx.mk_region(ty::ReLateBound(debruijn, br))
|
|
|
|
|
} else {
|
|
|
|
|
region
|
2018-10-24 23:41:40 +02:00
|
|
|
}
|
|
|
|
|
}
|
2022-06-02 12:02:30 +02:00
|
|
|
_ => r,
|
2018-10-24 23:41:40 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-03-08 01:19:53 +00:00
|
|
|
|
2022-02-02 14:24:45 +11:00
|
|
|
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
|
2022-06-10 11:18:06 +10:00
|
|
|
match ct.kind() {
|
2022-02-02 14:24:45 +11:00
|
|
|
ty::ConstKind::Bound(debruijn, bound_const) if debruijn == self.current_index => {
|
2022-06-02 12:02:30 +02:00
|
|
|
let ct = (self.fld_c)(bound_const, ct.ty());
|
|
|
|
|
ty::fold::shift_vars(self.tcx, ct, self.current_index.as_u32())
|
2019-03-09 16:54:50 +00:00
|
|
|
}
|
2022-06-02 12:02:30 +02:00
|
|
|
_ if ct.has_vars_bound_at_or_above(self.current_index) => ct.super_fold_with(self),
|
|
|
|
|
_ => ct,
|
2019-03-09 16:54:50 +00:00
|
|
|
}
|
2019-03-08 01:19:53 +00:00
|
|
|
}
|
2015-06-06 02:06:14 +03:00
|
|
|
}
|
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'tcx> TyCtxt<'tcx> {
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Replaces all regions bound by the given `Binder` with the
|
2017-11-21 11:17:48 -05:00
|
|
|
/// results returned by the closure; the closure is expected to
|
|
|
|
|
/// return a free region (relative to this binder), and hence the
|
|
|
|
|
/// binder is removed in the return type. The closure is invoked
|
2020-12-18 13:24:55 -05:00
|
|
|
/// once for each unique `BoundRegionKind`; multiple references to the
|
|
|
|
|
/// same `BoundRegionKind` will reuse the previous result. A map is
|
2017-11-21 11:17:48 -05:00
|
|
|
/// returned at the end with each bound region and the free region
|
|
|
|
|
/// that replaced it.
|
2018-10-24 23:41:40 +02:00
|
|
|
///
|
2022-06-02 12:02:30 +02:00
|
|
|
/// # Panics
|
|
|
|
|
///
|
|
|
|
|
/// This method only replaces late bound regions. Any types or
|
|
|
|
|
/// constants bound by `value` will cause an ICE.
|
2018-10-24 23:41:40 +02:00
|
|
|
pub fn replace_late_bound_regions<T, F>(
|
|
|
|
|
self,
|
2020-10-05 16:51:33 -04:00
|
|
|
value: Binder<'tcx, T>,
|
2020-12-18 13:24:55 -05:00
|
|
|
mut fld_r: F,
|
2018-10-24 23:41:40 +02:00
|
|
|
) -> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
|
2019-12-22 17:42:04 -05:00
|
|
|
where
|
|
|
|
|
F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
|
|
|
|
|
T: TypeFoldable<'tcx>,
|
2015-09-06 21:51:58 +03:00
|
|
|
{
|
2020-12-18 13:24:55 -05:00
|
|
|
let mut region_map = BTreeMap::new();
|
2022-06-02 13:31:04 +02:00
|
|
|
let real_fld_r = |br: ty::BoundRegion| *region_map.entry(br).or_insert_with(|| fld_r(br));
|
|
|
|
|
let value = self.replace_late_bound_regions_uncached(value, real_fld_r);
|
|
|
|
|
(value, region_map)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn replace_late_bound_regions_uncached<T, F>(
|
|
|
|
|
self,
|
|
|
|
|
value: Binder<'tcx, T>,
|
|
|
|
|
mut fld_r: F,
|
|
|
|
|
) -> T
|
|
|
|
|
where
|
|
|
|
|
F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
|
|
|
|
|
T: TypeFoldable<'tcx>,
|
|
|
|
|
{
|
2022-06-02 12:02:30 +02:00
|
|
|
let mut fld_t = |b| bug!("unexpected bound ty in binder: {b:?}");
|
|
|
|
|
let mut fld_c = |b, ty| bug!("unexpected bound ct in binder: {b:?} {ty}");
|
2021-03-13 13:44:00 -05:00
|
|
|
let value = value.skip_binder();
|
2022-06-02 13:31:04 +02:00
|
|
|
if !value.has_escaping_bound_vars() {
|
2021-03-13 13:44:00 -05:00
|
|
|
value
|
|
|
|
|
} else {
|
2022-06-02 13:31:04 +02:00
|
|
|
let mut replacer = BoundVarReplacer::new(self, &mut fld_r, &mut fld_t, &mut fld_c);
|
2021-12-01 00:55:57 +00:00
|
|
|
value.fold_with(&mut replacer)
|
2022-06-02 13:31:04 +02:00
|
|
|
}
|
2018-10-24 23:41:40 +02:00
|
|
|
}
|
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Replaces all escaping bound vars. The `fld_r` closure replaces escaping
|
2019-03-09 16:54:50 +00:00
|
|
|
/// bound regions; the `fld_t` closure replaces escaping bound types and the `fld_c`
|
|
|
|
|
/// closure replaces escaping bound consts.
|
2022-06-02 12:42:57 +02:00
|
|
|
pub fn replace_escaping_bound_vars_uncached<T, F, G, H>(
|
2018-10-24 23:41:40 +02:00
|
|
|
self,
|
2020-10-24 02:21:18 +02:00
|
|
|
value: T,
|
2018-10-24 23:41:40 +02:00
|
|
|
mut fld_r: F,
|
2019-03-09 16:54:50 +00:00
|
|
|
mut fld_t: G,
|
|
|
|
|
mut fld_c: H,
|
2020-12-18 13:24:55 -05:00
|
|
|
) -> T
|
2019-12-22 17:42:04 -05:00
|
|
|
where
|
|
|
|
|
F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
|
|
|
|
|
G: FnMut(ty::BoundTy) -> Ty<'tcx>,
|
2022-02-02 14:24:45 +11:00
|
|
|
H: FnMut(ty::BoundVar, Ty<'tcx>) -> ty::Const<'tcx>,
|
2019-12-22 17:42:04 -05:00
|
|
|
T: TypeFoldable<'tcx>,
|
2018-10-24 23:41:40 +02:00
|
|
|
{
|
|
|
|
|
if !value.has_escaping_bound_vars() {
|
2020-12-18 13:24:55 -05:00
|
|
|
value
|
2018-10-24 23:41:40 +02:00
|
|
|
} else {
|
2022-06-02 12:02:30 +02:00
|
|
|
let mut replacer = BoundVarReplacer::new(self, &mut fld_r, &mut fld_t, &mut fld_c);
|
2021-12-01 00:55:57 +00:00
|
|
|
value.fold_with(&mut replacer)
|
2018-10-24 23:41:40 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Replaces all types or regions bound by the given `Binder`. The `fld_r`
|
2022-06-02 12:02:30 +02:00
|
|
|
/// closure replaces bound regions, the `fld_t` closure replaces bound
|
|
|
|
|
/// types, and `fld_c` replaces bound constants.
|
2022-06-02 12:42:57 +02:00
|
|
|
pub fn replace_bound_vars_uncached<T, F, G, H>(
|
2018-10-24 23:41:40 +02:00
|
|
|
self,
|
2020-10-05 16:51:33 -04:00
|
|
|
value: Binder<'tcx, T>,
|
2022-06-02 12:42:57 +02:00
|
|
|
fld_r: F,
|
2019-03-09 16:54:50 +00:00
|
|
|
fld_t: G,
|
|
|
|
|
fld_c: H,
|
2022-06-02 12:42:57 +02:00
|
|
|
) -> T
|
2019-12-22 17:42:04 -05:00
|
|
|
where
|
|
|
|
|
F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
|
|
|
|
|
G: FnMut(ty::BoundTy) -> Ty<'tcx>,
|
2022-02-02 14:24:45 +11:00
|
|
|
H: FnMut(ty::BoundVar, Ty<'tcx>) -> ty::Const<'tcx>,
|
2019-12-22 17:42:04 -05:00
|
|
|
T: TypeFoldable<'tcx>,
|
2018-10-24 23:41:40 +02:00
|
|
|
{
|
2022-06-02 12:42:57 +02:00
|
|
|
self.replace_escaping_bound_vars_uncached(value.skip_binder(), fld_r, fld_t, fld_c)
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Replaces any late-bound regions bound in `value` with
|
2017-11-21 11:17:48 -05:00
|
|
|
/// free variants attached to `all_outlive_scope`.
|
2020-10-05 16:51:33 -04:00
|
|
|
pub fn liberate_late_bound_regions<T>(
|
|
|
|
|
self,
|
|
|
|
|
all_outlive_scope: DefId,
|
|
|
|
|
value: ty::Binder<'tcx, T>,
|
|
|
|
|
) -> T
|
2019-12-22 17:42:04 -05:00
|
|
|
where
|
|
|
|
|
T: TypeFoldable<'tcx>,
|
|
|
|
|
{
|
2022-06-02 13:31:04 +02:00
|
|
|
self.replace_late_bound_regions_uncached(value, |br| {
|
2017-11-21 11:17:48 -05:00
|
|
|
self.mk_region(ty::ReFree(ty::FreeRegion {
|
|
|
|
|
scope: all_outlive_scope,
|
2020-12-18 13:24:55 -05:00
|
|
|
bound_region: br.kind,
|
2017-11-21 11:17:48 -05:00
|
|
|
}))
|
2019-12-22 17:42:04 -05:00
|
|
|
})
|
2017-11-21 11:17:48 -05:00
|
|
|
}
|
|
|
|
|
|
2020-10-26 14:18:31 -04:00
|
|
|
pub fn shift_bound_var_indices<T>(self, bound_vars: usize, value: T) -> T
|
|
|
|
|
where
|
|
|
|
|
T: TypeFoldable<'tcx>,
|
|
|
|
|
{
|
2022-06-02 12:42:57 +02:00
|
|
|
self.replace_escaping_bound_vars_uncached(
|
2020-10-26 14:18:31 -04:00
|
|
|
value,
|
|
|
|
|
|r| {
|
|
|
|
|
self.mk_region(ty::ReLateBound(
|
|
|
|
|
ty::INNERMOST,
|
|
|
|
|
ty::BoundRegion {
|
|
|
|
|
var: ty::BoundVar::from_usize(r.var.as_usize() + bound_vars),
|
|
|
|
|
kind: r.kind,
|
|
|
|
|
},
|
|
|
|
|
))
|
|
|
|
|
},
|
|
|
|
|
|t| {
|
|
|
|
|
self.mk_ty(ty::Bound(
|
|
|
|
|
ty::INNERMOST,
|
|
|
|
|
ty::BoundTy {
|
|
|
|
|
var: ty::BoundVar::from_usize(t.var.as_usize() + bound_vars),
|
|
|
|
|
kind: t.kind,
|
|
|
|
|
},
|
|
|
|
|
))
|
|
|
|
|
},
|
|
|
|
|
|c, ty| {
|
2022-02-02 14:24:45 +11:00
|
|
|
self.mk_const(ty::ConstS {
|
2022-06-10 11:18:06 +10:00
|
|
|
kind: ty::ConstKind::Bound(
|
2020-10-26 14:18:31 -04:00
|
|
|
ty::INNERMOST,
|
|
|
|
|
ty::BoundVar::from_usize(c.as_usize() + bound_vars),
|
|
|
|
|
),
|
|
|
|
|
ty,
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Replaces any late-bound regions bound in `value` with `'erased`. Useful in codegen but also
|
2015-09-06 21:51:58 +03:00
|
|
|
/// method lookup and a few other places where precise region relationships are not required.
|
2020-10-05 16:51:33 -04:00
|
|
|
pub fn erase_late_bound_regions<T>(self, value: Binder<'tcx, T>) -> T
|
2019-12-22 17:42:04 -05:00
|
|
|
where
|
|
|
|
|
T: TypeFoldable<'tcx>,
|
2015-09-06 21:51:58 +03:00
|
|
|
{
|
2019-04-25 22:05:04 +01:00
|
|
|
self.replace_late_bound_regions(value, |_| self.lifetimes.re_erased).0
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Rewrite any late-bound regions so that they are anonymous. Region numbers are
|
2020-10-29 18:42:31 -04:00
|
|
|
/// assigned starting at 0 and increasing monotonically in the order traversed
|
2015-09-06 21:51:58 +03:00
|
|
|
/// by the fold operation.
|
|
|
|
|
///
|
|
|
|
|
/// The chief purpose of this function is to canonicalize regions so that two
|
|
|
|
|
/// `FnSig`s or `TraitRef`s which are equivalent up to region naming will become
|
2019-02-08 14:53:55 +01:00
|
|
|
/// structurally identical. For example, `for<'a, 'b> fn(&'a isize, &'b isize)` and
|
2015-09-06 21:51:58 +03:00
|
|
|
/// `for<'a, 'b> fn(&'b isize, &'a isize)` will become identical after anonymization.
|
2020-10-05 16:51:33 -04:00
|
|
|
pub fn anonymize_late_bound_regions<T>(self, sig: Binder<'tcx, T>) -> Binder<'tcx, T>
|
2019-12-22 17:42:04 -05:00
|
|
|
where
|
|
|
|
|
T: TypeFoldable<'tcx>,
|
2015-09-06 21:51:58 +03:00
|
|
|
{
|
|
|
|
|
let mut counter = 0;
|
2020-10-26 14:18:31 -04:00
|
|
|
let inner = self
|
|
|
|
|
.replace_late_bound_regions(sig, |_| {
|
|
|
|
|
let br = ty::BoundRegion {
|
|
|
|
|
var: ty::BoundVar::from_u32(counter),
|
|
|
|
|
kind: ty::BrAnon(counter),
|
|
|
|
|
};
|
2020-12-18 13:24:55 -05:00
|
|
|
let r = self.mk_region(ty::ReLateBound(ty::INNERMOST, br));
|
2019-12-22 17:42:04 -05:00
|
|
|
counter += 1;
|
2020-10-29 18:42:31 -04:00
|
|
|
r
|
2019-12-22 17:42:04 -05:00
|
|
|
})
|
2020-10-26 14:18:31 -04:00
|
|
|
.0;
|
|
|
|
|
let bound_vars = self.mk_bound_variable_kinds(
|
|
|
|
|
(0..counter).map(|i| ty::BoundVariableKind::Region(ty::BrAnon(i))),
|
|
|
|
|
);
|
|
|
|
|
Binder::bind_with_vars(inner, bound_vars)
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-15 16:47:59 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-10-22 22:38:51 +02:00
|
|
|
// Shifter
|
2014-11-15 16:47:59 -05:00
|
|
|
//
|
2018-10-22 22:38:51 +02:00
|
|
|
// Shifts the De Bruijn indices on all escaping bound vars by a
|
2014-11-15 16:47:59 -05:00
|
|
|
// fixed amount. Useful in substitution or when otherwise introducing
|
|
|
|
|
// a binding level that is not intended to capture the existing bound
|
2018-10-22 22:38:51 +02:00
|
|
|
// vars. See comment on `shift_vars_through_binders` method in
|
2014-11-15 16:47:59 -05:00
|
|
|
// `subst.rs` for more details.
|
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
struct Shifter<'tcx> {
|
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2018-10-22 22:38:51 +02:00
|
|
|
current_index: ty::DebruijnIndex,
|
|
|
|
|
amount: u32,
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 19:32:30 -05:00
|
|
|
impl<'tcx> Shifter<'tcx> {
|
2020-10-09 11:20:28 +02:00
|
|
|
pub fn new(tcx: TyCtxt<'tcx>, amount: u32) -> Self {
|
|
|
|
|
Shifter { tcx, current_index: ty::INNERMOST, amount }
|
2018-10-22 22:38:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 19:32:30 -05:00
|
|
|
impl<'tcx> TypeFolder<'tcx> for Shifter<'tcx> {
|
2019-06-14 00:48:52 +03:00
|
|
|
fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
|
2019-06-12 00:11:55 +03:00
|
|
|
self.tcx
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2018-10-22 22:38:51 +02:00
|
|
|
|
2020-10-05 16:51:33 -04:00
|
|
|
fn fold_binder<T: TypeFoldable<'tcx>>(
|
|
|
|
|
&mut self,
|
|
|
|
|
t: ty::Binder<'tcx, T>,
|
2021-12-01 00:55:57 +00:00
|
|
|
) -> ty::Binder<'tcx, T> {
|
2018-10-22 22:38:51 +02:00
|
|
|
self.current_index.shift_in(1);
|
|
|
|
|
let t = t.super_fold_with(self);
|
|
|
|
|
self.current_index.shift_out(1);
|
|
|
|
|
t
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-01 00:55:57 +00:00
|
|
|
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
|
2018-10-22 22:38:51 +02:00
|
|
|
match *r {
|
|
|
|
|
ty::ReLateBound(debruijn, br) => {
|
|
|
|
|
if self.amount == 0 || debruijn < self.current_index {
|
2021-12-01 00:55:57 +00:00
|
|
|
r
|
2018-10-22 22:38:51 +02:00
|
|
|
} else {
|
2020-10-09 11:20:28 +02:00
|
|
|
let debruijn = debruijn.shifted_in(self.amount);
|
2018-11-09 14:49:37 +01:00
|
|
|
let shifted = ty::ReLateBound(debruijn, br);
|
2021-12-01 00:55:57 +00:00
|
|
|
self.tcx.mk_region(shifted)
|
2018-10-22 22:38:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
2021-12-01 00:55:57 +00:00
|
|
|
_ => r,
|
2018-10-22 22:38:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-01 00:55:57 +00:00
|
|
|
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
|
2020-08-03 00:49:11 +02:00
|
|
|
match *ty.kind() {
|
2018-11-03 15:15:33 +01:00
|
|
|
ty::Bound(debruijn, bound_ty) => {
|
|
|
|
|
if self.amount == 0 || debruijn < self.current_index {
|
2021-12-01 00:55:57 +00:00
|
|
|
ty
|
2018-10-22 22:38:51 +02:00
|
|
|
} else {
|
2020-10-09 11:20:28 +02:00
|
|
|
let debruijn = debruijn.shifted_in(self.amount);
|
2021-12-01 00:55:57 +00:00
|
|
|
self.tcx.mk_ty(ty::Bound(debruijn, bound_ty))
|
2018-10-22 22:38:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_ => ty.super_fold_with(self),
|
2014-11-15 16:47:59 -05:00
|
|
|
}
|
|
|
|
|
}
|
2019-03-08 01:19:53 +00:00
|
|
|
|
2022-02-02 14:24:45 +11:00
|
|
|
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
|
2022-06-10 11:18:06 +10:00
|
|
|
if let ty::ConstKind::Bound(debruijn, bound_ct) = ct.kind() {
|
2019-03-01 01:16:04 -05:00
|
|
|
if self.amount == 0 || debruijn < self.current_index {
|
2021-12-01 00:55:57 +00:00
|
|
|
ct
|
2019-03-01 01:16:04 -05:00
|
|
|
} else {
|
2020-10-09 11:20:28 +02:00
|
|
|
let debruijn = debruijn.shifted_in(self.amount);
|
2022-02-02 14:24:45 +11:00
|
|
|
self.tcx.mk_const(ty::ConstS {
|
2022-06-10 11:18:06 +10:00
|
|
|
kind: ty::ConstKind::Bound(debruijn, bound_ct),
|
2022-02-02 14:24:45 +11:00
|
|
|
ty: ct.ty(),
|
|
|
|
|
})
|
2019-03-01 01:16:04 -05:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
ct.super_fold_with(self)
|
|
|
|
|
}
|
2019-03-08 01:19:53 +00:00
|
|
|
}
|
2014-11-15 16:47:59 -05:00
|
|
|
}
|
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
pub fn shift_region<'tcx>(
|
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2018-10-24 10:29:42 +02:00
|
|
|
region: ty::Region<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
amount: u32,
|
2018-10-24 10:29:42 +02:00
|
|
|
) -> ty::Region<'tcx> {
|
2022-01-28 11:25:15 +11:00
|
|
|
match *region {
|
2018-10-24 10:29:42 +02:00
|
|
|
ty::ReLateBound(debruijn, br) if amount > 0 => {
|
2022-01-28 11:25:15 +11:00
|
|
|
tcx.mk_region(ty::ReLateBound(debruijn.shifted_in(amount), br))
|
2017-04-20 01:58:12 +03:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => region,
|
2017-04-20 01:58:12 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-24 02:21:18 +02:00
|
|
|
pub fn shift_vars<'tcx, T>(tcx: TyCtxt<'tcx>, value: T, amount: u32) -> T
|
2019-06-12 00:11:55 +03:00
|
|
|
where
|
|
|
|
|
T: TypeFoldable<'tcx>,
|
|
|
|
|
{
|
2019-12-22 17:42:04 -05:00
|
|
|
debug!("shift_vars(value={:?}, amount={})", value, amount);
|
2014-11-15 16:47:59 -05:00
|
|
|
|
2021-12-01 00:55:57 +00:00
|
|
|
value.fold_with(&mut Shifter::new(tcx, amount))
|
2014-11-15 16:47:59 -05:00
|
|
|
}
|