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 three traits involved in each traversal type.
19 //! - `TypeFoldable`. This is implemented once for many types. This includes
21 //! - Types of interest, for which the the methods delegate to the
23 //! - All other types, including generic containers like `Vec` and `Option`.
24 //! It defines a "skeleton" of how they should be traversed, for both
25 //! folding and visiting.
26 //! - `TypeSuperFoldable`. This is implemented only for each type of interest,
27 //! and defines the traversal "skeleton" for these types.
28 //! - `TypeFolder`/`FallibleTypeFolder` (for infallible/fallible folding
29 //! traversals) or `TypeVisitor` (for visiting traversals). One of these is
30 //! implemented for each folder/visitor. This defines how types of interest
31 //! are folded/visited.
33 //! This means each traversal is a mixture of (a) generic traversal operations,
34 //! and (b) custom fold/visit operations that are specific to the
36 //! - The `TypeFoldable` impls handle most of the traversal, and call into
37 //! `TypeFolder`/`FallibleTypeFolder`/`TypeVisitor` when they encounter a
39 //! - A `TypeFolder`/`FallibleTypeFolder`/`TypeVisitor` may call into another
40 //! `TypeFoldable` impl, because some of the types of interest are recursive
41 //! and can contain other types of interest.
42 //! - A `TypeFolder`/`FallibleTypeFolder`/`TypeVisitor` may also call into
43 //! a `TypeSuperFoldable` impl, because each folder/visitor might provide
44 //! custom handling only for some types of interest, or only for some
45 //! variants of each type of interest, and then use default traversal for the
48 //! For example, if you have `struct S(Ty, U)` where `S: TypeFoldable` and `U:
49 //! TypeFoldable`, and an instance `s = S(ty, u)`, it would be visited like so:
51 //! s.visit_with(visitor) calls
52 //! - ty.visit_with(visitor) calls
53 //! - visitor.visit_ty(ty) may call
54 //! - ty.super_visit_with(visitor)
55 //! - u.visit_with(visitor)
58 use crate::ty::{self, flags::FlagComputation, Binder, Ty, TyCtxt, TypeFlags};
59 use rustc_errors::ErrorGuaranteed;
60 use rustc_hir::def_id::DefId;
62 use rustc_data_structures::fx::FxHashSet;
63 use rustc_data_structures::sso::SsoHashSet;
64 use std::collections::BTreeMap;
66 use std::ops::ControlFlow;
68 /// This trait is implemented for every type that can be folded/visited,
69 /// providing the skeleton of the traversal.
71 /// To implement this conveniently, use the derive macro located in
73 pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
74 /// The entry point for folding. To fold a value `t` with a folder `f`
75 /// call: `t.try_fold_with(f)`.
77 /// For most types, this just traverses the value, calling `try_fold_with`
78 /// on each field/element.
80 /// For types of interest (such as `Ty`), the implementation of method
81 /// calls a folder method specifically for that type (such as
82 /// `F::try_fold_ty`). This is where control transfers from `TypeFoldable`
84 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error>;
86 /// A convenient alternative to `try_fold_with` for use with infallible
87 /// folders. Do not override this method, to ensure coherence with
89 fn fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
90 self.try_fold_with(folder).into_ok()
93 /// The entry point for visiting. To visit a value `t` with a visitor `v`
94 /// call: `t.visit_with(v)`.
96 /// For most types, this just traverses the value, calling `visit_with` on
97 /// each field/element.
99 /// For types of interest (such as `Ty`), the implementation of this method
100 /// that calls a visitor method specifically for that type (such as
101 /// `V::visit_ty`). This is where control transfers from `TypeFoldable` to
103 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy>;
105 /// Returns `true` if `self` has any late-bound regions that are either
106 /// bound by `binder` or bound by some binder outside of `binder`.
107 /// If `binder` is `ty::INNERMOST`, this indicates whether
108 /// there are any late-bound regions that appear free.
109 fn has_vars_bound_at_or_above(&self, binder: ty::DebruijnIndex) -> bool {
110 self.visit_with(&mut HasEscapingVarsVisitor { outer_index: binder }).is_break()
113 /// Returns `true` if this `self` has any regions that escape `binder` (and
114 /// hence are not bound by it).
115 fn has_vars_bound_above(&self, binder: ty::DebruijnIndex) -> bool {
116 self.has_vars_bound_at_or_above(binder.shifted_in(1))
119 fn has_escaping_bound_vars(&self) -> bool {
120 self.has_vars_bound_at_or_above(ty::INNERMOST)
123 #[instrument(level = "trace")]
124 fn has_type_flags(&self, flags: TypeFlags) -> bool {
125 self.visit_with(&mut HasTypeFlagsVisitor { flags }).break_value() == Some(FoundFlags)
127 fn has_projections(&self) -> bool {
128 self.has_type_flags(TypeFlags::HAS_PROJECTION)
130 fn has_opaque_types(&self) -> bool {
131 self.has_type_flags(TypeFlags::HAS_TY_OPAQUE)
133 fn references_error(&self) -> bool {
134 self.has_type_flags(TypeFlags::HAS_ERROR)
136 fn error_reported(&self) -> Option<ErrorGuaranteed> {
137 if self.references_error() {
138 Some(ErrorGuaranteed::unchecked_claim_error_was_emitted())
143 fn has_param_types_or_consts(&self) -> bool {
144 self.has_type_flags(TypeFlags::HAS_TY_PARAM | TypeFlags::HAS_CT_PARAM)
146 fn has_infer_regions(&self) -> bool {
147 self.has_type_flags(TypeFlags::HAS_RE_INFER)
149 fn has_infer_types(&self) -> bool {
150 self.has_type_flags(TypeFlags::HAS_TY_INFER)
152 fn has_infer_types_or_consts(&self) -> bool {
153 self.has_type_flags(TypeFlags::HAS_TY_INFER | TypeFlags::HAS_CT_INFER)
155 fn needs_infer(&self) -> bool {
156 self.has_type_flags(TypeFlags::NEEDS_INFER)
158 fn has_placeholders(&self) -> bool {
160 TypeFlags::HAS_RE_PLACEHOLDER
161 | TypeFlags::HAS_TY_PLACEHOLDER
162 | TypeFlags::HAS_CT_PLACEHOLDER,
165 fn needs_subst(&self) -> bool {
166 self.has_type_flags(TypeFlags::NEEDS_SUBST)
168 /// "Free" regions in this context means that it has any region
169 /// that is not (a) erased or (b) late-bound.
170 fn has_free_regions(&self) -> bool {
171 self.has_type_flags(TypeFlags::HAS_FREE_REGIONS)
174 fn has_erased_regions(&self) -> bool {
175 self.has_type_flags(TypeFlags::HAS_RE_ERASED)
178 /// True if there are any un-erased free regions.
179 fn has_erasable_regions(&self) -> bool {
180 self.has_type_flags(TypeFlags::HAS_FREE_REGIONS)
183 /// Indicates whether this value references only 'global'
184 /// generic parameters that are the same regardless of what fn we are
185 /// in. This is used for caching.
186 fn is_global(&self) -> bool {
187 !self.has_type_flags(TypeFlags::HAS_FREE_LOCAL_NAMES)
190 /// True if there are any late-bound regions
191 fn has_late_bound_regions(&self) -> bool {
192 self.has_type_flags(TypeFlags::HAS_RE_LATE_BOUND)
195 /// Indicates whether this value still has parameters/placeholders/inference variables
196 /// which could be replaced later, in a way that would change the results of `impl`
198 fn still_further_specializable(&self) -> bool {
199 self.has_type_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE)
203 // This trait is implemented for types of interest.
204 pub trait TypeSuperFoldable<'tcx>: TypeFoldable<'tcx> {
205 /// Provides a default fold for a type of interest. This should only be
206 /// called within `TypeFolder` methods, when a non-custom traversal is
207 /// desired for the value of the type of interest passed to that method.
208 /// For example, in `MyFolder::try_fold_ty(ty)`, it is valid to call
209 /// `ty.try_super_fold_with(self)`, but any other folding should be done
210 /// with `xyz.try_fold_with(self)`.
211 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
214 ) -> Result<Self, F::Error>;
216 /// A convenient alternative to `try_super_fold_with` for use with
217 /// infallible folders. Do not override this method, to ensure coherence
218 /// with `try_super_fold_with`.
219 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
220 self.try_super_fold_with(folder).into_ok()
223 /// Provides a default visit for a type of interest. This should only be
224 /// called within `TypeVisitor` methods, when a non-custom traversal is
225 /// desired for the value of the type of interest passed to that method.
226 /// For example, in `MyVisitor::visit_ty(ty)`, it is valid to call
227 /// `ty.super_visit_with(self)`, but any other visiting should be done
228 /// with `xyz.visit_with(self)`.
229 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy>;
232 /// This trait is implemented for every infallible folding traversal. There is
233 /// a fold method defined for every type of interest. Each such method has a
234 /// default that does an "identity" fold. Implementations of these methods
235 /// often fall back to a `super_fold_with` method if the primary argument
236 /// doesn't satisfy a particular condition.
238 /// A blanket implementation of [`FallibleTypeFolder`] will defer to
239 /// the infallible methods of this trait to ensure that the two APIs
241 pub trait TypeFolder<'tcx>: FallibleTypeFolder<'tcx, Error = !> {
242 fn tcx<'a>(&'a self) -> TyCtxt<'tcx>;
244 fn fold_binder<T>(&mut self, t: Binder<'tcx, T>) -> Binder<'tcx, T>
246 T: TypeFoldable<'tcx>,
248 t.super_fold_with(self)
251 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
252 t.super_fold_with(self)
255 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
256 r.super_fold_with(self)
259 fn fold_const(&mut self, c: ty::Const<'tcx>) -> ty::Const<'tcx> {
260 c.super_fold_with(self)
263 fn fold_unevaluated(&mut self, uv: ty::Unevaluated<'tcx>) -> ty::Unevaluated<'tcx> {
264 uv.super_fold_with(self)
267 fn fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> {
268 p.super_fold_with(self)
271 fn fold_mir_const(&mut self, c: mir::ConstantKind<'tcx>) -> mir::ConstantKind<'tcx> {
272 bug!("most type folders should not be folding MIR datastructures: {:?}", c)
276 /// This trait is implemented for every folding traversal. There is a fold
277 /// method defined for every type of interest. Each such method has a default
278 /// that does an "identity" fold.
280 /// A blanket implementation of this trait (that defers to the relevant
281 /// method of [`TypeFolder`]) is provided for all infallible folders in
282 /// order to ensure the two APIs are coherent.
283 pub trait FallibleTypeFolder<'tcx>: Sized {
286 fn tcx<'a>(&'a self) -> TyCtxt<'tcx>;
288 fn try_fold_binder<T>(&mut self, t: Binder<'tcx, T>) -> Result<Binder<'tcx, T>, Self::Error>
290 T: TypeFoldable<'tcx>,
292 t.try_super_fold_with(self)
295 fn try_fold_ty(&mut self, t: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
296 t.try_super_fold_with(self)
299 fn try_fold_region(&mut self, r: ty::Region<'tcx>) -> Result<ty::Region<'tcx>, Self::Error> {
300 r.try_super_fold_with(self)
303 fn try_fold_const(&mut self, c: ty::Const<'tcx>) -> Result<ty::Const<'tcx>, Self::Error> {
304 c.try_super_fold_with(self)
307 fn try_fold_unevaluated(
309 c: ty::Unevaluated<'tcx>,
310 ) -> Result<ty::Unevaluated<'tcx>, Self::Error> {
311 c.try_super_fold_with(self)
314 fn try_fold_predicate(
316 p: ty::Predicate<'tcx>,
317 ) -> Result<ty::Predicate<'tcx>, Self::Error> {
318 p.try_super_fold_with(self)
321 fn try_fold_mir_const(
323 c: mir::ConstantKind<'tcx>,
324 ) -> Result<mir::ConstantKind<'tcx>, Self::Error> {
325 bug!("most type folders should not be folding MIR datastructures: {:?}", c)
329 // This blanket implementation of the fallible trait for infallible folders
330 // delegates to infallible methods to ensure coherence.
331 impl<'tcx, F> FallibleTypeFolder<'tcx> for F
337 fn tcx<'a>(&'a self) -> TyCtxt<'tcx> {
338 TypeFolder::tcx(self)
341 fn try_fold_binder<T>(&mut self, t: Binder<'tcx, T>) -> Result<Binder<'tcx, T>, !>
343 T: TypeFoldable<'tcx>,
345 Ok(self.fold_binder(t))
348 fn try_fold_ty(&mut self, t: Ty<'tcx>) -> Result<Ty<'tcx>, !> {
352 fn try_fold_region(&mut self, r: ty::Region<'tcx>) -> Result<ty::Region<'tcx>, !> {
353 Ok(self.fold_region(r))
356 fn try_fold_const(&mut self, c: ty::Const<'tcx>) -> Result<ty::Const<'tcx>, !> {
357 Ok(self.fold_const(c))
360 fn try_fold_unevaluated(
362 c: ty::Unevaluated<'tcx>,
363 ) -> Result<ty::Unevaluated<'tcx>, !> {
364 Ok(self.fold_unevaluated(c))
367 fn try_fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> Result<ty::Predicate<'tcx>, !> {
368 Ok(self.fold_predicate(p))
371 fn try_fold_mir_const(
373 c: mir::ConstantKind<'tcx>,
374 ) -> Result<mir::ConstantKind<'tcx>, !> {
375 Ok(self.fold_mir_const(c))
379 /// This trait is implemented for every visiting traversal. There is a visit
380 /// method defined for every type of interest. Each such method has a default
381 /// that recurses into the type's fields in a non-custom fashion.
382 pub trait TypeVisitor<'tcx>: Sized {
385 fn visit_binder<T: TypeFoldable<'tcx>>(
388 ) -> ControlFlow<Self::BreakTy> {
389 t.super_visit_with(self)
392 fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
393 t.super_visit_with(self)
396 fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
397 r.super_visit_with(self)
400 fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
401 c.super_visit_with(self)
404 fn visit_unevaluated(&mut self, uv: ty::Unevaluated<'tcx>) -> ControlFlow<Self::BreakTy> {
405 uv.super_visit_with(self)
408 fn visit_predicate(&mut self, p: ty::Predicate<'tcx>) -> ControlFlow<Self::BreakTy> {
409 p.super_visit_with(self)
412 fn visit_mir_const(&mut self, c: mir::ConstantKind<'tcx>) -> ControlFlow<Self::BreakTy> {
413 c.super_visit_with(self)
417 ///////////////////////////////////////////////////////////////////////////
418 // Some sample folders
420 pub struct BottomUpFolder<'tcx, F, G, H>
422 F: FnMut(Ty<'tcx>) -> Ty<'tcx>,
423 G: FnMut(ty::Region<'tcx>) -> ty::Region<'tcx>,
424 H: FnMut(ty::Const<'tcx>) -> ty::Const<'tcx>,
426 pub tcx: TyCtxt<'tcx>,
432 impl<'tcx, F, G, H> TypeFolder<'tcx> for BottomUpFolder<'tcx, F, G, H>
434 F: FnMut(Ty<'tcx>) -> Ty<'tcx>,
435 G: FnMut(ty::Region<'tcx>) -> ty::Region<'tcx>,
436 H: FnMut(ty::Const<'tcx>) -> ty::Const<'tcx>,
438 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
442 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
443 let t = ty.super_fold_with(self);
447 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
448 let r = r.super_fold_with(self);
452 fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
453 let ct = ct.super_fold_with(self);
458 ///////////////////////////////////////////////////////////////////////////
461 impl<'tcx> TyCtxt<'tcx> {
462 /// Folds the escaping and free regions in `value` using `f`, and
463 /// sets `skipped_regions` to true if any late-bound region was found
465 pub fn fold_regions<T>(
468 skipped_regions: &mut bool,
469 mut f: impl FnMut(ty::Region<'tcx>, ty::DebruijnIndex) -> ty::Region<'tcx>,
472 T: TypeFoldable<'tcx>,
474 value.fold_with(&mut RegionFolder::new(self, skipped_regions, &mut f))
477 /// Invoke `callback` on every region appearing free in `value`.
478 pub fn for_each_free_region(
480 value: &impl TypeFoldable<'tcx>,
481 mut callback: impl FnMut(ty::Region<'tcx>),
483 self.any_free_region_meets(value, |r| {
489 /// Returns `true` if `callback` returns true for every region appearing free in `value`.
490 pub fn all_free_regions_meet(
492 value: &impl TypeFoldable<'tcx>,
493 mut callback: impl FnMut(ty::Region<'tcx>) -> bool,
495 !self.any_free_region_meets(value, |r| !callback(r))
498 /// Returns `true` if `callback` returns true for some region appearing free in `value`.
499 pub fn any_free_region_meets(
501 value: &impl TypeFoldable<'tcx>,
502 callback: impl FnMut(ty::Region<'tcx>) -> bool,
504 struct RegionVisitor<F> {
505 /// The index of a binder *just outside* the things we have
506 /// traversed. If we encounter a bound region bound by this
507 /// binder or one outer to it, it appears free. Example:
509 /// ```ignore (illustrative)
510 /// for<'a> fn(for<'b> fn(), T)
512 /// // | | | | here, would be shifted in 1
513 /// // | | | here, would be shifted in 2
514 /// // | | here, would be `INNERMOST` shifted in by 1
515 /// // | here, initially, binder would be `INNERMOST`
518 /// You see that, initially, *any* bound value is free,
519 /// because we've not traversed any binders. As we pass
520 /// through a binder, we shift the `outer_index` by 1 to
521 /// account for the new binder that encloses us.
522 outer_index: ty::DebruijnIndex,
526 impl<'tcx, F> TypeVisitor<'tcx> for RegionVisitor<F>
528 F: FnMut(ty::Region<'tcx>) -> bool,
532 fn visit_binder<T: TypeFoldable<'tcx>>(
535 ) -> ControlFlow<Self::BreakTy> {
536 self.outer_index.shift_in(1);
537 let result = t.super_visit_with(self);
538 self.outer_index.shift_out(1);
542 fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
544 ty::ReLateBound(debruijn, _) if debruijn < self.outer_index => {
545 ControlFlow::CONTINUE
548 if (self.callback)(r) {
551 ControlFlow::CONTINUE
557 fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
558 // We're only interested in types involving regions
559 if ty.flags().intersects(TypeFlags::HAS_FREE_REGIONS) {
560 ty.super_visit_with(self)
562 ControlFlow::CONTINUE
567 value.visit_with(&mut RegionVisitor { outer_index: ty::INNERMOST, callback }).is_break()
571 /// Folds over the substructure of a type, visiting its component
572 /// types and all regions that occur *free* within it.
574 /// That is, `Ty` can contain function or method types that bind
575 /// regions at the call site (`ReLateBound`), and occurrences of
576 /// regions (aka "lifetimes") that are bound within a type are not
577 /// visited by this folder; only regions that occur free will be
578 /// visited by `fld_r`.
580 pub struct RegionFolder<'a, 'tcx> {
582 skipped_regions: &'a mut bool,
584 /// Stores the index of a binder *just outside* the stuff we have
585 /// visited. So this begins as INNERMOST; when we pass through a
586 /// binder, it is incremented (via `shift_in`).
587 current_index: ty::DebruijnIndex,
589 /// Callback invokes for each free region. The `DebruijnIndex`
590 /// points to the binder *just outside* the ones we have passed
593 &'a mut (dyn FnMut(ty::Region<'tcx>, ty::DebruijnIndex) -> ty::Region<'tcx> + 'a),
596 impl<'a, 'tcx> RegionFolder<'a, 'tcx> {
600 skipped_regions: &'a mut bool,
601 fold_region_fn: &'a mut dyn FnMut(ty::Region<'tcx>, ty::DebruijnIndex) -> ty::Region<'tcx>,
602 ) -> RegionFolder<'a, 'tcx> {
603 RegionFolder { tcx, skipped_regions, current_index: ty::INNERMOST, fold_region_fn }
607 impl<'a, 'tcx> TypeFolder<'tcx> for RegionFolder<'a, 'tcx> {
608 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
612 fn fold_binder<T: TypeFoldable<'tcx>>(
614 t: ty::Binder<'tcx, T>,
615 ) -> ty::Binder<'tcx, T> {
616 self.current_index.shift_in(1);
617 let t = t.super_fold_with(self);
618 self.current_index.shift_out(1);
622 #[instrument(skip(self), level = "debug")]
623 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
625 ty::ReLateBound(debruijn, _) if debruijn < self.current_index => {
626 debug!(?self.current_index, "skipped bound region");
627 *self.skipped_regions = true;
631 debug!(?self.current_index, "folding free region");
632 (self.fold_region_fn)(r, self.current_index)
638 ///////////////////////////////////////////////////////////////////////////
639 // Bound vars replacer
641 /// Replaces the escaping bound vars (late bound regions or bound types) in a type.
642 struct BoundVarReplacer<'a, 'tcx> {
645 /// As with `RegionFolder`, represents the index of a binder *just outside*
646 /// the ones we have visited.
647 current_index: ty::DebruijnIndex,
649 fld_r: &'a mut (dyn FnMut(ty::BoundRegion) -> ty::Region<'tcx> + 'a),
650 fld_t: &'a mut (dyn FnMut(ty::BoundTy) -> Ty<'tcx> + 'a),
651 fld_c: &'a mut (dyn FnMut(ty::BoundVar, Ty<'tcx>) -> ty::Const<'tcx> + 'a),
654 impl<'a, 'tcx> BoundVarReplacer<'a, 'tcx> {
657 fld_r: &'a mut (dyn FnMut(ty::BoundRegion) -> ty::Region<'tcx> + 'a),
658 fld_t: &'a mut (dyn FnMut(ty::BoundTy) -> Ty<'tcx> + 'a),
659 fld_c: &'a mut (dyn FnMut(ty::BoundVar, Ty<'tcx>) -> ty::Const<'tcx> + 'a),
661 BoundVarReplacer { tcx, current_index: ty::INNERMOST, fld_r, fld_t, fld_c }
665 impl<'a, 'tcx> TypeFolder<'tcx> for BoundVarReplacer<'a, 'tcx> {
666 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
670 fn fold_binder<T: TypeFoldable<'tcx>>(
672 t: ty::Binder<'tcx, T>,
673 ) -> ty::Binder<'tcx, T> {
674 self.current_index.shift_in(1);
675 let t = t.super_fold_with(self);
676 self.current_index.shift_out(1);
680 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
682 ty::Bound(debruijn, bound_ty) if debruijn == self.current_index => {
683 let ty = (self.fld_t)(bound_ty);
684 ty::fold::shift_vars(self.tcx, ty, self.current_index.as_u32())
686 _ if t.has_vars_bound_at_or_above(self.current_index) => t.super_fold_with(self),
691 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
693 ty::ReLateBound(debruijn, br) if debruijn == self.current_index => {
694 let region = (self.fld_r)(br);
695 if let ty::ReLateBound(debruijn1, br) = *region {
696 // If the callback returns a late-bound region,
697 // that region should always use the INNERMOST
698 // debruijn index. Then we adjust it to the
700 assert_eq!(debruijn1, ty::INNERMOST);
701 self.tcx.mk_region(ty::ReLateBound(debruijn, br))
710 fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
712 ty::ConstKind::Bound(debruijn, bound_const) if debruijn == self.current_index => {
713 let ct = (self.fld_c)(bound_const, ct.ty());
714 ty::fold::shift_vars(self.tcx, ct, self.current_index.as_u32())
716 _ if ct.has_vars_bound_at_or_above(self.current_index) => ct.super_fold_with(self),
722 impl<'tcx> TyCtxt<'tcx> {
723 /// Replaces all regions bound by the given `Binder` with the
724 /// results returned by the closure; the closure is expected to
725 /// return a free region (relative to this binder), and hence the
726 /// binder is removed in the return type. The closure is invoked
727 /// once for each unique `BoundRegionKind`; multiple references to the
728 /// same `BoundRegionKind` will reuse the previous result. A map is
729 /// returned at the end with each bound region and the free region
730 /// that replaced it.
734 /// This method only replaces late bound regions. Any types or
735 /// constants bound by `value` will cause an ICE.
736 pub fn replace_late_bound_regions<T, F>(
738 value: Binder<'tcx, T>,
740 ) -> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
742 F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
743 T: TypeFoldable<'tcx>,
745 let mut region_map = BTreeMap::new();
746 let real_fld_r = |br: ty::BoundRegion| *region_map.entry(br).or_insert_with(|| fld_r(br));
747 let value = self.replace_late_bound_regions_uncached(value, real_fld_r);
751 pub fn replace_late_bound_regions_uncached<T, F>(
753 value: Binder<'tcx, T>,
757 F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
758 T: TypeFoldable<'tcx>,
760 let mut fld_t = |b| bug!("unexpected bound ty in binder: {b:?}");
761 let mut fld_c = |b, ty| bug!("unexpected bound ct in binder: {b:?} {ty}");
762 let value = value.skip_binder();
763 if !value.has_escaping_bound_vars() {
766 let mut replacer = BoundVarReplacer::new(self, &mut fld_r, &mut fld_t, &mut fld_c);
767 value.fold_with(&mut replacer)
771 /// Replaces all escaping bound vars. The `fld_r` closure replaces escaping
772 /// bound regions; the `fld_t` closure replaces escaping bound types and the `fld_c`
773 /// closure replaces escaping bound consts.
774 pub fn replace_escaping_bound_vars_uncached<T, F, G, H>(
782 F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
783 G: FnMut(ty::BoundTy) -> Ty<'tcx>,
784 H: FnMut(ty::BoundVar, Ty<'tcx>) -> ty::Const<'tcx>,
785 T: TypeFoldable<'tcx>,
787 if !value.has_escaping_bound_vars() {
790 let mut replacer = BoundVarReplacer::new(self, &mut fld_r, &mut fld_t, &mut fld_c);
791 value.fold_with(&mut replacer)
795 /// Replaces all types or regions bound by the given `Binder`. The `fld_r`
796 /// closure replaces bound regions, the `fld_t` closure replaces bound
797 /// types, and `fld_c` replaces bound constants.
798 pub fn replace_bound_vars_uncached<T, F, G, H>(
800 value: Binder<'tcx, T>,
806 F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
807 G: FnMut(ty::BoundTy) -> Ty<'tcx>,
808 H: FnMut(ty::BoundVar, Ty<'tcx>) -> ty::Const<'tcx>,
809 T: TypeFoldable<'tcx>,
811 self.replace_escaping_bound_vars_uncached(value.skip_binder(), fld_r, fld_t, fld_c)
814 /// Replaces any late-bound regions bound in `value` with
815 /// free variants attached to `all_outlive_scope`.
816 pub fn liberate_late_bound_regions<T>(
818 all_outlive_scope: DefId,
819 value: ty::Binder<'tcx, T>,
822 T: TypeFoldable<'tcx>,
824 self.replace_late_bound_regions_uncached(value, |br| {
825 self.mk_region(ty::ReFree(ty::FreeRegion {
826 scope: all_outlive_scope,
827 bound_region: br.kind,
832 pub fn shift_bound_var_indices<T>(self, bound_vars: usize, value: T) -> T
834 T: TypeFoldable<'tcx>,
836 self.replace_escaping_bound_vars_uncached(
839 self.mk_region(ty::ReLateBound(
842 var: ty::BoundVar::from_usize(r.var.as_usize() + bound_vars),
848 self.mk_ty(ty::Bound(
851 var: ty::BoundVar::from_usize(t.var.as_usize() + bound_vars),
857 self.mk_const(ty::ConstS {
858 kind: ty::ConstKind::Bound(
860 ty::BoundVar::from_usize(c.as_usize() + bound_vars),
868 /// Returns a set of all late-bound regions that are constrained
869 /// by `value`, meaning that if we instantiate those LBR with
870 /// variables and equate `value` with something else, those
871 /// variables will also be equated.
872 pub fn collect_constrained_late_bound_regions<T>(
874 value: &Binder<'tcx, T>,
875 ) -> FxHashSet<ty::BoundRegionKind>
877 T: TypeFoldable<'tcx>,
879 self.collect_late_bound_regions(value, true)
882 /// Returns a set of all late-bound regions that appear in `value` anywhere.
883 pub fn collect_referenced_late_bound_regions<T>(
885 value: &Binder<'tcx, T>,
886 ) -> FxHashSet<ty::BoundRegionKind>
888 T: TypeFoldable<'tcx>,
890 self.collect_late_bound_regions(value, false)
893 fn collect_late_bound_regions<T>(
895 value: &Binder<'tcx, T>,
896 just_constraint: bool,
897 ) -> FxHashSet<ty::BoundRegionKind>
899 T: TypeFoldable<'tcx>,
901 let mut collector = LateBoundRegionsCollector::new(just_constraint);
902 let result = value.as_ref().skip_binder().visit_with(&mut collector);
903 assert!(result.is_continue()); // should never have stopped early
907 /// Replaces any late-bound regions bound in `value` with `'erased`. Useful in codegen but also
908 /// method lookup and a few other places where precise region relationships are not required.
909 pub fn erase_late_bound_regions<T>(self, value: Binder<'tcx, T>) -> T
911 T: TypeFoldable<'tcx>,
913 self.replace_late_bound_regions(value, |_| self.lifetimes.re_erased).0
916 /// Rewrite any late-bound regions so that they are anonymous. Region numbers are
917 /// assigned starting at 0 and increasing monotonically in the order traversed
918 /// by the fold operation.
920 /// The chief purpose of this function is to canonicalize regions so that two
921 /// `FnSig`s or `TraitRef`s which are equivalent up to region naming will become
922 /// structurally identical. For example, `for<'a, 'b> fn(&'a isize, &'b isize)` and
923 /// `for<'a, 'b> fn(&'b isize, &'a isize)` will become identical after anonymization.
924 pub fn anonymize_late_bound_regions<T>(self, sig: Binder<'tcx, T>) -> Binder<'tcx, T>
926 T: TypeFoldable<'tcx>,
930 .replace_late_bound_regions(sig, |_| {
931 let br = ty::BoundRegion {
932 var: ty::BoundVar::from_u32(counter),
933 kind: ty::BrAnon(counter),
935 let r = self.mk_region(ty::ReLateBound(ty::INNERMOST, br));
940 let bound_vars = self.mk_bound_variable_kinds(
941 (0..counter).map(|i| ty::BoundVariableKind::Region(ty::BrAnon(i))),
943 Binder::bind_with_vars(inner, bound_vars)
947 pub struct ValidateBoundVars<'tcx> {
948 bound_vars: &'tcx ty::List<ty::BoundVariableKind>,
949 binder_index: ty::DebruijnIndex,
950 // We may encounter the same variable at different levels of binding, so
951 // this can't just be `Ty`
952 visited: SsoHashSet<(ty::DebruijnIndex, Ty<'tcx>)>,
955 impl<'tcx> ValidateBoundVars<'tcx> {
956 pub fn new(bound_vars: &'tcx ty::List<ty::BoundVariableKind>) -> Self {
959 binder_index: ty::INNERMOST,
960 visited: SsoHashSet::default(),
965 impl<'tcx> TypeVisitor<'tcx> for ValidateBoundVars<'tcx> {
968 fn visit_binder<T: TypeFoldable<'tcx>>(
971 ) -> ControlFlow<Self::BreakTy> {
972 self.binder_index.shift_in(1);
973 let result = t.super_visit_with(self);
974 self.binder_index.shift_out(1);
978 fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
979 if t.outer_exclusive_binder() < self.binder_index
980 || !self.visited.insert((self.binder_index, t))
982 return ControlFlow::BREAK;
985 ty::Bound(debruijn, bound_ty) if debruijn == self.binder_index => {
986 if self.bound_vars.len() <= bound_ty.var.as_usize() {
987 bug!("Not enough bound vars: {:?} not found in {:?}", t, self.bound_vars);
989 let list_var = self.bound_vars[bound_ty.var.as_usize()];
991 ty::BoundVariableKind::Ty(kind) => {
992 if kind != bound_ty.kind {
994 "Mismatched type kinds: {:?} doesn't var in list {:?}",
1001 bug!("Mismatched bound variable kinds! Expected type, found {:?}", list_var)
1009 t.super_visit_with(self)
1012 fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
1014 ty::ReLateBound(index, br) if index == self.binder_index => {
1015 if self.bound_vars.len() <= br.var.as_usize() {
1016 bug!("Not enough bound vars: {:?} not found in {:?}", br, self.bound_vars);
1018 let list_var = self.bound_vars[br.var.as_usize()];
1020 ty::BoundVariableKind::Region(kind) => {
1021 if kind != br.kind {
1023 "Mismatched region kinds: {:?} doesn't match var ({:?}) in list ({:?})",
1031 "Mismatched bound variable kinds! Expected region, found {:?}",
1040 r.super_visit_with(self)
1044 ///////////////////////////////////////////////////////////////////////////
1047 // Shifts the De Bruijn indices on all escaping bound vars by a
1048 // fixed amount. Useful in substitution or when otherwise introducing
1049 // a binding level that is not intended to capture the existing bound
1050 // vars. See comment on `shift_vars_through_binders` method in
1051 // `subst.rs` for more details.
1053 struct Shifter<'tcx> {
1055 current_index: ty::DebruijnIndex,
1059 impl<'tcx> Shifter<'tcx> {
1060 pub fn new(tcx: TyCtxt<'tcx>, amount: u32) -> Self {
1061 Shifter { tcx, current_index: ty::INNERMOST, amount }
1065 impl<'tcx> TypeFolder<'tcx> for Shifter<'tcx> {
1066 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
1070 fn fold_binder<T: TypeFoldable<'tcx>>(
1072 t: ty::Binder<'tcx, T>,
1073 ) -> ty::Binder<'tcx, T> {
1074 self.current_index.shift_in(1);
1075 let t = t.super_fold_with(self);
1076 self.current_index.shift_out(1);
1080 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
1082 ty::ReLateBound(debruijn, br) => {
1083 if self.amount == 0 || debruijn < self.current_index {
1086 let debruijn = debruijn.shifted_in(self.amount);
1087 let shifted = ty::ReLateBound(debruijn, br);
1088 self.tcx.mk_region(shifted)
1095 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
1097 ty::Bound(debruijn, bound_ty) => {
1098 if self.amount == 0 || debruijn < self.current_index {
1101 let debruijn = debruijn.shifted_in(self.amount);
1102 self.tcx.mk_ty(ty::Bound(debruijn, bound_ty))
1106 _ => ty.super_fold_with(self),
1110 fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
1111 if let ty::ConstKind::Bound(debruijn, bound_ct) = ct.kind() {
1112 if self.amount == 0 || debruijn < self.current_index {
1115 let debruijn = debruijn.shifted_in(self.amount);
1116 self.tcx.mk_const(ty::ConstS {
1117 kind: ty::ConstKind::Bound(debruijn, bound_ct),
1122 ct.super_fold_with(self)
1127 pub fn shift_region<'tcx>(
1129 region: ty::Region<'tcx>,
1131 ) -> ty::Region<'tcx> {
1133 ty::ReLateBound(debruijn, br) if amount > 0 => {
1134 tcx.mk_region(ty::ReLateBound(debruijn.shifted_in(amount), br))
1140 pub fn shift_vars<'tcx, T>(tcx: TyCtxt<'tcx>, value: T, amount: u32) -> T
1142 T: TypeFoldable<'tcx>,
1144 debug!("shift_vars(value={:?}, amount={})", value, amount);
1146 value.fold_with(&mut Shifter::new(tcx, amount))
1149 #[derive(Debug, PartialEq, Eq, Copy, Clone)]
1150 struct FoundEscapingVars;
1152 /// An "escaping var" is a bound var whose binder is not part of `t`. A bound var can be a
1153 /// bound region or a bound type.
1155 /// So, for example, consider a type like the following, which has two binders:
1157 /// for<'a> fn(x: for<'b> fn(&'a isize, &'b isize))
1158 /// ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ outer scope
1159 /// ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ inner scope
1161 /// This type has *bound regions* (`'a`, `'b`), but it does not have escaping regions, because the
1162 /// binders of both `'a` and `'b` are part of the type itself. However, if we consider the *inner
1163 /// fn type*, that type has an escaping region: `'a`.
1165 /// Note that what I'm calling an "escaping var" is often just called a "free var". However,
1166 /// we already use the term "free var". It refers to the regions or types that we use to represent
1167 /// bound regions or type params on a fn definition while we are type checking its body.
1169 /// To clarify, conceptually there is no particular difference between
1170 /// an "escaping" var and a "free" var. However, there is a big
1171 /// difference in practice. Basically, when "entering" a binding
1172 /// level, one is generally required to do some sort of processing to
1173 /// a bound var, such as replacing it with a fresh/placeholder
1174 /// var, or making an entry in the environment to represent the
1175 /// scope to which it is attached, etc. An escaping var represents
1176 /// a bound var for which this processing has not yet been done.
1177 struct HasEscapingVarsVisitor {
1178 /// Anything bound by `outer_index` or "above" is escaping.
1179 outer_index: ty::DebruijnIndex,
1182 impl<'tcx> TypeVisitor<'tcx> for HasEscapingVarsVisitor {
1183 type BreakTy = FoundEscapingVars;
1185 fn visit_binder<T: TypeFoldable<'tcx>>(
1187 t: &Binder<'tcx, T>,
1188 ) -> ControlFlow<Self::BreakTy> {
1189 self.outer_index.shift_in(1);
1190 let result = t.super_visit_with(self);
1191 self.outer_index.shift_out(1);
1196 fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
1197 // If the outer-exclusive-binder is *strictly greater* than
1198 // `outer_index`, that means that `t` contains some content
1199 // bound at `outer_index` or above (because
1200 // `outer_exclusive_binder` is always 1 higher than the
1201 // content in `t`). Therefore, `t` has some escaping vars.
1202 if t.outer_exclusive_binder() > self.outer_index {
1203 ControlFlow::Break(FoundEscapingVars)
1205 ControlFlow::CONTINUE
1210 fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
1211 // If the region is bound by `outer_index` or anything outside
1212 // of outer index, then it escapes the binders we have
1214 if r.bound_at_or_above_binder(self.outer_index) {
1215 ControlFlow::Break(FoundEscapingVars)
1217 ControlFlow::CONTINUE
1221 fn visit_const(&mut self, ct: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
1222 // we don't have a `visit_infer_const` callback, so we have to
1223 // hook in here to catch this case (annoying...), but
1224 // otherwise we do want to remember to visit the rest of the
1225 // const, as it has types/regions embedded in a lot of other
1228 ty::ConstKind::Bound(debruijn, _) if debruijn >= self.outer_index => {
1229 ControlFlow::Break(FoundEscapingVars)
1231 _ => ct.super_visit_with(self),
1236 fn visit_predicate(&mut self, predicate: ty::Predicate<'tcx>) -> ControlFlow<Self::BreakTy> {
1237 if predicate.outer_exclusive_binder() > self.outer_index {
1238 ControlFlow::Break(FoundEscapingVars)
1240 ControlFlow::CONTINUE
1245 #[derive(Debug, PartialEq, Eq, Copy, Clone)]
1248 // FIXME: Optimize for checking for infer flags
1249 struct HasTypeFlagsVisitor {
1250 flags: ty::TypeFlags,
1253 impl std::fmt::Debug for HasTypeFlagsVisitor {
1254 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1259 impl<'tcx> TypeVisitor<'tcx> for HasTypeFlagsVisitor {
1260 type BreakTy = FoundFlags;
1263 #[instrument(skip(self), level = "trace")]
1264 fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
1265 let flags = t.flags();
1266 trace!(t.flags=?t.flags());
1267 if flags.intersects(self.flags) {
1268 ControlFlow::Break(FoundFlags)
1270 ControlFlow::CONTINUE
1275 #[instrument(skip(self), level = "trace")]
1276 fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
1277 let flags = r.type_flags();
1278 trace!(r.flags=?flags);
1279 if flags.intersects(self.flags) {
1280 ControlFlow::Break(FoundFlags)
1282 ControlFlow::CONTINUE
1287 #[instrument(level = "trace")]
1288 fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
1289 let flags = FlagComputation::for_const(c);
1290 trace!(r.flags=?flags);
1291 if flags.intersects(self.flags) {
1292 ControlFlow::Break(FoundFlags)
1294 ControlFlow::CONTINUE
1299 #[instrument(level = "trace")]
1300 fn visit_unevaluated(&mut self, uv: ty::Unevaluated<'tcx>) -> ControlFlow<Self::BreakTy> {
1301 let flags = FlagComputation::for_unevaluated_const(uv);
1302 trace!(r.flags=?flags);
1303 if flags.intersects(self.flags) {
1304 ControlFlow::Break(FoundFlags)
1306 ControlFlow::CONTINUE
1311 #[instrument(level = "trace")]
1312 fn visit_predicate(&mut self, predicate: ty::Predicate<'tcx>) -> ControlFlow<Self::BreakTy> {
1314 "HasTypeFlagsVisitor: predicate={:?} predicate.flags={:?} self.flags={:?}",
1319 if predicate.flags().intersects(self.flags) {
1320 ControlFlow::Break(FoundFlags)
1322 ControlFlow::CONTINUE
1327 /// Collects all the late-bound regions at the innermost binding level
1328 /// into a hash set.
1329 struct LateBoundRegionsCollector {
1330 current_index: ty::DebruijnIndex,
1331 regions: FxHashSet<ty::BoundRegionKind>,
1333 /// `true` if we only want regions that are known to be
1334 /// "constrained" when you equate this type with another type. In
1335 /// particular, if you have e.g., `&'a u32` and `&'b u32`, equating
1336 /// them constraints `'a == 'b`. But if you have `<&'a u32 as
1337 /// Trait>::Foo` and `<&'b u32 as Trait>::Foo`, normalizing those
1338 /// types may mean that `'a` and `'b` don't appear in the results,
1339 /// so they are not considered *constrained*.
1340 just_constrained: bool,
1343 impl LateBoundRegionsCollector {
1344 fn new(just_constrained: bool) -> Self {
1345 LateBoundRegionsCollector {
1346 current_index: ty::INNERMOST,
1347 regions: Default::default(),
1353 impl<'tcx> TypeVisitor<'tcx> for LateBoundRegionsCollector {
1354 fn visit_binder<T: TypeFoldable<'tcx>>(
1356 t: &Binder<'tcx, T>,
1357 ) -> ControlFlow<Self::BreakTy> {
1358 self.current_index.shift_in(1);
1359 let result = t.super_visit_with(self);
1360 self.current_index.shift_out(1);
1364 fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
1365 // if we are only looking for "constrained" region, we have to
1366 // ignore the inputs to a projection, as they may not appear
1367 // in the normalized form
1368 if self.just_constrained {
1369 if let ty::Projection(..) = t.kind() {
1370 return ControlFlow::CONTINUE;
1374 t.super_visit_with(self)
1377 fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
1378 // if we are only looking for "constrained" region, we have to
1379 // ignore the inputs of an unevaluated const, as they may not appear
1380 // in the normalized form
1381 if self.just_constrained {
1382 if let ty::ConstKind::Unevaluated(..) = c.kind() {
1383 return ControlFlow::CONTINUE;
1387 c.super_visit_with(self)
1390 fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
1391 if let ty::ReLateBound(debruijn, br) = *r {
1392 if debruijn == self.current_index {
1393 self.regions.insert(br.kind);
1396 ControlFlow::CONTINUE
1400 /// Finds the max universe present
1401 pub struct MaxUniverse {
1402 max_universe: ty::UniverseIndex,
1406 pub fn new() -> Self {
1407 MaxUniverse { max_universe: ty::UniverseIndex::ROOT }
1410 pub fn max_universe(self) -> ty::UniverseIndex {
1415 impl<'tcx> TypeVisitor<'tcx> for MaxUniverse {
1416 fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
1417 if let ty::Placeholder(placeholder) = t.kind() {
1418 self.max_universe = ty::UniverseIndex::from_u32(
1419 self.max_universe.as_u32().max(placeholder.universe.as_u32()),
1423 t.super_visit_with(self)
1426 fn visit_const(&mut self, c: ty::consts::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
1427 if let ty::ConstKind::Placeholder(placeholder) = c.kind() {
1428 self.max_universe = ty::UniverseIndex::from_u32(
1429 self.max_universe.as_u32().max(placeholder.universe.as_u32()),
1433 c.super_visit_with(self)
1436 fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
1437 if let ty::RePlaceholder(placeholder) = *r {
1438 self.max_universe = ty::UniverseIndex::from_u32(
1439 self.max_universe.as_u32().max(placeholder.universe.as_u32()),
1443 ControlFlow::CONTINUE