2013-10-29 05:25:18 -04:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
2014-11-25 21:17:11 -05:00
|
|
|
//! Generalized type folding mechanism. The setup is a bit convoluted
|
|
|
|
|
//! but allows for convenient usage. Let T be an instance of some
|
|
|
|
|
//! "foldable type" (one which implements `TypeFoldable`) and F be an
|
|
|
|
|
//! instance of a "folder" (a type which implements `TypeFolder`). Then
|
|
|
|
|
//! the setup is intended to be:
|
|
|
|
|
//!
|
2016-01-06 02:01:28 +00:00
|
|
|
//! T.fold_with(F) --calls--> F.fold_T(T) --calls--> T.super_fold_with(F)
|
2014-11-25 21:17:11 -05:00
|
|
|
//!
|
|
|
|
|
//! This way, when you define a new folder F, you can override
|
2016-01-06 02:01:28 +00:00
|
|
|
//! `fold_T()` to customize the behavior, and invoke `T.super_fold_with()`
|
2014-11-25 21:17:11 -05:00
|
|
|
//! to get the original behavior. Meanwhile, to actually fold
|
|
|
|
|
//! something, you can just write `T.fold_with(F)`, which is
|
|
|
|
|
//! convenient. (Note that `fold_with` will also transparently handle
|
|
|
|
|
//! things like a `Vec<T>` where T is foldable and so on.)
|
|
|
|
|
//!
|
|
|
|
|
//! In this ideal setup, the only function that actually *does*
|
2016-01-06 02:01:28 +00:00
|
|
|
//! anything is `T.super_fold_with()`, which traverses the type `T`.
|
|
|
|
|
//! Moreover, `T.super_fold_with()` should only ever call `T.fold_with()`.
|
2014-11-25 21:17:11 -05:00
|
|
|
//!
|
|
|
|
|
//! In some cases, we follow a degenerate pattern where we do not have
|
2015-11-18 09:38:57 +00:00
|
|
|
//! a `fold_T` method. Instead, `T.fold_with` traverses the structure directly.
|
|
|
|
|
//! This is suboptimal because the behavior cannot be overridden, but it's
|
|
|
|
|
//! much less work to implement. If you ever *do* need an override that
|
|
|
|
|
//! doesn't exist, it's not hard to convert the degenerate pattern into the
|
|
|
|
|
//! proper thing.
|
|
|
|
|
//!
|
|
|
|
|
//! A `TypeFoldable` T can also be visited by a `TypeVisitor` V using similar setup:
|
2016-01-06 02:01:28 +00:00
|
|
|
//! T.visit_with(V) --calls--> V.visit_T(T) --calls--> T.super_visit_with(V).
|
2015-11-18 09:38:57 +00:00
|
|
|
//! These methods return true to indicate that the visitor has found what it is looking for
|
|
|
|
|
//! and does not need to visit anything else.
|
2013-10-29 05:25:18 -04:00
|
|
|
|
2018-06-25 20:53:02 +02:00
|
|
|
use mir::interpret::ConstValue;
|
2017-11-21 11:17:48 -05:00
|
|
|
use hir::def_id::DefId;
|
2016-03-22 17:30:57 +02:00
|
|
|
use ty::{self, Binder, Ty, TyCtxt, TypeFlags};
|
2015-06-18 08:51:23 +03:00
|
|
|
|
2018-05-14 22:27:45 +10:00
|
|
|
use std::collections::BTreeMap;
|
2015-06-18 08:51:23 +03:00
|
|
|
use std::fmt;
|
2017-11-13 15:37:07 -05:00
|
|
|
use util::nodemap::FxHashSet;
|
2013-10-29 05:25:18 -04:00
|
|
|
|
2014-05-12 17:12:51 -04:00
|
|
|
/// The TypeFoldable trait is implemented for every type that can be folded.
|
|
|
|
|
/// Basically, every type that has a corresponding method in TypeFolder.
|
2018-02-09 10:34:23 -05:00
|
|
|
///
|
|
|
|
|
/// To implement this conveniently, use the
|
|
|
|
|
/// `BraceStructTypeFoldableImpl` etc macros found in `macros.rs`.
|
2015-06-18 08:51:23 +03:00
|
|
|
pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
|
2016-04-29 06:00:23 +03:00
|
|
|
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self;
|
|
|
|
|
fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
|
2016-01-06 02:01:28 +00:00
|
|
|
self.super_fold_with(folder)
|
2015-11-18 09:38:57 +00:00
|
|
|
}
|
|
|
|
|
|
2016-01-06 02:01:28 +00:00
|
|
|
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool;
|
|
|
|
|
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
|
|
|
|
|
self.super_visit_with(visitor)
|
2015-11-18 09:38:57 +00:00
|
|
|
}
|
2015-12-18 10:07:06 +00:00
|
|
|
|
2018-05-25 09:58:29 -04:00
|
|
|
/// True if `self` has any late-bound regions that are either
|
|
|
|
|
/// 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-05-28 12:38:39 -04:00
|
|
|
fn has_regions_bound_at_or_above(&self, binder: ty::DebruijnIndex) -> bool {
|
|
|
|
|
self.visit_with(&mut HasEscapingRegionsVisitor { outer_index: binder })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// True if this `self` has any regions that escape `binder` (and
|
|
|
|
|
/// hence are not bound by it).
|
|
|
|
|
fn has_regions_bound_above(&self, binder: ty::DebruijnIndex) -> bool {
|
|
|
|
|
self.has_regions_bound_at_or_above(binder.shifted_in(1))
|
2018-05-25 09:58:29 -04:00
|
|
|
}
|
|
|
|
|
|
2015-12-18 10:07:06 +00:00
|
|
|
fn has_escaping_regions(&self) -> bool {
|
2018-06-10 14:44:15 +02:00
|
|
|
self.has_regions_bound_at_or_above(ty::INNERMOST)
|
2015-12-18 10:07:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn has_type_flags(&self, flags: TypeFlags) -> bool {
|
|
|
|
|
self.visit_with(&mut HasTypeFlagsVisitor { flags: flags })
|
|
|
|
|
}
|
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)
|
|
|
|
|
}
|
|
|
|
|
fn references_error(&self) -> bool {
|
|
|
|
|
self.has_type_flags(TypeFlags::HAS_TY_ERR)
|
|
|
|
|
}
|
|
|
|
|
fn has_param_types(&self) -> bool {
|
|
|
|
|
self.has_type_flags(TypeFlags::HAS_PARAMS)
|
|
|
|
|
}
|
|
|
|
|
fn has_self_ty(&self) -> bool {
|
|
|
|
|
self.has_type_flags(TypeFlags::HAS_SELF)
|
|
|
|
|
}
|
|
|
|
|
fn has_infer_types(&self) -> bool {
|
|
|
|
|
self.has_type_flags(TypeFlags::HAS_TY_INFER)
|
|
|
|
|
}
|
|
|
|
|
fn needs_infer(&self) -> bool {
|
|
|
|
|
self.has_type_flags(TypeFlags::HAS_TY_INFER | TypeFlags::HAS_RE_INFER)
|
|
|
|
|
}
|
2018-04-04 17:21:50 -04:00
|
|
|
fn has_skol(&self) -> bool {
|
|
|
|
|
self.has_type_flags(TypeFlags::HAS_RE_SKOL)
|
|
|
|
|
}
|
2015-12-18 10:07:06 +00:00
|
|
|
fn needs_subst(&self) -> bool {
|
|
|
|
|
self.has_type_flags(TypeFlags::NEEDS_SUBST)
|
|
|
|
|
}
|
2016-10-19 18:39:49 -04:00
|
|
|
fn has_re_skol(&self) -> bool {
|
|
|
|
|
self.has_type_flags(TypeFlags::HAS_RE_SKOL)
|
|
|
|
|
}
|
2015-12-18 10:07:06 +00:00
|
|
|
fn has_closure_types(&self) -> bool {
|
|
|
|
|
self.has_type_flags(TypeFlags::HAS_TY_CLOSURE)
|
|
|
|
|
}
|
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.
|
|
|
|
|
fn has_free_regions(&self) -> bool {
|
|
|
|
|
self.has_type_flags(TypeFlags::HAS_FREE_REGIONS)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// True if there any any un-erased free regions.
|
2015-12-18 10:07:06 +00:00
|
|
|
fn has_erasable_regions(&self) -> bool {
|
2017-12-04 05:11:36 -05:00
|
|
|
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'
|
|
|
|
|
/// types/lifetimes that are the same regardless of what fn we are
|
2018-05-15 21:48:35 +01:00
|
|
|
/// in. This is used for caching.
|
2015-12-18 10:07:06 +00:00
|
|
|
fn is_global(&self) -> bool {
|
2018-05-15 21:48:35 +01:00
|
|
|
!self.has_type_flags(TypeFlags::HAS_FREE_LOCAL_NAMES)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
|
|
|
|
/// A visitor that does not recurse into types, works like `fn walk_shallow` in `Ty`.
|
|
|
|
|
fn visit_tys_shallow(&self, visit: impl FnMut(Ty<'tcx>) -> bool) -> bool {
|
|
|
|
|
|
|
|
|
|
pub struct Visitor<F>(F);
|
|
|
|
|
|
|
|
|
|
impl<'tcx, F: FnMut(Ty<'tcx>) -> bool> TypeVisitor<'tcx> for Visitor<F> {
|
|
|
|
|
fn visit_ty(&mut self, ty: Ty<'tcx>) -> bool {
|
|
|
|
|
self.0(ty)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.visit_with(&mut Visitor(visit))
|
|
|
|
|
}
|
2014-05-12 17:12:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The TypeFolder trait defines the actual *folding*. There is a
|
|
|
|
|
/// method defined for every foldable type. Each of these has a
|
|
|
|
|
/// default implementation that does an "identity" fold. Within each
|
|
|
|
|
/// identity fold, it should invoke `foo.fold_with(self)` to fold each
|
|
|
|
|
/// sub-item.
|
2016-04-29 06:00:23 +03:00
|
|
|
pub trait TypeFolder<'gcx: 'tcx, 'tcx> : Sized {
|
|
|
|
|
fn tcx<'a>(&'a self) -> TyCtxt<'a, 'gcx, 'tcx>;
|
2013-10-29 05:25:18 -04:00
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
fn fold_binder<T>(&mut self, t: &Binder<T>) -> Binder<T>
|
2015-06-18 08:51:23 +03: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
|
|
|
}
|
|
|
|
|
|
2014-09-29 22:11:30 +03: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
|
|
|
}
|
|
|
|
|
|
2017-04-20 04:45:53 -04: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
|
|
|
|
|
|
|
|
fn fold_const(&mut self, c: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
|
|
|
|
|
c.super_fold_with(self)
|
|
|
|
|
}
|
2014-05-12 17:12:51 -04:00
|
|
|
}
|
|
|
|
|
|
2015-11-18 09:38:57 +00:00
|
|
|
pub trait TypeVisitor<'tcx> : Sized {
|
2016-01-08 23:34:05 +00:00
|
|
|
fn visit_binder<T: TypeFoldable<'tcx>>(&mut self, t: &Binder<T>) -> bool {
|
|
|
|
|
t.super_visit_with(self)
|
|
|
|
|
}
|
2014-08-27 21:46:52 -04:00
|
|
|
|
2015-11-18 09:38:57 +00:00
|
|
|
fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
|
2016-01-06 02:01:28 +00:00
|
|
|
t.super_visit_with(self)
|
2014-05-12 17:12:51 -04:00
|
|
|
}
|
|
|
|
|
|
2017-04-20 04:45:53 -04:00
|
|
|
fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
|
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
|
|
|
|
|
|
|
|
fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> bool {
|
|
|
|
|
c.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
|
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
pub struct BottomUpFolder<'a, 'gcx: 'a+'tcx, 'tcx: 'a, F>
|
|
|
|
|
where F: FnMut(Ty<'tcx>) -> Ty<'tcx>
|
|
|
|
|
{
|
|
|
|
|
pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
2014-12-08 20:26:43 -05:00
|
|
|
pub fldop: F,
|
2013-10-29 05:25:18 -04:00
|
|
|
}
|
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
impl<'a, 'gcx, 'tcx, F> TypeFolder<'gcx, 'tcx> for BottomUpFolder<'a, 'gcx, 'tcx, F>
|
|
|
|
|
where F: FnMut(Ty<'tcx>) -> Ty<'tcx>,
|
2014-12-08 20:26:43 -05:00
|
|
|
{
|
2016-04-29 06:00:23 +03:00
|
|
|
fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> { self.tcx }
|
2013-10-29 05:25:18 -04:00
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
|
2016-01-06 02:01:28 +00:00
|
|
|
let t1 = ty.super_fold_with(self);
|
2013-10-29 05:25:18 -04:00
|
|
|
(self.fldop)(t1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Region folder
|
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
/// Collects the free and escaping regions in `value` into `region_set`. Returns
|
|
|
|
|
/// whether any late-bound regions were skipped
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn collect_regions<T>(self,
|
2015-09-06 21:51:58 +03:00
|
|
|
value: &T,
|
2017-04-20 04:45:53 -04:00
|
|
|
region_set: &mut FxHashSet<ty::Region<'tcx>>)
|
2015-09-06 21:51:58 +03:00
|
|
|
-> bool
|
|
|
|
|
where T : TypeFoldable<'tcx>
|
|
|
|
|
{
|
|
|
|
|
let mut have_bound_regions = false;
|
2016-08-25 23:58:52 +03:00
|
|
|
self.fold_regions(value, &mut have_bound_regions, |r, d| {
|
2018-05-25 09:58:29 -04:00
|
|
|
region_set.insert(self.mk_region(r.shifted_out_to_binder(d)));
|
2016-08-25 23:58:52 +03:00
|
|
|
r
|
|
|
|
|
});
|
2015-09-06 21:51:58 +03:00
|
|
|
have_bound_regions
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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,
|
2015-09-06 21:51:58 +03:00
|
|
|
value: &T,
|
|
|
|
|
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
|
|
|
|
|
T : TypeFoldable<'tcx>,
|
2015-09-06 21:51:58 +03:00
|
|
|
{
|
|
|
|
|
value.fold_with(&mut RegionFolder::new(self, skipped_regions, &mut f))
|
|
|
|
|
}
|
2017-10-24 14:20:32 -04:00
|
|
|
|
|
|
|
|
pub fn for_each_free_region<T,F>(self,
|
|
|
|
|
value: &T,
|
|
|
|
|
callback: F)
|
|
|
|
|
where F: FnMut(ty::Region<'tcx>),
|
|
|
|
|
T: TypeFoldable<'tcx>,
|
|
|
|
|
{
|
2018-05-28 10:13:21 -04:00
|
|
|
value.visit_with(&mut RegionVisitor {
|
2018-06-10 14:44:15 +02:00
|
|
|
outer_index: ty::INNERMOST,
|
2018-05-28 10:13:21 -04:00
|
|
|
callback
|
|
|
|
|
});
|
2017-10-24 14:20:32 -04: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
|
2018-06-10 14:44:15 +02:00
|
|
|
/// | | here, would be INNERMOST shifted in by 1
|
2018-05-28 10:13:21 -04:00
|
|
|
/// | here, initially, binder would be INNERMOST
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// 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,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'tcx, F> TypeVisitor<'tcx> for RegionVisitor<F>
|
|
|
|
|
where F : FnMut(ty::Region<'tcx>)
|
|
|
|
|
{
|
|
|
|
|
fn visit_binder<T: TypeFoldable<'tcx>>(&mut self, t: &Binder<T>) -> bool {
|
2018-05-28 10:13:21 -04:00
|
|
|
self.outer_index.shift_in(1);
|
2017-10-24 14:20:32 -04:00
|
|
|
t.skip_binder().visit_with(self);
|
2018-05-28 10:13:21 -04:00
|
|
|
self.outer_index.shift_out(1);
|
2017-10-24 14:20:32 -04:00
|
|
|
|
|
|
|
|
false // keep visiting
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
|
|
|
|
|
match *r {
|
2018-05-28 10:13:21 -04:00
|
|
|
ty::ReLateBound(debruijn, _) if debruijn < self.outer_index => {
|
2017-10-24 14:20:32 -04:00
|
|
|
/* ignore bound regions */
|
|
|
|
|
}
|
|
|
|
|
_ => (self.callback)(r),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
false // keep visiting
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
pub struct RegionFolder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
|
|
|
|
|
tcx: TyCtxt<'a, 'gcx, '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.
|
|
|
|
|
fold_region_fn: &'a mut (dyn FnMut(
|
|
|
|
|
ty::Region<'tcx>,
|
|
|
|
|
ty::DebruijnIndex,
|
|
|
|
|
) -> ty::Region<'tcx> + 'a),
|
2013-10-29 05:25:18 -04:00
|
|
|
}
|
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
impl<'a, 'gcx, 'tcx> RegionFolder<'a, 'gcx, 'tcx> {
|
2018-05-25 09:58:29 -04:00
|
|
|
pub fn new(
|
|
|
|
|
tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
|
|
|
|
skipped_regions: &'a mut bool,
|
|
|
|
|
fold_region_fn: &'a mut dyn FnMut(ty::Region<'tcx>, ty::DebruijnIndex) -> ty::Region<'tcx>,
|
|
|
|
|
) -> RegionFolder<'a, 'gcx, 'tcx> {
|
2013-10-29 05:25:18 -04:00
|
|
|
RegionFolder {
|
2017-07-03 11:19:51 -07:00
|
|
|
tcx,
|
|
|
|
|
skipped_regions,
|
2018-06-10 14:44:15 +02:00
|
|
|
current_index: ty::INNERMOST,
|
2018-05-25 09:58:29 -04:00
|
|
|
fold_region_fn,
|
2013-10-29 05:25:18 -04:00
|
|
|
}
|
|
|
|
|
}
|
2014-06-20 22:26:14 +02:00
|
|
|
}
|
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for RegionFolder<'a, 'gcx, 'tcx> {
|
|
|
|
|
fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> { self.tcx }
|
2013-10-29 05:25:18 -04:00
|
|
|
|
2016-01-08 23:34:05 +00:00
|
|
|
fn fold_binder<T: TypeFoldable<'tcx>>(&mut self, t: &ty::Binder<T>) -> ty::Binder<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
|
|
|
}
|
|
|
|
|
|
2017-04-20 04:45:53 -04: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 => {
|
|
|
|
|
debug!("RegionFolder.fold_region({:?}) skipped bound region (current index={:?})",
|
|
|
|
|
r, self.current_index);
|
2015-06-30 00:32:39 +03:00
|
|
|
*self.skipped_regions = true;
|
2014-06-20 22:26:14 +02:00
|
|
|
r
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
2018-05-25 09:58:29 -04:00
|
|
|
debug!("RegionFolder.fold_region({:?}) folding free region (current_index={:?})",
|
|
|
|
|
r, self.current_index);
|
|
|
|
|
(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
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Late-bound region replacer
|
|
|
|
|
|
|
|
|
|
// Replaces the escaping regions in a type.
|
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
struct RegionReplacer<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
|
|
|
|
|
tcx: TyCtxt<'a, 'gcx, '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,
|
|
|
|
|
|
2018-02-23 09:53:00 -08:00
|
|
|
fld_r: &'a mut (dyn FnMut(ty::BoundRegion) -> ty::Region<'tcx> + 'a),
|
2018-05-14 22:27:45 +10:00
|
|
|
map: BTreeMap<ty::BoundRegion, ty::Region<'tcx>>
|
2015-06-06 02:06:14 +03:00
|
|
|
}
|
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
|
2017-11-21 11:17:48 -05:00
|
|
|
/// Replace all regions bound by the given `Binder` with the
|
|
|
|
|
/// 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
|
|
|
|
|
/// once for each unique `BoundRegion`; multiple references to the
|
|
|
|
|
/// same `BoundRegion` will reuse the previous result. A map is
|
|
|
|
|
/// returned at the end with each bound region and the free region
|
|
|
|
|
/// that replaced it.
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn replace_late_bound_regions<T,F>(self,
|
2015-09-06 21:51:58 +03:00
|
|
|
value: &Binder<T>,
|
|
|
|
|
mut f: F)
|
2018-05-14 22:27:45 +10:00
|
|
|
-> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
|
2017-04-20 04:45:53 -04:00
|
|
|
where F : FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
|
2015-09-06 21:51:58 +03:00
|
|
|
T : TypeFoldable<'tcx>,
|
|
|
|
|
{
|
|
|
|
|
let mut replacer = RegionReplacer::new(self, &mut f);
|
|
|
|
|
let result = value.skip_binder().fold_with(&mut replacer);
|
|
|
|
|
(result, replacer.map)
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-21 11:17:48 -05:00
|
|
|
/// Replace any late-bound regions bound in `value` with
|
|
|
|
|
/// free variants attached to `all_outlive_scope`.
|
|
|
|
|
pub fn liberate_late_bound_regions<T>(
|
|
|
|
|
&self,
|
|
|
|
|
all_outlive_scope: DefId,
|
|
|
|
|
value: &ty::Binder<T>
|
|
|
|
|
) -> T
|
|
|
|
|
where T: TypeFoldable<'tcx> {
|
|
|
|
|
self.replace_late_bound_regions(value, |br| {
|
|
|
|
|
self.mk_region(ty::ReFree(ty::FreeRegion {
|
|
|
|
|
scope: all_outlive_scope,
|
|
|
|
|
bound_region: br
|
|
|
|
|
}))
|
|
|
|
|
}).0
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-25 09:58:29 -04:00
|
|
|
/// Flattens multiple binding levels into one. So `for<'a> for<'b> Foo`
|
2015-09-06 21:51:58 +03:00
|
|
|
/// becomes `for<'a,'b> Foo`.
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn flatten_late_bound_regions<T>(self, bound2_value: &Binder<Binder<T>>)
|
2015-09-06 21:51:58 +03:00
|
|
|
-> Binder<T>
|
|
|
|
|
where T: TypeFoldable<'tcx>
|
|
|
|
|
{
|
|
|
|
|
let bound0_value = bound2_value.skip_binder().skip_binder();
|
2018-05-25 09:58:29 -04:00
|
|
|
let value = self.fold_regions(bound0_value, &mut false, |region, current_depth| {
|
2016-08-25 23:58:52 +03:00
|
|
|
match *region {
|
2018-05-25 09:58:29 -04:00
|
|
|
ty::ReLateBound(debruijn, br) => {
|
|
|
|
|
// We assume no regions bound *outside* of the
|
|
|
|
|
// binders in `bound2_value` (nmatsakis added in
|
|
|
|
|
// the course of this PR; seems like a reasonable
|
|
|
|
|
// sanity check though).
|
|
|
|
|
assert!(debruijn == current_depth);
|
|
|
|
|
self.mk_region(ty::ReLateBound(current_depth, br))
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
region
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-04-24 21:45:49 -05:00
|
|
|
Binder::bind(value)
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
|
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.
|
|
|
|
|
pub fn collect_constrained_late_bound_regions<T>(&self, value: &Binder<T>)
|
2016-11-08 14:02:55 +11:00
|
|
|
-> FxHashSet<ty::BoundRegion>
|
2016-03-22 06:37:12 -04:00
|
|
|
where T : TypeFoldable<'tcx>
|
|
|
|
|
{
|
|
|
|
|
self.collect_late_bound_regions(value, true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns a set of all late-bound regions that appear in `value` anywhere.
|
|
|
|
|
pub fn collect_referenced_late_bound_regions<T>(&self, value: &Binder<T>)
|
2016-11-08 14:02:55 +11:00
|
|
|
-> FxHashSet<ty::BoundRegion>
|
2016-03-22 06:37:12 -04:00
|
|
|
where T : TypeFoldable<'tcx>
|
|
|
|
|
{
|
|
|
|
|
self.collect_late_bound_regions(value, false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn collect_late_bound_regions<T>(&self, value: &Binder<T>, just_constraint: bool)
|
2016-11-08 14:02:55 +11:00
|
|
|
-> FxHashSet<ty::BoundRegion>
|
2016-03-22 06:37:12 -04:00
|
|
|
where T : TypeFoldable<'tcx>
|
|
|
|
|
{
|
|
|
|
|
let mut collector = LateBoundRegionsCollector::new(just_constraint);
|
|
|
|
|
let result = value.skip_binder().visit_with(&mut collector);
|
|
|
|
|
assert!(!result); // should never have stopped early
|
|
|
|
|
collector.regions
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
/// Replace 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.
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn erase_late_bound_regions<T>(self, value: &Binder<T>) -> T
|
2015-09-06 21:51:58 +03:00
|
|
|
where T : TypeFoldable<'tcx>
|
|
|
|
|
{
|
2017-04-20 01:58:12 +03:00
|
|
|
self.replace_late_bound_regions(value, |_| self.types.re_erased).0
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Rewrite any late-bound regions so that they are anonymous. Region numbers are
|
|
|
|
|
/// assigned starting at 1 and increasing monotonically in the order traversed
|
|
|
|
|
/// 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
|
|
|
|
|
/// structurally identical. For example, `for<'a, 'b> fn(&'a isize, &'b isize)` and
|
|
|
|
|
/// `for<'a, 'b> fn(&'b isize, &'a isize)` will become identical after anonymization.
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn anonymize_late_bound_regions<T>(self, sig: &Binder<T>) -> Binder<T>
|
2015-09-06 21:51:58 +03:00
|
|
|
where T : TypeFoldable<'tcx>,
|
|
|
|
|
{
|
|
|
|
|
let mut counter = 0;
|
2018-04-24 21:45:49 -05:00
|
|
|
Binder::bind(self.replace_late_bound_regions(sig, |_| {
|
2015-09-06 21:51:58 +03:00
|
|
|
counter += 1;
|
2018-06-10 14:44:15 +02:00
|
|
|
self.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BrAnon(counter)))
|
2015-09-06 21:51:58 +03:00
|
|
|
}).0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
impl<'a, 'gcx, 'tcx> RegionReplacer<'a, 'gcx, 'tcx> {
|
|
|
|
|
fn new<F>(tcx: TyCtxt<'a, 'gcx, 'tcx>, fld_r: &'a mut F)
|
|
|
|
|
-> RegionReplacer<'a, 'gcx, 'tcx>
|
2017-04-20 04:45:53 -04:00
|
|
|
where F : FnMut(ty::BoundRegion) -> ty::Region<'tcx>
|
2015-06-06 02:06:14 +03:00
|
|
|
{
|
|
|
|
|
RegionReplacer {
|
2017-07-03 11:19:51 -07:00
|
|
|
tcx,
|
2018-06-10 14:44:15 +02:00
|
|
|
current_index: ty::INNERMOST,
|
2017-07-03 11:19:51 -07:00
|
|
|
fld_r,
|
2018-05-14 22:27:45 +10:00
|
|
|
map: BTreeMap::default()
|
2015-06-06 02:06:14 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for RegionReplacer<'a, 'gcx, 'tcx> {
|
|
|
|
|
fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> { self.tcx }
|
2015-06-06 02:06:14 +03:00
|
|
|
|
2016-01-08 23:34:05 +00:00
|
|
|
fn fold_binder<T: TypeFoldable<'tcx>>(&mut self, t: &ty::Binder<T>) -> ty::Binder<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
|
2015-06-06 02:06:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
|
2018-05-28 12:38:39 -04:00
|
|
|
if !t.has_regions_bound_at_or_above(self.current_index) {
|
2015-06-06 02:06:14 +03:00
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-06 02:01:28 +00:00
|
|
|
t.super_fold_with(self)
|
2015-06-06 02:06:14 +03:00
|
|
|
}
|
|
|
|
|
|
2017-04-20 04:45:53 -04: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, br) if debruijn == self.current_index => {
|
2015-06-06 02:06:14 +03:00
|
|
|
let fld_r = &mut self.fld_r;
|
|
|
|
|
let region = *self.map.entry(br).or_insert_with(|| fld_r(br));
|
2016-08-25 23:58:52 +03:00
|
|
|
if let ty::ReLateBound(debruijn1, br) = *region {
|
2015-06-06 02:06:14 +03:00
|
|
|
// If the callback returns a late-bound region,
|
2018-05-25 09:58:29 -04:00
|
|
|
// that region should always use the INNERMOST
|
|
|
|
|
// debruijn index. Then we adjust it to the
|
|
|
|
|
// correct depth.
|
2018-06-10 14:44:15 +02:00
|
|
|
assert_eq!(debruijn1, ty::INNERMOST);
|
2016-08-25 23:58:52 +03:00
|
|
|
self.tcx.mk_region(ty::ReLateBound(debruijn, br))
|
2015-06-06 02:06:14 +03:00
|
|
|
} else {
|
|
|
|
|
region
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-25 23:58:52 +03:00
|
|
|
_ => r
|
2015-06-06 02:06:14 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-15 16:47:59 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Region shifter
|
|
|
|
|
//
|
|
|
|
|
// Shifts the De Bruijn indices on all escaping bound regions by a
|
|
|
|
|
// fixed amount. Useful in substitution or when otherwise introducing
|
|
|
|
|
// a binding level that is not intended to capture the existing bound
|
|
|
|
|
// regions. See comment on `shift_regions_through_binders` method in
|
|
|
|
|
// `subst.rs` for more details.
|
|
|
|
|
|
2017-05-11 16:10:47 +03:00
|
|
|
pub fn shift_region(region: ty::RegionKind, amount: u32) -> ty::RegionKind {
|
2014-11-15 16:47:59 -05:00
|
|
|
match region {
|
|
|
|
|
ty::ReLateBound(debruijn, br) => {
|
2018-05-25 09:06:54 -04:00
|
|
|
ty::ReLateBound(debruijn.shifted_in(amount), br)
|
2014-11-15 16:47:59 -05:00
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
region
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-20 01:58:12 +03:00
|
|
|
pub fn shift_region_ref<'a, 'gcx, 'tcx>(
|
|
|
|
|
tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
2017-04-20 04:45:53 -04:00
|
|
|
region: ty::Region<'tcx>,
|
2017-04-20 01:58:12 +03:00
|
|
|
amount: u32)
|
2017-04-20 04:45:53 -04:00
|
|
|
-> ty::Region<'tcx>
|
2017-04-20 01:58:12 +03:00
|
|
|
{
|
|
|
|
|
match region {
|
|
|
|
|
&ty::ReLateBound(debruijn, br) if amount > 0 => {
|
2018-05-25 09:06:54 -04:00
|
|
|
tcx.mk_region(ty::ReLateBound(debruijn.shifted_in(amount), br))
|
2017-04-20 01:58:12 +03:00
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
region
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
pub fn shift_regions<'a, 'gcx, 'tcx, T>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
2017-04-20 04:45:53 -04:00
|
|
|
amount: u32,
|
|
|
|
|
value: &T) -> T
|
2016-04-29 06:00:23 +03:00
|
|
|
where T: TypeFoldable<'tcx>
|
|
|
|
|
{
|
2015-06-18 20:25:05 +03:00
|
|
|
debug!("shift_regions(value={:?}, amount={})",
|
|
|
|
|
value, amount);
|
2014-11-15 16:47:59 -05:00
|
|
|
|
2015-06-30 00:32:39 +03:00
|
|
|
value.fold_with(&mut RegionFolder::new(tcx, &mut false, &mut |region, _current_depth| {
|
2017-04-20 01:58:12 +03:00
|
|
|
shift_region_ref(tcx, region, amount)
|
2014-11-15 16:47:59 -05:00
|
|
|
}))
|
|
|
|
|
}
|
2015-11-18 09:38:57 +00:00
|
|
|
|
2015-12-18 10:07:06 +00:00
|
|
|
/// An "escaping region" is a bound region whose binder is not part of `t`.
|
|
|
|
|
///
|
|
|
|
|
/// 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`.
|
|
|
|
|
///
|
|
|
|
|
/// Note that what I'm calling an "escaping region" is often just called a "free region". However,
|
|
|
|
|
/// we already use the term "free region". It refers to the regions that we use to represent bound
|
|
|
|
|
/// regions on a fn definition while we are typechecking its body.
|
|
|
|
|
///
|
|
|
|
|
/// To clarify, conceptually there is no particular difference between an "escaping" region and a
|
|
|
|
|
/// "free" region. However, there is a big difference in practice. Basically, when "entering" a
|
|
|
|
|
/// binding level, one is generally required to do some sort of processing to a bound region, such
|
|
|
|
|
/// as replacing it with a fresh/skolemized region, or making an entry in the environment to
|
|
|
|
|
/// represent the scope to which it is attached, etc. An escaping region represents a bound region
|
|
|
|
|
/// for which this processing has not yet been done.
|
|
|
|
|
struct HasEscapingRegionsVisitor {
|
2018-05-28 12:38:39 -04:00
|
|
|
/// Anything bound by `outer_index` or "above" is escaping
|
|
|
|
|
outer_index: ty::DebruijnIndex,
|
2015-12-18 10:07:06 +00:00
|
|
|
}
|
2015-11-18 09:38:57 +00:00
|
|
|
|
2015-12-18 10:07:06 +00:00
|
|
|
impl<'tcx> TypeVisitor<'tcx> for HasEscapingRegionsVisitor {
|
2016-01-08 23:34:05 +00:00
|
|
|
fn visit_binder<T: TypeFoldable<'tcx>>(&mut self, t: &Binder<T>) -> bool {
|
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
|
|
|
|
2015-12-18 10:07:06 +00:00
|
|
|
fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
|
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
|
|
|
|
|
// content in `t`). Therefore, `t` has some escaping regions.
|
|
|
|
|
t.outer_exclusive_binder > self.outer_index
|
2015-12-18 10:07:06 +00:00
|
|
|
}
|
2015-11-18 09:38:57 +00:00
|
|
|
|
2017-04-20 04:45:53 -04:00
|
|
|
fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
|
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.
|
|
|
|
|
r.bound_at_or_above_binder(self.outer_index)
|
2015-11-18 09:38:57 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-18 10:07:06 +00:00
|
|
|
struct HasTypeFlagsVisitor {
|
|
|
|
|
flags: ty::TypeFlags,
|
|
|
|
|
}
|
2015-11-18 09:38:57 +00:00
|
|
|
|
2015-12-18 10:07:06 +00:00
|
|
|
impl<'tcx> TypeVisitor<'tcx> for HasTypeFlagsVisitor {
|
|
|
|
|
fn visit_ty(&mut self, t: Ty) -> bool {
|
2017-05-12 12:01:43 -04:00
|
|
|
debug!("HasTypeFlagsVisitor: t={:?} t.flags={:?} self.flags={:?}", t, t.flags, self.flags);
|
|
|
|
|
t.flags.intersects(self.flags)
|
2015-12-18 10:07:06 +00:00
|
|
|
}
|
2015-11-18 09:38:57 +00:00
|
|
|
|
2017-04-20 04:45:53 -04:00
|
|
|
fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
|
2016-10-19 18:39:49 -04:00
|
|
|
let flags = r.type_flags();
|
|
|
|
|
debug!("HasTypeFlagsVisitor: r={:?} r.flags={:?} self.flags={:?}", r, flags, self.flags);
|
|
|
|
|
flags.intersects(self.flags)
|
2015-11-18 09:38:57 +00:00
|
|
|
}
|
2017-08-07 08:08:53 +03:00
|
|
|
|
|
|
|
|
fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> bool {
|
2018-06-25 20:53:02 +02:00
|
|
|
if let ConstValue::Unevaluated(..) = c.val {
|
2017-08-07 08:08:53 +03:00
|
|
|
let projection_flags = TypeFlags::HAS_NORMALIZABLE_PROJECTION |
|
|
|
|
|
TypeFlags::HAS_PROJECTION;
|
|
|
|
|
if projection_flags.intersects(self.flags) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
c.super_visit_with(self)
|
|
|
|
|
}
|
2015-11-18 09:38:57 +00:00
|
|
|
}
|
2016-03-22 06:37:12 -04:00
|
|
|
|
2018-05-28 10:24:01 -04:00
|
|
|
/// Collects all the late-bound regions at the innermost binding level
|
|
|
|
|
/// into a hash set.
|
2016-03-22 06:37:12 -04:00
|
|
|
struct LateBoundRegionsCollector {
|
2018-05-28 10:24:01 -04:00
|
|
|
current_index: ty::DebruijnIndex,
|
2016-11-08 14:02:55 +11:00
|
|
|
regions: FxHashSet<ty::BoundRegion>,
|
2018-05-28 10:24:01 -04:00
|
|
|
|
|
|
|
|
/// If true, we only want regions that are known to be
|
|
|
|
|
/// "constrained" when you equate this type with another type. In
|
|
|
|
|
/// partcular, if you have e.g. `&'a u32` and `&'b u32`, equating
|
|
|
|
|
/// them constraints `'a == 'b`. But if you have `<&'a u32 as
|
|
|
|
|
/// 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,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl LateBoundRegionsCollector {
|
|
|
|
|
fn new(just_constrained: bool) -> Self {
|
|
|
|
|
LateBoundRegionsCollector {
|
2018-06-10 14:44:15 +02:00
|
|
|
current_index: ty::INNERMOST,
|
2016-11-08 14:02:55 +11:00
|
|
|
regions: FxHashSet(),
|
2017-07-03 11:19:51 -07:00
|
|
|
just_constrained,
|
2016-03-22 06:37:12 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'tcx> TypeVisitor<'tcx> for LateBoundRegionsCollector {
|
|
|
|
|
fn visit_binder<T: TypeFoldable<'tcx>>(&mut self, t: &Binder<T>) -> bool {
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
|
|
|
|
|
// 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 {
|
|
|
|
|
match t.sty {
|
2016-07-22 18:56:22 +03:00
|
|
|
ty::TyProjection(..) | ty::TyAnon(..) => { return false; }
|
2016-03-22 06:37:12 -04:00
|
|
|
_ => { }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t.super_visit_with(self)
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-20 04:45:53 -04:00
|
|
|
fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
|
2016-08-25 23:58:52 +03:00
|
|
|
match *r {
|
2018-05-28 10:24:01 -04:00
|
|
|
ty::ReLateBound(debruijn, br) if debruijn == self.current_index => {
|
2016-03-22 06:37:12 -04:00
|
|
|
self.regions.insert(br);
|
|
|
|
|
}
|
|
|
|
|
_ => { }
|
|
|
|
|
}
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|