1 //! This module contains implements of the `Lift` and `TypeFoldable`
2 //! traits for various types in the Rust compiler. Most are written by
3 //! hand, though we've recently added some macros and proc-macros to help with the tedium.
5 use crate::mir::interpret;
6 use crate::mir::{Field, ProjectionKind};
7 use crate::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeSuperFoldable};
8 use crate::ty::print::{with_no_trimmed_paths, FmtPrinter, Printer};
9 use crate::ty::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitor};
10 use crate::ty::{self, AliasTy, InferConst, Lift, Term, TermKind, Ty, TyCtxt};
11 use rustc_data_structures::functor::IdFunctor;
12 use rustc_hir::def::Namespace;
13 use rustc_index::vec::{Idx, IndexVec};
14 use rustc_target::abi::TyAndLayout;
17 use std::mem::ManuallyDrop;
18 use std::ops::ControlFlow;
22 impl fmt::Debug for ty::TraitDef {
23 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25 with_no_trimmed_paths!({
27 &FmtPrinter::new(tcx, Namespace::TypeNS)
28 .print_def_path(self.def_id, &[])?
36 impl<'tcx> fmt::Debug for ty::AdtDef<'tcx> {
37 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39 with_no_trimmed_paths!({
41 &FmtPrinter::new(tcx, Namespace::TypeNS)
42 .print_def_path(self.did(), &[])?
50 impl fmt::Debug for ty::UpvarId {
51 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52 let name = ty::tls::with(|tcx| tcx.hir().name(self.var_path.hir_id));
53 write!(f, "UpvarId({:?};`{}`;{:?})", self.var_path.hir_id, name, self.closure_expr_id)
57 impl<'tcx> fmt::Debug for ty::ExistentialTraitRef<'tcx> {
58 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59 with_no_trimmed_paths!(fmt::Display::fmt(self, f))
63 impl<'tcx> fmt::Debug for ty::adjustment::Adjustment<'tcx> {
64 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65 write!(f, "{:?} -> {}", self.kind, self.target)
69 impl fmt::Debug for ty::BoundRegionKind {
70 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72 ty::BrAnon(n, span) => write!(f, "BrAnon({n:?}, {span:?})"),
73 ty::BrNamed(did, name) => {
74 if did.is_crate_root() {
75 write!(f, "BrNamed({})", name)
77 write!(f, "BrNamed({:?}, {})", did, name)
80 ty::BrEnv => write!(f, "BrEnv"),
85 impl fmt::Debug for ty::FreeRegion {
86 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87 write!(f, "ReFree({:?}, {:?})", self.scope, self.bound_region)
91 impl<'tcx> fmt::Debug for ty::FnSig<'tcx> {
92 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93 write!(f, "({:?}; c_variadic: {})->{:?}", self.inputs(), self.c_variadic, self.output())
97 impl<'tcx> fmt::Debug for ty::ConstVid<'tcx> {
98 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99 write!(f, "_#{}c", self.index)
103 impl<'tcx> fmt::Debug for ty::TraitRef<'tcx> {
104 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
105 with_no_trimmed_paths!(fmt::Display::fmt(self, f))
109 impl<'tcx> fmt::Debug for Ty<'tcx> {
110 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
111 with_no_trimmed_paths!(fmt::Display::fmt(self, f))
115 impl fmt::Debug for ty::ParamTy {
116 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117 write!(f, "{}/#{}", self.name, self.index)
121 impl fmt::Debug for ty::ParamConst {
122 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
123 write!(f, "{}/#{}", self.name, self.index)
127 impl<'tcx> fmt::Debug for ty::TraitPredicate<'tcx> {
128 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
129 if let ty::BoundConstness::ConstIfConst = self.constness {
130 write!(f, "~const ")?;
132 write!(f, "TraitPredicate({:?}, polarity:{:?})", self.trait_ref, self.polarity)
136 impl<'tcx> fmt::Debug for ty::ProjectionPredicate<'tcx> {
137 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138 write!(f, "ProjectionPredicate({:?}, {:?})", self.projection_ty, self.term)
142 impl<'tcx> fmt::Debug for ty::Predicate<'tcx> {
143 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
144 write!(f, "{:?}", self.kind())
148 impl<'tcx> fmt::Debug for ty::Clause<'tcx> {
149 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
151 ty::Clause::Trait(ref a) => a.fmt(f),
152 ty::Clause::RegionOutlives(ref pair) => pair.fmt(f),
153 ty::Clause::TypeOutlives(ref pair) => pair.fmt(f),
154 ty::Clause::Projection(ref pair) => pair.fmt(f),
159 impl<'tcx> fmt::Debug for ty::PredicateKind<'tcx> {
160 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
162 ty::PredicateKind::Clause(ref a) => a.fmt(f),
163 ty::PredicateKind::Subtype(ref pair) => pair.fmt(f),
164 ty::PredicateKind::Coerce(ref pair) => pair.fmt(f),
165 ty::PredicateKind::WellFormed(data) => write!(f, "WellFormed({:?})", data),
166 ty::PredicateKind::ObjectSafe(trait_def_id) => {
167 write!(f, "ObjectSafe({:?})", trait_def_id)
169 ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => {
170 write!(f, "ClosureKind({:?}, {:?}, {:?})", closure_def_id, closure_substs, kind)
172 ty::PredicateKind::ConstEvaluatable(ct) => {
173 write!(f, "ConstEvaluatable({ct:?})")
175 ty::PredicateKind::ConstEquate(c1, c2) => write!(f, "ConstEquate({:?}, {:?})", c1, c2),
176 ty::PredicateKind::TypeWellFormedFromEnv(ty) => {
177 write!(f, "TypeWellFormedFromEnv({:?})", ty)
179 ty::PredicateKind::Ambiguous => write!(f, "Ambiguous"),
184 impl<'tcx> fmt::Debug for AliasTy<'tcx> {
185 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
186 f.debug_struct("AliasTy")
187 .field("substs", &self.substs)
188 .field("def_id", &self.def_id)
193 ///////////////////////////////////////////////////////////////////////////
196 // For things that don't carry any arena-allocated data (and are
197 // copy...), just add them to this list.
199 TrivialTypeTraversalAndLiftImpls! {
203 ::rustc_target::abi::VariantIdx,
207 crate::middle::region::Scope,
209 ::rustc_ast::InlineAsmOptions,
210 ::rustc_ast::InlineAsmTemplatePiece,
212 ::rustc_span::symbol::Symbol,
213 ::rustc_hir::def::Res,
214 ::rustc_hir::def_id::DefId,
215 ::rustc_hir::def_id::LocalDefId,
217 ::rustc_hir::MatchSource,
218 ::rustc_hir::Mutability,
219 ::rustc_hir::Unsafety,
220 ::rustc_target::asm::InlineAsmRegOrRegClass,
221 ::rustc_target::spec::abi::Abi,
222 crate::mir::coverage::ExpressionOperandId,
223 crate::mir::coverage::CounterValueReference,
224 crate::mir::coverage::InjectedExpressionId,
225 crate::mir::coverage::InjectedExpressionIndex,
226 crate::mir::coverage::MappedExpressionIndex,
228 crate::mir::Promoted,
229 crate::traits::Reveal,
230 crate::ty::adjustment::AutoBorrowMutability,
232 crate::ty::BoundConstness,
233 // Including `BoundRegionKind` is a *bit* dubious, but direct
234 // references to bound region appear in `ty::Error`, and aren't
235 // really meant to be folded. In general, we can only fold a fully
237 crate::ty::BoundRegionKind,
238 crate::ty::AssocItem,
239 crate::ty::AssocKind,
240 crate::ty::AliasKind,
241 crate::ty::Placeholder<crate::ty::BoundRegionKind>,
242 crate::ty::ClosureKind,
243 crate::ty::FreeRegion,
245 crate::ty::IntVarValue,
246 crate::ty::ParamConst,
248 crate::ty::adjustment::PointerCast,
249 crate::ty::RegionVid,
250 crate::ty::UniverseIndex,
253 ::rustc_errors::ErrorGuaranteed,
256 rustc_target::abi::Size,
257 rustc_type_ir::DebruijnIndex,
259 ty::Placeholder<ty::BoundVar>,
262 TrivialTypeTraversalAndLiftImpls! {
268 ///////////////////////////////////////////////////////////////////////////
269 // Lift implementations
271 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>> Lift<'tcx> for (A, B) {
272 type Lifted = (A::Lifted, B::Lifted);
273 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
274 Some((tcx.lift(self.0)?, tcx.lift(self.1)?))
278 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>, C: Lift<'tcx>> Lift<'tcx> for (A, B, C) {
279 type Lifted = (A::Lifted, B::Lifted, C::Lifted);
280 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
281 Some((tcx.lift(self.0)?, tcx.lift(self.1)?, tcx.lift(self.2)?))
285 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Option<T> {
286 type Lifted = Option<T::Lifted>;
287 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
289 Some(x) => Some(tcx.lift(x)?),
295 impl<'tcx, T: Lift<'tcx>, E: Lift<'tcx>> Lift<'tcx> for Result<T, E> {
296 type Lifted = Result<T::Lifted, E::Lifted>;
297 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
299 Ok(x) => tcx.lift(x).map(Ok),
300 Err(e) => tcx.lift(e).map(Err),
305 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Box<T> {
306 type Lifted = Box<T::Lifted>;
307 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
308 Some(Box::new(tcx.lift(*self)?))
312 impl<'tcx, T: Lift<'tcx> + Clone> Lift<'tcx> for Rc<T> {
313 type Lifted = Rc<T::Lifted>;
314 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
315 Some(Rc::new(tcx.lift(self.as_ref().clone())?))
319 impl<'tcx, T: Lift<'tcx> + Clone> Lift<'tcx> for Arc<T> {
320 type Lifted = Arc<T::Lifted>;
321 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
322 Some(Arc::new(tcx.lift(self.as_ref().clone())?))
325 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Vec<T> {
326 type Lifted = Vec<T::Lifted>;
327 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
328 self.into_iter().map(|v| tcx.lift(v)).collect()
332 impl<'tcx, I: Idx, T: Lift<'tcx>> Lift<'tcx> for IndexVec<I, T> {
333 type Lifted = IndexVec<I, T::Lifted>;
334 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
335 self.into_iter().map(|e| tcx.lift(e)).collect()
339 impl<'a, 'tcx> Lift<'tcx> for Term<'a> {
340 type Lifted = ty::Term<'tcx>;
341 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
343 match self.unpack() {
344 TermKind::Ty(ty) => TermKind::Ty(tcx.lift(ty)?),
345 TermKind::Const(c) => TermKind::Const(tcx.lift(c)?),
351 impl<'a, 'tcx> Lift<'tcx> for ty::ParamEnv<'a> {
352 type Lifted = ty::ParamEnv<'tcx>;
353 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
354 tcx.lift(self.caller_bounds())
355 .map(|caller_bounds| ty::ParamEnv::new(caller_bounds, self.reveal(), self.constness()))
359 ///////////////////////////////////////////////////////////////////////////
360 // TypeFoldable implementations.
362 /// AdtDefs are basically the same as a DefId.
363 impl<'tcx> TypeFoldable<'tcx> for ty::AdtDef<'tcx> {
364 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, _folder: &mut F) -> Result<Self, F::Error> {
369 impl<'tcx> TypeVisitable<'tcx> for ty::AdtDef<'tcx> {
370 fn visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
371 ControlFlow::Continue(())
375 impl<'tcx, T: TypeFoldable<'tcx>, U: TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
376 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
379 ) -> Result<(T, U), F::Error> {
380 Ok((self.0.try_fold_with(folder)?, self.1.try_fold_with(folder)?))
384 impl<'tcx, T: TypeVisitable<'tcx>, U: TypeVisitable<'tcx>> TypeVisitable<'tcx> for (T, U) {
385 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
386 self.0.visit_with(visitor)?;
387 self.1.visit_with(visitor)
391 impl<'tcx, A: TypeFoldable<'tcx>, B: TypeFoldable<'tcx>, C: TypeFoldable<'tcx>> TypeFoldable<'tcx>
394 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
397 ) -> Result<(A, B, C), F::Error> {
399 self.0.try_fold_with(folder)?,
400 self.1.try_fold_with(folder)?,
401 self.2.try_fold_with(folder)?,
406 impl<'tcx, A: TypeVisitable<'tcx>, B: TypeVisitable<'tcx>, C: TypeVisitable<'tcx>>
407 TypeVisitable<'tcx> for (A, B, C)
409 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
410 self.0.visit_with(visitor)?;
411 self.1.visit_with(visitor)?;
412 self.2.visit_with(visitor)
416 EnumTypeTraversalImpl! {
417 impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
420 } where T: TypeFoldable<'tcx>
422 EnumTypeTraversalImpl! {
423 impl<'tcx, T> TypeVisitable<'tcx> for Option<T> {
426 } where T: TypeVisitable<'tcx>
429 EnumTypeTraversalImpl! {
430 impl<'tcx, T, E> TypeFoldable<'tcx> for Result<T, E> {
433 } where T: TypeFoldable<'tcx>, E: TypeFoldable<'tcx>,
435 EnumTypeTraversalImpl! {
436 impl<'tcx, T, E> TypeVisitable<'tcx> for Result<T, E> {
439 } where T: TypeVisitable<'tcx>, E: TypeVisitable<'tcx>,
442 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
443 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
446 ) -> Result<Self, F::Error> {
447 // We merely want to replace the contained `T`, if at all possible,
448 // so that we don't needlessly allocate a new `Rc` or indeed clone
449 // the contained type.
451 // First step is to ensure that we have a unique reference to
452 // the contained type, which `Rc::make_mut` will accomplish (by
453 // allocating a new `Rc` and cloning the `T` only if required).
454 // This is done *before* casting to `Rc<ManuallyDrop<T>>` so that
455 // panicking during `make_mut` does not leak the `T`.
456 Rc::make_mut(&mut self);
458 // Casting to `Rc<ManuallyDrop<T>>` is safe because `ManuallyDrop`
459 // is `repr(transparent)`.
460 let ptr = Rc::into_raw(self).cast::<ManuallyDrop<T>>();
461 let mut unique = Rc::from_raw(ptr);
463 // Call to `Rc::make_mut` above guarantees that `unique` is the
464 // sole reference to the contained value, so we can avoid doing
465 // a checked `get_mut` here.
466 let slot = Rc::get_mut_unchecked(&mut unique);
468 // Semantically move the contained type out from `unique`, fold
469 // it, then move the folded value back into `unique`. Should
470 // folding fail, `ManuallyDrop` ensures that the "moved-out"
471 // value is not re-dropped.
472 let owned = ManuallyDrop::take(slot);
473 let folded = owned.try_fold_with(folder)?;
474 *slot = ManuallyDrop::new(folded);
476 // Cast back to `Rc<T>`.
477 Ok(Rc::from_raw(Rc::into_raw(unique).cast()))
482 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Rc<T> {
483 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
484 (**self).visit_with(visitor)
488 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Arc<T> {
489 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
492 ) -> Result<Self, F::Error> {
493 // We merely want to replace the contained `T`, if at all possible,
494 // so that we don't needlessly allocate a new `Arc` or indeed clone
495 // the contained type.
497 // First step is to ensure that we have a unique reference to
498 // the contained type, which `Arc::make_mut` will accomplish (by
499 // allocating a new `Arc` and cloning the `T` only if required).
500 // This is done *before* casting to `Arc<ManuallyDrop<T>>` so that
501 // panicking during `make_mut` does not leak the `T`.
502 Arc::make_mut(&mut self);
504 // Casting to `Arc<ManuallyDrop<T>>` is safe because `ManuallyDrop`
505 // is `repr(transparent)`.
506 let ptr = Arc::into_raw(self).cast::<ManuallyDrop<T>>();
507 let mut unique = Arc::from_raw(ptr);
509 // Call to `Arc::make_mut` above guarantees that `unique` is the
510 // sole reference to the contained value, so we can avoid doing
511 // a checked `get_mut` here.
512 let slot = Arc::get_mut_unchecked(&mut unique);
514 // Semantically move the contained type out from `unique`, fold
515 // it, then move the folded value back into `unique`. Should
516 // folding fail, `ManuallyDrop` ensures that the "moved-out"
517 // value is not re-dropped.
518 let owned = ManuallyDrop::take(slot);
519 let folded = owned.try_fold_with(folder)?;
520 *slot = ManuallyDrop::new(folded);
522 // Cast back to `Arc<T>`.
523 Ok(Arc::from_raw(Arc::into_raw(unique).cast()))
528 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Arc<T> {
529 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
530 (**self).visit_with(visitor)
534 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
535 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
536 self.try_map_id(|value| value.try_fold_with(folder))
540 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Box<T> {
541 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
542 (**self).visit_with(visitor)
546 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
547 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
548 self.try_map_id(|t| t.try_fold_with(folder))
552 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Vec<T> {
553 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
554 self.iter().try_for_each(|t| t.visit_with(visitor))
558 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for &[T] {
559 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
560 self.iter().try_for_each(|t| t.visit_with(visitor))
564 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<[T]> {
565 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
566 self.try_map_id(|t| t.try_fold_with(folder))
570 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Box<[T]> {
571 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
572 self.iter().try_for_each(|t| t.visit_with(visitor))
576 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<'tcx, T> {
577 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
578 folder.try_fold_binder(self)
582 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for ty::Binder<'tcx, T> {
583 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
584 visitor.visit_binder(self)
588 impl<'tcx, T: TypeFoldable<'tcx>> TypeSuperFoldable<'tcx> for ty::Binder<'tcx, T> {
589 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
592 ) -> Result<Self, F::Error> {
593 self.try_map_bound(|ty| ty.try_fold_with(folder))
597 impl<'tcx, T: TypeVisitable<'tcx>> TypeSuperVisitable<'tcx> for ty::Binder<'tcx, T> {
598 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
599 self.as_ref().skip_binder().visit_with(visitor)
603 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>> {
604 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
605 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_poly_existential_predicates(v))
609 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Const<'tcx>> {
610 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
611 ty::util::fold_list(self, folder, |tcx, v| tcx.mk_const_list(v.iter()))
615 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind> {
616 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
617 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_projs(v))
621 impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
622 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
623 folder.try_fold_ty(self)
627 impl<'tcx> TypeVisitable<'tcx> for Ty<'tcx> {
628 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
629 visitor.visit_ty(*self)
633 impl<'tcx> TypeSuperFoldable<'tcx> for Ty<'tcx> {
634 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
637 ) -> Result<Self, F::Error> {
638 let kind = match *self.kind() {
639 ty::RawPtr(tm) => ty::RawPtr(tm.try_fold_with(folder)?),
640 ty::Array(typ, sz) => ty::Array(typ.try_fold_with(folder)?, sz.try_fold_with(folder)?),
641 ty::Slice(typ) => ty::Slice(typ.try_fold_with(folder)?),
642 ty::Adt(tid, substs) => ty::Adt(tid, substs.try_fold_with(folder)?),
643 ty::Dynamic(trait_ty, region, representation) => ty::Dynamic(
644 trait_ty.try_fold_with(folder)?,
645 region.try_fold_with(folder)?,
648 ty::Tuple(ts) => ty::Tuple(ts.try_fold_with(folder)?),
649 ty::FnDef(def_id, substs) => ty::FnDef(def_id, substs.try_fold_with(folder)?),
650 ty::FnPtr(f) => ty::FnPtr(f.try_fold_with(folder)?),
651 ty::Ref(r, ty, mutbl) => {
652 ty::Ref(r.try_fold_with(folder)?, ty.try_fold_with(folder)?, mutbl)
654 ty::Generator(did, substs, movability) => {
655 ty::Generator(did, substs.try_fold_with(folder)?, movability)
657 ty::GeneratorWitness(types) => ty::GeneratorWitness(types.try_fold_with(folder)?),
658 ty::Closure(did, substs) => ty::Closure(did, substs.try_fold_with(folder)?),
659 ty::Alias(kind, data) => ty::Alias(kind, data.try_fold_with(folder)?),
671 | ty::Placeholder(..)
673 | ty::Foreign(..) => return Ok(self),
676 Ok(if *self.kind() == kind { self } else { folder.tcx().mk_ty(kind) })
680 impl<'tcx> TypeSuperVisitable<'tcx> for Ty<'tcx> {
681 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
683 ty::RawPtr(ref tm) => tm.visit_with(visitor),
684 ty::Array(typ, sz) => {
685 typ.visit_with(visitor)?;
686 sz.visit_with(visitor)
688 ty::Slice(typ) => typ.visit_with(visitor),
689 ty::Adt(_, substs) => substs.visit_with(visitor),
690 ty::Dynamic(ref trait_ty, ref reg, _) => {
691 trait_ty.visit_with(visitor)?;
692 reg.visit_with(visitor)
694 ty::Tuple(ts) => ts.visit_with(visitor),
695 ty::FnDef(_, substs) => substs.visit_with(visitor),
696 ty::FnPtr(ref f) => f.visit_with(visitor),
697 ty::Ref(r, ty, _) => {
698 r.visit_with(visitor)?;
699 ty.visit_with(visitor)
701 ty::Generator(_did, ref substs, _) => substs.visit_with(visitor),
702 ty::GeneratorWitness(ref types) => types.visit_with(visitor),
703 ty::Closure(_did, ref substs) => substs.visit_with(visitor),
704 ty::Alias(_, ref data) => data.visit_with(visitor),
715 | ty::Placeholder(..)
718 | ty::Foreign(..) => ControlFlow::Continue(()),
723 impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> {
724 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
725 folder.try_fold_region(self)
729 impl<'tcx> TypeVisitable<'tcx> for ty::Region<'tcx> {
730 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
731 visitor.visit_region(*self)
735 impl<'tcx> TypeSuperFoldable<'tcx> for ty::Region<'tcx> {
736 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
739 ) -> Result<Self, F::Error> {
744 impl<'tcx> TypeSuperVisitable<'tcx> for ty::Region<'tcx> {
745 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
746 ControlFlow::Continue(())
750 impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
751 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
752 folder.try_fold_predicate(self)
756 impl<'tcx> TypeVisitable<'tcx> for ty::Predicate<'tcx> {
757 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
758 visitor.visit_predicate(*self)
762 fn has_vars_bound_at_or_above(&self, binder: ty::DebruijnIndex) -> bool {
763 self.outer_exclusive_binder() > binder
767 fn has_type_flags(&self, flags: ty::TypeFlags) -> bool {
768 self.flags().intersects(flags)
772 impl<'tcx> TypeSuperFoldable<'tcx> for ty::Predicate<'tcx> {
773 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
776 ) -> Result<Self, F::Error> {
777 let new = self.kind().try_fold_with(folder)?;
778 Ok(folder.tcx().reuse_or_mk_predicate(self, new))
782 impl<'tcx> TypeSuperVisitable<'tcx> for ty::Predicate<'tcx> {
783 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
784 self.kind().visit_with(visitor)
788 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
789 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
790 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_predicates(v))
794 impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
795 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
796 self.try_map_id(|x| x.try_fold_with(folder))
800 impl<'tcx, T: TypeVisitable<'tcx>, I: Idx> TypeVisitable<'tcx> for IndexVec<I, T> {
801 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
802 self.iter().try_for_each(|t| t.visit_with(visitor))
806 impl<'tcx> TypeFoldable<'tcx> for ty::Const<'tcx> {
807 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
808 folder.try_fold_const(self)
812 impl<'tcx> TypeVisitable<'tcx> for ty::Const<'tcx> {
813 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
814 visitor.visit_const(*self)
818 impl<'tcx> TypeSuperFoldable<'tcx> for ty::Const<'tcx> {
819 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
822 ) -> Result<Self, F::Error> {
823 let ty = self.ty().try_fold_with(folder)?;
824 let kind = self.kind().try_fold_with(folder)?;
825 if ty != self.ty() || kind != self.kind() {
826 Ok(folder.tcx().mk_const(kind, ty))
833 impl<'tcx> TypeSuperVisitable<'tcx> for ty::Const<'tcx> {
834 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
835 self.ty().visit_with(visitor)?;
836 self.kind().visit_with(visitor)
840 impl<'tcx> TypeFoldable<'tcx> for InferConst<'tcx> {
841 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, _folder: &mut F) -> Result<Self, F::Error> {
846 impl<'tcx> TypeVisitable<'tcx> for InferConst<'tcx> {
847 fn visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
848 ControlFlow::Continue(())
852 impl<'tcx> TypeSuperVisitable<'tcx> for ty::UnevaluatedConst<'tcx> {
853 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
854 self.substs.visit_with(visitor)
858 impl<'tcx> TypeVisitable<'tcx> for TyAndLayout<'tcx, Ty<'tcx>> {
859 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
860 visitor.visit_ty(self.ty)