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<'tcx> fmt::Debug for GenericArg<'tcx> {
188 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
189 match self.kind() {
190 GenericArgKind::Lifetime(lt) => lt.fmt(f),
191 GenericArgKind::Type(ty) => ty.fmt(f),
192 GenericArgKind::Const(ct) => ct.fmt(f),
193 }
194 }
195}
196
197impl<'tcx> fmt::Debug for Region<'tcx> {
198 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
199 write!(f, "{:?}", self.kind())
200 }
201}
202
203TrivialLiftImpls! {
212 (),
213 bool,
214 usize,
215 u64,
216 crate::mir::Promoted,
218 crate::mir::interpret::AllocId,
219 crate::mir::interpret::Scalar,
220 crate::ty::ParamConst,
221 rustc_abi::ExternAbi,
222 rustc_abi::Size,
223 rustc_hir::Safety,
224 rustc_middle::mir::ConstValue,
225 rustc_type_ir::BoundConstness,
226 rustc_type_ir::PredicatePolarity,
227 }
229
230TrivialTypeTraversalImpls! {
234 crate::infer::canonical::Certainty,
236 crate::mir::BasicBlock,
237 crate::mir::BindingForm<'tcx>,
238 crate::mir::BlockTailInfo,
239 crate::mir::BorrowKind,
240 crate::mir::CastKind,
241 crate::mir::ConstValue,
242 crate::mir::CoroutineSavedLocal,
243 crate::mir::FakeReadCause,
244 crate::mir::Local,
245 crate::mir::MirPhase,
246 crate::mir::Promoted,
247 crate::mir::RawPtrKind,
248 crate::mir::RetagKind,
249 crate::mir::SourceInfo,
250 crate::mir::SourceScope,
251 crate::mir::SourceScopeLocalData,
252 crate::mir::SwitchTargets,
253 crate::traits::IsConstable,
254 crate::traits::OverflowError,
255 crate::ty::AdtKind,
256 crate::ty::AssocItem,
257 crate::ty::AssocKind,
258 crate::ty::BoundRegion,
259 crate::ty::ScalarInt,
260 crate::ty::UserTypeAnnotationIndex,
261 crate::ty::abstract_const::NotConstEvaluatable,
262 crate::ty::adjustment::AutoBorrowMutability,
263 crate::ty::adjustment::PointerCoercion,
264 rustc_abi::FieldIdx,
265 rustc_abi::VariantIdx,
266 rustc_ast::InlineAsmOptions,
267 rustc_ast::InlineAsmTemplatePiece,
268 rustc_hir::CoroutineKind,
269 rustc_hir::HirId,
270 rustc_hir::MatchSource,
271 rustc_hir::RangeEnd,
272 rustc_hir::def_id::LocalDefId,
273 rustc_span::Ident,
274 rustc_span::Span,
275 rustc_span::Symbol,
276 rustc_target::asm::InlineAsmRegOrRegClass,
277 }
279
280TrivialTypeTraversalAndLiftImpls! {
285 crate::mir::RuntimeChecks,
287 crate::ty::BoundTy,
288 crate::ty::ParamTy,
289 crate::ty::instance::ReifyReason,
290 rustc_hir::def_id::DefId,
291 }
293
294impl<'tcx> Lift<TyCtxt<'tcx>> for PhantomData<&()> {
298 type Lifted = PhantomData<&'tcx ()>;
299 fn lift_to_interner(self, _: TyCtxt<'tcx>) -> Option<Self::Lifted> {
300 Some(PhantomData)
301 }
302}
303
304impl<'tcx, T: Lift<TyCtxt<'tcx>>> Lift<TyCtxt<'tcx>> for Option<T> {
305 type Lifted = Option<T::Lifted>;
306 fn lift_to_interner(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
307 Some(match self {
308 Some(x) => Some(tcx.lift(x)?),
309 None => None,
310 })
311 }
312}
313
314impl<'a, 'tcx> Lift<TyCtxt<'tcx>> for Term<'a> {
315 type Lifted = ty::Term<'tcx>;
316 fn lift_to_interner(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
317 match self.kind() {
318 TermKind::Ty(ty) => tcx.lift(ty).map(Into::into),
319 TermKind::Const(c) => tcx.lift(c).map(Into::into),
320 }
321 }
322}
323
324impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ty::AdtDef<'tcx> {
328 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, _visitor: &mut V) -> V::Result {
329 V::Result::output()
330 }
331}
332
333impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for Pattern<'tcx> {
334 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
335 self,
336 folder: &mut F,
337 ) -> Result<Self, F::Error> {
338 let pat = (*self).clone().try_fold_with(folder)?;
339 Ok(if pat == *self { self } else { folder.cx().mk_pat(pat) })
340 }
341
342 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
343 let pat = (*self).clone().fold_with(folder);
344 if pat == *self { self } else { folder.cx().mk_pat(pat) }
345 }
346}
347
348impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for Pattern<'tcx> {
349 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
350 (**self).visit_with(visitor)
351 }
352}
353
354impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for Ty<'tcx> {
355 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
356 self,
357 folder: &mut F,
358 ) -> Result<Self, F::Error> {
359 folder.try_fold_ty(self)
360 }
361
362 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
363 folder.fold_ty(self)
364 }
365}
366
367impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for Ty<'tcx> {
368 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
369 visitor.visit_ty(*self)
370 }
371}
372
373impl<'tcx> TypeSuperFoldable<TyCtxt<'tcx>> for Ty<'tcx> {
374 fn try_super_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
375 self,
376 folder: &mut F,
377 ) -> Result<Self, F::Error> {
378 let kind = match *self.kind() {
379 ty::RawPtr(ty, mutbl) => ty::RawPtr(ty.try_fold_with(folder)?, mutbl),
380 ty::Array(typ, sz) => ty::Array(typ.try_fold_with(folder)?, sz.try_fold_with(folder)?),
381 ty::Slice(typ) => ty::Slice(typ.try_fold_with(folder)?),
382 ty::Adt(tid, args) => ty::Adt(tid, args.try_fold_with(folder)?),
383 ty::Dynamic(trait_ty, region) => {
384 ty::Dynamic(trait_ty.try_fold_with(folder)?, region.try_fold_with(folder)?)
385 }
386 ty::Tuple(ts) => ty::Tuple(ts.try_fold_with(folder)?),
387 ty::FnDef(def_id, args) => ty::FnDef(def_id, args.try_fold_with(folder)?),
388 ty::FnPtr(sig_tys, hdr) => ty::FnPtr(sig_tys.try_fold_with(folder)?, hdr),
389 ty::UnsafeBinder(f) => ty::UnsafeBinder(f.try_fold_with(folder)?),
390 ty::Ref(r, ty, mutbl) => {
391 ty::Ref(r.try_fold_with(folder)?, ty.try_fold_with(folder)?, mutbl)
392 }
393 ty::Coroutine(did, args) => ty::Coroutine(did, args.try_fold_with(folder)?),
394 ty::CoroutineWitness(did, args) => {
395 ty::CoroutineWitness(did, args.try_fold_with(folder)?)
396 }
397 ty::Closure(did, args) => ty::Closure(did, args.try_fold_with(folder)?),
398 ty::CoroutineClosure(did, args) => {
399 ty::CoroutineClosure(did, args.try_fold_with(folder)?)
400 }
401 ty::Alias(kind, data) => ty::Alias(kind, data.try_fold_with(folder)?),
402 ty::Pat(ty, pat) => ty::Pat(ty.try_fold_with(folder)?, pat.try_fold_with(folder)?),
403
404 ty::Bool
405 | ty::Char
406 | ty::Str
407 | ty::Int(_)
408 | ty::Uint(_)
409 | ty::Float(_)
410 | ty::Error(_)
411 | ty::Infer(_)
412 | ty::Param(..)
413 | ty::Bound(..)
414 | ty::Placeholder(..)
415 | ty::Never
416 | ty::Foreign(..) => return Ok(self),
417 };
418
419 Ok(if *self.kind() == kind { self } else { folder.cx().mk_ty_from_kind(kind) })
420 }
421
422 fn super_fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
423 let kind = match *self.kind() {
424 ty::RawPtr(ty, mutbl) => ty::RawPtr(ty.fold_with(folder), mutbl),
425 ty::Array(typ, sz) => ty::Array(typ.fold_with(folder), sz.fold_with(folder)),
426 ty::Slice(typ) => ty::Slice(typ.fold_with(folder)),
427 ty::Adt(tid, args) => ty::Adt(tid, args.fold_with(folder)),
428 ty::Dynamic(trait_ty, region) => {
429 ty::Dynamic(trait_ty.fold_with(folder), region.fold_with(folder))
430 }
431 ty::Tuple(ts) => ty::Tuple(ts.fold_with(folder)),
432 ty::FnDef(def_id, args) => ty::FnDef(def_id, args.fold_with(folder)),
433 ty::FnPtr(sig_tys, hdr) => ty::FnPtr(sig_tys.fold_with(folder), hdr),
434 ty::UnsafeBinder(f) => ty::UnsafeBinder(f.fold_with(folder)),
435 ty::Ref(r, ty, mutbl) => ty::Ref(r.fold_with(folder), ty.fold_with(folder), mutbl),
436 ty::Coroutine(did, args) => ty::Coroutine(did, args.fold_with(folder)),
437 ty::CoroutineWitness(did, args) => ty::CoroutineWitness(did, args.fold_with(folder)),
438 ty::Closure(did, args) => ty::Closure(did, args.fold_with(folder)),
439 ty::CoroutineClosure(did, args) => ty::CoroutineClosure(did, args.fold_with(folder)),
440 ty::Alias(kind, data) => ty::Alias(kind, data.fold_with(folder)),
441 ty::Pat(ty, pat) => ty::Pat(ty.fold_with(folder), pat.fold_with(folder)),
442
443 ty::Bool
444 | ty::Char
445 | ty::Str
446 | ty::Int(_)
447 | ty::Uint(_)
448 | ty::Float(_)
449 | ty::Error(_)
450 | ty::Infer(_)
451 | ty::Param(..)
452 | ty::Bound(..)
453 | ty::Placeholder(..)
454 | ty::Never
455 | ty::Foreign(..) => return self,
456 };
457
458 if *self.kind() == kind { self } else { folder.cx().mk_ty_from_kind(kind) }
459 }
460}
461
462impl<'tcx> TypeSuperVisitable<TyCtxt<'tcx>> for Ty<'tcx> {
463 fn super_visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
464 match self.kind() {
465 ty::RawPtr(ty, _mutbl) => ty.visit_with(visitor),
466 ty::Array(typ, sz) => {
467 try_visit!(typ.visit_with(visitor));
468 sz.visit_with(visitor)
469 }
470 ty::Slice(typ) => typ.visit_with(visitor),
471 ty::Adt(_, args) => args.visit_with(visitor),
472 ty::Dynamic(trait_ty, reg) => {
473 try_visit!(trait_ty.visit_with(visitor));
474 reg.visit_with(visitor)
475 }
476 ty::Tuple(ts) => ts.visit_with(visitor),
477 ty::FnDef(_, args) => args.visit_with(visitor),
478 ty::FnPtr(sig_tys, _) => sig_tys.visit_with(visitor),
479 ty::UnsafeBinder(f) => f.visit_with(visitor),
480 ty::Ref(r, ty, _) => {
481 try_visit!(r.visit_with(visitor));
482 ty.visit_with(visitor)
483 }
484 ty::Coroutine(_did, args) => args.visit_with(visitor),
485 ty::CoroutineWitness(_did, args) => args.visit_with(visitor),
486 ty::Closure(_did, args) => args.visit_with(visitor),
487 ty::CoroutineClosure(_did, args) => args.visit_with(visitor),
488 ty::Alias(_, data) => data.visit_with(visitor),
489
490 ty::Pat(ty, pat) => {
491 try_visit!(ty.visit_with(visitor));
492 pat.visit_with(visitor)
493 }
494
495 ty::Error(guar) => guar.visit_with(visitor),
496
497 ty::Bool
498 | ty::Char
499 | ty::Str
500 | ty::Int(_)
501 | ty::Uint(_)
502 | ty::Float(_)
503 | ty::Infer(_)
504 | ty::Bound(..)
505 | ty::Placeholder(..)
506 | ty::Param(..)
507 | ty::Never
508 | ty::Foreign(..) => V::Result::output(),
509 }
510 }
511}
512
513impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for ty::Region<'tcx> {
514 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
515 self,
516 folder: &mut F,
517 ) -> Result<Self, F::Error> {
518 folder.try_fold_region(self)
519 }
520
521 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
522 folder.fold_region(self)
523 }
524}
525
526impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ty::Region<'tcx> {
527 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
528 visitor.visit_region(*self)
529 }
530}
531
532impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for ty::Predicate<'tcx> {
533 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
534 self,
535 folder: &mut F,
536 ) -> Result<Self, F::Error> {
537 folder.try_fold_predicate(self)
538 }
539
540 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
541 folder.fold_predicate(self)
542 }
543}
544
545impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for ty::Clause<'tcx> {
547 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
548 self,
549 folder: &mut F,
550 ) -> Result<Self, F::Error> {
551 Ok(folder.try_fold_predicate(self.as_predicate())?.expect_clause())
552 }
553
554 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
555 folder.fold_predicate(self.as_predicate()).expect_clause()
556 }
557}
558
559impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for ty::Clauses<'tcx> {
560 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
561 self,
562 folder: &mut F,
563 ) -> Result<Self, F::Error> {
564 folder.try_fold_clauses(self)
565 }
566
567 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
568 folder.fold_clauses(self)
569 }
570}
571
572impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ty::Predicate<'tcx> {
573 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
574 visitor.visit_predicate(*self)
575 }
576}
577
578impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ty::Clause<'tcx> {
579 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
580 visitor.visit_predicate(self.as_predicate())
581 }
582}
583
584impl<'tcx> TypeSuperFoldable<TyCtxt<'tcx>> for ty::Predicate<'tcx> {
585 fn try_super_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
586 self,
587 folder: &mut F,
588 ) -> Result<Self, F::Error> {
589 let new = self.kind().try_fold_with(folder)?;
596 Ok(folder.cx().reuse_or_mk_predicate(self, new))
597 }
598
599 fn super_fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
600 let new = self.kind().fold_with(folder);
602 folder.cx().reuse_or_mk_predicate(self, new)
603 }
604}
605
606impl<'tcx> TypeSuperVisitable<TyCtxt<'tcx>> for ty::Predicate<'tcx> {
607 fn super_visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
608 self.kind().visit_with(visitor)
610 }
611}
612
613impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ty::Clauses<'tcx> {
614 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
615 visitor.visit_clauses(self)
616 }
617}
618
619impl<'tcx> TypeSuperVisitable<TyCtxt<'tcx>> for ty::Clauses<'tcx> {
620 fn super_visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
621 self.as_slice().visit_with(visitor)
622 }
623}
624
625impl<'tcx> TypeSuperFoldable<TyCtxt<'tcx>> for ty::Clauses<'tcx> {
626 fn try_super_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
627 self,
628 folder: &mut F,
629 ) -> Result<Self, F::Error> {
630 ty::util::try_fold_list(self, folder, |tcx, v| tcx.mk_clauses(v))
631 }
632
633 fn super_fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
634 ty::util::fold_list(self, folder, |tcx, v| tcx.mk_clauses(v))
635 }
636}
637
638impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for ty::Const<'tcx> {
639 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
640 self,
641 folder: &mut F,
642 ) -> Result<Self, F::Error> {
643 folder.try_fold_const(self)
644 }
645
646 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
647 folder.fold_const(self)
648 }
649}
650
651impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ty::Const<'tcx> {
652 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
653 visitor.visit_const(*self)
654 }
655}
656
657impl<'tcx> TypeSuperFoldable<TyCtxt<'tcx>> for ty::Const<'tcx> {
658 fn try_super_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
659 self,
660 folder: &mut F,
661 ) -> Result<Self, F::Error> {
662 let kind = match self.kind() {
663 ConstKind::Unevaluated(uv) => ConstKind::Unevaluated(uv.try_fold_with(folder)?),
664 ConstKind::Value(v) => ConstKind::Value(v.try_fold_with(folder)?),
665 ConstKind::Expr(e) => ConstKind::Expr(e.try_fold_with(folder)?),
666
667 ConstKind::Param(_)
668 | ConstKind::Infer(_)
669 | ConstKind::Bound(..)
670 | ConstKind::Placeholder(_)
671 | ConstKind::Error(_) => return Ok(self),
672 };
673 if kind != self.kind() { Ok(folder.cx().mk_ct_from_kind(kind)) } else { Ok(self) }
674 }
675
676 fn super_fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
677 let kind = match self.kind() {
678 ConstKind::Unevaluated(uv) => ConstKind::Unevaluated(uv.fold_with(folder)),
679 ConstKind::Value(v) => ConstKind::Value(v.fold_with(folder)),
680 ConstKind::Expr(e) => ConstKind::Expr(e.fold_with(folder)),
681
682 ConstKind::Param(_)
683 | ConstKind::Infer(_)
684 | ConstKind::Bound(..)
685 | ConstKind::Placeholder(_)
686 | ConstKind::Error(_) => return self,
687 };
688 if kind != self.kind() { folder.cx().mk_ct_from_kind(kind) } else { self }
689 }
690}
691
692impl<'tcx> TypeSuperVisitable<TyCtxt<'tcx>> for ty::Const<'tcx> {
693 fn super_visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
694 match self.kind() {
695 ConstKind::Unevaluated(uv) => uv.visit_with(visitor),
696 ConstKind::Value(v) => v.visit_with(visitor),
697 ConstKind::Expr(e) => e.visit_with(visitor),
698 ConstKind::Error(e) => e.visit_with(visitor),
699
700 ConstKind::Param(_)
701 | ConstKind::Infer(_)
702 | ConstKind::Bound(..)
703 | ConstKind::Placeholder(_) => V::Result::output(),
704 }
705 }
706}
707
708impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ty::ValTree<'tcx> {
709 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
710 let inner: &ty::ValTreeKind<TyCtxt<'tcx>> = &*self;
711 inner.visit_with(visitor)
712 }
713}
714
715impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for ty::ValTree<'tcx> {
716 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
717 self,
718 folder: &mut F,
719 ) -> Result<Self, F::Error> {
720 let inner: &ty::ValTreeKind<TyCtxt<'tcx>> = &*self;
721 let new_inner = inner.clone().try_fold_with(folder)?;
722
723 if inner == &new_inner {
724 Ok(self)
725 } else {
726 let valtree = folder.cx().intern_valtree(new_inner);
727 Ok(valtree)
728 }
729 }
730
731 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
732 let inner: &ty::ValTreeKind<TyCtxt<'tcx>> = &*self;
733 let new_inner = inner.clone().fold_with(folder);
734
735 if inner == &new_inner { self } else { folder.cx().intern_valtree(new_inner) }
736 }
737}
738
739impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for rustc_span::ErrorGuaranteed {
740 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
741 visitor.visit_error(*self)
742 }
743}
744
745impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for rustc_span::ErrorGuaranteed {
746 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
747 self,
748 _folder: &mut F,
749 ) -> Result<Self, F::Error> {
750 Ok(self)
751 }
752
753 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, _folder: &mut F) -> Self {
754 self
755 }
756}
757
758impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for TyAndLayout<'tcx, Ty<'tcx>> {
759 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
760 visitor.visit_ty(self.ty)
761 }
762}
763
764impl<'tcx, T: TypeVisitable<TyCtxt<'tcx>> + Debug + Clone> TypeVisitable<TyCtxt<'tcx>>
765 for Spanned<T>
766{
767 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
768 try_visit!(self.node.visit_with(visitor));
769 self.span.visit_with(visitor)
770 }
771}
772
773impl<'tcx, T: TypeFoldable<TyCtxt<'tcx>> + Debug + Clone> TypeFoldable<TyCtxt<'tcx>>
774 for Spanned<T>
775{
776 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
777 self,
778 folder: &mut F,
779 ) -> Result<Self, F::Error> {
780 Ok(Spanned {
781 node: self.node.try_fold_with(folder)?,
782 span: self.span.try_fold_with(folder)?,
783 })
784 }
785
786 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
787 Spanned { node: self.node.fold_with(folder), span: self.span.fold_with(folder) }
788 }
789}
790
791impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for &'tcx ty::List<LocalDefId> {
792 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
793 self,
794 _folder: &mut F,
795 ) -> Result<Self, F::Error> {
796 Ok(self)
797 }
798
799 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, _folder: &mut F) -> Self {
800 self
801 }
802}
803
804macro_rules! list_fold {
805 ($($ty:ty : $mk:ident),+ $(,)?) => {
806 $(
807 impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for $ty {
808 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
809 self,
810 folder: &mut F,
811 ) -> Result<Self, F::Error> {
812 ty::util::try_fold_list(self, folder, |tcx, v| tcx.$mk(v))
813 }
814
815 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(
816 self,
817 folder: &mut F,
818 ) -> Self {
819 ty::util::fold_list(self, folder, |tcx, v| tcx.$mk(v))
820 }
821 }
822 )*
823 }
824}
825
826list_fold! {
827 &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>> : mk_poly_existential_predicates,
828 &'tcx ty::List<(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)>: mk_predefined_opaques_in_body,
829 &'tcx ty::List<PlaceElem<'tcx>> : mk_place_elems,
830 &'tcx ty::List<ty::Pattern<'tcx>> : mk_patterns,
831 &'tcx ty::List<ty::ArgOutlivesPredicate<'tcx>> : mk_outlives,
832}