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,
208 crate::middle::region::Scope,
210 ::rustc_ast::InlineAsmOptions,
211 ::rustc_ast::InlineAsmTemplatePiece,
213 ::rustc_span::symbol::Symbol,
214 ::rustc_hir::def::Res,
215 ::rustc_hir::def_id::DefId,
216 ::rustc_hir::def_id::LocalDefId,
218 ::rustc_hir::MatchSource,
219 ::rustc_hir::Mutability,
220 ::rustc_hir::Unsafety,
221 ::rustc_target::asm::InlineAsmRegOrRegClass,
222 ::rustc_target::spec::abi::Abi,
223 crate::mir::coverage::ExpressionOperandId,
224 crate::mir::coverage::CounterValueReference,
225 crate::mir::coverage::InjectedExpressionId,
226 crate::mir::coverage::InjectedExpressionIndex,
227 crate::mir::coverage::MappedExpressionIndex,
229 crate::mir::Promoted,
230 crate::traits::Reveal,
231 crate::ty::adjustment::AutoBorrowMutability,
233 crate::ty::BoundConstness,
234 // Including `BoundRegionKind` is a *bit* dubious, but direct
235 // references to bound region appear in `ty::Error`, and aren't
236 // really meant to be folded. In general, we can only fold a fully
238 crate::ty::BoundRegionKind,
239 crate::ty::AssocItem,
240 crate::ty::AssocKind,
241 crate::ty::AliasKind,
242 crate::ty::Placeholder<crate::ty::BoundRegionKind>,
243 crate::ty::ClosureKind,
244 crate::ty::FreeRegion,
246 crate::ty::IntVarValue,
247 crate::ty::ParamConst,
249 crate::ty::adjustment::PointerCast,
250 crate::ty::RegionVid,
251 crate::ty::UniverseIndex,
254 ::rustc_errors::ErrorGuaranteed,
257 rustc_target::abi::Size,
258 rustc_type_ir::DebruijnIndex,
260 ty::Placeholder<ty::BoundVar>,
263 TrivialTypeTraversalAndLiftImpls! {
269 ///////////////////////////////////////////////////////////////////////////
270 // Lift implementations
272 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>> Lift<'tcx> for (A, B) {
273 type Lifted = (A::Lifted, B::Lifted);
274 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
275 Some((tcx.lift(self.0)?, tcx.lift(self.1)?))
279 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>, C: Lift<'tcx>> Lift<'tcx> for (A, B, C) {
280 type Lifted = (A::Lifted, B::Lifted, C::Lifted);
281 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
282 Some((tcx.lift(self.0)?, tcx.lift(self.1)?, tcx.lift(self.2)?))
286 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Option<T> {
287 type Lifted = Option<T::Lifted>;
288 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
290 Some(x) => Some(tcx.lift(x)?),
296 impl<'tcx, T: Lift<'tcx>, E: Lift<'tcx>> Lift<'tcx> for Result<T, E> {
297 type Lifted = Result<T::Lifted, E::Lifted>;
298 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
300 Ok(x) => tcx.lift(x).map(Ok),
301 Err(e) => tcx.lift(e).map(Err),
306 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Box<T> {
307 type Lifted = Box<T::Lifted>;
308 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
309 Some(Box::new(tcx.lift(*self)?))
313 impl<'tcx, T: Lift<'tcx> + Clone> Lift<'tcx> for Rc<T> {
314 type Lifted = Rc<T::Lifted>;
315 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
316 Some(Rc::new(tcx.lift(self.as_ref().clone())?))
320 impl<'tcx, T: Lift<'tcx> + Clone> Lift<'tcx> for Arc<T> {
321 type Lifted = Arc<T::Lifted>;
322 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
323 Some(Arc::new(tcx.lift(self.as_ref().clone())?))
326 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Vec<T> {
327 type Lifted = Vec<T::Lifted>;
328 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
329 self.into_iter().map(|v| tcx.lift(v)).collect()
333 impl<'tcx, I: Idx, T: Lift<'tcx>> Lift<'tcx> for IndexVec<I, T> {
334 type Lifted = IndexVec<I, T::Lifted>;
335 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
336 self.into_iter().map(|e| tcx.lift(e)).collect()
340 impl<'a, 'tcx> Lift<'tcx> for Term<'a> {
341 type Lifted = ty::Term<'tcx>;
342 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
344 match self.unpack() {
345 TermKind::Ty(ty) => TermKind::Ty(tcx.lift(ty)?),
346 TermKind::Const(c) => TermKind::Const(tcx.lift(c)?),
352 impl<'a, 'tcx> Lift<'tcx> for ty::ParamEnv<'a> {
353 type Lifted = ty::ParamEnv<'tcx>;
354 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
355 tcx.lift(self.caller_bounds())
356 .map(|caller_bounds| ty::ParamEnv::new(caller_bounds, self.reveal(), self.constness()))
360 ///////////////////////////////////////////////////////////////////////////
361 // TypeFoldable implementations.
363 /// AdtDefs are basically the same as a DefId.
364 impl<'tcx> TypeFoldable<'tcx> for ty::AdtDef<'tcx> {
365 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, _folder: &mut F) -> Result<Self, F::Error> {
370 impl<'tcx> TypeVisitable<'tcx> for ty::AdtDef<'tcx> {
371 fn visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
372 ControlFlow::Continue(())
376 impl<'tcx, T: TypeFoldable<'tcx>, U: TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
377 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
380 ) -> Result<(T, U), F::Error> {
381 Ok((self.0.try_fold_with(folder)?, self.1.try_fold_with(folder)?))
385 impl<'tcx, T: TypeVisitable<'tcx>, U: TypeVisitable<'tcx>> TypeVisitable<'tcx> for (T, U) {
386 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
387 self.0.visit_with(visitor)?;
388 self.1.visit_with(visitor)
392 impl<'tcx, A: TypeFoldable<'tcx>, B: TypeFoldable<'tcx>, C: TypeFoldable<'tcx>> TypeFoldable<'tcx>
395 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
398 ) -> Result<(A, B, C), F::Error> {
400 self.0.try_fold_with(folder)?,
401 self.1.try_fold_with(folder)?,
402 self.2.try_fold_with(folder)?,
407 impl<'tcx, A: TypeVisitable<'tcx>, B: TypeVisitable<'tcx>, C: TypeVisitable<'tcx>>
408 TypeVisitable<'tcx> for (A, B, C)
410 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
411 self.0.visit_with(visitor)?;
412 self.1.visit_with(visitor)?;
413 self.2.visit_with(visitor)
417 EnumTypeTraversalImpl! {
418 impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
421 } where T: TypeFoldable<'tcx>
423 EnumTypeTraversalImpl! {
424 impl<'tcx, T> TypeVisitable<'tcx> for Option<T> {
427 } where T: TypeVisitable<'tcx>
430 EnumTypeTraversalImpl! {
431 impl<'tcx, T, E> TypeFoldable<'tcx> for Result<T, E> {
434 } where T: TypeFoldable<'tcx>, E: TypeFoldable<'tcx>,
436 EnumTypeTraversalImpl! {
437 impl<'tcx, T, E> TypeVisitable<'tcx> for Result<T, E> {
440 } where T: TypeVisitable<'tcx>, E: TypeVisitable<'tcx>,
443 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
444 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
447 ) -> Result<Self, F::Error> {
448 // We merely want to replace the contained `T`, if at all possible,
449 // so that we don't needlessly allocate a new `Rc` or indeed clone
450 // the contained type.
452 // First step is to ensure that we have a unique reference to
453 // the contained type, which `Rc::make_mut` will accomplish (by
454 // allocating a new `Rc` and cloning the `T` only if required).
455 // This is done *before* casting to `Rc<ManuallyDrop<T>>` so that
456 // panicking during `make_mut` does not leak the `T`.
457 Rc::make_mut(&mut self);
459 // Casting to `Rc<ManuallyDrop<T>>` is safe because `ManuallyDrop`
460 // is `repr(transparent)`.
461 let ptr = Rc::into_raw(self).cast::<ManuallyDrop<T>>();
462 let mut unique = Rc::from_raw(ptr);
464 // Call to `Rc::make_mut` above guarantees that `unique` is the
465 // sole reference to the contained value, so we can avoid doing
466 // a checked `get_mut` here.
467 let slot = Rc::get_mut_unchecked(&mut unique);
469 // Semantically move the contained type out from `unique`, fold
470 // it, then move the folded value back into `unique`. Should
471 // folding fail, `ManuallyDrop` ensures that the "moved-out"
472 // value is not re-dropped.
473 let owned = ManuallyDrop::take(slot);
474 let folded = owned.try_fold_with(folder)?;
475 *slot = ManuallyDrop::new(folded);
477 // Cast back to `Rc<T>`.
478 Ok(Rc::from_raw(Rc::into_raw(unique).cast()))
483 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Rc<T> {
484 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
485 (**self).visit_with(visitor)
489 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Arc<T> {
490 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
493 ) -> Result<Self, F::Error> {
494 // We merely want to replace the contained `T`, if at all possible,
495 // so that we don't needlessly allocate a new `Arc` or indeed clone
496 // the contained type.
498 // First step is to ensure that we have a unique reference to
499 // the contained type, which `Arc::make_mut` will accomplish (by
500 // allocating a new `Arc` and cloning the `T` only if required).
501 // This is done *before* casting to `Arc<ManuallyDrop<T>>` so that
502 // panicking during `make_mut` does not leak the `T`.
503 Arc::make_mut(&mut self);
505 // Casting to `Arc<ManuallyDrop<T>>` is safe because `ManuallyDrop`
506 // is `repr(transparent)`.
507 let ptr = Arc::into_raw(self).cast::<ManuallyDrop<T>>();
508 let mut unique = Arc::from_raw(ptr);
510 // Call to `Arc::make_mut` above guarantees that `unique` is the
511 // sole reference to the contained value, so we can avoid doing
512 // a checked `get_mut` here.
513 let slot = Arc::get_mut_unchecked(&mut unique);
515 // Semantically move the contained type out from `unique`, fold
516 // it, then move the folded value back into `unique`. Should
517 // folding fail, `ManuallyDrop` ensures that the "moved-out"
518 // value is not re-dropped.
519 let owned = ManuallyDrop::take(slot);
520 let folded = owned.try_fold_with(folder)?;
521 *slot = ManuallyDrop::new(folded);
523 // Cast back to `Arc<T>`.
524 Ok(Arc::from_raw(Arc::into_raw(unique).cast()))
529 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Arc<T> {
530 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
531 (**self).visit_with(visitor)
535 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
536 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
537 self.try_map_id(|value| value.try_fold_with(folder))
541 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Box<T> {
542 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
543 (**self).visit_with(visitor)
547 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
548 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
549 self.try_map_id(|t| t.try_fold_with(folder))
553 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Vec<T> {
554 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
555 self.iter().try_for_each(|t| t.visit_with(visitor))
559 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for &[T] {
560 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
561 self.iter().try_for_each(|t| t.visit_with(visitor))
565 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<[T]> {
566 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
567 self.try_map_id(|t| t.try_fold_with(folder))
571 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Box<[T]> {
572 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
573 self.iter().try_for_each(|t| t.visit_with(visitor))
577 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<'tcx, T> {
578 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
579 folder.try_fold_binder(self)
583 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for ty::Binder<'tcx, T> {
584 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
585 visitor.visit_binder(self)
589 impl<'tcx, T: TypeFoldable<'tcx>> TypeSuperFoldable<'tcx> for ty::Binder<'tcx, T> {
590 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
593 ) -> Result<Self, F::Error> {
594 self.try_map_bound(|ty| ty.try_fold_with(folder))
598 impl<'tcx, T: TypeVisitable<'tcx>> TypeSuperVisitable<'tcx> for ty::Binder<'tcx, T> {
599 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
600 self.as_ref().skip_binder().visit_with(visitor)
604 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>> {
605 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
606 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_poly_existential_predicates(v))
610 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Const<'tcx>> {
611 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
612 ty::util::fold_list(self, folder, |tcx, v| tcx.mk_const_list(v.iter()))
616 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind> {
617 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
618 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_projs(v))
622 impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
623 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
624 folder.try_fold_ty(self)
628 impl<'tcx> TypeVisitable<'tcx> for Ty<'tcx> {
629 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
630 visitor.visit_ty(*self)
634 impl<'tcx> TypeSuperFoldable<'tcx> for Ty<'tcx> {
635 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
638 ) -> Result<Self, F::Error> {
639 let kind = match *self.kind() {
640 ty::RawPtr(tm) => ty::RawPtr(tm.try_fold_with(folder)?),
641 ty::Array(typ, sz) => ty::Array(typ.try_fold_with(folder)?, sz.try_fold_with(folder)?),
642 ty::Slice(typ) => ty::Slice(typ.try_fold_with(folder)?),
643 ty::Adt(tid, substs) => ty::Adt(tid, substs.try_fold_with(folder)?),
644 ty::Dynamic(trait_ty, region, representation) => ty::Dynamic(
645 trait_ty.try_fold_with(folder)?,
646 region.try_fold_with(folder)?,
649 ty::Tuple(ts) => ty::Tuple(ts.try_fold_with(folder)?),
650 ty::FnDef(def_id, substs) => ty::FnDef(def_id, substs.try_fold_with(folder)?),
651 ty::FnPtr(f) => ty::FnPtr(f.try_fold_with(folder)?),
652 ty::Ref(r, ty, mutbl) => {
653 ty::Ref(r.try_fold_with(folder)?, ty.try_fold_with(folder)?, mutbl)
655 ty::Generator(did, substs, movability) => {
656 ty::Generator(did, substs.try_fold_with(folder)?, movability)
658 ty::GeneratorWitness(types) => ty::GeneratorWitness(types.try_fold_with(folder)?),
659 ty::GeneratorWitnessMIR(did, substs) => {
660 ty::GeneratorWitnessMIR(did, substs.try_fold_with(folder)?)
662 ty::Closure(did, substs) => ty::Closure(did, substs.try_fold_with(folder)?),
663 ty::Alias(kind, data) => ty::Alias(kind, data.try_fold_with(folder)?),
675 | ty::Placeholder(..)
677 | ty::Foreign(..) => return Ok(self),
680 Ok(if *self.kind() == kind { self } else { folder.tcx().mk_ty(kind) })
684 impl<'tcx> TypeSuperVisitable<'tcx> for Ty<'tcx> {
685 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
687 ty::RawPtr(ref tm) => tm.visit_with(visitor),
688 ty::Array(typ, sz) => {
689 typ.visit_with(visitor)?;
690 sz.visit_with(visitor)
692 ty::Slice(typ) => typ.visit_with(visitor),
693 ty::Adt(_, substs) => substs.visit_with(visitor),
694 ty::Dynamic(ref trait_ty, ref reg, _) => {
695 trait_ty.visit_with(visitor)?;
696 reg.visit_with(visitor)
698 ty::Tuple(ts) => ts.visit_with(visitor),
699 ty::FnDef(_, substs) => substs.visit_with(visitor),
700 ty::FnPtr(ref f) => f.visit_with(visitor),
701 ty::Ref(r, ty, _) => {
702 r.visit_with(visitor)?;
703 ty.visit_with(visitor)
705 ty::Generator(_did, ref substs, _) => substs.visit_with(visitor),
706 ty::GeneratorWitness(ref types) => types.visit_with(visitor),
707 ty::GeneratorWitnessMIR(_did, ref substs) => substs.visit_with(visitor),
708 ty::Closure(_did, ref substs) => substs.visit_with(visitor),
709 ty::Alias(_, ref data) => data.visit_with(visitor),
720 | ty::Placeholder(..)
723 | ty::Foreign(..) => ControlFlow::Continue(()),
728 impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> {
729 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
730 folder.try_fold_region(self)
734 impl<'tcx> TypeVisitable<'tcx> for ty::Region<'tcx> {
735 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
736 visitor.visit_region(*self)
740 impl<'tcx> TypeSuperFoldable<'tcx> for ty::Region<'tcx> {
741 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
744 ) -> Result<Self, F::Error> {
749 impl<'tcx> TypeSuperVisitable<'tcx> for ty::Region<'tcx> {
750 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
751 ControlFlow::Continue(())
755 impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
756 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
757 folder.try_fold_predicate(self)
761 impl<'tcx> TypeVisitable<'tcx> for ty::Predicate<'tcx> {
762 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
763 visitor.visit_predicate(*self)
767 fn has_vars_bound_at_or_above(&self, binder: ty::DebruijnIndex) -> bool {
768 self.outer_exclusive_binder() > binder
772 fn has_type_flags(&self, flags: ty::TypeFlags) -> bool {
773 self.flags().intersects(flags)
777 impl<'tcx> TypeSuperFoldable<'tcx> for ty::Predicate<'tcx> {
778 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
781 ) -> Result<Self, F::Error> {
782 let new = self.kind().try_fold_with(folder)?;
783 Ok(folder.tcx().reuse_or_mk_predicate(self, new))
787 impl<'tcx> TypeSuperVisitable<'tcx> for ty::Predicate<'tcx> {
788 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
789 self.kind().visit_with(visitor)
793 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
794 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
795 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_predicates(v))
799 impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
800 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
801 self.try_map_id(|x| x.try_fold_with(folder))
805 impl<'tcx, T: TypeVisitable<'tcx>, I: Idx> TypeVisitable<'tcx> for IndexVec<I, T> {
806 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
807 self.iter().try_for_each(|t| t.visit_with(visitor))
811 impl<'tcx> TypeFoldable<'tcx> for ty::Const<'tcx> {
812 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
813 folder.try_fold_const(self)
817 impl<'tcx> TypeVisitable<'tcx> for ty::Const<'tcx> {
818 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
819 visitor.visit_const(*self)
823 impl<'tcx> TypeSuperFoldable<'tcx> for ty::Const<'tcx> {
824 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
827 ) -> Result<Self, F::Error> {
828 let ty = self.ty().try_fold_with(folder)?;
829 let kind = self.kind().try_fold_with(folder)?;
830 if ty != self.ty() || kind != self.kind() {
831 Ok(folder.tcx().mk_const(kind, ty))
838 impl<'tcx> TypeSuperVisitable<'tcx> for ty::Const<'tcx> {
839 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
840 self.ty().visit_with(visitor)?;
841 self.kind().visit_with(visitor)
845 impl<'tcx> TypeFoldable<'tcx> for InferConst<'tcx> {
846 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, _folder: &mut F) -> Result<Self, F::Error> {
851 impl<'tcx> TypeVisitable<'tcx> for InferConst<'tcx> {
852 fn visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
853 ControlFlow::Continue(())
857 impl<'tcx> TypeSuperVisitable<'tcx> for ty::UnevaluatedConst<'tcx> {
858 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
859 self.substs.visit_with(visitor)
863 impl<'tcx> TypeVisitable<'tcx> for TyAndLayout<'tcx, Ty<'tcx>> {
864 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
865 visitor.visit_ty(self.ty)