1 //! A generalized traversal mechanism for complex data structures that contain
4 //! There are two types of traversal.
5 //! - Folding. This is a modifying traversal. It consumes the data structure,
6 //! producing a (possibly) modified version of it. Both fallible and
7 //! infallible versions are available. The name is potentially
8 //! confusing, because this traversal is more like `Iterator::map` than
10 //! - Visiting. This is a read-only traversal of the data structure.
12 //! These traversals have limited flexibility. Only a small number of "types of
13 //! interest" within the complex data structures can receive custom
14 //! modification (when folding) or custom visitation (when visiting). These are
15 //! the ones containing the most important type-related information, such as
16 //! `Ty`, `Predicate`, `Region`, and `Const`.
18 //! There are two traits involved in each traversal type.
19 //! - The first trait is `TypeFoldable`, which is implemented once for many
20 //! types. This includes both (a) types of interest, and (b) all other
21 //! relevant types, including generic containers like `Vec` and `Option`. It
22 //! defines a "skeleton" of how they should be traversed, for both folding
24 //! - The second trait is `TypeFolder`/`FallibleTypeFolder` (for
25 //! infallible/fallible folding traversals) or `TypeVisitor` (for visiting
26 //! traversals). One of these is implemented for each folder/visitor. This
27 //! defines how types of interest are handled.
29 //! This means each traversal is a mixture of (a) generic traversal operations,
30 //! and (b) custom fold/visit operations that are specific to the
32 //! - The `TypeFoldable` impls handle most of the traversal, and call into
33 //! `TypeFolder`/`FallibleTypeFolder`/`TypeVisitor` when they encounter a
35 //! - A `TypeFolder`/`FallibleTypeFolder`/`TypeVisitor` may also call back into
36 //! a `TypeFoldable` impl, because (a) the types of interest are recursive
37 //! and can contain other types of interest, and (b) each folder/visitor
38 //! might provide custom handling only for some types of interest, or only
39 //! for some variants of each type of interest, and then use default
40 //! traversal for the remaining cases.
42 //! For example, if you have `struct S(Ty, U)` where `S: TypeFoldable` and `U:
43 //! TypeFoldable`, and an instance `S(ty, u)`, it would be visited like so:
45 //! s.visit_with(visitor) calls
46 //! - s.super_visit_with(visitor) calls
47 //! - ty.visit_with(visitor) calls
48 //! - visitor.visit_ty(ty) may call
49 //! - ty.super_visit_with(visitor)
50 //! - u.visit_with(visitor)
53 use crate::ty::{self, flags::FlagComputation, Binder, Ty, TyCtxt, TypeFlags};
54 use rustc_errors::ErrorGuaranteed;
55 use rustc_hir::def_id::DefId;
57 use rustc_data_structures::fx::FxHashSet;
58 use rustc_data_structures::sso::SsoHashSet;
59 use std::collections::BTreeMap;
61 use std::ops::ControlFlow;
63 /// This trait is implemented for every type that can be folded/visited,
64 /// providing the skeleton of the traversal.
66 /// To implement this conveniently, use the derive macro located in
68 pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
69 /// The main entry point for folding. To fold a value `t` with a folder `f`
70 /// call: `t.try_fold_with(f)`.
72 /// For types of interest (such as `Ty`), this default is overridden with a
73 /// method that calls a folder method specifically for that type (such as
74 /// `F::try_fold_ty`). This is where control transfers from `TypeFoldable`
77 /// For other types, this default is used.
78 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
79 self.try_super_fold_with(folder)
82 /// A convenient alternative to `try_fold_with` for use with infallible
83 /// folders. Do not override this method, to ensure coherence with
85 fn fold_with<F: TypeFolder<'tcx, Error = !>>(self, folder: &mut F) -> Self {
86 self.try_fold_with(folder).into_ok()
89 /// Traverses the type in question, typically by calling `try_fold_with` on
90 /// each field/element. This is true even for types of interest such as
91 /// `Ty`. This should only be called within `TypeFolder` methods, when
92 /// non-custom traversals are desired for types of interest.
93 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
96 ) -> Result<Self, F::Error>;
98 /// A convenient alternative to `try_super_fold_with` for use with
99 /// infallible folders. Do not override this method, to ensure coherence
100 /// with `try_super_fold_with`.
101 fn super_fold_with<F: TypeFolder<'tcx, Error = !>>(self, folder: &mut F) -> Self {
102 self.try_super_fold_with(folder).into_ok()
105 /// The entry point for visiting. To visit a value `t` with a visitor `v`
106 /// call: `t.visit_with(v)`.
108 /// For types of interest (such as `Ty`), this default is overridden with a
109 /// method that calls a visitor method specifically for that type (such as
110 /// `V::visit_ty`). This is where control transfers from `TypeFoldable` to
113 /// For other types, this default is used.
114 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
115 self.super_visit_with(visitor)
118 /// Traverses the type in question, typically by calling `visit_with` on
119 /// each field/element. This is true even for types of interest such as
120 /// `Ty`. This should only be called within `TypeVisitor` methods, when
121 /// non-custom traversals are desired for types of interest.
122 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy>;
124 /// Returns `true` if `self` has any late-bound regions that are either
125 /// bound by `binder` or bound by some binder outside of `binder`.
126 /// If `binder` is `ty::INNERMOST`, this indicates whether
127 /// there are any late-bound regions that appear free.
128 fn has_vars_bound_at_or_above(&self, binder: ty::DebruijnIndex) -> bool {
129 self.visit_with(&mut HasEscapingVarsVisitor { outer_index: binder }).is_break()
132 /// Returns `true` if this `self` has any regions that escape `binder` (and
133 /// hence are not bound by it).
134 fn has_vars_bound_above(&self, binder: ty::DebruijnIndex) -> bool {
135 self.has_vars_bound_at_or_above(binder.shifted_in(1))
138 fn has_escaping_bound_vars(&self) -> bool {
139 self.has_vars_bound_at_or_above(ty::INNERMOST)
142 #[instrument(level = "trace")]
143 fn has_type_flags(&self, flags: TypeFlags) -> bool {
144 self.visit_with(&mut HasTypeFlagsVisitor { flags }).break_value() == Some(FoundFlags)
146 fn has_projections(&self) -> bool {
147 self.has_type_flags(TypeFlags::HAS_PROJECTION)
149 fn has_opaque_types(&self) -> bool {
150 self.has_type_flags(TypeFlags::HAS_TY_OPAQUE)
152 fn references_error(&self) -> bool {
153 self.has_type_flags(TypeFlags::HAS_ERROR)
155 fn error_reported(&self) -> Option<ErrorGuaranteed> {
156 if self.references_error() {
157 Some(ErrorGuaranteed::unchecked_claim_error_was_emitted())
162 fn has_param_types_or_consts(&self) -> bool {
163 self.has_type_flags(TypeFlags::HAS_TY_PARAM | TypeFlags::HAS_CT_PARAM)
165 fn has_infer_regions(&self) -> bool {
166 self.has_type_flags(TypeFlags::HAS_RE_INFER)
168 fn has_infer_types(&self) -> bool {
169 self.has_type_flags(TypeFlags::HAS_TY_INFER)
171 fn has_infer_types_or_consts(&self) -> bool {
172 self.has_type_flags(TypeFlags::HAS_TY_INFER | TypeFlags::HAS_CT_INFER)
174 fn needs_infer(&self) -> bool {
175 self.has_type_flags(TypeFlags::NEEDS_INFER)
177 fn has_placeholders(&self) -> bool {
179 TypeFlags::HAS_RE_PLACEHOLDER
180 | TypeFlags::HAS_TY_PLACEHOLDER
181 | TypeFlags::HAS_CT_PLACEHOLDER,
184 fn needs_subst(&self) -> bool {
185 self.has_type_flags(TypeFlags::NEEDS_SUBST)
187 /// "Free" regions in this context means that it has any region
188 /// that is not (a) erased or (b) late-bound.
189 fn has_free_regions(&self) -> bool {
190 self.has_type_flags(TypeFlags::HAS_FREE_REGIONS)
193 fn has_erased_regions(&self) -> bool {
194 self.has_type_flags(TypeFlags::HAS_RE_ERASED)
197 /// True if there are any un-erased free regions.
198 fn has_erasable_regions(&self) -> bool {
199 self.has_type_flags(TypeFlags::HAS_FREE_REGIONS)
202 /// Indicates whether this value references only 'global'
203 /// generic parameters that are the same regardless of what fn we are
204 /// in. This is used for caching.
205 fn is_global(&self) -> bool {
206 !self.has_type_flags(TypeFlags::HAS_FREE_LOCAL_NAMES)
209 /// True if there are any late-bound regions
210 fn has_late_bound_regions(&self) -> bool {
211 self.has_type_flags(TypeFlags::HAS_RE_LATE_BOUND)
214 /// Indicates whether this value still has parameters/placeholders/inference variables
215 /// which could be replaced later, in a way that would change the results of `impl`
217 fn still_further_specializable(&self) -> bool {
218 self.has_type_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE)
222 /// This trait is implemented for every folding traversal. There is a fold
223 /// method defined for every type of interest. Each such method has a default
224 /// that does an "identity" fold.
226 /// If this folder is fallible (and therefore its [`Error`][`TypeFolder::Error`]
227 /// associated type is something other than the default `!`) then
228 /// [`FallibleTypeFolder`] should be implemented manually. Otherwise,
229 /// a blanket implementation of [`FallibleTypeFolder`] will defer to
230 /// the infallible methods of this trait to ensure that the two APIs
232 pub trait TypeFolder<'tcx>: Sized {
235 fn tcx<'a>(&'a self) -> TyCtxt<'tcx>;
237 fn fold_binder<T>(&mut self, t: Binder<'tcx, T>) -> Binder<'tcx, T>
239 T: TypeFoldable<'tcx>,
240 Self: TypeFolder<'tcx, Error = !>,
242 t.super_fold_with(self)
245 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx>
247 Self: TypeFolder<'tcx, Error = !>,
249 t.super_fold_with(self)
252 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx>
254 Self: TypeFolder<'tcx, Error = !>,
256 r.super_fold_with(self)
259 fn fold_const(&mut self, c: ty::Const<'tcx>) -> ty::Const<'tcx>
261 Self: TypeFolder<'tcx, Error = !>,
263 c.super_fold_with(self)
266 fn fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx>
268 Self: TypeFolder<'tcx, Error = !>,
270 p.super_fold_with(self)
273 fn fold_mir_const(&mut self, c: mir::ConstantKind<'tcx>) -> mir::ConstantKind<'tcx>
275 Self: TypeFolder<'tcx, Error = !>,
277 bug!("most type folders should not be folding MIR datastructures: {:?}", c)
281 /// This trait is implemented for every folding traversal. There is a fold
282 /// method defined for every type of interest. Each such method has a default
283 /// that does an "identity" fold.
285 /// A blanket implementation of this trait (that defers to the relevant
286 /// method of [`TypeFolder`]) is provided for all infallible folders in
287 /// order to ensure the two APIs are coherent.
288 pub trait FallibleTypeFolder<'tcx>: TypeFolder<'tcx> {
289 fn try_fold_binder<T>(&mut self, t: Binder<'tcx, T>) -> Result<Binder<'tcx, T>, Self::Error>
291 T: TypeFoldable<'tcx>,
293 t.try_super_fold_with(self)
296 fn try_fold_ty(&mut self, t: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
297 t.try_super_fold_with(self)
300 fn try_fold_region(&mut self, r: ty::Region<'tcx>) -> Result<ty::Region<'tcx>, Self::Error> {
301 r.try_super_fold_with(self)
304 fn try_fold_const(&mut self, c: ty::Const<'tcx>) -> Result<ty::Const<'tcx>, Self::Error> {
305 c.try_super_fold_with(self)
308 fn try_fold_predicate(
310 p: ty::Predicate<'tcx>,
311 ) -> Result<ty::Predicate<'tcx>, Self::Error> {
312 p.try_super_fold_with(self)
315 fn try_fold_mir_const(
317 c: mir::ConstantKind<'tcx>,
318 ) -> Result<mir::ConstantKind<'tcx>, Self::Error> {
319 bug!("most type folders should not be folding MIR datastructures: {:?}", c)
323 // This blanket implementation of the fallible trait for infallible folders
324 // delegates to infallible methods to ensure coherence.
325 impl<'tcx, F> FallibleTypeFolder<'tcx> for F
327 F: TypeFolder<'tcx, Error = !>,
329 fn try_fold_binder<T>(&mut self, t: Binder<'tcx, T>) -> Result<Binder<'tcx, T>, Self::Error>
331 T: TypeFoldable<'tcx>,
333 Ok(self.fold_binder(t))
336 fn try_fold_ty(&mut self, t: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
340 fn try_fold_region(&mut self, r: ty::Region<'tcx>) -> Result<ty::Region<'tcx>, Self::Error> {
341 Ok(self.fold_region(r))
344 fn try_fold_const(&mut self, c: ty::Const<'tcx>) -> Result<ty::Const<'tcx>, Self::Error> {
345 Ok(self.fold_const(c))
348 fn try_fold_predicate(
350 p: ty::Predicate<'tcx>,
351 ) -> Result<ty::Predicate<'tcx>, Self::Error> {
352 Ok(self.fold_predicate(p))
355 fn try_fold_mir_const(
357 c: mir::ConstantKind<'tcx>,
358 ) -> Result<mir::ConstantKind<'tcx>, Self::Error> {
359 Ok(self.fold_mir_const(c))
363 /// This trait is implemented for every visiting traversal. There is a visit
364 /// method defined for every type of interest. Each such method has a default
365 /// that recurses into the type's fields in a non-custom fashion.
366 pub trait TypeVisitor<'tcx>: Sized {
369 fn visit_binder<T: TypeFoldable<'tcx>>(
372 ) -> ControlFlow<Self::BreakTy> {
373 t.super_visit_with(self)
376 fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
377 t.super_visit_with(self)
380 fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
381 r.super_visit_with(self)
384 fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
385 c.super_visit_with(self)
388 fn visit_unevaluated_const(&mut self, uv: ty::Unevaluated<'tcx>) -> ControlFlow<Self::BreakTy> {
389 uv.super_visit_with(self)
392 fn visit_predicate(&mut self, p: ty::Predicate<'tcx>) -> ControlFlow<Self::BreakTy> {
393 p.super_visit_with(self)
397 ///////////////////////////////////////////////////////////////////////////
398 // Some sample folders
400 pub struct BottomUpFolder<'tcx, F, G, H>
402 F: FnMut(Ty<'tcx>) -> Ty<'tcx>,
403 G: FnMut(ty::Region<'tcx>) -> ty::Region<'tcx>,
404 H: FnMut(ty::Const<'tcx>) -> ty::Const<'tcx>,
406 pub tcx: TyCtxt<'tcx>,
412 impl<'tcx, F, G, H> TypeFolder<'tcx> for BottomUpFolder<'tcx, F, G, H>
414 F: FnMut(Ty<'tcx>) -> Ty<'tcx>,
415 G: FnMut(ty::Region<'tcx>) -> ty::Region<'tcx>,
416 H: FnMut(ty::Const<'tcx>) -> ty::Const<'tcx>,
418 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
422 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
423 let t = ty.super_fold_with(self);
427 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
428 let r = r.super_fold_with(self);
432 fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
433 let ct = ct.super_fold_with(self);
438 ///////////////////////////////////////////////////////////////////////////
441 impl<'tcx> TyCtxt<'tcx> {
442 /// Folds the escaping and free regions in `value` using `f`, and
443 /// sets `skipped_regions` to true if any late-bound region was found
445 pub fn fold_regions<T>(
448 skipped_regions: &mut bool,
449 mut f: impl FnMut(ty::Region<'tcx>, ty::DebruijnIndex) -> ty::Region<'tcx>,
452 T: TypeFoldable<'tcx>,
454 value.fold_with(&mut RegionFolder::new(self, skipped_regions, &mut f))
457 /// Invoke `callback` on every region appearing free in `value`.
458 pub fn for_each_free_region(
460 value: &impl TypeFoldable<'tcx>,
461 mut callback: impl FnMut(ty::Region<'tcx>),
463 self.any_free_region_meets(value, |r| {
469 /// Returns `true` if `callback` returns true for every region appearing free in `value`.
470 pub fn all_free_regions_meet(
472 value: &impl TypeFoldable<'tcx>,
473 mut callback: impl FnMut(ty::Region<'tcx>) -> bool,
475 !self.any_free_region_meets(value, |r| !callback(r))
478 /// Returns `true` if `callback` returns true for some region appearing free in `value`.
479 pub fn any_free_region_meets(
481 value: &impl TypeFoldable<'tcx>,
482 callback: impl FnMut(ty::Region<'tcx>) -> bool,
484 struct RegionVisitor<F> {
485 /// The index of a binder *just outside* the things we have
486 /// traversed. If we encounter a bound region bound by this
487 /// binder or one outer to it, it appears free. Example:
489 /// ```ignore (illustrative)
490 /// for<'a> fn(for<'b> fn(), T)
492 /// // | | | | here, would be shifted in 1
493 /// // | | | here, would be shifted in 2
494 /// // | | here, would be `INNERMOST` shifted in by 1
495 /// // | here, initially, binder would be `INNERMOST`
498 /// You see that, initially, *any* bound value is free,
499 /// because we've not traversed any binders. As we pass
500 /// through a binder, we shift the `outer_index` by 1 to
501 /// account for the new binder that encloses us.
502 outer_index: ty::DebruijnIndex,
506 impl<'tcx, F> TypeVisitor<'tcx> for RegionVisitor<F>
508 F: FnMut(ty::Region<'tcx>) -> bool,
512 fn visit_binder<T: TypeFoldable<'tcx>>(
515 ) -> ControlFlow<Self::BreakTy> {
516 self.outer_index.shift_in(1);
517 let result = t.as_ref().skip_binder().visit_with(self);
518 self.outer_index.shift_out(1);
522 fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
524 ty::ReLateBound(debruijn, _) if debruijn < self.outer_index => {
525 ControlFlow::CONTINUE
528 if (self.callback)(r) {
531 ControlFlow::CONTINUE
537 fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
538 // We're only interested in types involving regions
539 if ty.flags().intersects(TypeFlags::HAS_FREE_REGIONS) {
540 ty.super_visit_with(self)
542 ControlFlow::CONTINUE
547 value.visit_with(&mut RegionVisitor { outer_index: ty::INNERMOST, callback }).is_break()
551 /// Folds over the substructure of a type, visiting its component
552 /// types and all regions that occur *free* within it.
554 /// That is, `Ty` can contain function or method types that bind
555 /// regions at the call site (`ReLateBound`), and occurrences of
556 /// regions (aka "lifetimes") that are bound within a type are not
557 /// visited by this folder; only regions that occur free will be
558 /// visited by `fld_r`.
560 pub struct RegionFolder<'a, 'tcx> {
562 skipped_regions: &'a mut bool,
564 /// Stores the index of a binder *just outside* the stuff we have
565 /// visited. So this begins as INNERMOST; when we pass through a
566 /// binder, it is incremented (via `shift_in`).
567 current_index: ty::DebruijnIndex,
569 /// Callback invokes for each free region. The `DebruijnIndex`
570 /// points to the binder *just outside* the ones we have passed
573 &'a mut (dyn FnMut(ty::Region<'tcx>, ty::DebruijnIndex) -> ty::Region<'tcx> + 'a),
576 impl<'a, 'tcx> RegionFolder<'a, 'tcx> {
580 skipped_regions: &'a mut bool,
581 fold_region_fn: &'a mut dyn FnMut(ty::Region<'tcx>, ty::DebruijnIndex) -> ty::Region<'tcx>,
582 ) -> RegionFolder<'a, 'tcx> {
583 RegionFolder { tcx, skipped_regions, current_index: ty::INNERMOST, fold_region_fn }
587 impl<'a, 'tcx> TypeFolder<'tcx> for RegionFolder<'a, 'tcx> {
588 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
592 fn fold_binder<T: TypeFoldable<'tcx>>(
594 t: ty::Binder<'tcx, T>,
595 ) -> ty::Binder<'tcx, T> {
596 self.current_index.shift_in(1);
597 let t = t.super_fold_with(self);
598 self.current_index.shift_out(1);
602 #[instrument(skip(self), level = "debug")]
603 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
605 ty::ReLateBound(debruijn, _) if debruijn < self.current_index => {
606 debug!(?self.current_index, "skipped bound region");
607 *self.skipped_regions = true;
611 debug!(?self.current_index, "folding free region");
612 (self.fold_region_fn)(r, self.current_index)
618 ///////////////////////////////////////////////////////////////////////////
619 // Bound vars replacer
621 /// Replaces the escaping bound vars (late bound regions or bound types) in a type.
622 struct BoundVarReplacer<'a, 'tcx> {
625 /// As with `RegionFolder`, represents the index of a binder *just outside*
626 /// the ones we have visited.
627 current_index: ty::DebruijnIndex,
629 fld_r: Option<&'a mut (dyn FnMut(ty::BoundRegion) -> ty::Region<'tcx> + 'a)>,
630 fld_t: Option<&'a mut (dyn FnMut(ty::BoundTy) -> Ty<'tcx> + 'a)>,
631 fld_c: Option<&'a mut (dyn FnMut(ty::BoundVar, Ty<'tcx>) -> ty::Const<'tcx> + 'a)>,
634 impl<'a, 'tcx> BoundVarReplacer<'a, 'tcx> {
637 fld_r: Option<&'a mut (dyn FnMut(ty::BoundRegion) -> ty::Region<'tcx> + 'a)>,
638 fld_t: Option<&'a mut (dyn FnMut(ty::BoundTy) -> Ty<'tcx> + 'a)>,
639 fld_c: Option<&'a mut (dyn FnMut(ty::BoundVar, Ty<'tcx>) -> ty::Const<'tcx> + 'a)>,
641 BoundVarReplacer { tcx, current_index: ty::INNERMOST, fld_r, fld_t, fld_c }
645 impl<'a, 'tcx> TypeFolder<'tcx> for BoundVarReplacer<'a, 'tcx> {
646 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
650 fn fold_binder<T: TypeFoldable<'tcx>>(
652 t: ty::Binder<'tcx, T>,
653 ) -> ty::Binder<'tcx, T> {
654 self.current_index.shift_in(1);
655 let t = t.super_fold_with(self);
656 self.current_index.shift_out(1);
660 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
662 ty::Bound(debruijn, bound_ty) if debruijn == self.current_index => {
663 if let Some(fld_t) = self.fld_t.as_mut() {
664 let ty = fld_t(bound_ty);
665 return ty::fold::shift_vars(self.tcx, ty, self.current_index.as_u32());
668 _ if t.has_vars_bound_at_or_above(self.current_index) => {
669 return t.super_fold_with(self);
676 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
678 ty::ReLateBound(debruijn, br) if debruijn == self.current_index => {
679 if let Some(fld_r) = self.fld_r.as_mut() {
680 let region = fld_r(br);
681 return if let ty::ReLateBound(debruijn1, br) = *region {
682 // If the callback returns a late-bound region,
683 // that region should always use the INNERMOST
684 // debruijn index. Then we adjust it to the
686 assert_eq!(debruijn1, ty::INNERMOST);
687 self.tcx.mk_region(ty::ReLateBound(debruijn, br))
698 fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
700 ty::ConstKind::Bound(debruijn, bound_const) if debruijn == self.current_index => {
701 if let Some(fld_c) = self.fld_c.as_mut() {
702 let ct = fld_c(bound_const, ct.ty());
703 return ty::fold::shift_vars(self.tcx, ct, self.current_index.as_u32());
706 _ if ct.has_vars_bound_at_or_above(self.current_index) => {
707 return ct.super_fold_with(self);
715 impl<'tcx> TyCtxt<'tcx> {
716 /// Replaces all regions bound by the given `Binder` with the
717 /// results returned by the closure; the closure is expected to
718 /// return a free region (relative to this binder), and hence the
719 /// binder is removed in the return type. The closure is invoked
720 /// once for each unique `BoundRegionKind`; multiple references to the
721 /// same `BoundRegionKind` will reuse the previous result. A map is
722 /// returned at the end with each bound region and the free region
723 /// that replaced it.
725 /// This method only replaces late bound regions and the result may still
726 /// contain escaping bound types.
727 pub fn replace_late_bound_regions<T, F>(
729 value: Binder<'tcx, T>,
731 ) -> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
733 F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
734 T: TypeFoldable<'tcx>,
736 let mut region_map = BTreeMap::new();
738 |br: ty::BoundRegion| *region_map.entry(br).or_insert_with(|| fld_r(br));
739 let value = value.skip_binder();
740 let value = if !value.has_escaping_bound_vars() {
743 let mut replacer = BoundVarReplacer::new(self, Some(&mut real_fld_r), None, None);
744 value.fold_with(&mut replacer)
749 /// Replaces all escaping bound vars. The `fld_r` closure replaces escaping
750 /// bound regions; the `fld_t` closure replaces escaping bound types and the `fld_c`
751 /// closure replaces escaping bound consts.
752 pub fn replace_escaping_bound_vars<T, F, G, H>(
760 F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
761 G: FnMut(ty::BoundTy) -> Ty<'tcx>,
762 H: FnMut(ty::BoundVar, Ty<'tcx>) -> ty::Const<'tcx>,
763 T: TypeFoldable<'tcx>,
765 if !value.has_escaping_bound_vars() {
769 BoundVarReplacer::new(self, Some(&mut fld_r), Some(&mut fld_t), Some(&mut fld_c));
770 value.fold_with(&mut replacer)
774 /// Replaces all types or regions bound by the given `Binder`. The `fld_r`
775 /// closure replaces bound regions while the `fld_t` closure replaces bound
777 pub fn replace_bound_vars<T, F, G, H>(
779 value: Binder<'tcx, T>,
783 ) -> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
785 F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
786 G: FnMut(ty::BoundTy) -> Ty<'tcx>,
787 H: FnMut(ty::BoundVar, Ty<'tcx>) -> ty::Const<'tcx>,
788 T: TypeFoldable<'tcx>,
790 let mut region_map = BTreeMap::new();
791 let real_fld_r = |br: ty::BoundRegion| *region_map.entry(br).or_insert_with(|| fld_r(br));
792 let value = self.replace_escaping_bound_vars(value.skip_binder(), real_fld_r, fld_t, fld_c);
796 /// Replaces any late-bound regions bound in `value` with
797 /// free variants attached to `all_outlive_scope`.
798 pub fn liberate_late_bound_regions<T>(
800 all_outlive_scope: DefId,
801 value: ty::Binder<'tcx, T>,
804 T: TypeFoldable<'tcx>,
806 self.replace_late_bound_regions(value, |br| {
807 self.mk_region(ty::ReFree(ty::FreeRegion {
808 scope: all_outlive_scope,
809 bound_region: br.kind,
815 pub fn shift_bound_var_indices<T>(self, bound_vars: usize, value: T) -> T
817 T: TypeFoldable<'tcx>,
819 self.replace_escaping_bound_vars(
822 self.mk_region(ty::ReLateBound(
825 var: ty::BoundVar::from_usize(r.var.as_usize() + bound_vars),
831 self.mk_ty(ty::Bound(
834 var: ty::BoundVar::from_usize(t.var.as_usize() + bound_vars),
840 self.mk_const(ty::ConstS {
841 val: ty::ConstKind::Bound(
843 ty::BoundVar::from_usize(c.as_usize() + bound_vars),
851 /// Returns a set of all late-bound regions that are constrained
852 /// by `value`, meaning that if we instantiate those LBR with
853 /// variables and equate `value` with something else, those
854 /// variables will also be equated.
855 pub fn collect_constrained_late_bound_regions<T>(
857 value: &Binder<'tcx, T>,
858 ) -> FxHashSet<ty::BoundRegionKind>
860 T: TypeFoldable<'tcx>,
862 self.collect_late_bound_regions(value, true)
865 /// Returns a set of all late-bound regions that appear in `value` anywhere.
866 pub fn collect_referenced_late_bound_regions<T>(
868 value: &Binder<'tcx, T>,
869 ) -> FxHashSet<ty::BoundRegionKind>
871 T: TypeFoldable<'tcx>,
873 self.collect_late_bound_regions(value, false)
876 fn collect_late_bound_regions<T>(
878 value: &Binder<'tcx, T>,
879 just_constraint: bool,
880 ) -> FxHashSet<ty::BoundRegionKind>
882 T: TypeFoldable<'tcx>,
884 let mut collector = LateBoundRegionsCollector::new(just_constraint);
885 let result = value.as_ref().skip_binder().visit_with(&mut collector);
886 assert!(result.is_continue()); // should never have stopped early
890 /// Replaces any late-bound regions bound in `value` with `'erased`. Useful in codegen but also
891 /// method lookup and a few other places where precise region relationships are not required.
892 pub fn erase_late_bound_regions<T>(self, value: Binder<'tcx, T>) -> T
894 T: TypeFoldable<'tcx>,
896 self.replace_late_bound_regions(value, |_| self.lifetimes.re_erased).0
899 /// Rewrite any late-bound regions so that they are anonymous. Region numbers are
900 /// assigned starting at 0 and increasing monotonically in the order traversed
901 /// by the fold operation.
903 /// The chief purpose of this function is to canonicalize regions so that two
904 /// `FnSig`s or `TraitRef`s which are equivalent up to region naming will become
905 /// structurally identical. For example, `for<'a, 'b> fn(&'a isize, &'b isize)` and
906 /// `for<'a, 'b> fn(&'b isize, &'a isize)` will become identical after anonymization.
907 pub fn anonymize_late_bound_regions<T>(self, sig: Binder<'tcx, T>) -> Binder<'tcx, T>
909 T: TypeFoldable<'tcx>,
913 .replace_late_bound_regions(sig, |_| {
914 let br = ty::BoundRegion {
915 var: ty::BoundVar::from_u32(counter),
916 kind: ty::BrAnon(counter),
918 let r = self.mk_region(ty::ReLateBound(ty::INNERMOST, br));
923 let bound_vars = self.mk_bound_variable_kinds(
924 (0..counter).map(|i| ty::BoundVariableKind::Region(ty::BrAnon(i))),
926 Binder::bind_with_vars(inner, bound_vars)
930 pub struct ValidateBoundVars<'tcx> {
931 bound_vars: &'tcx ty::List<ty::BoundVariableKind>,
932 binder_index: ty::DebruijnIndex,
933 // We may encounter the same variable at different levels of binding, so
934 // this can't just be `Ty`
935 visited: SsoHashSet<(ty::DebruijnIndex, Ty<'tcx>)>,
938 impl<'tcx> ValidateBoundVars<'tcx> {
939 pub fn new(bound_vars: &'tcx ty::List<ty::BoundVariableKind>) -> Self {
942 binder_index: ty::INNERMOST,
943 visited: SsoHashSet::default(),
948 impl<'tcx> TypeVisitor<'tcx> for ValidateBoundVars<'tcx> {
951 fn visit_binder<T: TypeFoldable<'tcx>>(
954 ) -> ControlFlow<Self::BreakTy> {
955 self.binder_index.shift_in(1);
956 let result = t.super_visit_with(self);
957 self.binder_index.shift_out(1);
961 fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
962 if t.outer_exclusive_binder() < self.binder_index
963 || !self.visited.insert((self.binder_index, t))
965 return ControlFlow::BREAK;
968 ty::Bound(debruijn, bound_ty) if debruijn == self.binder_index => {
969 if self.bound_vars.len() <= bound_ty.var.as_usize() {
970 bug!("Not enough bound vars: {:?} not found in {:?}", t, self.bound_vars);
972 let list_var = self.bound_vars[bound_ty.var.as_usize()];
974 ty::BoundVariableKind::Ty(kind) => {
975 if kind != bound_ty.kind {
977 "Mismatched type kinds: {:?} doesn't var in list {:?}",
984 bug!("Mismatched bound variable kinds! Expected type, found {:?}", list_var)
992 t.super_visit_with(self)
995 fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
997 ty::ReLateBound(index, br) if index == self.binder_index => {
998 if self.bound_vars.len() <= br.var.as_usize() {
999 bug!("Not enough bound vars: {:?} not found in {:?}", br, self.bound_vars);
1001 let list_var = self.bound_vars[br.var.as_usize()];
1003 ty::BoundVariableKind::Region(kind) => {
1004 if kind != br.kind {
1006 "Mismatched region kinds: {:?} doesn't match var ({:?}) in list ({:?})",
1014 "Mismatched bound variable kinds! Expected region, found {:?}",
1023 r.super_visit_with(self)
1027 ///////////////////////////////////////////////////////////////////////////
1030 // Shifts the De Bruijn indices on all escaping bound vars by a
1031 // fixed amount. Useful in substitution or when otherwise introducing
1032 // a binding level that is not intended to capture the existing bound
1033 // vars. See comment on `shift_vars_through_binders` method in
1034 // `subst.rs` for more details.
1036 struct Shifter<'tcx> {
1038 current_index: ty::DebruijnIndex,
1042 impl<'tcx> Shifter<'tcx> {
1043 pub fn new(tcx: TyCtxt<'tcx>, amount: u32) -> Self {
1044 Shifter { tcx, current_index: ty::INNERMOST, amount }
1048 impl<'tcx> TypeFolder<'tcx> for Shifter<'tcx> {
1049 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
1053 fn fold_binder<T: TypeFoldable<'tcx>>(
1055 t: ty::Binder<'tcx, T>,
1056 ) -> ty::Binder<'tcx, T> {
1057 self.current_index.shift_in(1);
1058 let t = t.super_fold_with(self);
1059 self.current_index.shift_out(1);
1063 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
1065 ty::ReLateBound(debruijn, br) => {
1066 if self.amount == 0 || debruijn < self.current_index {
1069 let debruijn = debruijn.shifted_in(self.amount);
1070 let shifted = ty::ReLateBound(debruijn, br);
1071 self.tcx.mk_region(shifted)
1078 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
1080 ty::Bound(debruijn, bound_ty) => {
1081 if self.amount == 0 || debruijn < self.current_index {
1084 let debruijn = debruijn.shifted_in(self.amount);
1085 self.tcx.mk_ty(ty::Bound(debruijn, bound_ty))
1089 _ => ty.super_fold_with(self),
1093 fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
1094 if let ty::ConstKind::Bound(debruijn, bound_ct) = ct.val() {
1095 if self.amount == 0 || debruijn < self.current_index {
1098 let debruijn = debruijn.shifted_in(self.amount);
1099 self.tcx.mk_const(ty::ConstS {
1100 val: ty::ConstKind::Bound(debruijn, bound_ct),
1105 ct.super_fold_with(self)
1110 pub fn shift_region<'tcx>(
1112 region: ty::Region<'tcx>,
1114 ) -> ty::Region<'tcx> {
1116 ty::ReLateBound(debruijn, br) if amount > 0 => {
1117 tcx.mk_region(ty::ReLateBound(debruijn.shifted_in(amount), br))
1123 pub fn shift_vars<'tcx, T>(tcx: TyCtxt<'tcx>, value: T, amount: u32) -> T
1125 T: TypeFoldable<'tcx>,
1127 debug!("shift_vars(value={:?}, amount={})", value, amount);
1129 value.fold_with(&mut Shifter::new(tcx, amount))
1132 #[derive(Debug, PartialEq, Eq, Copy, Clone)]
1133 struct FoundEscapingVars;
1135 /// An "escaping var" is a bound var whose binder is not part of `t`. A bound var can be a
1136 /// bound region or a bound type.
1138 /// So, for example, consider a type like the following, which has two binders:
1140 /// for<'a> fn(x: for<'b> fn(&'a isize, &'b isize))
1141 /// ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ outer scope
1142 /// ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ inner scope
1144 /// This type has *bound regions* (`'a`, `'b`), but it does not have escaping regions, because the
1145 /// binders of both `'a` and `'b` are part of the type itself. However, if we consider the *inner
1146 /// fn type*, that type has an escaping region: `'a`.
1148 /// Note that what I'm calling an "escaping var" is often just called a "free var". However,
1149 /// we already use the term "free var". It refers to the regions or types that we use to represent
1150 /// bound regions or type params on a fn definition while we are type checking its body.
1152 /// To clarify, conceptually there is no particular difference between
1153 /// an "escaping" var and a "free" var. However, there is a big
1154 /// difference in practice. Basically, when "entering" a binding
1155 /// level, one is generally required to do some sort of processing to
1156 /// a bound var, such as replacing it with a fresh/placeholder
1157 /// var, or making an entry in the environment to represent the
1158 /// scope to which it is attached, etc. An escaping var represents
1159 /// a bound var for which this processing has not yet been done.
1160 struct HasEscapingVarsVisitor {
1161 /// Anything bound by `outer_index` or "above" is escaping.
1162 outer_index: ty::DebruijnIndex,
1165 impl<'tcx> TypeVisitor<'tcx> for HasEscapingVarsVisitor {
1166 type BreakTy = FoundEscapingVars;
1168 fn visit_binder<T: TypeFoldable<'tcx>>(
1170 t: &Binder<'tcx, T>,
1171 ) -> ControlFlow<Self::BreakTy> {
1172 self.outer_index.shift_in(1);
1173 let result = t.super_visit_with(self);
1174 self.outer_index.shift_out(1);
1179 fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
1180 // If the outer-exclusive-binder is *strictly greater* than
1181 // `outer_index`, that means that `t` contains some content
1182 // bound at `outer_index` or above (because
1183 // `outer_exclusive_binder` is always 1 higher than the
1184 // content in `t`). Therefore, `t` has some escaping vars.
1185 if t.outer_exclusive_binder() > self.outer_index {
1186 ControlFlow::Break(FoundEscapingVars)
1188 ControlFlow::CONTINUE
1193 fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
1194 // If the region is bound by `outer_index` or anything outside
1195 // of outer index, then it escapes the binders we have
1197 if r.bound_at_or_above_binder(self.outer_index) {
1198 ControlFlow::Break(FoundEscapingVars)
1200 ControlFlow::CONTINUE
1204 fn visit_const(&mut self, ct: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
1205 // we don't have a `visit_infer_const` callback, so we have to
1206 // hook in here to catch this case (annoying...), but
1207 // otherwise we do want to remember to visit the rest of the
1208 // const, as it has types/regions embedded in a lot of other
1211 ty::ConstKind::Bound(debruijn, _) if debruijn >= self.outer_index => {
1212 ControlFlow::Break(FoundEscapingVars)
1214 _ => ct.super_visit_with(self),
1219 fn visit_predicate(&mut self, predicate: ty::Predicate<'tcx>) -> ControlFlow<Self::BreakTy> {
1220 if predicate.outer_exclusive_binder() > self.outer_index {
1221 ControlFlow::Break(FoundEscapingVars)
1223 ControlFlow::CONTINUE
1228 #[derive(Debug, PartialEq, Eq, Copy, Clone)]
1231 // FIXME: Optimize for checking for infer flags
1232 struct HasTypeFlagsVisitor {
1233 flags: ty::TypeFlags,
1236 impl std::fmt::Debug for HasTypeFlagsVisitor {
1237 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1242 impl<'tcx> TypeVisitor<'tcx> for HasTypeFlagsVisitor {
1243 type BreakTy = FoundFlags;
1246 #[instrument(skip(self), level = "trace")]
1247 fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
1248 let flags = t.flags();
1249 trace!(t.flags=?t.flags());
1250 if flags.intersects(self.flags) {
1251 ControlFlow::Break(FoundFlags)
1253 ControlFlow::CONTINUE
1258 #[instrument(skip(self), level = "trace")]
1259 fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
1260 let flags = r.type_flags();
1261 trace!(r.flags=?flags);
1262 if flags.intersects(self.flags) {
1263 ControlFlow::Break(FoundFlags)
1265 ControlFlow::CONTINUE
1270 #[instrument(level = "trace")]
1271 fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
1272 let flags = FlagComputation::for_const(c);
1273 trace!(r.flags=?flags);
1274 if flags.intersects(self.flags) {
1275 ControlFlow::Break(FoundFlags)
1277 ControlFlow::CONTINUE
1282 #[instrument(level = "trace")]
1283 fn visit_unevaluated_const(&mut self, uv: ty::Unevaluated<'tcx>) -> ControlFlow<Self::BreakTy> {
1284 let flags = FlagComputation::for_unevaluated_const(uv);
1285 trace!(r.flags=?flags);
1286 if flags.intersects(self.flags) {
1287 ControlFlow::Break(FoundFlags)
1289 ControlFlow::CONTINUE
1294 #[instrument(level = "trace")]
1295 fn visit_predicate(&mut self, predicate: ty::Predicate<'tcx>) -> ControlFlow<Self::BreakTy> {
1297 "HasTypeFlagsVisitor: predicate={:?} predicate.flags={:?} self.flags={:?}",
1302 if predicate.flags().intersects(self.flags) {
1303 ControlFlow::Break(FoundFlags)
1305 ControlFlow::CONTINUE
1310 /// Collects all the late-bound regions at the innermost binding level
1311 /// into a hash set.
1312 struct LateBoundRegionsCollector {
1313 current_index: ty::DebruijnIndex,
1314 regions: FxHashSet<ty::BoundRegionKind>,
1316 /// `true` if we only want regions that are known to be
1317 /// "constrained" when you equate this type with another type. In
1318 /// particular, if you have e.g., `&'a u32` and `&'b u32`, equating
1319 /// them constraints `'a == 'b`. But if you have `<&'a u32 as
1320 /// Trait>::Foo` and `<&'b u32 as Trait>::Foo`, normalizing those
1321 /// types may mean that `'a` and `'b` don't appear in the results,
1322 /// so they are not considered *constrained*.
1323 just_constrained: bool,
1326 impl LateBoundRegionsCollector {
1327 fn new(just_constrained: bool) -> Self {
1328 LateBoundRegionsCollector {
1329 current_index: ty::INNERMOST,
1330 regions: Default::default(),
1336 impl<'tcx> TypeVisitor<'tcx> for LateBoundRegionsCollector {
1337 fn visit_binder<T: TypeFoldable<'tcx>>(
1339 t: &Binder<'tcx, T>,
1340 ) -> ControlFlow<Self::BreakTy> {
1341 self.current_index.shift_in(1);
1342 let result = t.super_visit_with(self);
1343 self.current_index.shift_out(1);
1347 fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
1348 // if we are only looking for "constrained" region, we have to
1349 // ignore the inputs to a projection, as they may not appear
1350 // in the normalized form
1351 if self.just_constrained {
1352 if let ty::Projection(..) | ty::Opaque(..) = t.kind() {
1353 return ControlFlow::CONTINUE;
1357 t.super_visit_with(self)
1360 fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
1361 // if we are only looking for "constrained" region, we have to
1362 // ignore the inputs of an unevaluated const, as they may not appear
1363 // in the normalized form
1364 if self.just_constrained {
1365 if let ty::ConstKind::Unevaluated(..) = c.val() {
1366 return ControlFlow::CONTINUE;
1370 c.super_visit_with(self)
1373 fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
1374 if let ty::ReLateBound(debruijn, br) = *r {
1375 if debruijn == self.current_index {
1376 self.regions.insert(br.kind);
1379 ControlFlow::CONTINUE
1383 /// Finds the max universe present
1384 pub struct MaxUniverse {
1385 max_universe: ty::UniverseIndex,
1389 pub fn new() -> Self {
1390 MaxUniverse { max_universe: ty::UniverseIndex::ROOT }
1393 pub fn max_universe(self) -> ty::UniverseIndex {
1398 impl<'tcx> TypeVisitor<'tcx> for MaxUniverse {
1399 fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
1400 if let ty::Placeholder(placeholder) = t.kind() {
1401 self.max_universe = ty::UniverseIndex::from_u32(
1402 self.max_universe.as_u32().max(placeholder.universe.as_u32()),
1406 t.super_visit_with(self)
1409 fn visit_const(&mut self, c: ty::consts::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
1410 if let ty::ConstKind::Placeholder(placeholder) = c.val() {
1411 self.max_universe = ty::UniverseIndex::from_u32(
1412 self.max_universe.as_u32().max(placeholder.universe.as_u32()),
1416 c.super_visit_with(self)
1419 fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
1420 if let ty::RePlaceholder(placeholder) = *r {
1421 self.max_universe = ty::UniverseIndex::from_u32(
1422 self.max_universe.as_u32().max(placeholder.universe.as_u32()),
1426 ControlFlow::CONTINUE