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::Placeholder<crate::ty::BoundTyKind>,
244 crate::ty::ClosureKind,
245 crate::ty::FreeRegion,
247 crate::ty::IntVarValue,
248 crate::ty::ParamConst,
250 crate::ty::adjustment::PointerCast,
251 crate::ty::RegionVid,
252 crate::ty::UniverseIndex,
255 ::rustc_errors::ErrorGuaranteed,
258 rustc_target::abi::Size,
259 rustc_type_ir::DebruijnIndex,
261 ty::Placeholder<ty::BoundVar>,
264 TrivialTypeTraversalAndLiftImpls! {
270 ///////////////////////////////////////////////////////////////////////////
271 // Lift implementations
273 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>> Lift<'tcx> for (A, B) {
274 type Lifted = (A::Lifted, B::Lifted);
275 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
276 Some((tcx.lift(self.0)?, tcx.lift(self.1)?))
280 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>, C: Lift<'tcx>> Lift<'tcx> for (A, B, C) {
281 type Lifted = (A::Lifted, B::Lifted, C::Lifted);
282 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
283 Some((tcx.lift(self.0)?, tcx.lift(self.1)?, tcx.lift(self.2)?))
287 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Option<T> {
288 type Lifted = Option<T::Lifted>;
289 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
291 Some(x) => Some(tcx.lift(x)?),
297 impl<'tcx, T: Lift<'tcx>, E: Lift<'tcx>> Lift<'tcx> for Result<T, E> {
298 type Lifted = Result<T::Lifted, E::Lifted>;
299 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
301 Ok(x) => tcx.lift(x).map(Ok),
302 Err(e) => tcx.lift(e).map(Err),
307 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Box<T> {
308 type Lifted = Box<T::Lifted>;
309 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
310 Some(Box::new(tcx.lift(*self)?))
314 impl<'tcx, T: Lift<'tcx> + Clone> Lift<'tcx> for Rc<T> {
315 type Lifted = Rc<T::Lifted>;
316 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
317 Some(Rc::new(tcx.lift(self.as_ref().clone())?))
321 impl<'tcx, T: Lift<'tcx> + Clone> Lift<'tcx> for Arc<T> {
322 type Lifted = Arc<T::Lifted>;
323 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
324 Some(Arc::new(tcx.lift(self.as_ref().clone())?))
327 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Vec<T> {
328 type Lifted = Vec<T::Lifted>;
329 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
330 self.into_iter().map(|v| tcx.lift(v)).collect()
334 impl<'tcx, I: Idx, T: Lift<'tcx>> Lift<'tcx> for IndexVec<I, T> {
335 type Lifted = IndexVec<I, T::Lifted>;
336 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
337 self.into_iter().map(|e| tcx.lift(e)).collect()
341 impl<'a, 'tcx> Lift<'tcx> for Term<'a> {
342 type Lifted = ty::Term<'tcx>;
343 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
345 match self.unpack() {
346 TermKind::Ty(ty) => TermKind::Ty(tcx.lift(ty)?),
347 TermKind::Const(c) => TermKind::Const(tcx.lift(c)?),
353 impl<'a, 'tcx> Lift<'tcx> for ty::ParamEnv<'a> {
354 type Lifted = ty::ParamEnv<'tcx>;
355 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
356 tcx.lift(self.caller_bounds())
357 .map(|caller_bounds| ty::ParamEnv::new(caller_bounds, self.reveal(), self.constness()))
361 ///////////////////////////////////////////////////////////////////////////
362 // TypeFoldable implementations.
364 /// AdtDefs are basically the same as a DefId.
365 impl<'tcx> TypeFoldable<'tcx> for ty::AdtDef<'tcx> {
366 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, _folder: &mut F) -> Result<Self, F::Error> {
371 impl<'tcx> TypeVisitable<'tcx> for ty::AdtDef<'tcx> {
372 fn visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
373 ControlFlow::Continue(())
377 impl<'tcx, T: TypeFoldable<'tcx>, U: TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
378 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
381 ) -> Result<(T, U), F::Error> {
382 Ok((self.0.try_fold_with(folder)?, self.1.try_fold_with(folder)?))
386 impl<'tcx, T: TypeVisitable<'tcx>, U: TypeVisitable<'tcx>> TypeVisitable<'tcx> for (T, U) {
387 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
388 self.0.visit_with(visitor)?;
389 self.1.visit_with(visitor)
393 impl<'tcx, A: TypeFoldable<'tcx>, B: TypeFoldable<'tcx>, C: TypeFoldable<'tcx>> TypeFoldable<'tcx>
396 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
399 ) -> Result<(A, B, C), F::Error> {
401 self.0.try_fold_with(folder)?,
402 self.1.try_fold_with(folder)?,
403 self.2.try_fold_with(folder)?,
408 impl<'tcx, A: TypeVisitable<'tcx>, B: TypeVisitable<'tcx>, C: TypeVisitable<'tcx>>
409 TypeVisitable<'tcx> for (A, B, C)
411 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
412 self.0.visit_with(visitor)?;
413 self.1.visit_with(visitor)?;
414 self.2.visit_with(visitor)
418 EnumTypeTraversalImpl! {
419 impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
422 } where T: TypeFoldable<'tcx>
424 EnumTypeTraversalImpl! {
425 impl<'tcx, T> TypeVisitable<'tcx> for Option<T> {
428 } where T: TypeVisitable<'tcx>
431 EnumTypeTraversalImpl! {
432 impl<'tcx, T, E> TypeFoldable<'tcx> for Result<T, E> {
435 } where T: TypeFoldable<'tcx>, E: TypeFoldable<'tcx>,
437 EnumTypeTraversalImpl! {
438 impl<'tcx, T, E> TypeVisitable<'tcx> for Result<T, E> {
441 } where T: TypeVisitable<'tcx>, E: TypeVisitable<'tcx>,
444 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
445 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
448 ) -> Result<Self, F::Error> {
449 // We merely want to replace the contained `T`, if at all possible,
450 // so that we don't needlessly allocate a new `Rc` or indeed clone
451 // the contained type.
453 // First step is to ensure that we have a unique reference to
454 // the contained type, which `Rc::make_mut` will accomplish (by
455 // allocating a new `Rc` and cloning the `T` only if required).
456 // This is done *before* casting to `Rc<ManuallyDrop<T>>` so that
457 // panicking during `make_mut` does not leak the `T`.
458 Rc::make_mut(&mut self);
460 // Casting to `Rc<ManuallyDrop<T>>` is safe because `ManuallyDrop`
461 // is `repr(transparent)`.
462 let ptr = Rc::into_raw(self).cast::<ManuallyDrop<T>>();
463 let mut unique = Rc::from_raw(ptr);
465 // Call to `Rc::make_mut` above guarantees that `unique` is the
466 // sole reference to the contained value, so we can avoid doing
467 // a checked `get_mut` here.
468 let slot = Rc::get_mut_unchecked(&mut unique);
470 // Semantically move the contained type out from `unique`, fold
471 // it, then move the folded value back into `unique`. Should
472 // folding fail, `ManuallyDrop` ensures that the "moved-out"
473 // value is not re-dropped.
474 let owned = ManuallyDrop::take(slot);
475 let folded = owned.try_fold_with(folder)?;
476 *slot = ManuallyDrop::new(folded);
478 // Cast back to `Rc<T>`.
479 Ok(Rc::from_raw(Rc::into_raw(unique).cast()))
484 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Rc<T> {
485 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
486 (**self).visit_with(visitor)
490 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Arc<T> {
491 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
494 ) -> Result<Self, F::Error> {
495 // We merely want to replace the contained `T`, if at all possible,
496 // so that we don't needlessly allocate a new `Arc` or indeed clone
497 // the contained type.
499 // First step is to ensure that we have a unique reference to
500 // the contained type, which `Arc::make_mut` will accomplish (by
501 // allocating a new `Arc` and cloning the `T` only if required).
502 // This is done *before* casting to `Arc<ManuallyDrop<T>>` so that
503 // panicking during `make_mut` does not leak the `T`.
504 Arc::make_mut(&mut self);
506 // Casting to `Arc<ManuallyDrop<T>>` is safe because `ManuallyDrop`
507 // is `repr(transparent)`.
508 let ptr = Arc::into_raw(self).cast::<ManuallyDrop<T>>();
509 let mut unique = Arc::from_raw(ptr);
511 // Call to `Arc::make_mut` above guarantees that `unique` is the
512 // sole reference to the contained value, so we can avoid doing
513 // a checked `get_mut` here.
514 let slot = Arc::get_mut_unchecked(&mut unique);
516 // Semantically move the contained type out from `unique`, fold
517 // it, then move the folded value back into `unique`. Should
518 // folding fail, `ManuallyDrop` ensures that the "moved-out"
519 // value is not re-dropped.
520 let owned = ManuallyDrop::take(slot);
521 let folded = owned.try_fold_with(folder)?;
522 *slot = ManuallyDrop::new(folded);
524 // Cast back to `Arc<T>`.
525 Ok(Arc::from_raw(Arc::into_raw(unique).cast()))
530 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Arc<T> {
531 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
532 (**self).visit_with(visitor)
536 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
537 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
538 self.try_map_id(|value| value.try_fold_with(folder))
542 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Box<T> {
543 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
544 (**self).visit_with(visitor)
548 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
549 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
550 self.try_map_id(|t| t.try_fold_with(folder))
554 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Vec<T> {
555 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
556 self.iter().try_for_each(|t| t.visit_with(visitor))
560 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for &[T] {
561 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
562 self.iter().try_for_each(|t| t.visit_with(visitor))
566 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<[T]> {
567 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
568 self.try_map_id(|t| t.try_fold_with(folder))
572 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Box<[T]> {
573 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
574 self.iter().try_for_each(|t| t.visit_with(visitor))
578 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<'tcx, T> {
579 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
580 folder.try_fold_binder(self)
584 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for ty::Binder<'tcx, T> {
585 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
586 visitor.visit_binder(self)
590 impl<'tcx, T: TypeFoldable<'tcx>> TypeSuperFoldable<'tcx> for ty::Binder<'tcx, T> {
591 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
594 ) -> Result<Self, F::Error> {
595 self.try_map_bound(|ty| ty.try_fold_with(folder))
599 impl<'tcx, T: TypeVisitable<'tcx>> TypeSuperVisitable<'tcx> for ty::Binder<'tcx, T> {
600 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
601 self.as_ref().skip_binder().visit_with(visitor)
605 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>> {
606 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
607 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_poly_existential_predicates(v))
611 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Const<'tcx>> {
612 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
613 ty::util::fold_list(self, folder, |tcx, v| tcx.mk_const_list(v.iter()))
617 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind> {
618 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
619 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_projs(v))
623 impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
624 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
625 folder.try_fold_ty(self)
629 impl<'tcx> TypeVisitable<'tcx> for Ty<'tcx> {
630 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
631 visitor.visit_ty(*self)
635 impl<'tcx> TypeSuperFoldable<'tcx> for Ty<'tcx> {
636 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
639 ) -> Result<Self, F::Error> {
640 let kind = match *self.kind() {
641 ty::RawPtr(tm) => ty::RawPtr(tm.try_fold_with(folder)?),
642 ty::Array(typ, sz) => ty::Array(typ.try_fold_with(folder)?, sz.try_fold_with(folder)?),
643 ty::Slice(typ) => ty::Slice(typ.try_fold_with(folder)?),
644 ty::Adt(tid, substs) => ty::Adt(tid, substs.try_fold_with(folder)?),
645 ty::Dynamic(trait_ty, region, representation) => ty::Dynamic(
646 trait_ty.try_fold_with(folder)?,
647 region.try_fold_with(folder)?,
650 ty::Tuple(ts) => ty::Tuple(ts.try_fold_with(folder)?),
651 ty::FnDef(def_id, substs) => ty::FnDef(def_id, substs.try_fold_with(folder)?),
652 ty::FnPtr(f) => ty::FnPtr(f.try_fold_with(folder)?),
653 ty::Ref(r, ty, mutbl) => {
654 ty::Ref(r.try_fold_with(folder)?, ty.try_fold_with(folder)?, mutbl)
656 ty::Generator(did, substs, movability) => {
657 ty::Generator(did, substs.try_fold_with(folder)?, movability)
659 ty::GeneratorWitness(types) => ty::GeneratorWitness(types.try_fold_with(folder)?),
660 ty::GeneratorWitnessMIR(did, substs) => {
661 ty::GeneratorWitnessMIR(did, substs.try_fold_with(folder)?)
663 ty::Closure(did, substs) => ty::Closure(did, substs.try_fold_with(folder)?),
664 ty::Alias(kind, data) => ty::Alias(kind, data.try_fold_with(folder)?),
676 | ty::Placeholder(..)
678 | ty::Foreign(..) => return Ok(self),
681 Ok(if *self.kind() == kind { self } else { folder.tcx().mk_ty(kind) })
685 impl<'tcx> TypeSuperVisitable<'tcx> for Ty<'tcx> {
686 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
688 ty::RawPtr(ref tm) => tm.visit_with(visitor),
689 ty::Array(typ, sz) => {
690 typ.visit_with(visitor)?;
691 sz.visit_with(visitor)
693 ty::Slice(typ) => typ.visit_with(visitor),
694 ty::Adt(_, substs) => substs.visit_with(visitor),
695 ty::Dynamic(ref trait_ty, ref reg, _) => {
696 trait_ty.visit_with(visitor)?;
697 reg.visit_with(visitor)
699 ty::Tuple(ts) => ts.visit_with(visitor),
700 ty::FnDef(_, substs) => substs.visit_with(visitor),
701 ty::FnPtr(ref f) => f.visit_with(visitor),
702 ty::Ref(r, ty, _) => {
703 r.visit_with(visitor)?;
704 ty.visit_with(visitor)
706 ty::Generator(_did, ref substs, _) => substs.visit_with(visitor),
707 ty::GeneratorWitness(ref types) => types.visit_with(visitor),
708 ty::GeneratorWitnessMIR(_did, ref substs) => substs.visit_with(visitor),
709 ty::Closure(_did, ref substs) => substs.visit_with(visitor),
710 ty::Alias(_, ref data) => data.visit_with(visitor),
721 | ty::Placeholder(..)
724 | ty::Foreign(..) => ControlFlow::Continue(()),
729 impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> {
730 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
731 folder.try_fold_region(self)
735 impl<'tcx> TypeVisitable<'tcx> for ty::Region<'tcx> {
736 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
737 visitor.visit_region(*self)
741 impl<'tcx> TypeSuperFoldable<'tcx> for ty::Region<'tcx> {
742 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
745 ) -> Result<Self, F::Error> {
750 impl<'tcx> TypeSuperVisitable<'tcx> for ty::Region<'tcx> {
751 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
752 ControlFlow::Continue(())
756 impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
757 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
758 folder.try_fold_predicate(self)
762 impl<'tcx> TypeVisitable<'tcx> for ty::Predicate<'tcx> {
763 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
764 visitor.visit_predicate(*self)
768 fn has_vars_bound_at_or_above(&self, binder: ty::DebruijnIndex) -> bool {
769 self.outer_exclusive_binder() > binder
773 fn has_type_flags(&self, flags: ty::TypeFlags) -> bool {
774 self.flags().intersects(flags)
778 impl<'tcx> TypeSuperFoldable<'tcx> for ty::Predicate<'tcx> {
779 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
782 ) -> Result<Self, F::Error> {
783 let new = self.kind().try_fold_with(folder)?;
784 Ok(folder.tcx().reuse_or_mk_predicate(self, new))
788 impl<'tcx> TypeSuperVisitable<'tcx> for ty::Predicate<'tcx> {
789 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
790 self.kind().visit_with(visitor)
794 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
795 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
796 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_predicates(v))
800 impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
801 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
802 self.try_map_id(|x| x.try_fold_with(folder))
806 impl<'tcx, T: TypeVisitable<'tcx>, I: Idx> TypeVisitable<'tcx> for IndexVec<I, T> {
807 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
808 self.iter().try_for_each(|t| t.visit_with(visitor))
812 impl<'tcx> TypeFoldable<'tcx> for ty::Const<'tcx> {
813 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
814 folder.try_fold_const(self)
818 impl<'tcx> TypeVisitable<'tcx> for ty::Const<'tcx> {
819 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
820 visitor.visit_const(*self)
824 impl<'tcx> TypeSuperFoldable<'tcx> for ty::Const<'tcx> {
825 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
828 ) -> Result<Self, F::Error> {
829 let ty = self.ty().try_fold_with(folder)?;
830 let kind = self.kind().try_fold_with(folder)?;
831 if ty != self.ty() || kind != self.kind() {
832 Ok(folder.tcx().mk_const(kind, ty))
839 impl<'tcx> TypeSuperVisitable<'tcx> for ty::Const<'tcx> {
840 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
841 self.ty().visit_with(visitor)?;
842 self.kind().visit_with(visitor)
846 impl<'tcx> TypeFoldable<'tcx> for InferConst<'tcx> {
847 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, _folder: &mut F) -> Result<Self, F::Error> {
852 impl<'tcx> TypeVisitable<'tcx> for InferConst<'tcx> {
853 fn visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
854 ControlFlow::Continue(())
858 impl<'tcx> TypeSuperVisitable<'tcx> for ty::UnevaluatedConst<'tcx> {
859 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
860 self.substs.visit_with(visitor)
864 impl<'tcx> TypeVisitable<'tcx> for TyAndLayout<'tcx, Ty<'tcx>> {
865 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
866 visitor.visit_ty(self.ty)