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 fmt::Debug for ty::RegionVid {
103 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104 write!(f, "'_#{}r", self.index())
108 impl<'tcx> fmt::Debug for ty::TraitRef<'tcx> {
109 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110 with_no_trimmed_paths!(fmt::Display::fmt(self, f))
114 impl<'tcx> fmt::Debug for Ty<'tcx> {
115 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
116 with_no_trimmed_paths!(fmt::Display::fmt(self, f))
120 impl fmt::Debug for ty::ParamTy {
121 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
122 write!(f, "{}/#{}", self.name, self.index)
126 impl fmt::Debug for ty::ParamConst {
127 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
128 write!(f, "{}/#{}", self.name, self.index)
132 impl<'tcx> fmt::Debug for ty::TraitPredicate<'tcx> {
133 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
134 if let ty::BoundConstness::ConstIfConst = self.constness {
135 write!(f, "~const ")?;
137 write!(f, "TraitPredicate({:?}, polarity:{:?})", self.trait_ref, self.polarity)
141 impl<'tcx> fmt::Debug for ty::ProjectionPredicate<'tcx> {
142 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
143 write!(f, "ProjectionPredicate({:?}, {:?})", self.projection_ty, self.term)
147 impl<'tcx> fmt::Debug for ty::Predicate<'tcx> {
148 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
149 write!(f, "{:?}", self.kind())
153 impl<'tcx> fmt::Debug for ty::Clause<'tcx> {
154 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
156 ty::Clause::Trait(ref a) => a.fmt(f),
157 ty::Clause::RegionOutlives(ref pair) => pair.fmt(f),
158 ty::Clause::TypeOutlives(ref pair) => pair.fmt(f),
159 ty::Clause::Projection(ref pair) => pair.fmt(f),
164 impl<'tcx> fmt::Debug for ty::PredicateKind<'tcx> {
165 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
167 ty::PredicateKind::Clause(ref a) => a.fmt(f),
168 ty::PredicateKind::Subtype(ref pair) => pair.fmt(f),
169 ty::PredicateKind::Coerce(ref pair) => pair.fmt(f),
170 ty::PredicateKind::WellFormed(data) => write!(f, "WellFormed({:?})", data),
171 ty::PredicateKind::ObjectSafe(trait_def_id) => {
172 write!(f, "ObjectSafe({:?})", trait_def_id)
174 ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => {
175 write!(f, "ClosureKind({:?}, {:?}, {:?})", closure_def_id, closure_substs, kind)
177 ty::PredicateKind::ConstEvaluatable(ct) => {
178 write!(f, "ConstEvaluatable({ct:?})")
180 ty::PredicateKind::ConstEquate(c1, c2) => write!(f, "ConstEquate({:?}, {:?})", c1, c2),
181 ty::PredicateKind::TypeWellFormedFromEnv(ty) => {
182 write!(f, "TypeWellFormedFromEnv({:?})", ty)
184 ty::PredicateKind::Ambiguous => write!(f, "Ambiguous"),
189 ///////////////////////////////////////////////////////////////////////////
192 // For things that don't carry any arena-allocated data (and are
193 // copy...), just add them to this list.
195 TrivialTypeTraversalAndLiftImpls! {
199 ::rustc_target::abi::VariantIdx,
203 crate::middle::region::Scope,
205 ::rustc_ast::InlineAsmOptions,
206 ::rustc_ast::InlineAsmTemplatePiece,
208 ::rustc_span::symbol::Symbol,
209 ::rustc_hir::def::Res,
210 ::rustc_hir::def_id::DefId,
211 ::rustc_hir::def_id::LocalDefId,
213 ::rustc_hir::MatchSource,
214 ::rustc_hir::Mutability,
215 ::rustc_hir::Unsafety,
216 ::rustc_target::asm::InlineAsmRegOrRegClass,
217 ::rustc_target::spec::abi::Abi,
218 crate::mir::coverage::ExpressionOperandId,
219 crate::mir::coverage::CounterValueReference,
220 crate::mir::coverage::InjectedExpressionId,
221 crate::mir::coverage::InjectedExpressionIndex,
222 crate::mir::coverage::MappedExpressionIndex,
224 crate::mir::Promoted,
225 crate::traits::Reveal,
226 crate::ty::adjustment::AutoBorrowMutability,
228 crate::ty::BoundConstness,
229 // Including `BoundRegionKind` is a *bit* dubious, but direct
230 // references to bound region appear in `ty::Error`, and aren't
231 // really meant to be folded. In general, we can only fold a fully
233 crate::ty::BoundRegionKind,
234 crate::ty::AssocItem,
235 crate::ty::AssocKind,
236 crate::ty::Placeholder<crate::ty::BoundRegionKind>,
237 crate::ty::ClosureKind,
238 crate::ty::FreeRegion,
240 crate::ty::IntVarValue,
241 crate::ty::ParamConst,
243 crate::ty::adjustment::PointerCast,
244 crate::ty::RegionVid,
245 crate::ty::UniverseIndex,
248 ::rustc_errors::ErrorGuaranteed,
251 rustc_target::abi::Size,
252 rustc_type_ir::DebruijnIndex,
254 ty::Placeholder<ty::BoundVar>,
257 TrivialTypeTraversalAndLiftImpls! {
263 ///////////////////////////////////////////////////////////////////////////
264 // Lift implementations
266 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>> Lift<'tcx> for (A, B) {
267 type Lifted = (A::Lifted, B::Lifted);
268 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
269 Some((tcx.lift(self.0)?, tcx.lift(self.1)?))
273 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>, C: Lift<'tcx>> Lift<'tcx> for (A, B, C) {
274 type Lifted = (A::Lifted, B::Lifted, C::Lifted);
275 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
276 Some((tcx.lift(self.0)?, tcx.lift(self.1)?, tcx.lift(self.2)?))
280 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Option<T> {
281 type Lifted = Option<T::Lifted>;
282 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
284 Some(x) => Some(tcx.lift(x)?),
290 impl<'tcx, T: Lift<'tcx>, E: Lift<'tcx>> Lift<'tcx> for Result<T, E> {
291 type Lifted = Result<T::Lifted, E::Lifted>;
292 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
294 Ok(x) => tcx.lift(x).map(Ok),
295 Err(e) => tcx.lift(e).map(Err),
300 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Box<T> {
301 type Lifted = Box<T::Lifted>;
302 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
303 Some(Box::new(tcx.lift(*self)?))
307 impl<'tcx, T: Lift<'tcx> + Clone> Lift<'tcx> for Rc<T> {
308 type Lifted = Rc<T::Lifted>;
309 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
310 Some(Rc::new(tcx.lift(self.as_ref().clone())?))
314 impl<'tcx, T: Lift<'tcx> + Clone> Lift<'tcx> for Arc<T> {
315 type Lifted = Arc<T::Lifted>;
316 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
317 Some(Arc::new(tcx.lift(self.as_ref().clone())?))
320 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Vec<T> {
321 type Lifted = Vec<T::Lifted>;
322 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
323 self.into_iter().map(|v| tcx.lift(v)).collect()
327 impl<'tcx, I: Idx, T: Lift<'tcx>> Lift<'tcx> for IndexVec<I, T> {
328 type Lifted = IndexVec<I, T::Lifted>;
329 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
330 self.into_iter().map(|e| tcx.lift(e)).collect()
334 impl<'a, 'tcx> Lift<'tcx> for Term<'a> {
335 type Lifted = ty::Term<'tcx>;
336 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
338 match self.unpack() {
339 TermKind::Ty(ty) => TermKind::Ty(tcx.lift(ty)?),
340 TermKind::Const(c) => TermKind::Const(tcx.lift(c)?),
346 impl<'a, 'tcx> Lift<'tcx> for ty::ParamEnv<'a> {
347 type Lifted = ty::ParamEnv<'tcx>;
348 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
349 tcx.lift(self.caller_bounds())
350 .map(|caller_bounds| ty::ParamEnv::new(caller_bounds, self.reveal(), self.constness()))
354 ///////////////////////////////////////////////////////////////////////////
355 // TypeFoldable implementations.
357 /// AdtDefs are basically the same as a DefId.
358 impl<'tcx> TypeFoldable<'tcx> for ty::AdtDef<'tcx> {
359 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, _folder: &mut F) -> Result<Self, F::Error> {
364 impl<'tcx> TypeVisitable<'tcx> for ty::AdtDef<'tcx> {
365 fn visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
366 ControlFlow::CONTINUE
370 impl<'tcx, T: TypeFoldable<'tcx>, U: TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
371 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
374 ) -> Result<(T, U), F::Error> {
375 Ok((self.0.try_fold_with(folder)?, self.1.try_fold_with(folder)?))
379 impl<'tcx, T: TypeVisitable<'tcx>, U: TypeVisitable<'tcx>> TypeVisitable<'tcx> for (T, U) {
380 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
381 self.0.visit_with(visitor)?;
382 self.1.visit_with(visitor)
386 impl<'tcx, A: TypeFoldable<'tcx>, B: TypeFoldable<'tcx>, C: TypeFoldable<'tcx>> TypeFoldable<'tcx>
389 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
392 ) -> Result<(A, B, C), F::Error> {
394 self.0.try_fold_with(folder)?,
395 self.1.try_fold_with(folder)?,
396 self.2.try_fold_with(folder)?,
401 impl<'tcx, A: TypeVisitable<'tcx>, B: TypeVisitable<'tcx>, C: TypeVisitable<'tcx>>
402 TypeVisitable<'tcx> for (A, B, C)
404 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
405 self.0.visit_with(visitor)?;
406 self.1.visit_with(visitor)?;
407 self.2.visit_with(visitor)
411 EnumTypeTraversalImpl! {
412 impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
415 } where T: TypeFoldable<'tcx>
417 EnumTypeTraversalImpl! {
418 impl<'tcx, T> TypeVisitable<'tcx> for Option<T> {
421 } where T: TypeVisitable<'tcx>
424 EnumTypeTraversalImpl! {
425 impl<'tcx, T, E> TypeFoldable<'tcx> for Result<T, E> {
428 } where T: TypeFoldable<'tcx>, E: TypeFoldable<'tcx>,
430 EnumTypeTraversalImpl! {
431 impl<'tcx, T, E> TypeVisitable<'tcx> for Result<T, E> {
434 } where T: TypeVisitable<'tcx>, E: TypeVisitable<'tcx>,
437 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
438 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
441 ) -> Result<Self, F::Error> {
442 // We merely want to replace the contained `T`, if at all possible,
443 // so that we don't needlessly allocate a new `Rc` or indeed clone
444 // the contained type.
446 // First step is to ensure that we have a unique reference to
447 // the contained type, which `Rc::make_mut` will accomplish (by
448 // allocating a new `Rc` and cloning the `T` only if required).
449 // This is done *before* casting to `Rc<ManuallyDrop<T>>` so that
450 // panicking during `make_mut` does not leak the `T`.
451 Rc::make_mut(&mut self);
453 // Casting to `Rc<ManuallyDrop<T>>` is safe because `ManuallyDrop`
454 // is `repr(transparent)`.
455 let ptr = Rc::into_raw(self).cast::<ManuallyDrop<T>>();
456 let mut unique = Rc::from_raw(ptr);
458 // Call to `Rc::make_mut` above guarantees that `unique` is the
459 // sole reference to the contained value, so we can avoid doing
460 // a checked `get_mut` here.
461 let slot = Rc::get_mut_unchecked(&mut unique);
463 // Semantically move the contained type out from `unique`, fold
464 // it, then move the folded value back into `unique`. Should
465 // folding fail, `ManuallyDrop` ensures that the "moved-out"
466 // value is not re-dropped.
467 let owned = ManuallyDrop::take(slot);
468 let folded = owned.try_fold_with(folder)?;
469 *slot = ManuallyDrop::new(folded);
471 // Cast back to `Rc<T>`.
472 Ok(Rc::from_raw(Rc::into_raw(unique).cast()))
477 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Rc<T> {
478 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
479 (**self).visit_with(visitor)
483 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Arc<T> {
484 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
487 ) -> Result<Self, F::Error> {
488 // We merely want to replace the contained `T`, if at all possible,
489 // so that we don't needlessly allocate a new `Arc` or indeed clone
490 // the contained type.
492 // First step is to ensure that we have a unique reference to
493 // the contained type, which `Arc::make_mut` will accomplish (by
494 // allocating a new `Arc` and cloning the `T` only if required).
495 // This is done *before* casting to `Arc<ManuallyDrop<T>>` so that
496 // panicking during `make_mut` does not leak the `T`.
497 Arc::make_mut(&mut self);
499 // Casting to `Arc<ManuallyDrop<T>>` is safe because `ManuallyDrop`
500 // is `repr(transparent)`.
501 let ptr = Arc::into_raw(self).cast::<ManuallyDrop<T>>();
502 let mut unique = Arc::from_raw(ptr);
504 // Call to `Arc::make_mut` above guarantees that `unique` is the
505 // sole reference to the contained value, so we can avoid doing
506 // a checked `get_mut` here.
507 let slot = Arc::get_mut_unchecked(&mut unique);
509 // Semantically move the contained type out from `unique`, fold
510 // it, then move the folded value back into `unique`. Should
511 // folding fail, `ManuallyDrop` ensures that the "moved-out"
512 // value is not re-dropped.
513 let owned = ManuallyDrop::take(slot);
514 let folded = owned.try_fold_with(folder)?;
515 *slot = ManuallyDrop::new(folded);
517 // Cast back to `Arc<T>`.
518 Ok(Arc::from_raw(Arc::into_raw(unique).cast()))
523 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Arc<T> {
524 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
525 (**self).visit_with(visitor)
529 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
530 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
531 self.try_map_id(|value| value.try_fold_with(folder))
535 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Box<T> {
536 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
537 (**self).visit_with(visitor)
541 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
542 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
543 self.try_map_id(|t| t.try_fold_with(folder))
547 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Vec<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: TypeVisitable<'tcx>> TypeVisitable<'tcx> for &[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: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<[T]> {
560 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
561 self.try_map_id(|t| t.try_fold_with(folder))
565 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Box<[T]> {
566 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
567 self.iter().try_for_each(|t| t.visit_with(visitor))
571 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<'tcx, T> {
572 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
573 folder.try_fold_binder(self)
577 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for ty::Binder<'tcx, T> {
578 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
579 visitor.visit_binder(self)
583 impl<'tcx, T: TypeFoldable<'tcx>> TypeSuperFoldable<'tcx> for ty::Binder<'tcx, T> {
584 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
587 ) -> Result<Self, F::Error> {
588 self.try_map_bound(|ty| ty.try_fold_with(folder))
592 impl<'tcx, T: TypeVisitable<'tcx>> TypeSuperVisitable<'tcx> for ty::Binder<'tcx, T> {
593 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
594 self.as_ref().skip_binder().visit_with(visitor)
598 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::PolyExistentialPredicate<'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.intern_poly_existential_predicates(v))
604 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Const<'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.mk_const_list(v.iter()))
610 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind> {
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.intern_projs(v))
616 impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
617 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
618 folder.try_fold_ty(self)
622 impl<'tcx> TypeVisitable<'tcx> for Ty<'tcx> {
623 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
624 visitor.visit_ty(*self)
628 impl<'tcx> TypeSuperFoldable<'tcx> for Ty<'tcx> {
629 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
632 ) -> Result<Self, F::Error> {
633 let kind = match *self.kind() {
634 ty::RawPtr(tm) => ty::RawPtr(tm.try_fold_with(folder)?),
635 ty::Array(typ, sz) => ty::Array(typ.try_fold_with(folder)?, sz.try_fold_with(folder)?),
636 ty::Slice(typ) => ty::Slice(typ.try_fold_with(folder)?),
637 ty::Adt(tid, substs) => ty::Adt(tid, substs.try_fold_with(folder)?),
638 ty::Dynamic(trait_ty, region, representation) => ty::Dynamic(
639 trait_ty.try_fold_with(folder)?,
640 region.try_fold_with(folder)?,
643 ty::Tuple(ts) => ty::Tuple(ts.try_fold_with(folder)?),
644 ty::FnDef(def_id, substs) => ty::FnDef(def_id, substs.try_fold_with(folder)?),
645 ty::FnPtr(f) => ty::FnPtr(f.try_fold_with(folder)?),
646 ty::Ref(r, ty, mutbl) => {
647 ty::Ref(r.try_fold_with(folder)?, ty.try_fold_with(folder)?, mutbl)
649 ty::Generator(did, substs, movability) => {
650 ty::Generator(did, substs.try_fold_with(folder)?, movability)
652 ty::GeneratorWitness(types) => ty::GeneratorWitness(types.try_fold_with(folder)?),
653 ty::Closure(did, substs) => ty::Closure(did, substs.try_fold_with(folder)?),
654 ty::Projection(data) => ty::Projection(data.try_fold_with(folder)?),
655 ty::Opaque(ty::AliasTy { def_id, substs }) => {
656 ty::Opaque(ty::AliasTy { def_id, substs: substs.try_fold_with(folder)? })
669 | ty::Placeholder(..)
671 | ty::Foreign(..) => return Ok(self),
674 Ok(if *self.kind() == kind { self } else { folder.tcx().mk_ty(kind) })
678 impl<'tcx> TypeSuperVisitable<'tcx> for Ty<'tcx> {
679 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
681 ty::RawPtr(ref tm) => tm.visit_with(visitor),
682 ty::Array(typ, sz) => {
683 typ.visit_with(visitor)?;
684 sz.visit_with(visitor)
686 ty::Slice(typ) => typ.visit_with(visitor),
687 ty::Adt(_, substs) => substs.visit_with(visitor),
688 ty::Dynamic(ref trait_ty, ref reg, _) => {
689 trait_ty.visit_with(visitor)?;
690 reg.visit_with(visitor)
692 ty::Tuple(ts) => ts.visit_with(visitor),
693 ty::FnDef(_, substs) => substs.visit_with(visitor),
694 ty::FnPtr(ref f) => f.visit_with(visitor),
695 ty::Ref(r, ty, _) => {
696 r.visit_with(visitor)?;
697 ty.visit_with(visitor)
699 ty::Generator(_did, ref substs, _) => substs.visit_with(visitor),
700 ty::GeneratorWitness(ref types) => types.visit_with(visitor),
701 ty::Closure(_did, ref substs) => substs.visit_with(visitor),
702 ty::Projection(ref data) => data.visit_with(visitor),
703 ty::Opaque(ty::AliasTy { def_id: _, ref substs }) => substs.visit_with(visitor),
714 | ty::Placeholder(..)
717 | ty::Foreign(..) => ControlFlow::CONTINUE,
722 impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> {
723 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
724 folder.try_fold_region(self)
728 impl<'tcx> TypeVisitable<'tcx> for ty::Region<'tcx> {
729 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
730 visitor.visit_region(*self)
734 impl<'tcx> TypeSuperFoldable<'tcx> for ty::Region<'tcx> {
735 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
738 ) -> Result<Self, F::Error> {
743 impl<'tcx> TypeSuperVisitable<'tcx> for ty::Region<'tcx> {
744 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
745 ControlFlow::CONTINUE
749 impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
750 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
751 folder.try_fold_predicate(self)
755 impl<'tcx> TypeVisitable<'tcx> for ty::Predicate<'tcx> {
756 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
757 visitor.visit_predicate(*self)
761 fn has_vars_bound_at_or_above(&self, binder: ty::DebruijnIndex) -> bool {
762 self.outer_exclusive_binder() > binder
766 fn has_type_flags(&self, flags: ty::TypeFlags) -> bool {
767 self.flags().intersects(flags)
771 impl<'tcx> TypeSuperFoldable<'tcx> for ty::Predicate<'tcx> {
772 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
775 ) -> Result<Self, F::Error> {
776 let new = self.kind().try_fold_with(folder)?;
777 Ok(folder.tcx().reuse_or_mk_predicate(self, new))
781 impl<'tcx> TypeSuperVisitable<'tcx> for ty::Predicate<'tcx> {
782 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
783 self.kind().visit_with(visitor)
787 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
788 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
789 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_predicates(v))
793 impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
794 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
795 self.try_map_id(|x| x.try_fold_with(folder))
799 impl<'tcx, T: TypeVisitable<'tcx>, I: Idx> TypeVisitable<'tcx> for IndexVec<I, T> {
800 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
801 self.iter().try_for_each(|t| t.visit_with(visitor))
805 impl<'tcx> TypeFoldable<'tcx> for ty::Const<'tcx> {
806 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
807 folder.try_fold_const(self)
811 impl<'tcx> TypeVisitable<'tcx> for ty::Const<'tcx> {
812 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
813 visitor.visit_const(*self)
817 impl<'tcx> TypeSuperFoldable<'tcx> for ty::Const<'tcx> {
818 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
821 ) -> Result<Self, F::Error> {
822 let ty = self.ty().try_fold_with(folder)?;
823 let kind = self.kind().try_fold_with(folder)?;
824 if ty != self.ty() || kind != self.kind() {
825 Ok(folder.tcx().mk_const(kind, ty))
832 impl<'tcx> TypeSuperVisitable<'tcx> for ty::Const<'tcx> {
833 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
834 self.ty().visit_with(visitor)?;
835 self.kind().visit_with(visitor)
839 impl<'tcx> TypeFoldable<'tcx> for InferConst<'tcx> {
840 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, _folder: &mut F) -> Result<Self, F::Error> {
845 impl<'tcx> TypeVisitable<'tcx> for InferConst<'tcx> {
846 fn visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
847 ControlFlow::CONTINUE
851 impl<'tcx> TypeSuperVisitable<'tcx> for ty::UnevaluatedConst<'tcx> {
852 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
853 self.substs.visit_with(visitor)