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