2022-02-08 11:46:53 +11:00
|
|
|
//! A generalized traversal mechanism for complex data structures that contain
|
|
|
|
|
//! type information.
|
2014-11-25 21:17:11 -05:00
|
|
|
//!
|
2022-02-08 11:46:53 +11:00
|
|
|
//! There are two types of traversal.
|
|
|
|
|
//! - Folding. 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`.
|
|
|
|
|
//! - Visiting. This is a read-only traversal of the data structure.
|
2014-11-25 21:17:11 -05:00
|
|
|
//!
|
2022-02-08 11:46:53 +11:00
|
|
|
//! These traversals have limited flexibility. Only a small number of "types of
|
|
|
|
|
//! interest" within the complex data structures can receive custom
|
|
|
|
|
//! modification (when folding) or custom visitation (when visiting). 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-02-08 11:46:53 +11:00
|
|
|
//! There are two traits involved in each traversal type.
|
|
|
|
|
//! - The first trait is `TypeFoldable`, which is implemented once for many
|
|
|
|
|
//! types. This includes both (a) types of interest, and (b) all other
|
|
|
|
|
//! relevant types, including generic containers like `Vec` and `Option`. It
|
|
|
|
|
//! defines a "skeleton" of how they should be traversed, for both folding
|
|
|
|
|
//! and visiting.
|
|
|
|
|
//! - The second trait is `TypeFolder`/`FallibleTypeFolder` (for
|
|
|
|
|
//! infallible/fallible folding traversals) or `TypeVisitor` (for visiting
|
|
|
|
|
//! traversals). One of these is implemented for each folder/visitor. This
|
|
|
|
|
//! defines how types of interest are handled.
|
2014-11-25 21:17:11 -05:00
|
|
|
//!
|
2022-02-08 11:46:53 +11:00
|
|
|
//! This means each traversal is a mixture of (a) generic traversal operations,
|
|
|
|
|
//! and (b) custom fold/visit operations that are specific to the
|
|
|
|
|
//! folder/visitor.
|
|
|
|
|
//! - The `TypeFoldable` impls handle most of the traversal, and call into
|
|
|
|
|
//! `TypeFolder`/`FallibleTypeFolder`/`TypeVisitor` when they encounter a
|
|
|
|
|
//! type of interest.
|
|
|
|
|
//! - A `TypeFolder`/`FallibleTypeFolder`/`TypeVisitor` may also call back into
|
|
|
|
|
//! a `TypeFoldable` impl, because (a) the types of interest are recursive
|
|
|
|
|
//! and can contain other types of interest, and (b) each folder/visitor
|
|
|
|
|
//! 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:
|
|
|
|
|
//! TypeFoldable`, and an instance `S(ty, u)`, it would be visited like so:
|
|
|
|
|
//! ```
|
|
|
|
|
//! s.visit_with(visitor) calls
|
|
|
|
|
//! - s.super_visit_with(visitor) calls
|
|
|
|
|
//! - ty.visit_with(visitor) calls
|
|
|
|
|
//! - visitor.visit_ty(ty) may call
|
|
|
|
|
//! - ty.super_visit_with(visitor)
|
|
|
|
|
//! - u.visit_with(visitor)
|
|
|
|
|
//! ```
|
2021-03-30 14:26:40 +00:00
|
|
|
use crate::mir;
|
2019-12-22 17:42:04 -05:00
|
|
|
use crate::ty::{self, flags::FlagComputation, Binder, Ty, TyCtxt, TypeFlags};
|
2022-01-22 18:49:12 -06:00
|
|
|
use rustc_errors::ErrorGuaranteed;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir::def_id::DefId;
|
2015-06-18 08:51:23 +03:00
|
|
|
|
2019-12-24 05:02:53 +01:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2020-10-05 20:41:46 -04:00
|
|
|
use rustc_data_structures::sso::SsoHashSet;
|
2018-05-14 22:27:45 +10:00
|
|
|
use std::collections::BTreeMap;
|
2015-06-18 08:51:23 +03:00
|
|
|
use std::fmt;
|
2020-10-21 14:22:44 +02:00
|
|
|
use std::ops::ControlFlow;
|
2013-10-29 05:25:18 -04:00
|
|
|
|
2022-02-08 11:46:53 +11:00
|
|
|
/// This trait is implemented for every type that can be folded/visited,
|
|
|
|
|
/// 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`.
|
2015-06-18 08:51:23 +03:00
|
|
|
pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
|
2022-02-08 11:46:53 +11:00
|
|
|
/// The main entry point for folding. To fold a value `t` with a folder `f`
|
|
|
|
|
/// call: `t.try_fold_with(f)`.
|
|
|
|
|
///
|
|
|
|
|
/// For types of interest (such as `Ty`), this default is overridden with a
|
|
|
|
|
/// method that calls a folder method specifically for that type (such as
|
|
|
|
|
/// `F::try_fold_ty`). This is where control transfers from `TypeFoldable`
|
|
|
|
|
/// to `TypeFolder`.
|
|
|
|
|
///
|
|
|
|
|
/// For other types, this default is used.
|
|
|
|
|
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
|
|
|
|
|
self.try_super_fold_with(folder)
|
2021-12-01 00:55:57 +00:00
|
|
|
}
|
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`.
|
2021-12-01 00:55:57 +00:00
|
|
|
fn fold_with<F: TypeFolder<'tcx, Error = !>>(self, folder: &mut F) -> Self {
|
|
|
|
|
self.try_fold_with(folder).into_ok()
|
|
|
|
|
}
|
2021-12-01 12:48:49 +00:00
|
|
|
|
2022-02-08 11:46:53 +11:00
|
|
|
/// Traverses the type in question, typically by calling `try_fold_with` on
|
|
|
|
|
/// each field/element. This is true even for types of interest such as
|
|
|
|
|
/// `Ty`. This should only be called within `TypeFolder` methods, when
|
|
|
|
|
/// non-custom traversals are desired for types of interest.
|
2021-12-01 15:11:24 +00:00
|
|
|
fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
|
2021-12-01 00:55:57 +00:00
|
|
|
self,
|
|
|
|
|
folder: &mut F,
|
|
|
|
|
) -> Result<Self, F::Error>;
|
2021-12-01 12:48:49 +00:00
|
|
|
|
2022-02-08 11:46:53 +11:00
|
|
|
/// 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`.
|
|
|
|
|
fn super_fold_with<F: TypeFolder<'tcx, Error = !>>(self, folder: &mut F) -> Self {
|
|
|
|
|
self.try_super_fold_with(folder).into_ok()
|
2015-11-18 09:38:57 +00:00
|
|
|
}
|
|
|
|
|
|
2022-02-08 11:46:53 +11:00
|
|
|
/// The entry point for visiting. To visit a value `t` with a visitor `v`
|
|
|
|
|
/// call: `t.visit_with(v)`.
|
|
|
|
|
///
|
|
|
|
|
/// For types of interest (such as `Ty`), this default is overridden with a
|
|
|
|
|
/// method that calls a visitor method specifically for that type (such as
|
|
|
|
|
/// `V::visit_ty`). This is where control transfers from `TypeFoldable` to
|
|
|
|
|
/// `TypeVisitor`.
|
|
|
|
|
///
|
|
|
|
|
/// For other types, this default is used.
|
2020-11-05 17:30:39 +01:00
|
|
|
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
|
2016-01-06 02:01:28 +00:00
|
|
|
self.super_visit_with(visitor)
|
2015-11-18 09:38:57 +00:00
|
|
|
}
|
2015-12-18 10:07:06 +00:00
|
|
|
|
2022-02-08 11:46:53 +11:00
|
|
|
/// Traverses the type in question, typically by calling `visit_with` on
|
|
|
|
|
/// each field/element. This is true even for types of interest such as
|
|
|
|
|
/// `Ty`. This should only be called within `TypeVisitor` methods, when
|
|
|
|
|
/// non-custom traversals are desired for types of interest.
|
|
|
|
|
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy>;
|
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Returns `true` if `self` has any late-bound regions that are either
|
2018-05-25 09:58:29 -04:00
|
|
|
/// bound by `binder` or bound by some binder outside of `binder`.
|
2018-06-10 16:28:27 +02:00
|
|
|
/// If `binder` is `ty::INNERMOST`, this indicates whether
|
2018-05-25 09:58:29 -04:00
|
|
|
/// there are any late-bound regions that appear free.
|
2018-10-22 22:38:51 +02:00
|
|
|
fn has_vars_bound_at_or_above(&self, binder: ty::DebruijnIndex) -> bool {
|
2020-10-22 10:20:24 +02:00
|
|
|
self.visit_with(&mut HasEscapingVarsVisitor { outer_index: binder }).is_break()
|
2018-05-28 12:38:39 -04:00
|
|
|
}
|
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Returns `true` if this `self` has any regions that escape `binder` (and
|
2018-05-28 12:38:39 -04:00
|
|
|
/// hence are not bound by it).
|
2018-10-22 22:38:51 +02:00
|
|
|
fn has_vars_bound_above(&self, binder: ty::DebruijnIndex) -> bool {
|
|
|
|
|
self.has_vars_bound_at_or_above(binder.shifted_in(1))
|
2018-05-25 09:58:29 -04:00
|
|
|
}
|
|
|
|
|
|
2018-10-22 22:38:51 +02:00
|
|
|
fn has_escaping_bound_vars(&self) -> bool {
|
|
|
|
|
self.has_vars_bound_at_or_above(ty::INNERMOST)
|
2015-12-18 10:07:06 +00:00
|
|
|
}
|
|
|
|
|
|
2021-08-20 13:36:04 +00:00
|
|
|
#[instrument(level = "trace")]
|
2015-12-18 10:07:06 +00:00
|
|
|
fn has_type_flags(&self, flags: TypeFlags) -> bool {
|
2022-01-12 03:19:52 +00:00
|
|
|
self.visit_with(&mut HasTypeFlagsVisitor { flags }).break_value() == Some(FoundFlags)
|
2015-12-18 10:07:06 +00:00
|
|
|
}
|
2017-08-07 08:08:53 +03:00
|
|
|
fn has_projections(&self) -> bool {
|
2015-12-18 10:07:06 +00:00
|
|
|
self.has_type_flags(TypeFlags::HAS_PROJECTION)
|
|
|
|
|
}
|
2020-01-17 20:11:51 +00:00
|
|
|
fn has_opaque_types(&self) -> bool {
|
|
|
|
|
self.has_type_flags(TypeFlags::HAS_TY_OPAQUE)
|
|
|
|
|
}
|
2015-12-18 10:07:06 +00:00
|
|
|
fn references_error(&self) -> bool {
|
2020-04-16 13:47:47 +03:00
|
|
|
self.has_type_flags(TypeFlags::HAS_ERROR)
|
2015-12-18 10:07:06 +00:00
|
|
|
}
|
2022-01-22 18:49:12 -06:00
|
|
|
fn error_reported(&self) -> Option<ErrorGuaranteed> {
|
|
|
|
|
if self.references_error() {
|
|
|
|
|
Some(ErrorGuaranteed::unchecked_claim_error_was_emitted())
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-12 03:19:52 +00:00
|
|
|
fn has_param_types_or_consts(&self) -> bool {
|
|
|
|
|
self.has_type_flags(TypeFlags::HAS_TY_PARAM | TypeFlags::HAS_CT_PARAM)
|
2015-12-18 10:07:06 +00:00
|
|
|
}
|
2020-06-16 18:27:40 +01:00
|
|
|
fn has_infer_regions(&self) -> bool {
|
|
|
|
|
self.has_type_flags(TypeFlags::HAS_RE_INFER)
|
|
|
|
|
}
|
2015-12-18 10:07:06 +00:00
|
|
|
fn has_infer_types(&self) -> bool {
|
|
|
|
|
self.has_type_flags(TypeFlags::HAS_TY_INFER)
|
|
|
|
|
}
|
2020-02-22 14:10:17 +00:00
|
|
|
fn has_infer_types_or_consts(&self) -> bool {
|
|
|
|
|
self.has_type_flags(TypeFlags::HAS_TY_INFER | TypeFlags::HAS_CT_INFER)
|
|
|
|
|
}
|
2015-12-18 10:07:06 +00:00
|
|
|
fn needs_infer(&self) -> bool {
|
2020-02-22 15:09:17 +00:00
|
|
|
self.has_type_flags(TypeFlags::NEEDS_INFER)
|
2015-12-18 10:07:06 +00:00
|
|
|
}
|
2018-11-02 18:56:30 +01:00
|
|
|
fn has_placeholders(&self) -> bool {
|
2019-03-12 20:25:41 +00:00
|
|
|
self.has_type_flags(
|
2019-12-22 17:42:04 -05:00
|
|
|
TypeFlags::HAS_RE_PLACEHOLDER
|
|
|
|
|
| TypeFlags::HAS_TY_PLACEHOLDER
|
|
|
|
|
| TypeFlags::HAS_CT_PLACEHOLDER,
|
2019-03-12 20:25:41 +00:00
|
|
|
)
|
2018-04-04 17:21:50 -04:00
|
|
|
}
|
2022-01-12 03:19:52 +00:00
|
|
|
fn needs_subst(&self) -> bool {
|
|
|
|
|
self.has_type_flags(TypeFlags::NEEDS_SUBST)
|
2015-12-18 10:07:06 +00:00
|
|
|
}
|
2017-12-04 05:11:36 -05:00
|
|
|
/// "Free" regions in this context means that it has any region
|
|
|
|
|
/// that is not (a) erased or (b) late-bound.
|
2022-01-12 03:19:52 +00:00
|
|
|
fn has_free_regions(&self) -> bool {
|
|
|
|
|
self.has_type_flags(TypeFlags::HAS_FREE_REGIONS)
|
2017-12-04 05:11:36 -05:00
|
|
|
}
|
|
|
|
|
|
2019-12-28 15:52:51 +00:00
|
|
|
fn has_erased_regions(&self) -> bool {
|
|
|
|
|
self.has_type_flags(TypeFlags::HAS_RE_ERASED)
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-04 17:49:35 +00:00
|
|
|
/// True if there are any un-erased free regions.
|
2022-01-12 03:19:52 +00:00
|
|
|
fn has_erasable_regions(&self) -> bool {
|
|
|
|
|
self.has_type_flags(TypeFlags::HAS_FREE_REGIONS)
|
2015-12-18 10:07:06 +00:00
|
|
|
}
|
2017-12-04 05:11:36 -05:00
|
|
|
|
2015-12-18 10:07:06 +00:00
|
|
|
/// Indicates whether this value references only 'global'
|
2019-02-20 01:15:21 +00:00
|
|
|
/// generic parameters that are the same regardless of what fn we are
|
2018-05-15 21:48:35 +01:00
|
|
|
/// in. This is used for caching.
|
2022-01-12 03:19:52 +00:00
|
|
|
fn is_global(&self) -> bool {
|
|
|
|
|
!self.has_type_flags(TypeFlags::HAS_FREE_LOCAL_NAMES)
|
2018-05-15 21:48:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// True if there are any late-bound regions
|
|
|
|
|
fn has_late_bound_regions(&self) -> bool {
|
|
|
|
|
self.has_type_flags(TypeFlags::HAS_RE_LATE_BOUND)
|
2015-12-18 10:07:06 +00:00
|
|
|
}
|
2018-05-22 12:09:35 -03:00
|
|
|
|
2020-04-01 16:20:27 +01:00
|
|
|
/// Indicates whether this value still has parameters/placeholders/inference variables
|
|
|
|
|
/// which could be replaced later, in a way that would change the results of `impl`
|
|
|
|
|
/// specialization.
|
|
|
|
|
fn still_further_specializable(&self) -> bool {
|
|
|
|
|
self.has_type_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE)
|
|
|
|
|
}
|
2014-05-12 17:12:51 -04:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
///
|
|
|
|
|
/// If this folder is fallible (and therefore its [`Error`][`TypeFolder::Error`]
|
2022-02-08 11:46:53 +11:00
|
|
|
/// associated type is something other than the default `!`) then
|
|
|
|
|
/// [`FallibleTypeFolder`] should be implemented manually. Otherwise,
|
2021-12-01 15:11:24 +00: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.
|
2019-06-14 00:48:52 +03:00
|
|
|
pub trait TypeFolder<'tcx>: Sized {
|
2021-05-19 13:20:39 +02:00
|
|
|
type 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>,
|
2021-12-01 00:55:57 +00:00
|
|
|
Self: TypeFolder<'tcx, Error = !>,
|
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
|
|
|
}
|
|
|
|
|
|
2021-12-01 00:55:57 +00:00
|
|
|
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx>
|
|
|
|
|
where
|
|
|
|
|
Self: TypeFolder<'tcx, Error = !>,
|
|
|
|
|
{
|
2016-01-06 02:01:28 +00:00
|
|
|
t.super_fold_with(self)
|
2013-10-29 05:25:18 -04:00
|
|
|
}
|
|
|
|
|
|
2021-12-01 00:55:57 +00:00
|
|
|
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx>
|
|
|
|
|
where
|
|
|
|
|
Self: TypeFolder<'tcx, Error = !>,
|
|
|
|
|
{
|
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-02-02 14:24:45 +11:00
|
|
|
fn fold_const(&mut self, c: ty::Const<'tcx>) -> ty::Const<'tcx>
|
2021-12-01 00:55:57 +00:00
|
|
|
where
|
|
|
|
|
Self: TypeFolder<'tcx, Error = !>,
|
|
|
|
|
{
|
|
|
|
|
c.super_fold_with(self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx>
|
|
|
|
|
where
|
|
|
|
|
Self: TypeFolder<'tcx, Error = !>,
|
|
|
|
|
{
|
|
|
|
|
p.super_fold_with(self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn fold_mir_const(&mut self, c: mir::ConstantKind<'tcx>) -> mir::ConstantKind<'tcx>
|
|
|
|
|
where
|
|
|
|
|
Self: TypeFolder<'tcx, Error = !>,
|
|
|
|
|
{
|
|
|
|
|
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.
|
2021-12-01 15:11:24 +00:00
|
|
|
pub trait FallibleTypeFolder<'tcx>: TypeFolder<'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
|
|
|
|
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
|
|
|
|
|
F: TypeFolder<'tcx, Error = !>,
|
|
|
|
|
{
|
|
|
|
|
fn try_fold_binder<T>(&mut self, t: Binder<'tcx, T>) -> Result<Binder<'tcx, T>, Self::Error>
|
|
|
|
|
where
|
|
|
|
|
T: TypeFoldable<'tcx>,
|
|
|
|
|
{
|
|
|
|
|
Ok(self.fold_binder(t))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn try_fold_ty(&mut self, t: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
|
|
|
|
|
Ok(self.fold_ty(t))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn try_fold_region(&mut self, r: ty::Region<'tcx>) -> Result<ty::Region<'tcx>, Self::Error> {
|
|
|
|
|
Ok(self.fold_region(r))
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
Ok(self.fold_const(c))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn try_fold_predicate(
|
|
|
|
|
&mut self,
|
|
|
|
|
p: ty::Predicate<'tcx>,
|
|
|
|
|
) -> Result<ty::Predicate<'tcx>, Self::Error> {
|
|
|
|
|
Ok(self.fold_predicate(p))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn try_fold_mir_const(
|
|
|
|
|
&mut self,
|
|
|
|
|
c: mir::ConstantKind<'tcx>,
|
|
|
|
|
) -> Result<mir::ConstantKind<'tcx>, Self::Error> {
|
|
|
|
|
Ok(self.fold_mir_const(c))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-08 11:46:53 +11:00
|
|
|
/// This trait is implemented for every visiting traversal. There is a visit
|
|
|
|
|
/// method defined for every type of interest. Each such method has a default
|
|
|
|
|
/// that recurses into the type's fields in a non-custom fashion.
|
2019-12-22 17:42:04 -05:00
|
|
|
pub trait TypeVisitor<'tcx>: Sized {
|
2020-11-14 21:46:39 +01:00
|
|
|
type BreakTy = !;
|
2020-11-05 17:30:39 +01:00
|
|
|
|
2020-10-05 16:51:33 -04:00
|
|
|
fn visit_binder<T: TypeFoldable<'tcx>>(
|
|
|
|
|
&mut self,
|
|
|
|
|
t: &Binder<'tcx, T>,
|
|
|
|
|
) -> ControlFlow<Self::BreakTy> {
|
2016-01-08 23:34:05 +00:00
|
|
|
t.super_visit_with(self)
|
|
|
|
|
}
|
2014-08-27 21:46:52 -04:00
|
|
|
|
2020-11-05 17:30:39 +01:00
|
|
|
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2016-01-06 02:01:28 +00:00
|
|
|
t.super_visit_with(self)
|
2014-05-12 17:12:51 -04:00
|
|
|
}
|
|
|
|
|
|
2020-11-05 17:30:39 +01:00
|
|
|
fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2016-01-06 02:01:28 +00:00
|
|
|
r.super_visit_with(self)
|
2014-05-06 15:16:11 -04:00
|
|
|
}
|
2017-08-07 08:08:53 +03:00
|
|
|
|
2022-02-02 14:24:45 +11:00
|
|
|
fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2017-08-07 08:08:53 +03:00
|
|
|
c.super_visit_with(self)
|
|
|
|
|
}
|
2020-10-23 13:58:32 +02:00
|
|
|
|
2022-01-12 23:29:10 +00:00
|
|
|
fn visit_unevaluated_const(&mut self, uv: ty::Unevaluated<'tcx>) -> ControlFlow<Self::BreakTy> {
|
|
|
|
|
uv.super_visit_with(self)
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-05 17:30:39 +01:00
|
|
|
fn visit_predicate(&mut self, p: ty::Predicate<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2020-10-23 13:58:32 +02:00
|
|
|
p.super_visit_with(self)
|
|
|
|
|
}
|
2014-05-06 15:16:11 -04:00
|
|
|
}
|
|
|
|
|
|
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,
|
2015-09-06 21:51:58 +03:00
|
|
|
skipped_regions: &mut bool,
|
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
|
|
|
{
|
2021-12-01 00:55:57 +00:00
|
|
|
value.fold_with(&mut RegionFolder::new(self, skipped_regions, &mut f))
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
2017-10-24 14:20:32 -04:00
|
|
|
|
2018-07-03 06:24:45 -04:00
|
|
|
/// Invoke `callback` on every region appearing free in `value`.
|
|
|
|
|
pub fn for_each_free_region(
|
|
|
|
|
self,
|
|
|
|
|
value: &impl TypeFoldable<'tcx>,
|
|
|
|
|
mut callback: impl FnMut(ty::Region<'tcx>),
|
|
|
|
|
) {
|
|
|
|
|
self.any_free_region_meets(value, |r| {
|
|
|
|
|
callback(r);
|
|
|
|
|
false
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Returns `true` if `callback` returns true for every region appearing free in `value`.
|
2018-07-03 06:24:45 -04:00
|
|
|
pub fn all_free_regions_meet(
|
|
|
|
|
self,
|
|
|
|
|
value: &impl TypeFoldable<'tcx>,
|
|
|
|
|
mut callback: impl FnMut(ty::Region<'tcx>) -> bool,
|
|
|
|
|
) -> bool {
|
|
|
|
|
!self.any_free_region_meets(value, |r| !callback(r))
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Returns `true` if `callback` returns true for some region appearing free in `value`.
|
2018-07-03 06:24:45 -04:00
|
|
|
pub fn any_free_region_meets(
|
|
|
|
|
self,
|
|
|
|
|
value: &impl TypeFoldable<'tcx>,
|
|
|
|
|
callback: impl FnMut(ty::Region<'tcx>) -> bool,
|
|
|
|
|
) -> bool {
|
2022-01-12 03:19:52 +00:00
|
|
|
struct RegionVisitor<F> {
|
2018-05-28 10:13:21 -04:00
|
|
|
/// The index of a binder *just outside* the things we have
|
|
|
|
|
/// traversed. If we encounter a bound region bound by this
|
|
|
|
|
/// binder or one outer to it, it appears free. Example:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// for<'a> fn(for<'b> fn(), T)
|
|
|
|
|
/// ^ ^ ^ ^
|
|
|
|
|
/// | | | | here, would be shifted in 1
|
|
|
|
|
/// | | | here, would be shifted in 2
|
2019-02-08 14:53:55 +01:00
|
|
|
/// | | here, would be `INNERMOST` shifted in by 1
|
|
|
|
|
/// | here, initially, binder would be `INNERMOST`
|
2018-05-28 10:13:21 -04:00
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// You see that, initially, *any* bound value is free,
|
|
|
|
|
/// because we've not traversed any binders. As we pass
|
|
|
|
|
/// through a binder, we shift the `outer_index` by 1 to
|
|
|
|
|
/// account for the new binder that encloses us.
|
|
|
|
|
outer_index: ty::DebruijnIndex,
|
2017-10-24 14:20:32 -04:00
|
|
|
callback: F,
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 03:19:52 +00:00
|
|
|
impl<'tcx, F> TypeVisitor<'tcx> for RegionVisitor<F>
|
2019-12-22 17:42:04 -05:00
|
|
|
where
|
|
|
|
|
F: FnMut(ty::Region<'tcx>) -> bool,
|
2017-10-24 14:20:32 -04:00
|
|
|
{
|
2020-11-14 21:46:39 +01:00
|
|
|
type BreakTy = ();
|
|
|
|
|
|
2020-11-05 17:30:39 +01:00
|
|
|
fn visit_binder<T: TypeFoldable<'tcx>>(
|
|
|
|
|
&mut self,
|
2020-10-05 16:51:33 -04:00
|
|
|
t: &Binder<'tcx, T>,
|
2020-11-05 17:30:39 +01:00
|
|
|
) -> ControlFlow<Self::BreakTy> {
|
2018-05-28 10:13:21 -04:00
|
|
|
self.outer_index.shift_in(1);
|
2020-06-24 23:40:33 +02:00
|
|
|
let result = t.as_ref().skip_binder().visit_with(self);
|
2018-05-28 10:13:21 -04:00
|
|
|
self.outer_index.shift_out(1);
|
2018-07-03 06:24:45 -04:00
|
|
|
result
|
2017-10-24 14:20:32 -04:00
|
|
|
}
|
|
|
|
|
|
2020-11-05 17:30:39 +01:00
|
|
|
fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2017-10-24 14:20:32 -04:00
|
|
|
match *r {
|
2018-05-28 10:13:21 -04:00
|
|
|
ty::ReLateBound(debruijn, _) if debruijn < self.outer_index => {
|
2020-10-21 14:22:44 +02:00
|
|
|
ControlFlow::CONTINUE
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
if (self.callback)(r) {
|
|
|
|
|
ControlFlow::BREAK
|
|
|
|
|
} else {
|
|
|
|
|
ControlFlow::CONTINUE
|
|
|
|
|
}
|
2017-10-24 14:20:32 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-04 00:31:12 +02:00
|
|
|
|
2020-11-05 17:30:39 +01:00
|
|
|
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2018-07-04 00:31:12 +02:00
|
|
|
// We're only interested in types involving regions
|
2022-01-12 03:19:52 +00:00
|
|
|
if ty.flags().intersects(TypeFlags::HAS_FREE_REGIONS) {
|
2018-07-04 00:31:12 +02:00
|
|
|
ty.super_visit_with(self)
|
|
|
|
|
} else {
|
2020-10-21 14:22:44 +02:00
|
|
|
ControlFlow::CONTINUE
|
2018-07-04 00:31:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
2017-10-24 14:20:32 -04:00
|
|
|
}
|
2020-10-21 14:22:44 +02:00
|
|
|
|
2022-01-12 03:19:52 +00:00
|
|
|
value.visit_with(&mut RegionVisitor { outer_index: ty::INNERMOST, callback }).is_break()
|
2017-10-24 14:20:32 -04:00
|
|
|
}
|
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>,
|
2015-06-30 00:32:39 +03:00
|
|
|
skipped_regions: &'a mut bool,
|
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
|
|
|
skipped_regions: &'a mut bool,
|
|
|
|
|
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> {
|
2019-12-22 17:42:04 -05:00
|
|
|
RegionFolder { tcx, skipped_regions, 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");
|
2015-06-30 00:32:39 +03:00
|
|
|
*self.skipped_regions = true;
|
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,
|
|
|
|
|
|
2021-03-13 13:44:00 -05:00
|
|
|
fld_r: Option<&'a mut (dyn FnMut(ty::BoundRegion) -> ty::Region<'tcx> + 'a)>,
|
|
|
|
|
fld_t: Option<&'a mut (dyn FnMut(ty::BoundTy) -> Ty<'tcx> + 'a)>,
|
2022-02-02 14:24:45 +11:00
|
|
|
fld_c: Option<&'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>,
|
|
|
|
|
fld_r: Option<&'a mut (dyn FnMut(ty::BoundRegion) -> ty::Region<'tcx> + 'a)>,
|
|
|
|
|
fld_t: Option<&'a mut (dyn FnMut(ty::BoundTy) -> Ty<'tcx> + 'a)>,
|
2022-02-02 14:24:45 +11:00
|
|
|
fld_c: Option<&'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 => {
|
|
|
|
|
if let Some(fld_t) = self.fld_t.as_mut() {
|
2018-10-24 23:41:40 +02:00
|
|
|
let ty = fld_t(bound_ty);
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 14:13:38 +11:00
|
|
|
return ty::fold::shift_vars(self.tcx, ty, self.current_index.as_u32());
|
2018-10-24 23:41:40 +02:00
|
|
|
}
|
|
|
|
|
}
|
2021-03-13 13:44:00 -05:00
|
|
|
_ if t.has_vars_bound_at_or_above(self.current_index) => {
|
|
|
|
|
return t.super_fold_with(self);
|
2018-10-24 23:41:40 +02:00
|
|
|
}
|
2021-03-13 13:44:00 -05:00
|
|
|
_ => {}
|
2018-10-24 23:41:40 +02:00
|
|
|
}
|
2021-12-01 00:55:57 +00:00
|
|
|
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 => {
|
2021-03-13 13:44:00 -05:00
|
|
|
if let Some(fld_r) = self.fld_r.as_mut() {
|
|
|
|
|
let region = fld_r(br);
|
|
|
|
|
return 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);
|
2021-12-01 00:55:57 +00:00
|
|
|
self.tcx.mk_region(ty::ReLateBound(debruijn, br))
|
2021-03-13 13:44:00 -05:00
|
|
|
} else {
|
2021-12-01 00:55:57 +00:00
|
|
|
region
|
2021-03-13 13:44:00 -05:00
|
|
|
};
|
2018-10-24 23:41:40 +02:00
|
|
|
}
|
|
|
|
|
}
|
2021-03-13 13:44:00 -05:00
|
|
|
_ => {}
|
2018-10-24 23:41:40 +02:00
|
|
|
}
|
2021-12-01 00:55:57 +00: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> {
|
|
|
|
|
match ct.val() {
|
|
|
|
|
ty::ConstKind::Bound(debruijn, bound_const) if debruijn == self.current_index => {
|
2021-03-13 13:44:00 -05:00
|
|
|
if let Some(fld_c) = self.fld_c.as_mut() {
|
2022-02-02 14:24:45 +11:00
|
|
|
let ct = fld_c(bound_const, ct.ty());
|
|
|
|
|
return ty::fold::shift_vars(self.tcx, ct, self.current_index.as_u32());
|
2021-03-13 13:44:00 -05:00
|
|
|
}
|
2019-03-09 16:54:50 +00:00
|
|
|
}
|
2021-03-13 13:44:00 -05:00
|
|
|
_ if ct.has_vars_bound_at_or_above(self.current_index) => {
|
|
|
|
|
return ct.super_fold_with(self);
|
2019-03-09 16:54:50 +00:00
|
|
|
}
|
2021-03-13 13:44:00 -05:00
|
|
|
_ => {}
|
2019-03-09 16:54:50 +00:00
|
|
|
}
|
2021-12-01 00:55:57 +00:00
|
|
|
ct
|
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
|
|
|
///
|
|
|
|
|
/// This method only replaces late bound regions and the result may still
|
|
|
|
|
/// contain escaping bound types.
|
|
|
|
|
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();
|
2021-03-13 13:44:00 -05:00
|
|
|
let mut real_fld_r =
|
|
|
|
|
|br: ty::BoundRegion| *region_map.entry(br).or_insert_with(|| fld_r(br));
|
|
|
|
|
let value = value.skip_binder();
|
|
|
|
|
let value = if !value.has_escaping_bound_vars() {
|
|
|
|
|
value
|
|
|
|
|
} else {
|
|
|
|
|
let mut replacer = BoundVarReplacer::new(self, Some(&mut real_fld_r), None, None);
|
2021-12-01 00:55:57 +00:00
|
|
|
value.fold_with(&mut replacer)
|
2021-03-13 13:44:00 -05:00
|
|
|
};
|
2020-12-18 13:24:55 -05:00
|
|
|
(value, region_map)
|
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.
|
|
|
|
|
pub fn replace_escaping_bound_vars<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 {
|
2021-03-13 13:44:00 -05:00
|
|
|
let mut replacer =
|
|
|
|
|
BoundVarReplacer::new(self, Some(&mut fld_r), Some(&mut fld_t), Some(&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`
|
2018-10-25 14:13:24 +02:00
|
|
|
/// closure replaces bound regions while the `fld_t` closure replaces bound
|
2018-10-24 23:41:40 +02:00
|
|
|
/// types.
|
2019-03-09 16:54:50 +00:00
|
|
|
pub fn replace_bound_vars<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>,
|
2020-12-18 13:24:55 -05:00
|
|
|
mut fld_r: F,
|
2019-03-09 16:54:50 +00:00
|
|
|
fld_t: G,
|
|
|
|
|
fld_c: H,
|
2018-11-01 15:30:37 +01:00
|
|
|
) -> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
|
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
|
|
|
{
|
2020-12-18 13:24:55 -05:00
|
|
|
let mut region_map = BTreeMap::new();
|
|
|
|
|
let real_fld_r = |br: ty::BoundRegion| *region_map.entry(br).or_insert_with(|| fld_r(br));
|
|
|
|
|
let value = self.replace_escaping_bound_vars(value.skip_binder(), real_fld_r, fld_t, fld_c);
|
|
|
|
|
(value, region_map)
|
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>,
|
|
|
|
|
{
|
2017-11-21 11:17:48 -05:00
|
|
|
self.replace_late_bound_regions(value, |br| {
|
|
|
|
|
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
|
|
|
})
|
|
|
|
|
.0
|
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>,
|
|
|
|
|
{
|
|
|
|
|
self.replace_escaping_bound_vars(
|
|
|
|
|
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 {
|
2020-10-26 14:18:31 -04:00
|
|
|
val: ty::ConstKind::Bound(
|
|
|
|
|
ty::INNERMOST,
|
|
|
|
|
ty::BoundVar::from_usize(c.as_usize() + bound_vars),
|
|
|
|
|
),
|
|
|
|
|
ty,
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-22 06:37:12 -04:00
|
|
|
/// Returns a set of all late-bound regions that are constrained
|
|
|
|
|
/// by `value`, meaning that if we instantiate those LBR with
|
|
|
|
|
/// variables and equate `value` with something else, those
|
|
|
|
|
/// variables will also be equated.
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn collect_constrained_late_bound_regions<T>(
|
2020-09-18 20:49:25 +02:00
|
|
|
self,
|
2020-10-05 16:51:33 -04:00
|
|
|
value: &Binder<'tcx, T>,
|
2020-12-18 13:24:55 -05:00
|
|
|
) -> FxHashSet<ty::BoundRegionKind>
|
2019-12-22 17:42:04 -05:00
|
|
|
where
|
|
|
|
|
T: TypeFoldable<'tcx>,
|
2016-03-22 06:37:12 -04:00
|
|
|
{
|
|
|
|
|
self.collect_late_bound_regions(value, true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns a set of all late-bound regions that appear in `value` anywhere.
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn collect_referenced_late_bound_regions<T>(
|
2020-09-18 20:49:25 +02:00
|
|
|
self,
|
2020-10-05 16:51:33 -04:00
|
|
|
value: &Binder<'tcx, T>,
|
2020-12-18 13:24:55 -05:00
|
|
|
) -> FxHashSet<ty::BoundRegionKind>
|
2019-12-22 17:42:04 -05:00
|
|
|
where
|
|
|
|
|
T: TypeFoldable<'tcx>,
|
2016-03-22 06:37:12 -04:00
|
|
|
{
|
|
|
|
|
self.collect_late_bound_regions(value, false)
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn collect_late_bound_regions<T>(
|
2020-09-18 20:49:25 +02:00
|
|
|
self,
|
2020-10-05 16:51:33 -04:00
|
|
|
value: &Binder<'tcx, T>,
|
2019-12-22 17:42:04 -05:00
|
|
|
just_constraint: bool,
|
2020-12-18 13:24:55 -05:00
|
|
|
) -> FxHashSet<ty::BoundRegionKind>
|
2019-12-22 17:42:04 -05:00
|
|
|
where
|
|
|
|
|
T: TypeFoldable<'tcx>,
|
2016-03-22 06:37:12 -04:00
|
|
|
{
|
2022-01-12 03:19:52 +00:00
|
|
|
let mut collector = LateBoundRegionsCollector::new(just_constraint);
|
2020-06-24 23:40:33 +02:00
|
|
|
let result = value.as_ref().skip_binder().visit_with(&mut collector);
|
2020-10-22 10:20:24 +02:00
|
|
|
assert!(result.is_continue()); // should never have stopped early
|
2016-03-22 06:37:12 -04:00
|
|
|
collector.regions
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-26 14:18:31 -04:00
|
|
|
pub struct ValidateBoundVars<'tcx> {
|
|
|
|
|
bound_vars: &'tcx ty::List<ty::BoundVariableKind>,
|
|
|
|
|
binder_index: ty::DebruijnIndex,
|
|
|
|
|
// We may encounter the same variable at different levels of binding, so
|
|
|
|
|
// this can't just be `Ty`
|
|
|
|
|
visited: SsoHashSet<(ty::DebruijnIndex, Ty<'tcx>)>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'tcx> ValidateBoundVars<'tcx> {
|
|
|
|
|
pub fn new(bound_vars: &'tcx ty::List<ty::BoundVariableKind>) -> Self {
|
|
|
|
|
ValidateBoundVars {
|
|
|
|
|
bound_vars,
|
|
|
|
|
binder_index: ty::INNERMOST,
|
|
|
|
|
visited: SsoHashSet::default(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'tcx> TypeVisitor<'tcx> for ValidateBoundVars<'tcx> {
|
|
|
|
|
type BreakTy = ();
|
|
|
|
|
|
|
|
|
|
fn visit_binder<T: TypeFoldable<'tcx>>(
|
|
|
|
|
&mut self,
|
|
|
|
|
t: &Binder<'tcx, T>,
|
|
|
|
|
) -> ControlFlow<Self::BreakTy> {
|
|
|
|
|
self.binder_index.shift_in(1);
|
|
|
|
|
let result = t.super_visit_with(self);
|
|
|
|
|
self.binder_index.shift_out(1);
|
|
|
|
|
result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 14:13:38 +11:00
|
|
|
if t.outer_exclusive_binder() < self.binder_index
|
2020-10-26 14:18:31 -04:00
|
|
|
|| !self.visited.insert((self.binder_index, t))
|
|
|
|
|
{
|
|
|
|
|
return ControlFlow::BREAK;
|
|
|
|
|
}
|
|
|
|
|
match *t.kind() {
|
|
|
|
|
ty::Bound(debruijn, bound_ty) if debruijn == self.binder_index => {
|
|
|
|
|
if self.bound_vars.len() <= bound_ty.var.as_usize() {
|
2021-03-11 21:08:49 -05:00
|
|
|
bug!("Not enough bound vars: {:?} not found in {:?}", t, self.bound_vars);
|
2020-10-26 14:18:31 -04:00
|
|
|
}
|
|
|
|
|
let list_var = self.bound_vars[bound_ty.var.as_usize()];
|
|
|
|
|
match list_var {
|
|
|
|
|
ty::BoundVariableKind::Ty(kind) => {
|
|
|
|
|
if kind != bound_ty.kind {
|
2021-03-11 21:08:49 -05:00
|
|
|
bug!(
|
2020-10-26 14:18:31 -04:00
|
|
|
"Mismatched type kinds: {:?} doesn't var in list {:?}",
|
2021-03-11 21:49:41 -05:00
|
|
|
bound_ty.kind,
|
|
|
|
|
list_var
|
2020-10-26 14:18:31 -04:00
|
|
|
);
|
|
|
|
|
}
|
2020-10-05 20:41:46 -04:00
|
|
|
}
|
2021-03-11 21:49:41 -05:00
|
|
|
_ => {
|
|
|
|
|
bug!("Mismatched bound variable kinds! Expected type, found {:?}", list_var)
|
|
|
|
|
}
|
2020-10-26 14:18:31 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_ => (),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
t.super_visit_with(self)
|
|
|
|
|
}
|
2020-10-05 20:41:46 -04:00
|
|
|
|
2020-10-26 14:18:31 -04:00
|
|
|
fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2022-01-28 11:25:15 +11:00
|
|
|
match *r {
|
|
|
|
|
ty::ReLateBound(index, br) if index == self.binder_index => {
|
2020-10-26 14:18:31 -04:00
|
|
|
if self.bound_vars.len() <= br.var.as_usize() {
|
2022-01-28 11:25:15 +11:00
|
|
|
bug!("Not enough bound vars: {:?} not found in {:?}", br, self.bound_vars);
|
2020-10-05 20:41:46 -04:00
|
|
|
}
|
2020-10-26 14:18:31 -04:00
|
|
|
let list_var = self.bound_vars[br.var.as_usize()];
|
|
|
|
|
match list_var {
|
|
|
|
|
ty::BoundVariableKind::Region(kind) => {
|
|
|
|
|
if kind != br.kind {
|
2021-03-11 21:08:49 -05:00
|
|
|
bug!(
|
2020-10-26 14:18:31 -04:00
|
|
|
"Mismatched region kinds: {:?} doesn't match var ({:?}) in list ({:?})",
|
2021-03-11 21:49:41 -05:00
|
|
|
br.kind,
|
|
|
|
|
list_var,
|
|
|
|
|
self.bound_vars
|
2020-10-26 14:18:31 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-11 21:08:49 -05:00
|
|
|
_ => bug!(
|
2020-10-26 14:18:31 -04:00
|
|
|
"Mismatched bound variable kinds! Expected region, found {:?}",
|
|
|
|
|
list_var
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-05 20:41:46 -04:00
|
|
|
|
|
|
|
|
_ => (),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
r.super_visit_with(self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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> {
|
|
|
|
|
if let ty::ConstKind::Bound(debruijn, bound_ct) = ct.val() {
|
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 {
|
|
|
|
|
val: ty::ConstKind::Bound(debruijn, bound_ct),
|
|
|
|
|
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
|
|
|
}
|
2015-11-18 09:38:57 +00:00
|
|
|
|
2020-11-05 19:39:48 +01:00
|
|
|
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
|
|
|
|
struct FoundEscapingVars;
|
|
|
|
|
|
2018-10-22 22:38:51 +02:00
|
|
|
/// An "escaping var" is a bound var whose binder is not part of `t`. A bound var can be a
|
|
|
|
|
/// bound region or a bound type.
|
2015-12-18 10:07:06 +00:00
|
|
|
///
|
|
|
|
|
/// So, for example, consider a type like the following, which has two binders:
|
|
|
|
|
///
|
|
|
|
|
/// for<'a> fn(x: for<'b> fn(&'a isize, &'b isize))
|
|
|
|
|
/// ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ outer scope
|
|
|
|
|
/// ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ inner scope
|
|
|
|
|
///
|
|
|
|
|
/// This type has *bound regions* (`'a`, `'b`), but it does not have escaping regions, because the
|
|
|
|
|
/// binders of both `'a` and `'b` are part of the type itself. However, if we consider the *inner
|
|
|
|
|
/// fn type*, that type has an escaping region: `'a`.
|
|
|
|
|
///
|
2018-10-22 22:38:51 +02:00
|
|
|
/// Note that what I'm calling an "escaping var" is often just called a "free var". However,
|
|
|
|
|
/// we already use the term "free var". It refers to the regions or types that we use to represent
|
2018-11-12 13:05:20 -05:00
|
|
|
/// bound regions or type params on a fn definition while we are type checking its body.
|
2015-12-18 10:07:06 +00:00
|
|
|
///
|
2018-09-07 09:46:53 -04:00
|
|
|
/// To clarify, conceptually there is no particular difference between
|
2018-10-22 22:38:51 +02:00
|
|
|
/// an "escaping" var and a "free" var. However, there is a big
|
2018-09-07 09:46:53 -04:00
|
|
|
/// difference in practice. Basically, when "entering" a binding
|
|
|
|
|
/// level, one is generally required to do some sort of processing to
|
2018-10-22 22:38:51 +02:00
|
|
|
/// a bound var, such as replacing it with a fresh/placeholder
|
|
|
|
|
/// var, or making an entry in the environment to represent the
|
|
|
|
|
/// scope to which it is attached, etc. An escaping var represents
|
|
|
|
|
/// a bound var for which this processing has not yet been done.
|
|
|
|
|
struct HasEscapingVarsVisitor {
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Anything bound by `outer_index` or "above" is escaping.
|
2018-05-28 12:38:39 -04:00
|
|
|
outer_index: ty::DebruijnIndex,
|
2015-12-18 10:07:06 +00:00
|
|
|
}
|
2015-11-18 09:38:57 +00:00
|
|
|
|
2018-10-22 22:38:51 +02:00
|
|
|
impl<'tcx> TypeVisitor<'tcx> for HasEscapingVarsVisitor {
|
2020-11-05 19:39:48 +01:00
|
|
|
type BreakTy = FoundEscapingVars;
|
|
|
|
|
|
2020-10-05 16:51:33 -04:00
|
|
|
fn visit_binder<T: TypeFoldable<'tcx>>(
|
|
|
|
|
&mut self,
|
|
|
|
|
t: &Binder<'tcx, T>,
|
|
|
|
|
) -> ControlFlow<Self::BreakTy> {
|
2018-05-28 12:38:39 -04:00
|
|
|
self.outer_index.shift_in(1);
|
2016-01-08 23:34:05 +00:00
|
|
|
let result = t.super_visit_with(self);
|
2018-05-28 12:38:39 -04:00
|
|
|
self.outer_index.shift_out(1);
|
2016-01-08 23:34:05 +00:00
|
|
|
result
|
2015-12-18 10:07:06 +00:00
|
|
|
}
|
2015-11-18 09:38:57 +00:00
|
|
|
|
2021-02-26 00:00:00 +00:00
|
|
|
#[inline]
|
2020-11-05 17:30:39 +01:00
|
|
|
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2018-05-28 12:38:39 -04:00
|
|
|
// If the outer-exclusive-binder is *strictly greater* than
|
|
|
|
|
// `outer_index`, that means that `t` contains some content
|
|
|
|
|
// bound at `outer_index` or above (because
|
|
|
|
|
// `outer_exclusive_binder` is always 1 higher than the
|
2018-10-22 22:38:51 +02:00
|
|
|
// content in `t`). Therefore, `t` has some escaping vars.
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 14:13:38 +11:00
|
|
|
if t.outer_exclusive_binder() > self.outer_index {
|
2020-11-05 19:39:48 +01:00
|
|
|
ControlFlow::Break(FoundEscapingVars)
|
2020-10-21 14:22:44 +02:00
|
|
|
} else {
|
|
|
|
|
ControlFlow::CONTINUE
|
|
|
|
|
}
|
2015-12-18 10:07:06 +00:00
|
|
|
}
|
2015-11-18 09:38:57 +00:00
|
|
|
|
2021-02-26 00:00:00 +00:00
|
|
|
#[inline]
|
2020-11-05 17:30:39 +01:00
|
|
|
fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2018-05-28 12:38:39 -04:00
|
|
|
// If the region is bound by `outer_index` or anything outside
|
|
|
|
|
// of outer index, then it escapes the binders we have
|
|
|
|
|
// visited.
|
2020-10-21 14:22:44 +02:00
|
|
|
if r.bound_at_or_above_binder(self.outer_index) {
|
2020-11-05 19:39:48 +01:00
|
|
|
ControlFlow::Break(FoundEscapingVars)
|
2020-10-21 14:22:44 +02:00
|
|
|
} else {
|
|
|
|
|
ControlFlow::CONTINUE
|
|
|
|
|
}
|
2015-11-18 09:38:57 +00:00
|
|
|
}
|
2019-02-28 23:09:44 -05:00
|
|
|
|
2022-02-02 14:24:45 +11:00
|
|
|
fn visit_const(&mut self, ct: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2019-10-07 10:58:14 -04:00
|
|
|
// we don't have a `visit_infer_const` callback, so we have to
|
|
|
|
|
// hook in here to catch this case (annoying...), but
|
|
|
|
|
// otherwise we do want to remember to visit the rest of the
|
|
|
|
|
// const, as it has types/regions embedded in a lot of other
|
|
|
|
|
// places.
|
2022-02-02 14:24:45 +11:00
|
|
|
match ct.val() {
|
2020-11-05 19:39:48 +01:00
|
|
|
ty::ConstKind::Bound(debruijn, _) if debruijn >= self.outer_index => {
|
|
|
|
|
ControlFlow::Break(FoundEscapingVars)
|
|
|
|
|
}
|
2019-10-07 10:58:14 -04:00
|
|
|
_ => ct.super_visit_with(self),
|
2019-02-28 23:09:44 -05:00
|
|
|
}
|
|
|
|
|
}
|
2015-11-18 09:38:57 +00:00
|
|
|
|
2021-02-26 00:00:00 +00:00
|
|
|
#[inline]
|
2020-11-05 17:30:39 +01:00
|
|
|
fn visit_predicate(&mut self, predicate: ty::Predicate<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2022-01-27 17:00:16 +11:00
|
|
|
if predicate.outer_exclusive_binder() > self.outer_index {
|
2020-11-05 19:39:48 +01:00
|
|
|
ControlFlow::Break(FoundEscapingVars)
|
2020-10-21 14:22:44 +02:00
|
|
|
} else {
|
|
|
|
|
ControlFlow::CONTINUE
|
|
|
|
|
}
|
2020-06-10 09:30:39 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-05 19:30:42 +01:00
|
|
|
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
|
|
|
|
struct FoundFlags;
|
|
|
|
|
|
2019-05-31 10:23:22 +02:00
|
|
|
// FIXME: Optimize for checking for infer flags
|
2022-01-12 03:19:52 +00:00
|
|
|
struct HasTypeFlagsVisitor {
|
2015-12-18 10:07:06 +00:00
|
|
|
flags: ty::TypeFlags,
|
|
|
|
|
}
|
2015-11-18 09:38:57 +00:00
|
|
|
|
2022-01-12 03:19:52 +00:00
|
|
|
impl std::fmt::Debug for HasTypeFlagsVisitor {
|
2021-08-20 13:36:04 +00:00
|
|
|
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
|
self.flags.fmt(fmt)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 03:19:52 +00:00
|
|
|
impl<'tcx> TypeVisitor<'tcx> for HasTypeFlagsVisitor {
|
2020-11-05 19:30:42 +01:00
|
|
|
type BreakTy = FoundFlags;
|
|
|
|
|
|
2021-02-26 00:00:00 +00:00
|
|
|
#[inline]
|
2022-02-11 07:18:06 +00:00
|
|
|
#[instrument(level = "trace")]
|
|
|
|
|
fn visit_ty(&mut self, t: Ty<'_>) -> ControlFlow<Self::BreakTy> {
|
|
|
|
|
debug!(
|
|
|
|
|
"HasTypeFlagsVisitor: t={:?} t.flags={:?} self.flags={:?}",
|
|
|
|
|
t,
|
|
|
|
|
t.flags(),
|
|
|
|
|
self.flags
|
|
|
|
|
);
|
|
|
|
|
if t.flags().intersects(self.flags) {
|
2020-11-05 19:30:42 +01:00
|
|
|
ControlFlow::Break(FoundFlags)
|
|
|
|
|
} else {
|
2022-01-12 03:19:52 +00:00
|
|
|
ControlFlow::CONTINUE
|
2020-11-05 19:30:42 +01:00
|
|
|
}
|
2015-12-18 10:07:06 +00:00
|
|
|
}
|
2015-11-18 09:38:57 +00:00
|
|
|
|
2021-02-26 00:00:00 +00:00
|
|
|
#[inline]
|
2021-08-20 13:36:04 +00:00
|
|
|
#[instrument(skip(self), level = "trace")]
|
2020-11-05 17:30:39 +01:00
|
|
|
fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2016-10-19 18:39:49 -04:00
|
|
|
let flags = r.type_flags();
|
2021-08-20 13:36:04 +00:00
|
|
|
trace!(r.flags=?flags);
|
2020-11-05 19:30:42 +01:00
|
|
|
if flags.intersects(self.flags) {
|
|
|
|
|
ControlFlow::Break(FoundFlags)
|
|
|
|
|
} else {
|
|
|
|
|
ControlFlow::CONTINUE
|
|
|
|
|
}
|
2015-11-18 09:38:57 +00:00
|
|
|
}
|
2017-08-07 08:08:53 +03:00
|
|
|
|
2021-02-26 00:00:00 +00:00
|
|
|
#[inline]
|
2022-02-11 07:18:06 +00:00
|
|
|
#[instrument(level = "trace")]
|
2022-02-02 14:24:45 +11:00
|
|
|
fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2019-03-14 10:19:31 +01:00
|
|
|
let flags = FlagComputation::for_const(c);
|
2021-08-20 13:36:04 +00:00
|
|
|
trace!(r.flags=?flags);
|
2020-11-05 19:30:42 +01:00
|
|
|
if flags.intersects(self.flags) {
|
|
|
|
|
ControlFlow::Break(FoundFlags)
|
2021-07-17 18:48:07 +02:00
|
|
|
} else {
|
2022-01-12 23:29:10 +00:00
|
|
|
ControlFlow::CONTINUE
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
#[instrument(level = "trace")]
|
|
|
|
|
fn visit_unevaluated_const(&mut self, uv: ty::Unevaluated<'tcx>) -> ControlFlow<Self::BreakTy> {
|
|
|
|
|
let flags = FlagComputation::for_unevaluated_const(uv);
|
|
|
|
|
trace!(r.flags=?flags);
|
|
|
|
|
if flags.intersects(self.flags) {
|
|
|
|
|
ControlFlow::Break(FoundFlags)
|
|
|
|
|
} else {
|
2022-01-12 03:19:52 +00:00
|
|
|
ControlFlow::CONTINUE
|
2020-11-05 19:30:42 +01:00
|
|
|
}
|
2017-08-07 08:08:53 +03:00
|
|
|
}
|
2016-03-22 06:37:12 -04:00
|
|
|
|
2021-02-26 00:00:00 +00:00
|
|
|
#[inline]
|
2021-08-20 13:36:04 +00:00
|
|
|
#[instrument(level = "trace")]
|
2020-11-05 17:30:39 +01:00
|
|
|
fn visit_predicate(&mut self, predicate: ty::Predicate<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2022-01-12 03:19:52 +00:00
|
|
|
debug!(
|
|
|
|
|
"HasTypeFlagsVisitor: predicate={:?} predicate.flags={:?} self.flags={:?}",
|
2022-01-27 17:00:16 +11:00
|
|
|
predicate,
|
|
|
|
|
predicate.flags(),
|
|
|
|
|
self.flags
|
2022-01-12 03:19:52 +00:00
|
|
|
);
|
2022-01-27 17:00:16 +11:00
|
|
|
if predicate.flags().intersects(self.flags) {
|
2020-11-05 19:30:42 +01:00
|
|
|
ControlFlow::Break(FoundFlags)
|
2021-08-17 10:23:51 +02:00
|
|
|
} else {
|
|
|
|
|
ControlFlow::CONTINUE
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-28 10:24:01 -04:00
|
|
|
/// Collects all the late-bound regions at the innermost binding level
|
|
|
|
|
/// into a hash set.
|
2022-01-12 03:19:52 +00:00
|
|
|
struct LateBoundRegionsCollector {
|
2018-05-28 10:24:01 -04:00
|
|
|
current_index: ty::DebruijnIndex,
|
2020-12-18 13:24:55 -05:00
|
|
|
regions: FxHashSet<ty::BoundRegionKind>,
|
2018-05-28 10:24:01 -04:00
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// `true` if we only want regions that are known to be
|
2018-05-28 10:24:01 -04:00
|
|
|
/// "constrained" when you equate this type with another type. In
|
2018-11-27 02:59:49 +00:00
|
|
|
/// particular, if you have e.g., `&'a u32` and `&'b u32`, equating
|
2019-02-08 14:53:55 +01:00
|
|
|
/// them constraints `'a == 'b`. But if you have `<&'a u32 as
|
2018-05-28 10:24:01 -04:00
|
|
|
/// Trait>::Foo` and `<&'b u32 as Trait>::Foo`, normalizing those
|
|
|
|
|
/// types may mean that `'a` and `'b` don't appear in the results,
|
|
|
|
|
/// so they are not considered *constrained*.
|
2016-03-22 06:37:12 -04:00
|
|
|
just_constrained: bool,
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 03:19:52 +00:00
|
|
|
impl LateBoundRegionsCollector {
|
|
|
|
|
fn new(just_constrained: bool) -> Self {
|
2016-03-22 06:37:12 -04:00
|
|
|
LateBoundRegionsCollector {
|
2018-06-10 14:44:15 +02:00
|
|
|
current_index: ty::INNERMOST,
|
2018-10-16 16:57:53 +02:00
|
|
|
regions: Default::default(),
|
2017-07-03 11:19:51 -07:00
|
|
|
just_constrained,
|
2016-03-22 06:37:12 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 03:19:52 +00:00
|
|
|
impl<'tcx> TypeVisitor<'tcx> for LateBoundRegionsCollector {
|
2020-10-05 16:51:33 -04:00
|
|
|
fn visit_binder<T: TypeFoldable<'tcx>>(
|
|
|
|
|
&mut self,
|
|
|
|
|
t: &Binder<'tcx, T>,
|
|
|
|
|
) -> ControlFlow<Self::BreakTy> {
|
2018-05-28 10:24:01 -04:00
|
|
|
self.current_index.shift_in(1);
|
2016-03-22 06:37:12 -04:00
|
|
|
let result = t.super_visit_with(self);
|
2018-05-28 10:24:01 -04:00
|
|
|
self.current_index.shift_out(1);
|
2016-03-22 06:37:12 -04:00
|
|
|
result
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-05 17:30:39 +01:00
|
|
|
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2016-03-22 06:37:12 -04:00
|
|
|
// if we are only looking for "constrained" region, we have to
|
|
|
|
|
// ignore the inputs to a projection, as they may not appear
|
|
|
|
|
// in the normalized form
|
|
|
|
|
if self.just_constrained {
|
2020-08-03 00:49:11 +02:00
|
|
|
if let ty::Projection(..) | ty::Opaque(..) = t.kind() {
|
2020-10-21 14:22:44 +02:00
|
|
|
return ControlFlow::CONTINUE;
|
2016-03-22 06:37:12 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t.super_visit_with(self)
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-02 14:24:45 +11:00
|
|
|
fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2020-03-23 18:39:25 +01:00
|
|
|
// if we are only looking for "constrained" region, we have to
|
|
|
|
|
// ignore the inputs of an unevaluated const, as they may not appear
|
|
|
|
|
// in the normalized form
|
|
|
|
|
if self.just_constrained {
|
2022-02-02 14:24:45 +11:00
|
|
|
if let ty::ConstKind::Unevaluated(..) = c.val() {
|
2020-10-21 14:22:44 +02:00
|
|
|
return ControlFlow::CONTINUE;
|
2020-03-23 18:39:25 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.super_visit_with(self)
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-05 17:30:39 +01:00
|
|
|
fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2018-10-02 10:56:36 +02:00
|
|
|
if let ty::ReLateBound(debruijn, br) = *r {
|
2019-12-22 17:42:04 -05:00
|
|
|
if debruijn == self.current_index {
|
2020-12-18 13:24:55 -05:00
|
|
|
self.regions.insert(br.kind);
|
2016-03-22 06:37:12 -04:00
|
|
|
}
|
|
|
|
|
}
|
2020-10-21 14:22:44 +02:00
|
|
|
ControlFlow::CONTINUE
|
2016-03-22 06:37:12 -04:00
|
|
|
}
|
|
|
|
|
}
|