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, 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};
16 use std::mem::ManuallyDrop;
17 use std::ops::ControlFlow;
21 impl fmt::Debug for ty::TraitDef {
22 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24 with_no_trimmed_paths!({
26 &FmtPrinter::new(tcx, Namespace::TypeNS)
27 .print_def_path(self.def_id, &[])?
35 impl<'tcx> fmt::Debug for ty::AdtDef<'tcx> {
36 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38 with_no_trimmed_paths!({
40 &FmtPrinter::new(tcx, Namespace::TypeNS)
41 .print_def_path(self.did(), &[])?
49 impl fmt::Debug for ty::UpvarId {
50 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51 let name = ty::tls::with(|tcx| tcx.hir().name(self.var_path.hir_id));
52 write!(f, "UpvarId({:?};`{}`;{:?})", self.var_path.hir_id, name, self.closure_expr_id)
56 impl<'tcx> fmt::Debug for ty::ExistentialTraitRef<'tcx> {
57 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58 with_no_trimmed_paths!(fmt::Display::fmt(self, f))
62 impl<'tcx> fmt::Debug for ty::adjustment::Adjustment<'tcx> {
63 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64 write!(f, "{:?} -> {}", self.kind, self.target)
68 impl fmt::Debug for ty::BoundRegionKind {
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71 ty::BrAnon(n, span) => write!(f, "BrAnon({n:?}, {span:?})"),
72 ty::BrNamed(did, name) => {
73 if did.is_crate_root() {
74 write!(f, "BrNamed({})", name)
76 write!(f, "BrNamed({:?}, {})", did, name)
79 ty::BrEnv => write!(f, "BrEnv"),
84 impl fmt::Debug for ty::FreeRegion {
85 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86 write!(f, "ReFree({:?}, {:?})", self.scope, self.bound_region)
90 impl<'tcx> fmt::Debug for ty::FnSig<'tcx> {
91 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92 write!(f, "({:?}; c_variadic: {})->{:?}", self.inputs(), self.c_variadic, self.output())
96 impl<'tcx> fmt::Debug for ty::ConstVid<'tcx> {
97 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98 write!(f, "_#{}c", self.index)
102 impl<'tcx> fmt::Debug for ty::TraitRef<'tcx> {
103 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104 with_no_trimmed_paths!(fmt::Display::fmt(self, f))
108 impl<'tcx> fmt::Debug for Ty<'tcx> {
109 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110 with_no_trimmed_paths!(fmt::Display::fmt(self, f))
114 impl fmt::Debug for ty::ParamTy {
115 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
116 write!(f, "{}/#{}", self.name, self.index)
120 impl fmt::Debug for ty::ParamConst {
121 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
122 write!(f, "{}/#{}", self.name, self.index)
126 impl<'tcx> fmt::Debug for ty::TraitPredicate<'tcx> {
127 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
128 if let ty::BoundConstness::ConstIfConst = self.constness {
129 write!(f, "~const ")?;
131 write!(f, "TraitPredicate({:?}, polarity:{:?})", self.trait_ref, self.polarity)
135 impl<'tcx> fmt::Debug for ty::ProjectionPredicate<'tcx> {
136 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
137 write!(f, "ProjectionPredicate({:?}, {:?})", self.projection_ty, self.term)
141 impl<'tcx> fmt::Debug for ty::Predicate<'tcx> {
142 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
143 write!(f, "{:?}", self.kind())
147 impl<'tcx> fmt::Debug for ty::Clause<'tcx> {
148 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
150 ty::Clause::Trait(ref a) => a.fmt(f),
151 ty::Clause::RegionOutlives(ref pair) => pair.fmt(f),
152 ty::Clause::TypeOutlives(ref pair) => pair.fmt(f),
153 ty::Clause::Projection(ref pair) => pair.fmt(f),
158 impl<'tcx> fmt::Debug for ty::PredicateKind<'tcx> {
159 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
161 ty::PredicateKind::Clause(ref a) => a.fmt(f),
162 ty::PredicateKind::Subtype(ref pair) => pair.fmt(f),
163 ty::PredicateKind::Coerce(ref pair) => pair.fmt(f),
164 ty::PredicateKind::WellFormed(data) => write!(f, "WellFormed({:?})", data),
165 ty::PredicateKind::ObjectSafe(trait_def_id) => {
166 write!(f, "ObjectSafe({:?})", trait_def_id)
168 ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => {
169 write!(f, "ClosureKind({:?}, {:?}, {:?})", closure_def_id, closure_substs, kind)
171 ty::PredicateKind::ConstEvaluatable(ct) => {
172 write!(f, "ConstEvaluatable({ct:?})")
174 ty::PredicateKind::ConstEquate(c1, c2) => write!(f, "ConstEquate({:?}, {:?})", c1, c2),
175 ty::PredicateKind::TypeWellFormedFromEnv(ty) => {
176 write!(f, "TypeWellFormedFromEnv({:?})", ty)
178 ty::PredicateKind::Ambiguous => write!(f, "Ambiguous"),
183 ///////////////////////////////////////////////////////////////////////////
186 // For things that don't carry any arena-allocated data (and are
187 // copy...), just add them to this list.
189 TrivialTypeTraversalAndLiftImpls! {
193 ::rustc_target::abi::VariantIdx,
197 crate::middle::region::Scope,
199 ::rustc_ast::InlineAsmOptions,
200 ::rustc_ast::InlineAsmTemplatePiece,
202 ::rustc_span::symbol::Symbol,
203 ::rustc_hir::def::Res,
204 ::rustc_hir::def_id::DefId,
205 ::rustc_hir::def_id::LocalDefId,
207 ::rustc_hir::MatchSource,
208 ::rustc_hir::Mutability,
209 ::rustc_hir::Unsafety,
210 ::rustc_target::asm::InlineAsmRegOrRegClass,
211 ::rustc_target::spec::abi::Abi,
212 crate::mir::coverage::ExpressionOperandId,
213 crate::mir::coverage::CounterValueReference,
214 crate::mir::coverage::InjectedExpressionId,
215 crate::mir::coverage::InjectedExpressionIndex,
216 crate::mir::coverage::MappedExpressionIndex,
218 crate::mir::Promoted,
219 crate::traits::Reveal,
220 crate::ty::adjustment::AutoBorrowMutability,
222 crate::ty::BoundConstness,
223 // Including `BoundRegionKind` is a *bit* dubious, but direct
224 // references to bound region appear in `ty::Error`, and aren't
225 // really meant to be folded. In general, we can only fold a fully
227 crate::ty::BoundRegionKind,
228 crate::ty::AssocItem,
229 crate::ty::AssocKind,
230 crate::ty::Placeholder<crate::ty::BoundRegionKind>,
231 crate::ty::ClosureKind,
232 crate::ty::FreeRegion,
234 crate::ty::IntVarValue,
235 crate::ty::ParamConst,
237 crate::ty::adjustment::PointerCast,
238 crate::ty::RegionVid,
239 crate::ty::UniverseIndex,
242 ::rustc_errors::ErrorGuaranteed,
245 rustc_target::abi::Size,
246 rustc_type_ir::DebruijnIndex,
248 ty::Placeholder<ty::BoundVar>,
251 TrivialTypeTraversalAndLiftImpls! {
257 ///////////////////////////////////////////////////////////////////////////
258 // Lift implementations
260 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>> Lift<'tcx> for (A, B) {
261 type Lifted = (A::Lifted, B::Lifted);
262 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
263 Some((tcx.lift(self.0)?, tcx.lift(self.1)?))
267 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>, C: Lift<'tcx>> Lift<'tcx> for (A, B, C) {
268 type Lifted = (A::Lifted, B::Lifted, C::Lifted);
269 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
270 Some((tcx.lift(self.0)?, tcx.lift(self.1)?, tcx.lift(self.2)?))
274 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Option<T> {
275 type Lifted = Option<T::Lifted>;
276 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
278 Some(x) => Some(tcx.lift(x)?),
284 impl<'tcx, T: Lift<'tcx>, E: Lift<'tcx>> Lift<'tcx> for Result<T, E> {
285 type Lifted = Result<T::Lifted, E::Lifted>;
286 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
288 Ok(x) => tcx.lift(x).map(Ok),
289 Err(e) => tcx.lift(e).map(Err),
294 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Box<T> {
295 type Lifted = Box<T::Lifted>;
296 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
297 Some(Box::new(tcx.lift(*self)?))
301 impl<'tcx, T: Lift<'tcx> + Clone> Lift<'tcx> for Rc<T> {
302 type Lifted = Rc<T::Lifted>;
303 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
304 Some(Rc::new(tcx.lift(self.as_ref().clone())?))
308 impl<'tcx, T: Lift<'tcx> + Clone> Lift<'tcx> for Arc<T> {
309 type Lifted = Arc<T::Lifted>;
310 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
311 Some(Arc::new(tcx.lift(self.as_ref().clone())?))
314 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Vec<T> {
315 type Lifted = Vec<T::Lifted>;
316 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
317 self.into_iter().map(|v| tcx.lift(v)).collect()
321 impl<'tcx, I: Idx, T: Lift<'tcx>> Lift<'tcx> for IndexVec<I, T> {
322 type Lifted = IndexVec<I, T::Lifted>;
323 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
324 self.into_iter().map(|e| tcx.lift(e)).collect()
328 impl<'a, 'tcx> Lift<'tcx> for Term<'a> {
329 type Lifted = ty::Term<'tcx>;
330 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
332 match self.unpack() {
333 TermKind::Ty(ty) => TermKind::Ty(tcx.lift(ty)?),
334 TermKind::Const(c) => TermKind::Const(tcx.lift(c)?),
340 impl<'a, 'tcx> Lift<'tcx> for ty::ParamEnv<'a> {
341 type Lifted = ty::ParamEnv<'tcx>;
342 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
343 tcx.lift(self.caller_bounds())
344 .map(|caller_bounds| ty::ParamEnv::new(caller_bounds, self.reveal(), self.constness()))
348 ///////////////////////////////////////////////////////////////////////////
349 // TypeFoldable implementations.
351 /// AdtDefs are basically the same as a DefId.
352 impl<'tcx> TypeFoldable<'tcx> for ty::AdtDef<'tcx> {
353 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, _folder: &mut F) -> Result<Self, F::Error> {
358 impl<'tcx> TypeVisitable<'tcx> for ty::AdtDef<'tcx> {
359 fn visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
360 ControlFlow::CONTINUE
364 impl<'tcx, T: TypeFoldable<'tcx>, U: TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
365 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
368 ) -> Result<(T, U), F::Error> {
369 Ok((self.0.try_fold_with(folder)?, self.1.try_fold_with(folder)?))
373 impl<'tcx, T: TypeVisitable<'tcx>, U: TypeVisitable<'tcx>> TypeVisitable<'tcx> for (T, U) {
374 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
375 self.0.visit_with(visitor)?;
376 self.1.visit_with(visitor)
380 impl<'tcx, A: TypeFoldable<'tcx>, B: TypeFoldable<'tcx>, C: TypeFoldable<'tcx>> TypeFoldable<'tcx>
383 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
386 ) -> Result<(A, B, C), F::Error> {
388 self.0.try_fold_with(folder)?,
389 self.1.try_fold_with(folder)?,
390 self.2.try_fold_with(folder)?,
395 impl<'tcx, A: TypeVisitable<'tcx>, B: TypeVisitable<'tcx>, C: TypeVisitable<'tcx>>
396 TypeVisitable<'tcx> for (A, B, C)
398 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
399 self.0.visit_with(visitor)?;
400 self.1.visit_with(visitor)?;
401 self.2.visit_with(visitor)
405 EnumTypeTraversalImpl! {
406 impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
409 } where T: TypeFoldable<'tcx>
411 EnumTypeTraversalImpl! {
412 impl<'tcx, T> TypeVisitable<'tcx> for Option<T> {
415 } where T: TypeVisitable<'tcx>
418 EnumTypeTraversalImpl! {
419 impl<'tcx, T, E> TypeFoldable<'tcx> for Result<T, E> {
422 } where T: TypeFoldable<'tcx>, E: TypeFoldable<'tcx>,
424 EnumTypeTraversalImpl! {
425 impl<'tcx, T, E> TypeVisitable<'tcx> for Result<T, E> {
428 } where T: TypeVisitable<'tcx>, E: TypeVisitable<'tcx>,
431 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
432 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
435 ) -> Result<Self, F::Error> {
436 // We merely want to replace the contained `T`, if at all possible,
437 // so that we don't needlessly allocate a new `Rc` or indeed clone
438 // the contained type.
440 // First step is to ensure that we have a unique reference to
441 // the contained type, which `Rc::make_mut` will accomplish (by
442 // allocating a new `Rc` and cloning the `T` only if required).
443 // This is done *before* casting to `Rc<ManuallyDrop<T>>` so that
444 // panicking during `make_mut` does not leak the `T`.
445 Rc::make_mut(&mut self);
447 // Casting to `Rc<ManuallyDrop<T>>` is safe because `ManuallyDrop`
448 // is `repr(transparent)`.
449 let ptr = Rc::into_raw(self).cast::<ManuallyDrop<T>>();
450 let mut unique = Rc::from_raw(ptr);
452 // Call to `Rc::make_mut` above guarantees that `unique` is the
453 // sole reference to the contained value, so we can avoid doing
454 // a checked `get_mut` here.
455 let slot = Rc::get_mut_unchecked(&mut unique);
457 // Semantically move the contained type out from `unique`, fold
458 // it, then move the folded value back into `unique`. Should
459 // folding fail, `ManuallyDrop` ensures that the "moved-out"
460 // value is not re-dropped.
461 let owned = ManuallyDrop::take(slot);
462 let folded = owned.try_fold_with(folder)?;
463 *slot = ManuallyDrop::new(folded);
465 // Cast back to `Rc<T>`.
466 Ok(Rc::from_raw(Rc::into_raw(unique).cast()))
471 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Rc<T> {
472 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
473 (**self).visit_with(visitor)
477 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Arc<T> {
478 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
481 ) -> Result<Self, F::Error> {
482 // We merely want to replace the contained `T`, if at all possible,
483 // so that we don't needlessly allocate a new `Arc` or indeed clone
484 // the contained type.
486 // First step is to ensure that we have a unique reference to
487 // the contained type, which `Arc::make_mut` will accomplish (by
488 // allocating a new `Arc` and cloning the `T` only if required).
489 // This is done *before* casting to `Arc<ManuallyDrop<T>>` so that
490 // panicking during `make_mut` does not leak the `T`.
491 Arc::make_mut(&mut self);
493 // Casting to `Arc<ManuallyDrop<T>>` is safe because `ManuallyDrop`
494 // is `repr(transparent)`.
495 let ptr = Arc::into_raw(self).cast::<ManuallyDrop<T>>();
496 let mut unique = Arc::from_raw(ptr);
498 // Call to `Arc::make_mut` above guarantees that `unique` is the
499 // sole reference to the contained value, so we can avoid doing
500 // a checked `get_mut` here.
501 let slot = Arc::get_mut_unchecked(&mut unique);
503 // Semantically move the contained type out from `unique`, fold
504 // it, then move the folded value back into `unique`. Should
505 // folding fail, `ManuallyDrop` ensures that the "moved-out"
506 // value is not re-dropped.
507 let owned = ManuallyDrop::take(slot);
508 let folded = owned.try_fold_with(folder)?;
509 *slot = ManuallyDrop::new(folded);
511 // Cast back to `Arc<T>`.
512 Ok(Arc::from_raw(Arc::into_raw(unique).cast()))
517 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Arc<T> {
518 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
519 (**self).visit_with(visitor)
523 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
524 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
525 self.try_map_id(|value| value.try_fold_with(folder))
529 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Box<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 Vec<T> {
536 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
537 self.try_map_id(|t| t.try_fold_with(folder))
541 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Vec<T> {
542 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
543 self.iter().try_for_each(|t| t.visit_with(visitor))
547 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for &[T] {
548 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
549 self.iter().try_for_each(|t| t.visit_with(visitor))
553 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<[T]> {
554 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
555 self.try_map_id(|t| t.try_fold_with(folder))
559 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Box<[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 ty::Binder<'tcx, T> {
566 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
567 folder.try_fold_binder(self)
571 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for ty::Binder<'tcx, T> {
572 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
573 visitor.visit_binder(self)
577 impl<'tcx, T: TypeFoldable<'tcx>> TypeSuperFoldable<'tcx> for ty::Binder<'tcx, T> {
578 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
581 ) -> Result<Self, F::Error> {
582 self.try_map_bound(|ty| ty.try_fold_with(folder))
586 impl<'tcx, T: TypeVisitable<'tcx>> TypeSuperVisitable<'tcx> for ty::Binder<'tcx, T> {
587 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
588 self.as_ref().skip_binder().visit_with(visitor)
592 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>> {
593 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
594 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_poly_existential_predicates(v))
598 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Const<'tcx>> {
599 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
600 ty::util::fold_list(self, folder, |tcx, v| tcx.mk_const_list(v.iter()))
604 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind> {
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_projs(v))
610 impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
611 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
612 folder.try_fold_ty(self)
616 impl<'tcx> TypeVisitable<'tcx> for Ty<'tcx> {
617 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
618 visitor.visit_ty(*self)
622 impl<'tcx> TypeSuperFoldable<'tcx> for Ty<'tcx> {
623 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
626 ) -> Result<Self, F::Error> {
627 let kind = match *self.kind() {
628 ty::RawPtr(tm) => ty::RawPtr(tm.try_fold_with(folder)?),
629 ty::Array(typ, sz) => ty::Array(typ.try_fold_with(folder)?, sz.try_fold_with(folder)?),
630 ty::Slice(typ) => ty::Slice(typ.try_fold_with(folder)?),
631 ty::Adt(tid, substs) => ty::Adt(tid, substs.try_fold_with(folder)?),
632 ty::Dynamic(trait_ty, region, representation) => ty::Dynamic(
633 trait_ty.try_fold_with(folder)?,
634 region.try_fold_with(folder)?,
637 ty::Tuple(ts) => ty::Tuple(ts.try_fold_with(folder)?),
638 ty::FnDef(def_id, substs) => ty::FnDef(def_id, substs.try_fold_with(folder)?),
639 ty::FnPtr(f) => ty::FnPtr(f.try_fold_with(folder)?),
640 ty::Ref(r, ty, mutbl) => {
641 ty::Ref(r.try_fold_with(folder)?, ty.try_fold_with(folder)?, mutbl)
643 ty::Generator(did, substs, movability) => {
644 ty::Generator(did, substs.try_fold_with(folder)?, movability)
646 ty::GeneratorWitness(types) => ty::GeneratorWitness(types.try_fold_with(folder)?),
647 ty::Closure(did, substs) => ty::Closure(did, substs.try_fold_with(folder)?),
648 ty::Alias(kind, data) => ty::Alias(kind, data.try_fold_with(folder)?),
660 | ty::Placeholder(..)
662 | ty::Foreign(..) => return Ok(self),
665 Ok(if *self.kind() == kind { self } else { folder.tcx().mk_ty(kind) })
669 impl<'tcx> TypeSuperVisitable<'tcx> for Ty<'tcx> {
670 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
672 ty::RawPtr(ref tm) => tm.visit_with(visitor),
673 ty::Array(typ, sz) => {
674 typ.visit_with(visitor)?;
675 sz.visit_with(visitor)
677 ty::Slice(typ) => typ.visit_with(visitor),
678 ty::Adt(_, substs) => substs.visit_with(visitor),
679 ty::Dynamic(ref trait_ty, ref reg, _) => {
680 trait_ty.visit_with(visitor)?;
681 reg.visit_with(visitor)
683 ty::Tuple(ts) => ts.visit_with(visitor),
684 ty::FnDef(_, substs) => substs.visit_with(visitor),
685 ty::FnPtr(ref f) => f.visit_with(visitor),
686 ty::Ref(r, ty, _) => {
687 r.visit_with(visitor)?;
688 ty.visit_with(visitor)
690 ty::Generator(_did, ref substs, _) => substs.visit_with(visitor),
691 ty::GeneratorWitness(ref types) => types.visit_with(visitor),
692 ty::Closure(_did, ref substs) => substs.visit_with(visitor),
693 ty::Alias(_, ref data) => data.visit_with(visitor),
704 | ty::Placeholder(..)
707 | ty::Foreign(..) => ControlFlow::CONTINUE,
712 impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> {
713 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
714 folder.try_fold_region(self)
718 impl<'tcx> TypeVisitable<'tcx> for ty::Region<'tcx> {
719 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
720 visitor.visit_region(*self)
724 impl<'tcx> TypeSuperFoldable<'tcx> for ty::Region<'tcx> {
725 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
728 ) -> Result<Self, F::Error> {
733 impl<'tcx> TypeSuperVisitable<'tcx> for ty::Region<'tcx> {
734 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
735 ControlFlow::CONTINUE
739 impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
740 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
741 folder.try_fold_predicate(self)
745 impl<'tcx> TypeVisitable<'tcx> for ty::Predicate<'tcx> {
746 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
747 visitor.visit_predicate(*self)
751 fn has_vars_bound_at_or_above(&self, binder: ty::DebruijnIndex) -> bool {
752 self.outer_exclusive_binder() > binder
756 fn has_type_flags(&self, flags: ty::TypeFlags) -> bool {
757 self.flags().intersects(flags)
761 impl<'tcx> TypeSuperFoldable<'tcx> for ty::Predicate<'tcx> {
762 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
765 ) -> Result<Self, F::Error> {
766 let new = self.kind().try_fold_with(folder)?;
767 Ok(folder.tcx().reuse_or_mk_predicate(self, new))
771 impl<'tcx> TypeSuperVisitable<'tcx> for ty::Predicate<'tcx> {
772 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
773 self.kind().visit_with(visitor)
777 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
778 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
779 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_predicates(v))
783 impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
784 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
785 self.try_map_id(|x| x.try_fold_with(folder))
789 impl<'tcx, T: TypeVisitable<'tcx>, I: Idx> TypeVisitable<'tcx> for IndexVec<I, T> {
790 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
791 self.iter().try_for_each(|t| t.visit_with(visitor))
795 impl<'tcx> TypeFoldable<'tcx> for ty::Const<'tcx> {
796 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
797 folder.try_fold_const(self)
801 impl<'tcx> TypeVisitable<'tcx> for ty::Const<'tcx> {
802 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
803 visitor.visit_const(*self)
807 impl<'tcx> TypeSuperFoldable<'tcx> for ty::Const<'tcx> {
808 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
811 ) -> Result<Self, F::Error> {
812 let ty = self.ty().try_fold_with(folder)?;
813 let kind = self.kind().try_fold_with(folder)?;
814 if ty != self.ty() || kind != self.kind() {
815 Ok(folder.tcx().mk_const(kind, ty))
822 impl<'tcx> TypeSuperVisitable<'tcx> for ty::Const<'tcx> {
823 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
824 self.ty().visit_with(visitor)?;
825 self.kind().visit_with(visitor)
829 impl<'tcx> TypeFoldable<'tcx> for InferConst<'tcx> {
830 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, _folder: &mut F) -> Result<Self, F::Error> {
835 impl<'tcx> TypeVisitable<'tcx> for InferConst<'tcx> {
836 fn visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
837 ControlFlow::CONTINUE
841 impl<'tcx> TypeSuperVisitable<'tcx> for ty::UnevaluatedConst<'tcx> {
842 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
843 self.substs.visit_with(visitor)