blob: 8707b03e4b8f21276e72d9c21e1698257448c743 [file] [log] [blame]
Alan Egerton459e1422023-02-10 16:14:18 +00001//! This module contains implementations of the `Lift`, `TypeFoldable` and
2//! `TypeVisitable` traits for various types in the Rust compiler. Most are
3//! written by hand, though we've recently added some macros and proc-macros
4//! to help with the tedium.
Niko Matsakis27d58722017-11-11 13:04:36 -05005
Nicholas Nethercote84ac80f2024-07-29 08:13:50 +10006use std::fmt::{self, Debug};
Camille GILLOT9ff07122023-10-13 20:20:57 +00007use std::marker::PhantomData;
Nicholas Nethercote84ac80f2024-07-29 08:13:50 +10008
Jubilee Young236fe332024-11-02 19:33:00 -07009use rustc_abi::TyAndLayout;
Mazdak Farrokhzadebfd8672020-01-05 02:37:57 +010010use rustc_hir::def::Namespace;
Nicholas Nethercoted28678e2025-02-05 12:02:04 +110011use rustc_hir::def_id::LocalDefId;
Martin Nordholts924ea052024-01-12 08:22:05 +010012use rustc_span::source_map::Spanned;
Michael Gouletc774adc2025-04-14 15:10:43 +000013use rustc_type_ir::{ConstKind, TypeFolder, VisitorResult, try_visit};
Ariel Ben-Yehuda5f564fb2015-09-06 21:51:58 +030014
Nicholas Nethercote84ac80f2024-07-29 08:13:50 +100015use super::{GenericArg, GenericArgKind, Pattern, Region};
Nicholas Nethercoted28678e2025-02-05 12:02:04 +110016use crate::mir::PlaceElem;
Michael Gouletc682aa12024-09-22 19:05:04 -040017use crate::ty::print::{FmtPrinter, Printer, with_no_trimmed_paths};
Michael Gouletdc0cdfd2025-03-13 16:59:55 +000018use crate::ty::{
Michael Gouletc774adc2025-04-14 15:10:43 +000019 self, FallibleTypeFolder, Lift, Term, TermKind, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable,
20 TypeSuperVisitable, TypeVisitable, TypeVisitor,
Michael Gouletdc0cdfd2025-03-13 16:59:55 +000021};
Oli Scherer84acfe82023-02-02 13:57:36 +000022
Eduard-Mihai Burtescufb53bb92019-01-19 06:33:44 +020023impl fmt::Debug for ty::TraitDef {
24 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Eduard-Mihai Burtescu52b4f2d2019-01-25 12:11:50 +020025 ty::tls::with(|tcx| {
Mark Rousskovefb99d72022-02-18 16:15:29 -050026 with_no_trimmed_paths!({
Nicholas Nethercote1698c8e2025-08-01 10:41:11 +100027 let s = FmtPrinter::print_string(tcx, Namespace::TypeNS, |p| {
28 p.print_def_path(self.def_id, &[])
Nilstrieb5acf26b2023-10-17 19:46:14 +020029 })?;
30 f.write_str(&s)
Mark Rousskovefb99d72022-02-18 16:15:29 -050031 })
Eduard-Mihai Burtescufb53bb92019-01-19 06:33:44 +020032 })
33 }
34}
35
Nicholas Nethercoteca5525d2022-03-05 07:28:41 +110036impl<'tcx> fmt::Debug for ty::AdtDef<'tcx> {
Eduard-Mihai Burtescufb53bb92019-01-19 06:33:44 +020037 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Eduard-Mihai Burtescu52b4f2d2019-01-25 12:11:50 +020038 ty::tls::with(|tcx| {
Mark Rousskovefb99d72022-02-18 16:15:29 -050039 with_no_trimmed_paths!({
Nicholas Nethercote1698c8e2025-08-01 10:41:11 +100040 let s = FmtPrinter::print_string(tcx, Namespace::TypeNS, |p| {
41 p.print_def_path(self.did(), &[])
Nilstrieb5acf26b2023-10-17 19:46:14 +020042 })?;
43 f.write_str(&s)
Mark Rousskovefb99d72022-02-18 16:15:29 -050044 })
Eduard-Mihai Burtescufb53bb92019-01-19 06:33:44 +020045 })
46 }
47}
48
Eduard-Mihai Burtescufb53bb92019-01-19 06:33:44 +020049impl fmt::Debug for ty::UpvarId {
50 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Nicholas Nethercote256c27e2025-02-21 18:33:05 +110051 let name = ty::tls::with(|tcx| tcx.hir_name(self.var_path.hir_id));
Mark Rousskova06baa52019-12-22 17:42:04 -050052 write!(f, "UpvarId({:?};`{}`;{:?})", self.var_path.hir_id, name, self.closure_expr_id)
Eduard-Mihai Burtescufb53bb92019-01-19 06:33:44 +020053 }
54}
55
Aaron Hill070bf942021-12-15 19:32:30 -050056impl<'tcx> fmt::Debug for ty::adjustment::Adjustment<'tcx> {
Eduard-Mihai Burtescufb53bb92019-01-19 06:33:44 +020057 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58 write!(f, "{:?} -> {}", self.kind, self.target)
59 }
60}
61
diannef35eae72025-03-14 18:56:15 -070062impl<'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
lcnr15f21562023-11-14 13:13:27 +000068impl fmt::Debug for ty::LateParamRegion {
Eduard-Mihai Burtescufb53bb92019-01-19 06:33:44 +020069 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
lcnr085d9312024-12-06 14:46:28 +010070 write!(f, "ReLateParam({:?}, {:?})", self.scope, self.kind)
71 }
72}
73
74impl fmt::Debug for ty::LateParamRegionKind {
75 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76 match *self {
Nicholas Nethercotefadf9102025-04-04 16:50:56 +110077 ty::LateParamRegionKind::Anon(idx) => write!(f, "LateAnon({idx})"),
Michael Goulet42c9bfd2025-04-11 04:17:19 +000078 ty::LateParamRegionKind::NamedAnon(idx, name) => {
79 write!(f, "LateNamedAnon({idx:?}, {name})")
80 }
81 ty::LateParamRegionKind::Named(did) => {
82 write!(f, "LateNamed({did:?})")
lcnr085d9312024-12-06 14:46:28 +010083 }
Nicholas Nethercotefadf9102025-04-04 16:50:56 +110084 ty::LateParamRegionKind::ClosureEnv => write!(f, "LateEnv"),
lcnr085d9312024-12-06 14:46:28 +010085 }
Eduard-Mihai Burtescufb53bb92019-01-19 06:33:44 +020086 }
87}
88
Aaron Hill070bf942021-12-15 19:32:30 -050089impl<'tcx> fmt::Debug for Ty<'tcx> {
Eduard-Mihai Burtescufb53bb92019-01-19 06:33:44 +020090 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Nicholas Nethercote64ea8eb2023-09-07 15:14:40 +100091 with_no_trimmed_paths!(fmt::Debug::fmt(self.kind(), f))
Eduard-Mihai Burtescufb53bb92019-01-19 06:33:44 +020092 }
93}
94
95impl fmt::Debug for ty::ParamTy {
96 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
varkorc3694e52019-05-06 13:12:04 +010097 write!(f, "{}/#{}", self.name, self.index)
Eduard-Mihai Burtescufb53bb92019-01-19 06:33:44 +020098 }
99}
100
101impl fmt::Debug for ty::ParamConst {
102 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
103 write!(f, "{}/#{}", self.name, self.index)
104 }
105}
106
Aaron Hill070bf942021-12-15 19:32:30 -0500107impl<'tcx> fmt::Debug for ty::Predicate<'tcx> {
Bastian Kauschkef3164792020-05-11 22:06:41 +0200108 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Jack Huey3dea68d2021-01-07 11:20:28 -0500109 write!(f, "{:?}", self.kind())
Bastian Kauschkef3164792020-05-11 22:06:41 +0200110 }
111}
112
Michael Goulet21226ee2023-06-16 06:27:41 +0000113impl<'tcx> fmt::Debug for ty::Clause<'tcx> {
114 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
115 write!(f, "{:?}", self.kind())
116 }
117}
118
Boxy3fdb4432023-07-06 10:17:26 +0100119impl<'tcx> fmt::Debug for ty::consts::Expr<'tcx> {
120 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Michael Goulet0fc18e32024-06-06 10:08:55 -0400121 match self.kind {
Boxyf076dec2024-06-04 02:24:57 +0100122 ty::ExprKind::Binop(op) => {
Michael Goulet0fc18e32024-06-06 10:08:55 -0400123 let (lhs_ty, rhs_ty, lhs, rhs) = self.binop_args();
124 write!(f, "({op:?}: ({:?}: {:?}), ({:?}: {:?}))", lhs, lhs_ty, rhs, rhs_ty,)
Boxy3fdb4432023-07-06 10:17:26 +0100125 }
Boxyf076dec2024-06-04 02:24:57 +0100126 ty::ExprKind::UnOp(op) => {
Michael Goulet0fc18e32024-06-06 10:08:55 -0400127 let (rhs_ty, rhs) = self.unop_args();
128 write!(f, "({op:?}: ({:?}: {:?}))", rhs, rhs_ty)
Boxyf076dec2024-06-04 02:24:57 +0100129 }
130 ty::ExprKind::FunctionCall => {
Michael Goulet0fc18e32024-06-06 10:08:55 -0400131 let (func_ty, func, args) = self.call_args();
Boxyf076dec2024-06-04 02:24:57 +0100132 let args = args.collect::<Vec<_>>();
Michael Goulet0fc18e32024-06-06 10:08:55 -0400133 write!(f, "({:?}: {:?})(", func, func_ty)?;
Boxyf076dec2024-06-04 02:24:57 +0100134 for arg in args.iter().rev().skip(1).rev() {
Michael Goulet0fc18e32024-06-06 10:08:55 -0400135 write!(f, "{:?}, ", arg)?;
Boxy3fdb4432023-07-06 10:17:26 +0100136 }
137 if let Some(arg) = args.last() {
Michael Goulet0fc18e32024-06-06 10:08:55 -0400138 write!(f, "{:?}", arg)?;
Boxy3fdb4432023-07-06 10:17:26 +0100139 }
140
141 write!(f, ")")
142 }
Boxyf076dec2024-06-04 02:24:57 +0100143 ty::ExprKind::Cast(kind) => {
Michael Goulet0fc18e32024-06-06 10:08:55 -0400144 let (value_ty, value, to_ty) = self.cast_args();
145 write!(f, "({kind:?}: ({:?}: {:?}), {:?})", value, value_ty, to_ty)
Boxy3fdb4432023-07-06 10:17:26 +0100146 }
147 }
148 }
149}
150
Boxye16d71b2023-05-16 04:25:25 +0100151impl<'tcx> fmt::Debug for ty::Const<'tcx> {
152 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Ralf Jung5a3410a2023-09-16 08:36:28 +0200153 // If this is a value, we spend some effort to make it look nice.
Lukas Markeffsky885e0f12025-02-07 19:33:58 +0100154 if let ConstKind::Value(cv) = self.kind() {
Ralf Jungd61fdbf2025-07-28 18:16:47 +0200155 write!(f, "{}", cv)
156 } else {
157 // Fall back to something verbose.
158 write!(f, "{:?}", self.kind())
Ralf Jung5a3410a2023-09-16 08:36:28 +0200159 }
Boxye16d71b2023-05-16 04:25:25 +0100160 }
161}
162
Boxy3fdb4432023-07-06 10:17:26 +0100163impl<'tcx> fmt::Debug for GenericArg<'tcx> {
164 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Michael Goulet29c3bab2025-05-24 12:07:32 +0000165 match self.kind() {
Boxy3fdb4432023-07-06 10:17:26 +0100166 GenericArgKind::Lifetime(lt) => lt.fmt(f),
167 GenericArgKind::Type(ty) => ty.fmt(f),
168 GenericArgKind::Const(ct) => ct.fmt(f),
169 }
170 }
171}
Boxy3fdb4432023-07-06 10:17:26 +0100172
173impl<'tcx> fmt::Debug for Region<'tcx> {
174 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
175 write!(f, "{:?}", self.kind())
176 }
177}
Boxy3fdb4432023-07-06 10:17:26 +0100178
Niko Matsakis27d58722017-11-11 13:04:36 -0500179///////////////////////////////////////////////////////////////////////////
180// Atomic structs
181//
182// For things that don't carry any arena-allocated data (and are
Josh Sorefe09d0d22023-04-09 17:35:02 -0400183// copy...), just add them to one of these lists as appropriate.
Niko Matsakis27d58722017-11-11 13:04:36 -0500184
Alan Egerton459e1422023-02-10 16:14:18 +0000185// For things for which the type library provides traversal implementations
Nicholas Nethercoted28678e2025-02-05 12:02:04 +1100186// for all Interners, we only need to provide a Lift implementation.
Nicholas Nethercote6b1980f2023-09-14 12:03:56 +1000187TrivialLiftImpls! {
Nicholas Nethercoted28678e2025-02-05 12:02:04 +1100188 (),
189 bool,
190 usize,
191 u64,
Nicholas Nethercoted282a672025-02-05 14:54:13 +1100192 // tidy-alphabetical-start
Folkert de Vries1dfc8402025-05-20 20:23:47 +0200193 crate::mir::Promoted,
Nicholas Nethercoted28678e2025-02-05 12:02:04 +1100194 crate::mir::interpret::AllocId,
195 crate::mir::interpret::Scalar,
Nicholas Nethercote94cc5bb2025-07-30 14:29:28 +1000196 crate::ty::ParamConst,
Nicholas Nethercoted28678e2025-02-05 12:02:04 +1100197 rustc_abi::ExternAbi,
198 rustc_abi::Size,
199 rustc_hir::Safety,
Camille GILLOT0460c922025-07-03 18:41:12 +0000200 rustc_middle::mir::ConstValue,
Nicholas Nethercoted28678e2025-02-05 12:02:04 +1100201 rustc_type_ir::BoundConstness,
202 rustc_type_ir::PredicatePolarity,
Nicholas Nethercoted282a672025-02-05 14:54:13 +1100203 // tidy-alphabetical-end
Alan Egerton459e1422023-02-10 16:14:18 +0000204}
205
Nicholas Nethercoteabe2a682023-09-14 12:05:05 +1000206// For some things about which the type library does not know, or does not
207// provide any traversal implementations, we need to provide a traversal
208// implementation (only for TyCtxt<'_> interners).
209TrivialTypeTraversalImpls! {
Nicholas Nethercoted282a672025-02-05 14:54:13 +1100210 // tidy-alphabetical-start
Nicholas Nethercoted28678e2025-02-05 12:02:04 +1100211 crate::infer::canonical::Certainty,
212 crate::mir::BasicBlock,
213 crate::mir::BindingForm<'tcx>,
214 crate::mir::BlockTailInfo,
215 crate::mir::BorrowKind,
216 crate::mir::CastKind,
Camille GILLOT0460c922025-07-03 18:41:12 +0000217 crate::mir::ConstValue,
Nicholas Nethercoted28678e2025-02-05 12:02:04 +1100218 crate::mir::CoroutineSavedLocal,
219 crate::mir::FakeReadCause,
Mark Mansie957ed92019-02-05 11:20:45 -0600220 crate::mir::Local,
Nicholas Nethercoted28678e2025-02-05 12:02:04 +1100221 crate::mir::MirPhase,
Mark Mansie957ed92019-02-05 11:20:45 -0600222 crate::mir::Promoted,
Nicholas Nethercoted28678e2025-02-05 12:02:04 +1100223 crate::mir::RawPtrKind,
224 crate::mir::RetagKind,
225 crate::mir::SourceInfo,
226 crate::mir::SourceScope,
227 crate::mir::SourceScopeLocalData,
228 crate::mir::SwitchTargets,
229 crate::traits::IsConstable,
230 crate::traits::OverflowError,
Mark Mansie957ed92019-02-05 11:20:45 -0600231 crate::ty::AdtKind,
Camille GILLOT058e0212020-08-02 15:42:08 +0200232 crate::ty::AssocItem,
Michael Goulet3bbe95c2022-07-24 19:33:26 +0000233 crate::ty::AssocKind,
James Barford-Evans25c13652025-12-22 14:45:08 +0000234 crate::ty::BoundRegion<'tcx>,
235 crate::ty::BoundTy<'tcx>,
Boxy Uwu67228052025-11-19 19:30:09 +0000236 crate::ty::ScalarInt,
Nicholas Nethercoted28678e2025-02-05 12:02:04 +1100237 crate::ty::UserTypeAnnotationIndex,
Folkert de Vries1dfc8402025-05-20 20:23:47 +0200238 crate::ty::abstract_const::NotConstEvaluatable,
239 crate::ty::adjustment::AutoBorrowMutability,
240 crate::ty::adjustment::PointerCoercion,
Nicholas Nethercoted28678e2025-02-05 12:02:04 +1100241 rustc_abi::FieldIdx,
242 rustc_abi::VariantIdx,
243 rustc_ast::InlineAsmOptions,
244 rustc_ast::InlineAsmTemplatePiece,
245 rustc_hir::CoroutineKind,
246 rustc_hir::HirId,
247 rustc_hir::MatchSource,
Oli Scherer4f2b1082025-02-12 10:37:49 +0000248 rustc_hir::RangeEnd,
Folkert de Vries1dfc8402025-05-20 20:23:47 +0200249 rustc_hir::def_id::LocalDefId,
Nicholas Nethercoted28678e2025-02-05 12:02:04 +1100250 rustc_span::Ident,
251 rustc_span::Span,
252 rustc_span::Symbol,
253 rustc_target::asm::InlineAsmRegOrRegClass,
Nicholas Nethercoted282a672025-02-05 14:54:13 +1100254 // tidy-alphabetical-end
Nicholas Nethercoteabe2a682023-09-14 12:05:05 +1000255}
Nicholas Nethercoted28678e2025-02-05 12:02:04 +1100256
Nicholas Nethercoteabe2a682023-09-14 12:05:05 +1000257// For some things about which the type library does not know, or does not
258// provide any traversal implementations, we need to provide a traversal
259// implementation and a lift implementation (the former only for TyCtxt<'_>
260// interners).
261TrivialTypeTraversalAndLiftImpls! {
Nicholas Nethercoted282a672025-02-05 14:54:13 +1100262 // tidy-alphabetical-start
Camille Gillot1a227bd2025-11-09 02:57:31 +0000263 crate::mir::RuntimeChecks,
Nicholas Nethercoteabe2a682023-09-14 12:05:05 +1000264 crate::ty::ParamTy,
Folkert de Vries1dfc8402025-05-20 20:23:47 +0200265 crate::ty::instance::ReifyReason,
Nicholas Nethercoted28678e2025-02-05 12:02:04 +1100266 rustc_hir::def_id::DefId,
Nicholas Nethercoted282a672025-02-05 14:54:13 +1100267 // tidy-alphabetical-end
lcnr196fdf12024-10-22 23:07:51 +0200268}
269
Niko Matsakis27d58722017-11-11 13:04:36 -0500270///////////////////////////////////////////////////////////////////////////
Ariel Ben-Yehuda5f564fb2015-09-06 21:51:58 +0300271// Lift implementations
272
Camille GILLOT9ff07122023-10-13 20:20:57 +0000273impl<'tcx> Lift<TyCtxt<'tcx>> for PhantomData<&()> {
274 type Lifted = PhantomData<&'tcx ()>;
275 fn lift_to_interner(self, _: TyCtxt<'tcx>) -> Option<Self::Lifted> {
276 Some(PhantomData)
277 }
278}
279
Michael Goulet5e606c02024-05-10 14:27:48 -0400280impl<'tcx, T: Lift<TyCtxt<'tcx>>> Lift<TyCtxt<'tcx>> for Option<T> {
Eduard Burtescu8f72d812016-04-29 06:00:23 +0300281 type Lifted = Option<T::Lifted>;
Michael Gouleta6510502024-07-16 00:03:37 -0400282 fn lift_to_interner(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
Oli Scherer3e6c9e52022-09-21 08:29:19 +0000283 Some(match self {
284 Some(x) => Some(tcx.lift(x)?),
285 None => None,
286 })
Eduard Burtescu8f72d812016-04-29 06:00:23 +0300287 }
288}
289
Michael Goulet5e606c02024-05-10 14:27:48 -0400290impl<'a, 'tcx> Lift<TyCtxt<'tcx>> for Term<'a> {
kadmin67f56672022-01-08 09:28:12 +0000291 type Lifted = ty::Term<'tcx>;
Michael Gouleta6510502024-07-16 00:03:37 -0400292 fn lift_to_interner(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
Michael Goulet29c3bab2025-05-24 12:07:32 +0000293 match self.kind() {
Michael Goulet9fa07a42024-05-19 13:44:50 -0400294 TermKind::Ty(ty) => tcx.lift(ty).map(Into::into),
295 TermKind::Const(c) => tcx.lift(c).map(Into::into),
296 }
kadmin67f56672022-01-08 09:28:12 +0000297 }
298}
Eduard-Mihai Burtescu74349fa2017-08-07 08:08:53 +0300299
Ariel Ben-Yehuda5f564fb2015-09-06 21:51:58 +0300300///////////////////////////////////////////////////////////////////////////
Alan Egerton459e1422023-02-10 16:14:18 +0000301// Traversal implementations.
Ariel Ben-Yehuda5f564fb2015-09-06 21:51:58 +0300302
Alan Egerton695072d2023-02-22 02:18:40 +0000303impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ty::AdtDef<'tcx> {
Jason Newcombbe9b1252024-02-24 17:22:28 -0500304 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, _visitor: &mut V) -> V::Result {
305 V::Result::output()
Niko Matsakis23837c12018-02-09 10:34:23 -0500306 }
307}
308
Oli Scherer84acfe82023-02-02 13:57:36 +0000309impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for Pattern<'tcx> {
310 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
311 self,
312 folder: &mut F,
313 ) -> Result<Self, F::Error> {
314 let pat = (*self).clone().try_fold_with(folder)?;
Michael Gouletdb638ab2024-06-18 19:13:54 -0400315 Ok(if pat == *self { self } else { folder.cx().mk_pat(pat) })
Oli Scherer84acfe82023-02-02 13:57:36 +0000316 }
Michael Gouletc774adc2025-04-14 15:10:43 +0000317
318 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
319 let pat = (*self).clone().fold_with(folder);
320 if pat == *self { self } else { folder.cx().mk_pat(pat) }
321 }
Oli Scherer84acfe82023-02-02 13:57:36 +0000322}
323
324impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for Pattern<'tcx> {
325 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
326 (**self).visit_with(visitor)
327 }
328}
329
Alan Egerton695072d2023-02-22 02:18:40 +0000330impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for Ty<'tcx> {
331 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
332 self,
333 folder: &mut F,
334 ) -> Result<Self, F::Error> {
Nicholas Nethercote90db0332022-06-02 11:38:15 +1000335 folder.try_fold_ty(self)
336 }
Michael Gouletc774adc2025-04-14 15:10:43 +0000337
338 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
339 folder.fold_ty(self)
340 }
Alan Egertonf66c06f2022-06-17 12:09:23 +0100341}
Nicholas Nethercote90db0332022-06-02 11:38:15 +1000342
Alan Egerton695072d2023-02-22 02:18:40 +0000343impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for Ty<'tcx> {
Jason Newcombbe9b1252024-02-24 17:22:28 -0500344 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
Nicholas Nethercote90db0332022-06-02 11:38:15 +1000345 visitor.visit_ty(*self)
346 }
347}
348
Alan Egerton9783fcc2023-02-11 09:13:27 +0000349impl<'tcx> TypeSuperFoldable<TyCtxt<'tcx>> for Ty<'tcx> {
Alan Egerton695072d2023-02-22 02:18:40 +0000350 fn try_super_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
Alan Egertonbfc434b2021-12-01 00:55:57 +0000351 self,
352 folder: &mut F,
353 ) -> Result<Self, F::Error> {
lcnra6cbd642020-10-24 09:27:15 +0200354 let kind = match *self.kind() {
Michael Goulet7be0dbe2024-03-21 17:33:10 -0400355 ty::RawPtr(ty, mutbl) => ty::RawPtr(ty.try_fold_with(folder)?, mutbl),
Alan Egertonbfc434b2021-12-01 00:55:57 +0000356 ty::Array(typ, sz) => ty::Array(typ.try_fold_with(folder)?, sz.try_fold_with(folder)?),
357 ty::Slice(typ) => ty::Slice(typ.try_fold_with(folder)?),
Mahdi Dibaieee55583c2023-07-11 22:35:29 +0100358 ty::Adt(tid, args) => ty::Adt(tid, args.try_fold_with(folder)?),
León Orell Valerian Liehr26f33372025-09-17 04:16:47 +0200359 ty::Dynamic(trait_ty, region) => {
360 ty::Dynamic(trait_ty.try_fold_with(folder)?, region.try_fold_with(folder)?)
361 }
Alan Egertonbfc434b2021-12-01 00:55:57 +0000362 ty::Tuple(ts) => ty::Tuple(ts.try_fold_with(folder)?),
Mahdi Dibaieee55583c2023-07-11 22:35:29 +0100363 ty::FnDef(def_id, args) => ty::FnDef(def_id, args.try_fold_with(folder)?),
Nicholas Nethercotec4717cc2024-08-08 17:18:20 +1000364 ty::FnPtr(sig_tys, hdr) => ty::FnPtr(sig_tys.try_fold_with(folder)?, hdr),
Michael Goulet9a1c5eb2024-12-21 17:05:40 +0000365 ty::UnsafeBinder(f) => ty::UnsafeBinder(f.try_fold_with(folder)?),
Alan Egertonbfc434b2021-12-01 00:55:57 +0000366 ty::Ref(r, ty, mutbl) => {
367 ty::Ref(r.try_fold_with(folder)?, ty.try_fold_with(folder)?, mutbl)
368 }
Michael Gouletfcb42b42023-12-21 01:52:10 +0000369 ty::Coroutine(did, args) => ty::Coroutine(did, args.try_fold_with(folder)?),
Oli Scherer60956832023-10-19 16:06:43 +0000370 ty::CoroutineWitness(did, args) => {
371 ty::CoroutineWitness(did, args.try_fold_with(folder)?)
Camille GILLOT1974b6b2022-10-01 14:56:24 +0200372 }
Mahdi Dibaieee55583c2023-07-11 22:35:29 +0100373 ty::Closure(did, args) => ty::Closure(did, args.try_fold_with(folder)?),
Michael Gouletc567edd2024-01-24 18:01:56 +0000374 ty::CoroutineClosure(did, args) => {
375 ty::CoroutineClosure(did, args.try_fold_with(folder)?)
376 }
Michael Goulet96cb18e2022-11-27 17:52:17 +0000377 ty::Alias(kind, data) => ty::Alias(kind, data.try_fold_with(folder)?),
Oli Scherer84acfe82023-02-02 13:57:36 +0000378 ty::Pat(ty, pat) => ty::Pat(ty.try_fold_with(folder)?, pat.try_fold_with(folder)?),
scalexm1003b7f2018-10-22 20:37:56 +0200379
Mark Rousskova06baa52019-12-22 17:42:04 -0500380 ty::Bool
381 | ty::Char
382 | ty::Str
383 | ty::Int(_)
384 | ty::Uint(_)
385 | ty::Float(_)
mark268decb2020-05-05 23:02:09 -0500386 | ty::Error(_)
Mark Rousskova06baa52019-12-22 17:42:04 -0500387 | ty::Infer(_)
388 | ty::Param(..)
389 | ty::Bound(..)
390 | ty::Placeholder(..)
391 | ty::Never
LeSeulArtichaut6e3fa202021-05-19 13:34:54 +0200392 | ty::Foreign(..) => return Ok(self),
Jeffrey Seyfriedf9808ea2015-11-18 09:38:57 +0000393 };
Nicholas Nethercote4d0618e2016-11-24 21:10:08 +1100394
Michael Gouletdb638ab2024-06-18 19:13:54 -0400395 Ok(if *self.kind() == kind { self } else { folder.cx().mk_ty_from_kind(kind) })
Jeffrey Seyfriedf9808ea2015-11-18 09:38:57 +0000396 }
Michael Gouletc774adc2025-04-14 15:10:43 +0000397
398 fn super_fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
399 let kind = match *self.kind() {
400 ty::RawPtr(ty, mutbl) => ty::RawPtr(ty.fold_with(folder), mutbl),
401 ty::Array(typ, sz) => ty::Array(typ.fold_with(folder), sz.fold_with(folder)),
402 ty::Slice(typ) => ty::Slice(typ.fold_with(folder)),
403 ty::Adt(tid, args) => ty::Adt(tid, args.fold_with(folder)),
León Orell Valerian Liehr26f33372025-09-17 04:16:47 +0200404 ty::Dynamic(trait_ty, region) => {
405 ty::Dynamic(trait_ty.fold_with(folder), region.fold_with(folder))
Michael Gouletc774adc2025-04-14 15:10:43 +0000406 }
407 ty::Tuple(ts) => ty::Tuple(ts.fold_with(folder)),
408 ty::FnDef(def_id, args) => ty::FnDef(def_id, args.fold_with(folder)),
409 ty::FnPtr(sig_tys, hdr) => ty::FnPtr(sig_tys.fold_with(folder), hdr),
410 ty::UnsafeBinder(f) => ty::UnsafeBinder(f.fold_with(folder)),
411 ty::Ref(r, ty, mutbl) => ty::Ref(r.fold_with(folder), ty.fold_with(folder), mutbl),
412 ty::Coroutine(did, args) => ty::Coroutine(did, args.fold_with(folder)),
413 ty::CoroutineWitness(did, args) => ty::CoroutineWitness(did, args.fold_with(folder)),
414 ty::Closure(did, args) => ty::Closure(did, args.fold_with(folder)),
415 ty::CoroutineClosure(did, args) => ty::CoroutineClosure(did, args.fold_with(folder)),
416 ty::Alias(kind, data) => ty::Alias(kind, data.fold_with(folder)),
417 ty::Pat(ty, pat) => ty::Pat(ty.fold_with(folder), pat.fold_with(folder)),
418
419 ty::Bool
420 | ty::Char
421 | ty::Str
422 | ty::Int(_)
423 | ty::Uint(_)
424 | ty::Float(_)
425 | ty::Error(_)
426 | ty::Infer(_)
427 | ty::Param(..)
428 | ty::Bound(..)
429 | ty::Placeholder(..)
430 | ty::Never
431 | ty::Foreign(..) => return self,
432 };
433
434 if *self.kind() == kind { self } else { folder.cx().mk_ty_from_kind(kind) }
435 }
Alan Egertonf66c06f2022-06-17 12:09:23 +0100436}
Jeffrey Seyfriedf9808ea2015-11-18 09:38:57 +0000437
Alan Egertondea342d2023-02-09 19:38:07 +0000438impl<'tcx> TypeSuperVisitable<TyCtxt<'tcx>> for Ty<'tcx> {
Jason Newcombbe9b1252024-02-24 17:22:28 -0500439 fn super_visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
LeSeulArtichaut3e14b682020-08-03 00:49:11 +0200440 match self.kind() {
Michael Goulet7be0dbe2024-03-21 17:33:10 -0400441 ty::RawPtr(ty, _mutbl) => ty.visit_with(visitor),
LeSeulArtichaut2c85b6f2020-10-21 14:22:44 +0200442 ty::Array(typ, sz) => {
Jason Newcombbe9b1252024-02-24 17:22:28 -0500443 try_visit!(typ.visit_with(visitor));
LeSeulArtichaut2c85b6f2020-10-21 14:22:44 +0200444 sz.visit_with(visitor)
445 }
varkor6f637da2018-08-22 01:35:02 +0100446 ty::Slice(typ) => typ.visit_with(visitor),
Mahdi Dibaieee55583c2023-07-11 22:35:29 +0100447 ty::Adt(_, args) => args.visit_with(visitor),
León Orell Valerian Liehr26f33372025-09-17 04:16:47 +0200448 ty::Dynamic(trait_ty, reg) => {
Jason Newcombbe9b1252024-02-24 17:22:28 -0500449 try_visit!(trait_ty.visit_with(visitor));
LeSeulArtichaut2c85b6f2020-10-21 14:22:44 +0200450 reg.visit_with(visitor)
Mark Rousskova06baa52019-12-22 17:42:04 -0500451 }
varkor6f637da2018-08-22 01:35:02 +0100452 ty::Tuple(ts) => ts.visit_with(visitor),
Mahdi Dibaieee55583c2023-07-11 22:35:29 +0100453 ty::FnDef(_, args) => args.visit_with(visitor),
Michael Goulet3d5438ac2025-02-20 18:28:48 +0000454 ty::FnPtr(sig_tys, _) => sig_tys.visit_with(visitor),
455 ty::UnsafeBinder(f) => f.visit_with(visitor),
LeSeulArtichaut2c85b6f2020-10-21 14:22:44 +0200456 ty::Ref(r, ty, _) => {
Jason Newcombbe9b1252024-02-24 17:22:28 -0500457 try_visit!(r.visit_with(visitor));
LeSeulArtichaut2c85b6f2020-10-21 14:22:44 +0200458 ty.visit_with(visitor)
459 }
Michael Goulet3d5438ac2025-02-20 18:28:48 +0000460 ty::Coroutine(_did, args) => args.visit_with(visitor),
461 ty::CoroutineWitness(_did, args) => args.visit_with(visitor),
462 ty::Closure(_did, args) => args.visit_with(visitor),
463 ty::CoroutineClosure(_did, args) => args.visit_with(visitor),
464 ty::Alias(_, data) => data.visit_with(visitor),
scalexm1003b7f2018-10-22 20:37:56 +0200465
Oli Scherer84acfe82023-02-02 13:57:36 +0000466 ty::Pat(ty, pat) => {
467 try_visit!(ty.visit_with(visitor));
468 pat.visit_with(visitor)
469 }
470
Oli Schererfb98fbb2024-07-16 11:31:04 +0000471 ty::Error(guar) => guar.visit_with(visitor),
472
Mark Rousskova06baa52019-12-22 17:42:04 -0500473 ty::Bool
474 | ty::Char
475 | ty::Str
476 | ty::Int(_)
477 | ty::Uint(_)
478 | ty::Float(_)
Mark Rousskova06baa52019-12-22 17:42:04 -0500479 | ty::Infer(_)
480 | ty::Bound(..)
481 | ty::Placeholder(..)
482 | ty::Param(..)
483 | ty::Never
Jason Newcombbe9b1252024-02-24 17:22:28 -0500484 | ty::Foreign(..) => V::Result::output(),
Jeffrey Seyfriedf9808ea2015-11-18 09:38:57 +0000485 }
486 }
Ariel Ben-Yehuda5f564fb2015-09-06 21:51:58 +0300487}
488
Alan Egerton695072d2023-02-22 02:18:40 +0000489impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for ty::Region<'tcx> {
490 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
491 self,
492 folder: &mut F,
493 ) -> Result<Self, F::Error> {
Alan Egertonbfc434b2021-12-01 00:55:57 +0000494 folder.try_fold_region(self)
Ariel Ben-Yehuda5f564fb2015-09-06 21:51:58 +0300495 }
Michael Gouletc774adc2025-04-14 15:10:43 +0000496
497 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
498 folder.fold_region(self)
499 }
Alan Egertonf66c06f2022-06-17 12:09:23 +0100500}
Jeffrey Seyfriedf9808ea2015-11-18 09:38:57 +0000501
Alan Egerton695072d2023-02-22 02:18:40 +0000502impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ty::Region<'tcx> {
Jason Newcombbe9b1252024-02-24 17:22:28 -0500503 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
Jeffrey Seyfriedf9808ea2015-11-18 09:38:57 +0000504 visitor.visit_region(*self)
505 }
Ariel Ben-Yehuda5f564fb2015-09-06 21:51:58 +0300506}
507
Alan Egerton695072d2023-02-22 02:18:40 +0000508impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for ty::Predicate<'tcx> {
509 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
510 self,
511 folder: &mut F,
512 ) -> Result<Self, F::Error> {
Alan Egertonbfc434b2021-12-01 00:55:57 +0000513 folder.try_fold_predicate(self)
lcnr21400162021-07-19 12:13:25 +0200514 }
Michael Gouletc774adc2025-04-14 15:10:43 +0000515
516 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
517 folder.fold_predicate(self)
518 }
Alan Egertonf66c06f2022-06-17 12:09:23 +0100519}
lcnr21400162021-07-19 12:13:25 +0200520
Michael Goulet21226ee2023-06-16 06:27:41 +0000521// FIXME(clause): This is wonky
522impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for ty::Clause<'tcx> {
523 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
524 self,
525 folder: &mut F,
526 ) -> Result<Self, F::Error> {
Michael Goulet2fa796a2023-06-19 20:48:46 +0000527 Ok(folder.try_fold_predicate(self.as_predicate())?.expect_clause())
Michael Goulet21226ee2023-06-16 06:27:41 +0000528 }
Michael Gouletc774adc2025-04-14 15:10:43 +0000529
530 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
531 folder.fold_predicate(self.as_predicate()).expect_clause()
532 }
Michael Goulet21226ee2023-06-16 06:27:41 +0000533}
534
lcnrc56efae2025-05-23 14:46:38 +0000535impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for ty::Clauses<'tcx> {
536 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
537 self,
538 folder: &mut F,
539 ) -> Result<Self, F::Error> {
540 folder.try_fold_clauses(self)
541 }
542
543 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
544 folder.fold_clauses(self)
545 }
546}
547
Alan Egerton695072d2023-02-22 02:18:40 +0000548impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ty::Predicate<'tcx> {
Jason Newcombbe9b1252024-02-24 17:22:28 -0500549 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
Matthew Jasperf802ee12020-06-10 09:30:39 +0100550 visitor.visit_predicate(*self)
551 }
Matthew Jasperf802ee12020-06-10 09:30:39 +0100552}
553
Michael Goulet21226ee2023-06-16 06:27:41 +0000554impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ty::Clause<'tcx> {
Jason Newcombbe9b1252024-02-24 17:22:28 -0500555 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
Michael Goulet21226ee2023-06-16 06:27:41 +0000556 visitor.visit_predicate(self.as_predicate())
557 }
558}
559
Alan Egerton9783fcc2023-02-11 09:13:27 +0000560impl<'tcx> TypeSuperFoldable<TyCtxt<'tcx>> for ty::Predicate<'tcx> {
Alan Egerton695072d2023-02-22 02:18:40 +0000561 fn try_super_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
Alan Egertonbfc434b2021-12-01 00:55:57 +0000562 self,
563 folder: &mut F,
564 ) -> Result<Self, F::Error> {
Nicholas Nethercotea07f7172026-01-07 17:19:36 +1100565 // This method looks different to `Ty::try_super_fold_with` and `Const::super_fold_with`.
566 // Why is that? `PredicateKind` provides little scope for optimized folding, unlike
567 // `TyKind` and `ConstKind` (which have common variants that don't require recursive
568 // `fold_with` calls on their fields). So we just derive the `TypeFoldable` impl for
569 // `PredicateKind` and call it here because the derived code is as fast as hand-written
570 // code would be.
Nicholas Nethercote90db0332022-06-02 11:38:15 +1000571 let new = self.kind().try_fold_with(folder)?;
Michael Gouletdb638ab2024-06-18 19:13:54 -0400572 Ok(folder.cx().reuse_or_mk_predicate(self, new))
Niko Matsakis5fb0f0d2017-05-23 04:19:47 -0400573 }
Michael Gouletc774adc2025-04-14 15:10:43 +0000574
575 fn super_fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
Nicholas Nethercotea07f7172026-01-07 17:19:36 +1100576 // See comment in `Predicate::try_super_fold_with`.
Michael Gouletc774adc2025-04-14 15:10:43 +0000577 let new = self.kind().fold_with(folder);
578 folder.cx().reuse_or_mk_predicate(self, new)
579 }
Alan Egertonf66c06f2022-06-17 12:09:23 +0100580}
Niko Matsakis5fb0f0d2017-05-23 04:19:47 -0400581
Alan Egertondea342d2023-02-09 19:38:07 +0000582impl<'tcx> TypeSuperVisitable<TyCtxt<'tcx>> for ty::Predicate<'tcx> {
Jason Newcombbe9b1252024-02-24 17:22:28 -0500583 fn super_visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
Nicholas Nethercotea07f7172026-01-07 17:19:36 +1100584 // See comment in `Predicate::try_super_fold_with`.
Nicholas Nethercote90db0332022-06-02 11:38:15 +1000585 self.kind().visit_with(visitor)
586 }
587}
588
Lukas Markeffskyfcc477f2024-03-24 22:49:31 +0100589impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ty::Clauses<'tcx> {
590 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
591 visitor.visit_clauses(self)
592 }
593}
594
595impl<'tcx> TypeSuperVisitable<TyCtxt<'tcx>> for ty::Clauses<'tcx> {
596 fn super_visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
597 self.as_slice().visit_with(visitor)
598 }
599}
600
lcnrc56efae2025-05-23 14:46:38 +0000601impl<'tcx> TypeSuperFoldable<TyCtxt<'tcx>> for ty::Clauses<'tcx> {
602 fn try_super_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
603 self,
604 folder: &mut F,
605 ) -> Result<Self, F::Error> {
606 ty::util::try_fold_list(self, folder, |tcx, v| tcx.mk_clauses(v))
607 }
608
609 fn super_fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
610 ty::util::fold_list(self, folder, |tcx, v| tcx.mk_clauses(v))
611 }
612}
613
Alan Egerton695072d2023-02-22 02:18:40 +0000614impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for ty::Const<'tcx> {
615 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
616 self,
617 folder: &mut F,
618 ) -> Result<Self, F::Error> {
Nicholas Nethercote90db0332022-06-02 11:38:15 +1000619 folder.try_fold_const(self)
620 }
Michael Gouletc774adc2025-04-14 15:10:43 +0000621
622 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
623 folder.fold_const(self)
624 }
Alan Egertonf66c06f2022-06-17 12:09:23 +0100625}
Nicholas Nethercote90db0332022-06-02 11:38:15 +1000626
Alan Egerton695072d2023-02-22 02:18:40 +0000627impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ty::Const<'tcx> {
Jason Newcombbe9b1252024-02-24 17:22:28 -0500628 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
Nicholas Nethercote90db0332022-06-02 11:38:15 +1000629 visitor.visit_const(*self)
630 }
631}
632
Alan Egerton9783fcc2023-02-11 09:13:27 +0000633impl<'tcx> TypeSuperFoldable<TyCtxt<'tcx>> for ty::Const<'tcx> {
Alan Egerton695072d2023-02-22 02:18:40 +0000634 fn try_super_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
Alan Egertonbfc434b2021-12-01 00:55:57 +0000635 self,
636 folder: &mut F,
637 ) -> Result<Self, F::Error> {
Boxy62174bf2023-07-04 15:41:45 +0100638 let kind = match self.kind() {
Boxy62174bf2023-07-04 15:41:45 +0100639 ConstKind::Unevaluated(uv) => ConstKind::Unevaluated(uv.try_fold_with(folder)?),
Lukas Markeffsky10fc0b12025-01-27 04:30:00 +0100640 ConstKind::Value(v) => ConstKind::Value(v.try_fold_with(folder)?),
Boxy62174bf2023-07-04 15:41:45 +0100641 ConstKind::Expr(e) => ConstKind::Expr(e.try_fold_with(folder)?),
Nicholas Nethercote94cc5bb2025-07-30 14:29:28 +1000642
643 ConstKind::Param(_)
644 | ConstKind::Infer(_)
645 | ConstKind::Bound(..)
646 | ConstKind::Placeholder(_)
647 | ConstKind::Error(_) => return Ok(self),
Boxy62174bf2023-07-04 15:41:45 +0100648 };
Michael Gouletdb638ab2024-06-18 19:13:54 -0400649 if kind != self.kind() { Ok(folder.cx().mk_ct_from_kind(kind)) } else { Ok(self) }
Eduard-Mihai Burtescu932289c2017-08-04 11:25:13 +0300650 }
Michael Gouletc774adc2025-04-14 15:10:43 +0000651
652 fn super_fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
653 let kind = match self.kind() {
Michael Gouletc774adc2025-04-14 15:10:43 +0000654 ConstKind::Unevaluated(uv) => ConstKind::Unevaluated(uv.fold_with(folder)),
655 ConstKind::Value(v) => ConstKind::Value(v.fold_with(folder)),
Michael Gouletc774adc2025-04-14 15:10:43 +0000656 ConstKind::Expr(e) => ConstKind::Expr(e.fold_with(folder)),
Nicholas Nethercote94cc5bb2025-07-30 14:29:28 +1000657
658 ConstKind::Param(_)
659 | ConstKind::Infer(_)
660 | ConstKind::Bound(..)
661 | ConstKind::Placeholder(_)
662 | ConstKind::Error(_) => return self,
Michael Gouletc774adc2025-04-14 15:10:43 +0000663 };
664 if kind != self.kind() { folder.cx().mk_ct_from_kind(kind) } else { self }
665 }
Alan Egertonf66c06f2022-06-17 12:09:23 +0100666}
Eduard-Mihai Burtescu932289c2017-08-04 11:25:13 +0300667
Alan Egertondea342d2023-02-09 19:38:07 +0000668impl<'tcx> TypeSuperVisitable<TyCtxt<'tcx>> for ty::Const<'tcx> {
Jason Newcombbe9b1252024-02-24 17:22:28 -0500669 fn super_visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
Boxy62174bf2023-07-04 15:41:45 +0100670 match self.kind() {
Boxy62174bf2023-07-04 15:41:45 +0100671 ConstKind::Unevaluated(uv) => uv.visit_with(visitor),
Lukas Markeffsky10fc0b12025-01-27 04:30:00 +0100672 ConstKind::Value(v) => v.visit_with(visitor),
Boxy62174bf2023-07-04 15:41:45 +0100673 ConstKind::Expr(e) => e.visit_with(visitor),
Nicholas Nethercote94cc5bb2025-07-30 14:29:28 +1000674 ConstKind::Error(e) => e.visit_with(visitor),
675
676 ConstKind::Param(_)
677 | ConstKind::Infer(_)
678 | ConstKind::Bound(..)
679 | ConstKind::Placeholder(_) => V::Result::output(),
Boxy62174bf2023-07-04 15:41:45 +0100680 }
Eduard-Mihai Burtescu932289c2017-08-04 11:25:13 +0300681 }
Eduard-Mihai Burtescu932289c2017-08-04 11:25:13 +0300682}
Ariel Ben-Yehuda5f564fb2015-09-06 21:51:58 +0300683
Boxy Uwu67228052025-11-19 19:30:09 +0000684impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ty::ValTree<'tcx> {
685 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
686 let inner: &ty::ValTreeKind<TyCtxt<'tcx>> = &*self;
687 inner.visit_with(visitor)
688 }
689}
690
691impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for ty::ValTree<'tcx> {
692 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
693 self,
694 folder: &mut F,
695 ) -> Result<Self, F::Error> {
696 let inner: &ty::ValTreeKind<TyCtxt<'tcx>> = &*self;
697 let new_inner = inner.clone().try_fold_with(folder)?;
698
699 if inner == &new_inner {
700 Ok(self)
701 } else {
702 let valtree = folder.cx().intern_valtree(new_inner);
703 Ok(valtree)
704 }
705 }
706
707 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
708 let inner: &ty::ValTreeKind<TyCtxt<'tcx>> = &*self;
709 let new_inner = inner.clone().fold_with(folder);
710
711 if inner == &new_inner { self } else { folder.cx().intern_valtree(new_inner) }
712 }
713}
714
Oli Schererfb98fbb2024-07-16 11:31:04 +0000715impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for rustc_span::ErrorGuaranteed {
716 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
717 visitor.visit_error(*self)
718 }
719}
720
721impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for rustc_span::ErrorGuaranteed {
722 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
723 self,
724 _folder: &mut F,
725 ) -> Result<Self, F::Error> {
726 Ok(self)
727 }
Oli Schererfb98fbb2024-07-16 11:31:04 +0000728
Michael Gouletc774adc2025-04-14 15:10:43 +0000729 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, _folder: &mut F) -> Self {
730 self
varkorc6197542019-05-06 14:05:26 +0100731 }
732}
Ellendec8ed42022-01-12 23:29:10 +0000733
Alan Egerton695072d2023-02-22 02:18:40 +0000734impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for TyAndLayout<'tcx, Ty<'tcx>> {
Jason Newcombbe9b1252024-02-24 17:22:28 -0500735 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
Ben Kimock5bfad5c2023-01-22 17:06:28 -0500736 visitor.visit_ty(self.ty)
737 }
738}
Martin Nordholts924ea052024-01-12 08:22:05 +0100739
740impl<'tcx, T: TypeVisitable<TyCtxt<'tcx>> + Debug + Clone> TypeVisitable<TyCtxt<'tcx>>
741 for Spanned<T>
742{
Jason Newcombbe9b1252024-02-24 17:22:28 -0500743 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
744 try_visit!(self.node.visit_with(visitor));
745 self.span.visit_with(visitor)
Martin Nordholts924ea052024-01-12 08:22:05 +0100746 }
747}
748
749impl<'tcx, T: TypeFoldable<TyCtxt<'tcx>> + Debug + Clone> TypeFoldable<TyCtxt<'tcx>>
750 for Spanned<T>
751{
752 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
753 self,
754 folder: &mut F,
755 ) -> Result<Self, F::Error> {
756 Ok(Spanned {
757 node: self.node.try_fold_with(folder)?,
758 span: self.span.try_fold_with(folder)?,
759 })
760 }
Nicholas Nethercoted28678e2025-02-05 12:02:04 +1100761
Michael Gouletc774adc2025-04-14 15:10:43 +0000762 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
763 Spanned { node: self.node.fold_with(folder), span: self.span.fold_with(folder) }
Nicholas Nethercoted28678e2025-02-05 12:02:04 +1100764 }
765}
766
767impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for &'tcx ty::List<LocalDefId> {
768 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
769 self,
770 _folder: &mut F,
771 ) -> Result<Self, F::Error> {
772 Ok(self)
773 }
Michael Gouletc774adc2025-04-14 15:10:43 +0000774
775 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, _folder: &mut F) -> Self {
776 self
777 }
Nicholas Nethercoted28678e2025-02-05 12:02:04 +1100778}
779
Michael Gouletc774adc2025-04-14 15:10:43 +0000780macro_rules! list_fold {
781 ($($ty:ty : $mk:ident),+ $(,)?) => {
782 $(
783 impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for $ty {
784 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
785 self,
786 folder: &mut F,
787 ) -> Result<Self, F::Error> {
788 ty::util::try_fold_list(self, folder, |tcx, v| tcx.$mk(v))
789 }
790
791 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(
792 self,
793 folder: &mut F,
794 ) -> Self {
795 ty::util::fold_list(self, folder, |tcx, v| tcx.$mk(v))
796 }
797 }
798 )*
Nicholas Nethercoted28678e2025-02-05 12:02:04 +1100799 }
800}
Michael Gouletc774adc2025-04-14 15:10:43 +0000801
802list_fold! {
Michael Gouletc774adc2025-04-14 15:10:43 +0000803 &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>> : mk_poly_existential_predicates,
lcnr1acd65c2025-09-22 14:09:03 +0200804 &'tcx ty::List<(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)>: mk_predefined_opaques_in_body,
Michael Gouletc774adc2025-04-14 15:10:43 +0000805 &'tcx ty::List<PlaceElem<'tcx>> : mk_place_elems,
Oli Schererb0238562025-02-27 09:46:46 +0000806 &'tcx ty::List<ty::Pattern<'tcx>> : mk_patterns,
Michael Goulet3634f462025-07-15 16:01:43 +0000807 &'tcx ty::List<ty::ArgOutlivesPredicate<'tcx>> : mk_outlives,
Michael Gouletc774adc2025-04-14 15:10:43 +0000808}