1use std::fmt::{self, Debug};
7use std::marker::PhantomData;
8
9use rustc_abi::TyAndLayout;
10use rustc_hir::def::Namespace;
11use rustc_hir::def_id::LocalDefId;
12use rustc_span::source_map::Spanned;
13use rustc_type_ir::{ConstKind, TypeFolder, VisitorResult, try_visit};
14
15use super::{GenericArg, GenericArgKind, Pattern, Region};
16use crate::mir::PlaceElem;
17use crate::ty::print::{FmtPrinter, Printer, with_no_trimmed_paths};
18use crate::ty::{
19 self, FallibleTypeFolder, Lift, Term, TermKind, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable,
20 TypeSuperVisitable, TypeVisitable, TypeVisitor,
21};
22
23impl fmt::Debug for ty::TraitDef {
24 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25 ty::tls::with(|tcx| {
26 with_no_trimmed_paths!({
27 let s = FmtPrinter::print_string(tcx, Namespace::TypeNS, |p| {
28 p.print_def_path(self.def_id, &[])
29 })?;
30 f.write_str(&s)
31 })
32 })
33 }
34}
35
36impl<'tcx> fmt::Debug for ty::AdtDef<'tcx> {
37 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38 ty::tls::with(|tcx| {
39 with_no_trimmed_paths!({
40 let s = FmtPrinter::print_string(tcx, Namespace::TypeNS, |p| {
41 p.print_def_path(self.did(), &[])
42 })?;
43 f.write_str(&s)
44 })
45 })
46 }
47}
48
49impl 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)
53 }
54}
55
56impl<'tcx> fmt::Debug for ty::adjustment::Adjustment<'tcx> {
57 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58 write!(f, "{:?} -> {}", self.kind, self.target)
59 }
60}
61
62impl<'tcx> fmt::Debug for ty::adjustment::PatAdjustment<'tcx> {
63 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64 write!(f, "{} -> {:?}", self.source, self.kind)
65 }
66}
67
68impl fmt::Debug for ty::BoundRegionKind {
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70 match *self {
71 ty::BoundRegionKind::Anon => write!(f, "BrAnon"),
72 ty::BoundRegionKind::NamedAnon(name) => {
73 write!(f, "BrNamedAnon({name})")
74 }
75 ty::BoundRegionKind::Named(did) => {
76 write!(f, "BrNamed({did:?})")
77 }
78 ty::BoundRegionKind::ClosureEnv => write!(f, "BrEnv"),
79 }
80 }
81}
82
83impl fmt::Debug for ty::LateParamRegion {
84 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85 write!(f, "ReLateParam({:?}, {:?})", self.scope, self.kind)
86 }
87}
88
89impl fmt::Debug for ty::LateParamRegionKind {
90 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91 match *self {
92 ty::LateParamRegionKind::Anon(idx) => write!(f, "LateAnon({idx})"),
93 ty::LateParamRegionKind::NamedAnon(idx, name) => {
94 write!(f, "LateNamedAnon({idx:?}, {name})")
95 }
96 ty::LateParamRegionKind::Named(did) => {
97 write!(f, "LateNamed({did:?})")
98 }
99 ty::LateParamRegionKind::ClosureEnv => write!(f, "LateEnv"),
100 }
101 }
102}
103
104impl<'tcx> fmt::Debug for Ty<'tcx> {
105 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106 with_no_trimmed_paths!(fmt::Debug::fmt(self.kind(), f))
107 }
108}
109
110impl fmt::Debug for ty::ParamTy {
111 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112 write!(f, "{}/#{}", self.name, self.index)
113 }
114}
115
116impl fmt::Debug for ty::ParamConst {
117 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118 write!(f, "{}/#{}", self.name, self.index)
119 }
120}
121
122impl<'tcx> fmt::Debug for ty::Predicate<'tcx> {
123 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
124 write!(f, "{:?}", self.kind())
125 }
126}
127
128impl<'tcx> fmt::Debug for ty::Clause<'tcx> {
129 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
130 write!(f, "{:?}", self.kind())
131 }
132}
133
134impl<'tcx> fmt::Debug for ty::consts::Expr<'tcx> {
135 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
136 match self.kind {
137 ty::ExprKind::Binop(op) => {
138 let (lhs_ty, rhs_ty, lhs, rhs) = self.binop_args();
139 write!(f, "({op:?}: ({:?}: {:?}), ({:?}: {:?}))", lhs, lhs_ty, rhs, rhs_ty,)
140 }
141 ty::ExprKind::UnOp(op) => {
142 let (rhs_ty, rhs) = self.unop_args();
143 write!(f, "({op:?}: ({:?}: {:?}))", rhs, rhs_ty)
144 }
145 ty::ExprKind::FunctionCall => {
146 let (func_ty, func, args) = self.call_args();
147 let args = args.collect::<Vec<_>>();
148 write!(f, "({:?}: {:?})(", func, func_ty)?;
149 for arg in args.iter().rev().skip(1).rev() {
150 write!(f, "{:?}, ", arg)?;
151 }
152 if let Some(arg) = args.last() {
153 write!(f, "{:?}", arg)?;
154 }
155
156 write!(f, ")")
157 }
158 ty::ExprKind::Cast(kind) => {
159 let (value_ty, value, to_ty) = self.cast_args();
160 write!(f, "({kind:?}: ({:?}: {:?}), {:?})", value, value_ty, to_ty)
161 }
162 }
163 }
164}
165
166impl<'tcx> fmt::Debug for ty::Const<'tcx> {
167 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
168 if let ConstKind::Value(cv) = self.kind() {
170 write!(f, "{}", cv)
171 } else {
172 write!(f, "{:?}", self.kind())
174 }
175 }
176}
177
178impl fmt::Debug for ty::BoundTy {
179 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
180 match self.kind {
181 ty::BoundTyKind::Anon => write!(f, "{:?}", self.var),
182 ty::BoundTyKind::Param(def_id) => write!(f, "{def_id:?}"),
183 }
184 }
185}
186
187impl<T: fmt::Debug> fmt::Debug for ty::Placeholder<T> {
188 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
189 if self.universe == ty::UniverseIndex::ROOT {
190 write!(f, "!{:?}", self.bound)
191 } else {
192 write!(f, "!{}_{:?}", self.universe.index(), self.bound)
193 }
194 }
195}
196
197impl<'tcx> fmt::Debug for GenericArg<'tcx> {
198 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
199 match self.kind() {
200 GenericArgKind::Lifetime(lt) => lt.fmt(f),
201 GenericArgKind::Type(ty) => ty.fmt(f),
202 GenericArgKind::Const(ct) => ct.fmt(f),
203 }
204 }
205}
206
207impl<'tcx> fmt::Debug for Region<'tcx> {
208 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
209 write!(f, "{:?}", self.kind())
210 }
211}
212
213TrivialLiftImpls! {
222 (),
223 bool,
224 usize,
225 u64,
226 crate::mir::Promoted,
228 crate::mir::interpret::AllocId,
229 crate::mir::interpret::Scalar,
230 crate::ty::ParamConst,
231 rustc_abi::ExternAbi,
232 rustc_abi::Size,
233 rustc_hir::Safety,
234 rustc_middle::mir::ConstValue,
235 rustc_type_ir::BoundConstness,
236 rustc_type_ir::PredicatePolarity,
237 }
239
240TrivialTypeTraversalImpls! {
244 crate::infer::canonical::Certainty,
246 crate::mir::BasicBlock,
247 crate::mir::BindingForm<'tcx>,
248 crate::mir::BlockTailInfo,
249 crate::mir::BorrowKind,
250 crate::mir::CastKind,
251 crate::mir::ConstValue,
252 crate::mir::CoroutineSavedLocal,
253 crate::mir::FakeReadCause,
254 crate::mir::Local,
255 crate::mir::MirPhase,
256 crate::mir::NullOp<'tcx>,
257 crate::mir::Promoted,
258 crate::mir::RawPtrKind,
259 crate::mir::RetagKind,
260 crate::mir::SourceInfo,
261 crate::mir::SourceScope,
262 crate::mir::SourceScopeLocalData,
263 crate::mir::SwitchTargets,
264 crate::traits::IsConstable,
265 crate::traits::OverflowError,
266 crate::ty::AdtKind,
267 crate::ty::AssocItem,
268 crate::ty::AssocKind,
269 crate::ty::BoundRegion,
270 crate::ty::UserTypeAnnotationIndex,
271 crate::ty::ValTree<'tcx>,
272 crate::ty::abstract_const::NotConstEvaluatable,
273 crate::ty::adjustment::AutoBorrowMutability,
274 crate::ty::adjustment::PointerCoercion,
275 rustc_abi::FieldIdx,
276 rustc_abi::VariantIdx,
277 rustc_ast::InlineAsmOptions,
278 rustc_ast::InlineAsmTemplatePiece,
279 rustc_hir::CoroutineKind,
280 rustc_hir::HirId,
281 rustc_hir::MatchSource,
282 rustc_hir::RangeEnd,
283 rustc_hir::def_id::LocalDefId,
284 rustc_span::Ident,
285 rustc_span::Span,
286 rustc_span::Symbol,
287 rustc_target::asm::InlineAsmRegOrRegClass,
288 }
290
291TrivialTypeTraversalAndLiftImpls! {
296 crate::ty::ParamTy,
298 crate::ty::PlaceholderType,
299 crate::ty::instance::ReifyReason,
300 rustc_hir::def_id::DefId,
301 }
303
304impl<'tcx> Lift<TyCtxt<'tcx>> for PhantomData<&()> {
308 type Lifted = PhantomData<&'tcx ()>;
309 fn lift_to_interner(self, _: TyCtxt<'tcx>) -> Option<Self::Lifted> {
310 Some(PhantomData)
311 }
312}
313
314impl<'tcx, T: Lift<TyCtxt<'tcx>>> Lift<TyCtxt<'tcx>> for Option<T> {
315 type Lifted = Option<T::Lifted>;
316 fn lift_to_interner(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
317 Some(match self {
318 Some(x) => Some(tcx.lift(x)?),
319 None => None,
320 })
321 }
322}
323
324impl<'a, 'tcx> Lift<TyCtxt<'tcx>> for Term<'a> {
325 type Lifted = ty::Term<'tcx>;
326 fn lift_to_interner(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
327 match self.kind() {
328 TermKind::Ty(ty) => tcx.lift(ty).map(Into::into),
329 TermKind::Const(c) => tcx.lift(c).map(Into::into),
330 }
331 }
332}
333
334impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ty::AdtDef<'tcx> {
338 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, _visitor: &mut V) -> V::Result {
339 V::Result::output()
340 }
341}
342
343impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for Pattern<'tcx> {
344 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
345 self,
346 folder: &mut F,
347 ) -> Result<Self, F::Error> {
348 let pat = (*self).clone().try_fold_with(folder)?;
349 Ok(if pat == *self { self } else { folder.cx().mk_pat(pat) })
350 }
351
352 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
353 let pat = (*self).clone().fold_with(folder);
354 if pat == *self { self } else { folder.cx().mk_pat(pat) }
355 }
356}
357
358impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for Pattern<'tcx> {
359 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
360 (**self).visit_with(visitor)
361 }
362}
363
364impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for Ty<'tcx> {
365 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
366 self,
367 folder: &mut F,
368 ) -> Result<Self, F::Error> {
369 folder.try_fold_ty(self)
370 }
371
372 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
373 folder.fold_ty(self)
374 }
375}
376
377impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for Ty<'tcx> {
378 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
379 visitor.visit_ty(*self)
380 }
381}
382
383impl<'tcx> TypeSuperFoldable<TyCtxt<'tcx>> for Ty<'tcx> {
384 fn try_super_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
385 self,
386 folder: &mut F,
387 ) -> Result<Self, F::Error> {
388 let kind = match *self.kind() {
389 ty::RawPtr(ty, mutbl) => ty::RawPtr(ty.try_fold_with(folder)?, mutbl),
390 ty::Array(typ, sz) => ty::Array(typ.try_fold_with(folder)?, sz.try_fold_with(folder)?),
391 ty::Slice(typ) => ty::Slice(typ.try_fold_with(folder)?),
392 ty::Adt(tid, args) => ty::Adt(tid, args.try_fold_with(folder)?),
393 ty::Dynamic(trait_ty, region) => {
394 ty::Dynamic(trait_ty.try_fold_with(folder)?, region.try_fold_with(folder)?)
395 }
396 ty::Tuple(ts) => ty::Tuple(ts.try_fold_with(folder)?),
397 ty::FnDef(def_id, args) => ty::FnDef(def_id, args.try_fold_with(folder)?),
398 ty::FnPtr(sig_tys, hdr) => ty::FnPtr(sig_tys.try_fold_with(folder)?, hdr),
399 ty::UnsafeBinder(f) => ty::UnsafeBinder(f.try_fold_with(folder)?),
400 ty::Ref(r, ty, mutbl) => {
401 ty::Ref(r.try_fold_with(folder)?, ty.try_fold_with(folder)?, mutbl)
402 }
403 ty::Coroutine(did, args) => ty::Coroutine(did, args.try_fold_with(folder)?),
404 ty::CoroutineWitness(did, args) => {
405 ty::CoroutineWitness(did, args.try_fold_with(folder)?)
406 }
407 ty::Closure(did, args) => ty::Closure(did, args.try_fold_with(folder)?),
408 ty::CoroutineClosure(did, args) => {
409 ty::CoroutineClosure(did, args.try_fold_with(folder)?)
410 }
411 ty::Alias(kind, data) => ty::Alias(kind, data.try_fold_with(folder)?),
412 ty::Pat(ty, pat) => ty::Pat(ty.try_fold_with(folder)?, pat.try_fold_with(folder)?),
413
414 ty::Bool
415 | ty::Char
416 | ty::Str
417 | ty::Int(_)
418 | ty::Uint(_)
419 | ty::Float(_)
420 | ty::Error(_)
421 | ty::Infer(_)
422 | ty::Param(..)
423 | ty::Bound(..)
424 | ty::Placeholder(..)
425 | ty::Never
426 | ty::Foreign(..) => return Ok(self),
427 };
428
429 Ok(if *self.kind() == kind { self } else { folder.cx().mk_ty_from_kind(kind) })
430 }
431
432 fn super_fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
433 let kind = match *self.kind() {
434 ty::RawPtr(ty, mutbl) => ty::RawPtr(ty.fold_with(folder), mutbl),
435 ty::Array(typ, sz) => ty::Array(typ.fold_with(folder), sz.fold_with(folder)),
436 ty::Slice(typ) => ty::Slice(typ.fold_with(folder)),
437 ty::Adt(tid, args) => ty::Adt(tid, args.fold_with(folder)),
438 ty::Dynamic(trait_ty, region) => {
439 ty::Dynamic(trait_ty.fold_with(folder), region.fold_with(folder))
440 }
441 ty::Tuple(ts) => ty::Tuple(ts.fold_with(folder)),
442 ty::FnDef(def_id, args) => ty::FnDef(def_id, args.fold_with(folder)),
443 ty::FnPtr(sig_tys, hdr) => ty::FnPtr(sig_tys.fold_with(folder), hdr),
444 ty::UnsafeBinder(f) => ty::UnsafeBinder(f.fold_with(folder)),
445 ty::Ref(r, ty, mutbl) => ty::Ref(r.fold_with(folder), ty.fold_with(folder), mutbl),
446 ty::Coroutine(did, args) => ty::Coroutine(did, args.fold_with(folder)),
447 ty::CoroutineWitness(did, args) => ty::CoroutineWitness(did, args.fold_with(folder)),
448 ty::Closure(did, args) => ty::Closure(did, args.fold_with(folder)),
449 ty::CoroutineClosure(did, args) => ty::CoroutineClosure(did, args.fold_with(folder)),
450 ty::Alias(kind, data) => ty::Alias(kind, data.fold_with(folder)),
451 ty::Pat(ty, pat) => ty::Pat(ty.fold_with(folder), pat.fold_with(folder)),
452
453 ty::Bool
454 | ty::Char
455 | ty::Str
456 | ty::Int(_)
457 | ty::Uint(_)
458 | ty::Float(_)
459 | ty::Error(_)
460 | ty::Infer(_)
461 | ty::Param(..)
462 | ty::Bound(..)
463 | ty::Placeholder(..)
464 | ty::Never
465 | ty::Foreign(..) => return self,
466 };
467
468 if *self.kind() == kind { self } else { folder.cx().mk_ty_from_kind(kind) }
469 }
470}
471
472impl<'tcx> TypeSuperVisitable<TyCtxt<'tcx>> for Ty<'tcx> {
473 fn super_visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
474 match self.kind() {
475 ty::RawPtr(ty, _mutbl) => ty.visit_with(visitor),
476 ty::Array(typ, sz) => {
477 try_visit!(typ.visit_with(visitor));
478 sz.visit_with(visitor)
479 }
480 ty::Slice(typ) => typ.visit_with(visitor),
481 ty::Adt(_, args) => args.visit_with(visitor),
482 ty::Dynamic(trait_ty, reg) => {
483 try_visit!(trait_ty.visit_with(visitor));
484 reg.visit_with(visitor)
485 }
486 ty::Tuple(ts) => ts.visit_with(visitor),
487 ty::FnDef(_, args) => args.visit_with(visitor),
488 ty::FnPtr(sig_tys, _) => sig_tys.visit_with(visitor),
489 ty::UnsafeBinder(f) => f.visit_with(visitor),
490 ty::Ref(r, ty, _) => {
491 try_visit!(r.visit_with(visitor));
492 ty.visit_with(visitor)
493 }
494 ty::Coroutine(_did, args) => args.visit_with(visitor),
495 ty::CoroutineWitness(_did, args) => args.visit_with(visitor),
496 ty::Closure(_did, args) => args.visit_with(visitor),
497 ty::CoroutineClosure(_did, args) => args.visit_with(visitor),
498 ty::Alias(_, data) => data.visit_with(visitor),
499
500 ty::Pat(ty, pat) => {
501 try_visit!(ty.visit_with(visitor));
502 pat.visit_with(visitor)
503 }
504
505 ty::Error(guar) => guar.visit_with(visitor),
506
507 ty::Bool
508 | ty::Char
509 | ty::Str
510 | ty::Int(_)
511 | ty::Uint(_)
512 | ty::Float(_)
513 | ty::Infer(_)
514 | ty::Bound(..)
515 | ty::Placeholder(..)
516 | ty::Param(..)
517 | ty::Never
518 | ty::Foreign(..) => V::Result::output(),
519 }
520 }
521}
522
523impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for ty::Region<'tcx> {
524 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
525 self,
526 folder: &mut F,
527 ) -> Result<Self, F::Error> {
528 folder.try_fold_region(self)
529 }
530
531 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
532 folder.fold_region(self)
533 }
534}
535
536impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ty::Region<'tcx> {
537 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
538 visitor.visit_region(*self)
539 }
540}
541
542impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for ty::Predicate<'tcx> {
543 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
544 self,
545 folder: &mut F,
546 ) -> Result<Self, F::Error> {
547 folder.try_fold_predicate(self)
548 }
549
550 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
551 folder.fold_predicate(self)
552 }
553}
554
555impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for ty::Clause<'tcx> {
557 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
558 self,
559 folder: &mut F,
560 ) -> Result<Self, F::Error> {
561 Ok(folder.try_fold_predicate(self.as_predicate())?.expect_clause())
562 }
563
564 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
565 folder.fold_predicate(self.as_predicate()).expect_clause()
566 }
567}
568
569impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for ty::Clauses<'tcx> {
570 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
571 self,
572 folder: &mut F,
573 ) -> Result<Self, F::Error> {
574 folder.try_fold_clauses(self)
575 }
576
577 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
578 folder.fold_clauses(self)
579 }
580}
581
582impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ty::Predicate<'tcx> {
583 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
584 visitor.visit_predicate(*self)
585 }
586}
587
588impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ty::Clause<'tcx> {
589 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
590 visitor.visit_predicate(self.as_predicate())
591 }
592}
593
594impl<'tcx> TypeSuperFoldable<TyCtxt<'tcx>> for ty::Predicate<'tcx> {
595 fn try_super_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
596 self,
597 folder: &mut F,
598 ) -> Result<Self, F::Error> {
599 let new = self.kind().try_fold_with(folder)?;
600 Ok(folder.cx().reuse_or_mk_predicate(self, new))
601 }
602
603 fn super_fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
604 let new = self.kind().fold_with(folder);
605 folder.cx().reuse_or_mk_predicate(self, new)
606 }
607}
608
609impl<'tcx> TypeSuperVisitable<TyCtxt<'tcx>> for ty::Predicate<'tcx> {
610 fn super_visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
611 self.kind().visit_with(visitor)
612 }
613}
614
615impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ty::Clauses<'tcx> {
616 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
617 visitor.visit_clauses(self)
618 }
619}
620
621impl<'tcx> TypeSuperVisitable<TyCtxt<'tcx>> for ty::Clauses<'tcx> {
622 fn super_visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
623 self.as_slice().visit_with(visitor)
624 }
625}
626
627impl<'tcx> TypeSuperFoldable<TyCtxt<'tcx>> for ty::Clauses<'tcx> {
628 fn try_super_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
629 self,
630 folder: &mut F,
631 ) -> Result<Self, F::Error> {
632 ty::util::try_fold_list(self, folder, |tcx, v| tcx.mk_clauses(v))
633 }
634
635 fn super_fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
636 ty::util::fold_list(self, folder, |tcx, v| tcx.mk_clauses(v))
637 }
638}
639
640impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for ty::Const<'tcx> {
641 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
642 self,
643 folder: &mut F,
644 ) -> Result<Self, F::Error> {
645 folder.try_fold_const(self)
646 }
647
648 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
649 folder.fold_const(self)
650 }
651}
652
653impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ty::Const<'tcx> {
654 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
655 visitor.visit_const(*self)
656 }
657}
658
659impl<'tcx> TypeSuperFoldable<TyCtxt<'tcx>> for ty::Const<'tcx> {
660 fn try_super_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
661 self,
662 folder: &mut F,
663 ) -> Result<Self, F::Error> {
664 let kind = match self.kind() {
665 ConstKind::Unevaluated(uv) => ConstKind::Unevaluated(uv.try_fold_with(folder)?),
666 ConstKind::Value(v) => ConstKind::Value(v.try_fold_with(folder)?),
667 ConstKind::Expr(e) => ConstKind::Expr(e.try_fold_with(folder)?),
668
669 ConstKind::Param(_)
670 | ConstKind::Infer(_)
671 | ConstKind::Bound(..)
672 | ConstKind::Placeholder(_)
673 | ConstKind::Error(_) => return Ok(self),
674 };
675 if kind != self.kind() { Ok(folder.cx().mk_ct_from_kind(kind)) } else { Ok(self) }
676 }
677
678 fn super_fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
679 let kind = match self.kind() {
680 ConstKind::Unevaluated(uv) => ConstKind::Unevaluated(uv.fold_with(folder)),
681 ConstKind::Value(v) => ConstKind::Value(v.fold_with(folder)),
682 ConstKind::Expr(e) => ConstKind::Expr(e.fold_with(folder)),
683
684 ConstKind::Param(_)
685 | ConstKind::Infer(_)
686 | ConstKind::Bound(..)
687 | ConstKind::Placeholder(_)
688 | ConstKind::Error(_) => return self,
689 };
690 if kind != self.kind() { folder.cx().mk_ct_from_kind(kind) } else { self }
691 }
692}
693
694impl<'tcx> TypeSuperVisitable<TyCtxt<'tcx>> for ty::Const<'tcx> {
695 fn super_visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
696 match self.kind() {
697 ConstKind::Unevaluated(uv) => uv.visit_with(visitor),
698 ConstKind::Value(v) => v.visit_with(visitor),
699 ConstKind::Expr(e) => e.visit_with(visitor),
700 ConstKind::Error(e) => e.visit_with(visitor),
701
702 ConstKind::Param(_)
703 | ConstKind::Infer(_)
704 | ConstKind::Bound(..)
705 | ConstKind::Placeholder(_) => V::Result::output(),
706 }
707 }
708}
709
710impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for rustc_span::ErrorGuaranteed {
711 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
712 visitor.visit_error(*self)
713 }
714}
715
716impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for rustc_span::ErrorGuaranteed {
717 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
718 self,
719 _folder: &mut F,
720 ) -> Result<Self, F::Error> {
721 Ok(self)
722 }
723
724 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, _folder: &mut F) -> Self {
725 self
726 }
727}
728
729impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for TyAndLayout<'tcx, Ty<'tcx>> {
730 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
731 visitor.visit_ty(self.ty)
732 }
733}
734
735impl<'tcx, T: TypeVisitable<TyCtxt<'tcx>> + Debug + Clone> TypeVisitable<TyCtxt<'tcx>>
736 for Spanned<T>
737{
738 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
739 try_visit!(self.node.visit_with(visitor));
740 self.span.visit_with(visitor)
741 }
742}
743
744impl<'tcx, T: TypeFoldable<TyCtxt<'tcx>> + Debug + Clone> TypeFoldable<TyCtxt<'tcx>>
745 for Spanned<T>
746{
747 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
748 self,
749 folder: &mut F,
750 ) -> Result<Self, F::Error> {
751 Ok(Spanned {
752 node: self.node.try_fold_with(folder)?,
753 span: self.span.try_fold_with(folder)?,
754 })
755 }
756
757 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
758 Spanned { node: self.node.fold_with(folder), span: self.span.fold_with(folder) }
759 }
760}
761
762impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for &'tcx ty::List<LocalDefId> {
763 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
764 self,
765 _folder: &mut F,
766 ) -> Result<Self, F::Error> {
767 Ok(self)
768 }
769
770 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, _folder: &mut F) -> Self {
771 self
772 }
773}
774
775macro_rules! list_fold {
776 ($($ty:ty : $mk:ident),+ $(,)?) => {
777 $(
778 impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for $ty {
779 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
780 self,
781 folder: &mut F,
782 ) -> Result<Self, F::Error> {
783 ty::util::try_fold_list(self, folder, |tcx, v| tcx.$mk(v))
784 }
785
786 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(
787 self,
788 folder: &mut F,
789 ) -> Self {
790 ty::util::fold_list(self, folder, |tcx, v| tcx.$mk(v))
791 }
792 }
793 )*
794 }
795}
796
797list_fold! {
798 &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>> : mk_poly_existential_predicates,
799 &'tcx ty::List<PlaceElem<'tcx>> : mk_place_elems,
800 &'tcx ty::List<ty::Pattern<'tcx>> : mk_patterns,
801 &'tcx ty::List<ty::ArgOutlivesPredicate<'tcx>> : mk_outlives,
802}