| Alan Egerton | 459e142 | 2023-02-10 16:14:18 +0000 | [diff] [blame] | 1 | //! 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 Matsakis | 27d5872 | 2017-11-11 13:04:36 -0500 | [diff] [blame] | 5 | |
| Nicholas Nethercote | 84ac80f | 2024-07-29 08:13:50 +1000 | [diff] [blame] | 6 | use std::fmt::{self, Debug}; |
| Camille GILLOT | 9ff0712 | 2023-10-13 20:20:57 +0000 | [diff] [blame] | 7 | use std::marker::PhantomData; |
| Nicholas Nethercote | 84ac80f | 2024-07-29 08:13:50 +1000 | [diff] [blame] | 8 | |
| Jubilee Young | 236fe33 | 2024-11-02 19:33:00 -0700 | [diff] [blame] | 9 | use rustc_abi::TyAndLayout; |
| Mazdak Farrokhzad | ebfd867 | 2020-01-05 02:37:57 +0100 | [diff] [blame] | 10 | use rustc_hir::def::Namespace; |
| Nicholas Nethercote | d28678e | 2025-02-05 12:02:04 +1100 | [diff] [blame] | 11 | use rustc_hir::def_id::LocalDefId; |
| Martin Nordholts | 924ea05 | 2024-01-12 08:22:05 +0100 | [diff] [blame] | 12 | use rustc_span::source_map::Spanned; |
| Michael Goulet | c774adc | 2025-04-14 15:10:43 +0000 | [diff] [blame] | 13 | use rustc_type_ir::{ConstKind, TypeFolder, VisitorResult, try_visit}; |
| Ariel Ben-Yehuda | 5f564fb | 2015-09-06 21:51:58 +0300 | [diff] [blame] | 14 | |
| Nicholas Nethercote | 84ac80f | 2024-07-29 08:13:50 +1000 | [diff] [blame] | 15 | use super::{GenericArg, GenericArgKind, Pattern, Region}; |
| Nicholas Nethercote | d28678e | 2025-02-05 12:02:04 +1100 | [diff] [blame] | 16 | use crate::mir::PlaceElem; |
| Michael Goulet | c682aa1 | 2024-09-22 19:05:04 -0400 | [diff] [blame] | 17 | use crate::ty::print::{FmtPrinter, Printer, with_no_trimmed_paths}; |
| Michael Goulet | dc0cdfd | 2025-03-13 16:59:55 +0000 | [diff] [blame] | 18 | use crate::ty::{ |
| Michael Goulet | c774adc | 2025-04-14 15:10:43 +0000 | [diff] [blame] | 19 | self, FallibleTypeFolder, Lift, Term, TermKind, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable, |
| 20 | TypeSuperVisitable, TypeVisitable, TypeVisitor, |
| Michael Goulet | dc0cdfd | 2025-03-13 16:59:55 +0000 | [diff] [blame] | 21 | }; |
| Oli Scherer | 84acfe8 | 2023-02-02 13:57:36 +0000 | [diff] [blame] | 22 | |
| Eduard-Mihai Burtescu | fb53bb9 | 2019-01-19 06:33:44 +0200 | [diff] [blame] | 23 | impl fmt::Debug for ty::TraitDef { |
| 24 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| Eduard-Mihai Burtescu | 52b4f2d | 2019-01-25 12:11:50 +0200 | [diff] [blame] | 25 | ty::tls::with(|tcx| { |
| Mark Rousskov | efb99d7 | 2022-02-18 16:15:29 -0500 | [diff] [blame] | 26 | with_no_trimmed_paths!({ |
| Nicholas Nethercote | 1698c8e | 2025-08-01 10:41:11 +1000 | [diff] [blame] | 27 | let s = FmtPrinter::print_string(tcx, Namespace::TypeNS, |p| { |
| 28 | p.print_def_path(self.def_id, &[]) |
| Nilstrieb | 5acf26b | 2023-10-17 19:46:14 +0200 | [diff] [blame] | 29 | })?; |
| 30 | f.write_str(&s) |
| Mark Rousskov | efb99d7 | 2022-02-18 16:15:29 -0500 | [diff] [blame] | 31 | }) |
| Eduard-Mihai Burtescu | fb53bb9 | 2019-01-19 06:33:44 +0200 | [diff] [blame] | 32 | }) |
| 33 | } |
| 34 | } |
| 35 | |
| Nicholas Nethercote | ca5525d | 2022-03-05 07:28:41 +1100 | [diff] [blame] | 36 | impl<'tcx> fmt::Debug for ty::AdtDef<'tcx> { |
| Eduard-Mihai Burtescu | fb53bb9 | 2019-01-19 06:33:44 +0200 | [diff] [blame] | 37 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| Eduard-Mihai Burtescu | 52b4f2d | 2019-01-25 12:11:50 +0200 | [diff] [blame] | 38 | ty::tls::with(|tcx| { |
| Mark Rousskov | efb99d7 | 2022-02-18 16:15:29 -0500 | [diff] [blame] | 39 | with_no_trimmed_paths!({ |
| Nicholas Nethercote | 1698c8e | 2025-08-01 10:41:11 +1000 | [diff] [blame] | 40 | let s = FmtPrinter::print_string(tcx, Namespace::TypeNS, |p| { |
| 41 | p.print_def_path(self.did(), &[]) |
| Nilstrieb | 5acf26b | 2023-10-17 19:46:14 +0200 | [diff] [blame] | 42 | })?; |
| 43 | f.write_str(&s) |
| Mark Rousskov | efb99d7 | 2022-02-18 16:15:29 -0500 | [diff] [blame] | 44 | }) |
| Eduard-Mihai Burtescu | fb53bb9 | 2019-01-19 06:33:44 +0200 | [diff] [blame] | 45 | }) |
| 46 | } |
| 47 | } |
| 48 | |
| Eduard-Mihai Burtescu | fb53bb9 | 2019-01-19 06:33:44 +0200 | [diff] [blame] | 49 | impl fmt::Debug for ty::UpvarId { |
| 50 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| Nicholas Nethercote | 256c27e | 2025-02-21 18:33:05 +1100 | [diff] [blame] | 51 | let name = ty::tls::with(|tcx| tcx.hir_name(self.var_path.hir_id)); |
| Mark Rousskov | a06baa5 | 2019-12-22 17:42:04 -0500 | [diff] [blame] | 52 | write!(f, "UpvarId({:?};`{}`;{:?})", self.var_path.hir_id, name, self.closure_expr_id) |
| Eduard-Mihai Burtescu | fb53bb9 | 2019-01-19 06:33:44 +0200 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
| Aaron Hill | 070bf94 | 2021-12-15 19:32:30 -0500 | [diff] [blame] | 56 | impl<'tcx> fmt::Debug for ty::adjustment::Adjustment<'tcx> { |
| Eduard-Mihai Burtescu | fb53bb9 | 2019-01-19 06:33:44 +0200 | [diff] [blame] | 57 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 58 | write!(f, "{:?} -> {}", self.kind, self.target) |
| 59 | } |
| 60 | } |
| 61 | |
| dianne | f35eae7 | 2025-03-14 18:56:15 -0700 | [diff] [blame] | 62 | impl<'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 | |
| lcnr | 15f2156 | 2023-11-14 13:13:27 +0000 | [diff] [blame] | 68 | impl fmt::Debug for ty::LateParamRegion { |
| Eduard-Mihai Burtescu | fb53bb9 | 2019-01-19 06:33:44 +0200 | [diff] [blame] | 69 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| lcnr | 085d931 | 2024-12-06 14:46:28 +0100 | [diff] [blame] | 70 | write!(f, "ReLateParam({:?}, {:?})", self.scope, self.kind) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | impl fmt::Debug for ty::LateParamRegionKind { |
| 75 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 76 | match *self { |
| Nicholas Nethercote | fadf910 | 2025-04-04 16:50:56 +1100 | [diff] [blame] | 77 | ty::LateParamRegionKind::Anon(idx) => write!(f, "LateAnon({idx})"), |
| Michael Goulet | 42c9bfd | 2025-04-11 04:17:19 +0000 | [diff] [blame] | 78 | ty::LateParamRegionKind::NamedAnon(idx, name) => { |
| 79 | write!(f, "LateNamedAnon({idx:?}, {name})") |
| 80 | } |
| 81 | ty::LateParamRegionKind::Named(did) => { |
| 82 | write!(f, "LateNamed({did:?})") |
| lcnr | 085d931 | 2024-12-06 14:46:28 +0100 | [diff] [blame] | 83 | } |
| Nicholas Nethercote | fadf910 | 2025-04-04 16:50:56 +1100 | [diff] [blame] | 84 | ty::LateParamRegionKind::ClosureEnv => write!(f, "LateEnv"), |
| lcnr | 085d931 | 2024-12-06 14:46:28 +0100 | [diff] [blame] | 85 | } |
| Eduard-Mihai Burtescu | fb53bb9 | 2019-01-19 06:33:44 +0200 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
| Aaron Hill | 070bf94 | 2021-12-15 19:32:30 -0500 | [diff] [blame] | 89 | impl<'tcx> fmt::Debug for Ty<'tcx> { |
| Eduard-Mihai Burtescu | fb53bb9 | 2019-01-19 06:33:44 +0200 | [diff] [blame] | 90 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| Nicholas Nethercote | 64ea8eb | 2023-09-07 15:14:40 +1000 | [diff] [blame] | 91 | with_no_trimmed_paths!(fmt::Debug::fmt(self.kind(), f)) |
| Eduard-Mihai Burtescu | fb53bb9 | 2019-01-19 06:33:44 +0200 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | impl fmt::Debug for ty::ParamTy { |
| 96 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| varkor | c3694e5 | 2019-05-06 13:12:04 +0100 | [diff] [blame] | 97 | write!(f, "{}/#{}", self.name, self.index) |
| Eduard-Mihai Burtescu | fb53bb9 | 2019-01-19 06:33:44 +0200 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | impl 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 Hill | 070bf94 | 2021-12-15 19:32:30 -0500 | [diff] [blame] | 107 | impl<'tcx> fmt::Debug for ty::Predicate<'tcx> { |
| Bastian Kauschke | f316479 | 2020-05-11 22:06:41 +0200 | [diff] [blame] | 108 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| Jack Huey | 3dea68d | 2021-01-07 11:20:28 -0500 | [diff] [blame] | 109 | write!(f, "{:?}", self.kind()) |
| Bastian Kauschke | f316479 | 2020-05-11 22:06:41 +0200 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| Michael Goulet | 21226ee | 2023-06-16 06:27:41 +0000 | [diff] [blame] | 113 | impl<'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 | |
| Boxy | 3fdb443 | 2023-07-06 10:17:26 +0100 | [diff] [blame] | 119 | impl<'tcx> fmt::Debug for ty::consts::Expr<'tcx> { |
| 120 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| Michael Goulet | 0fc18e3 | 2024-06-06 10:08:55 -0400 | [diff] [blame] | 121 | match self.kind { |
| Boxy | f076dec | 2024-06-04 02:24:57 +0100 | [diff] [blame] | 122 | ty::ExprKind::Binop(op) => { |
| Michael Goulet | 0fc18e3 | 2024-06-06 10:08:55 -0400 | [diff] [blame] | 123 | let (lhs_ty, rhs_ty, lhs, rhs) = self.binop_args(); |
| 124 | write!(f, "({op:?}: ({:?}: {:?}), ({:?}: {:?}))", lhs, lhs_ty, rhs, rhs_ty,) |
| Boxy | 3fdb443 | 2023-07-06 10:17:26 +0100 | [diff] [blame] | 125 | } |
| Boxy | f076dec | 2024-06-04 02:24:57 +0100 | [diff] [blame] | 126 | ty::ExprKind::UnOp(op) => { |
| Michael Goulet | 0fc18e3 | 2024-06-06 10:08:55 -0400 | [diff] [blame] | 127 | let (rhs_ty, rhs) = self.unop_args(); |
| 128 | write!(f, "({op:?}: ({:?}: {:?}))", rhs, rhs_ty) |
| Boxy | f076dec | 2024-06-04 02:24:57 +0100 | [diff] [blame] | 129 | } |
| 130 | ty::ExprKind::FunctionCall => { |
| Michael Goulet | 0fc18e3 | 2024-06-06 10:08:55 -0400 | [diff] [blame] | 131 | let (func_ty, func, args) = self.call_args(); |
| Boxy | f076dec | 2024-06-04 02:24:57 +0100 | [diff] [blame] | 132 | let args = args.collect::<Vec<_>>(); |
| Michael Goulet | 0fc18e3 | 2024-06-06 10:08:55 -0400 | [diff] [blame] | 133 | write!(f, "({:?}: {:?})(", func, func_ty)?; |
| Boxy | f076dec | 2024-06-04 02:24:57 +0100 | [diff] [blame] | 134 | for arg in args.iter().rev().skip(1).rev() { |
| Michael Goulet | 0fc18e3 | 2024-06-06 10:08:55 -0400 | [diff] [blame] | 135 | write!(f, "{:?}, ", arg)?; |
| Boxy | 3fdb443 | 2023-07-06 10:17:26 +0100 | [diff] [blame] | 136 | } |
| 137 | if let Some(arg) = args.last() { |
| Michael Goulet | 0fc18e3 | 2024-06-06 10:08:55 -0400 | [diff] [blame] | 138 | write!(f, "{:?}", arg)?; |
| Boxy | 3fdb443 | 2023-07-06 10:17:26 +0100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | write!(f, ")") |
| 142 | } |
| Boxy | f076dec | 2024-06-04 02:24:57 +0100 | [diff] [blame] | 143 | ty::ExprKind::Cast(kind) => { |
| Michael Goulet | 0fc18e3 | 2024-06-06 10:08:55 -0400 | [diff] [blame] | 144 | let (value_ty, value, to_ty) = self.cast_args(); |
| 145 | write!(f, "({kind:?}: ({:?}: {:?}), {:?})", value, value_ty, to_ty) |
| Boxy | 3fdb443 | 2023-07-06 10:17:26 +0100 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| Boxy | e16d71b | 2023-05-16 04:25:25 +0100 | [diff] [blame] | 151 | impl<'tcx> fmt::Debug for ty::Const<'tcx> { |
| 152 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| Ralf Jung | 5a3410a | 2023-09-16 08:36:28 +0200 | [diff] [blame] | 153 | // If this is a value, we spend some effort to make it look nice. |
| Lukas Markeffsky | 885e0f1 | 2025-02-07 19:33:58 +0100 | [diff] [blame] | 154 | if let ConstKind::Value(cv) = self.kind() { |
| Ralf Jung | d61fdbf | 2025-07-28 18:16:47 +0200 | [diff] [blame] | 155 | write!(f, "{}", cv) |
| 156 | } else { |
| 157 | // Fall back to something verbose. |
| 158 | write!(f, "{:?}", self.kind()) |
| Ralf Jung | 5a3410a | 2023-09-16 08:36:28 +0200 | [diff] [blame] | 159 | } |
| Boxy | e16d71b | 2023-05-16 04:25:25 +0100 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| Boxy | 3fdb443 | 2023-07-06 10:17:26 +0100 | [diff] [blame] | 163 | impl<'tcx> fmt::Debug for GenericArg<'tcx> { |
| 164 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| Michael Goulet | 29c3bab | 2025-05-24 12:07:32 +0000 | [diff] [blame] | 165 | match self.kind() { |
| Boxy | 3fdb443 | 2023-07-06 10:17:26 +0100 | [diff] [blame] | 166 | GenericArgKind::Lifetime(lt) => lt.fmt(f), |
| 167 | GenericArgKind::Type(ty) => ty.fmt(f), |
| 168 | GenericArgKind::Const(ct) => ct.fmt(f), |
| 169 | } |
| 170 | } |
| 171 | } |
| Boxy | 3fdb443 | 2023-07-06 10:17:26 +0100 | [diff] [blame] | 172 | |
| 173 | impl<'tcx> fmt::Debug for Region<'tcx> { |
| 174 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 175 | write!(f, "{:?}", self.kind()) |
| 176 | } |
| 177 | } |
| Boxy | 3fdb443 | 2023-07-06 10:17:26 +0100 | [diff] [blame] | 178 | |
| Niko Matsakis | 27d5872 | 2017-11-11 13:04:36 -0500 | [diff] [blame] | 179 | /////////////////////////////////////////////////////////////////////////// |
| 180 | // Atomic structs |
| 181 | // |
| 182 | // For things that don't carry any arena-allocated data (and are |
| Josh Soref | e09d0d2 | 2023-04-09 17:35:02 -0400 | [diff] [blame] | 183 | // copy...), just add them to one of these lists as appropriate. |
| Niko Matsakis | 27d5872 | 2017-11-11 13:04:36 -0500 | [diff] [blame] | 184 | |
| Alan Egerton | 459e142 | 2023-02-10 16:14:18 +0000 | [diff] [blame] | 185 | // For things for which the type library provides traversal implementations |
| Nicholas Nethercote | d28678e | 2025-02-05 12:02:04 +1100 | [diff] [blame] | 186 | // for all Interners, we only need to provide a Lift implementation. |
| Nicholas Nethercote | 6b1980f | 2023-09-14 12:03:56 +1000 | [diff] [blame] | 187 | TrivialLiftImpls! { |
| Nicholas Nethercote | d28678e | 2025-02-05 12:02:04 +1100 | [diff] [blame] | 188 | (), |
| 189 | bool, |
| 190 | usize, |
| 191 | u64, |
| Nicholas Nethercote | d282a67 | 2025-02-05 14:54:13 +1100 | [diff] [blame] | 192 | // tidy-alphabetical-start |
| Folkert de Vries | 1dfc840 | 2025-05-20 20:23:47 +0200 | [diff] [blame] | 193 | crate::mir::Promoted, |
| Nicholas Nethercote | d28678e | 2025-02-05 12:02:04 +1100 | [diff] [blame] | 194 | crate::mir::interpret::AllocId, |
| 195 | crate::mir::interpret::Scalar, |
| Nicholas Nethercote | 94cc5bb | 2025-07-30 14:29:28 +1000 | [diff] [blame] | 196 | crate::ty::ParamConst, |
| Nicholas Nethercote | d28678e | 2025-02-05 12:02:04 +1100 | [diff] [blame] | 197 | rustc_abi::ExternAbi, |
| 198 | rustc_abi::Size, |
| 199 | rustc_hir::Safety, |
| Camille GILLOT | 0460c92 | 2025-07-03 18:41:12 +0000 | [diff] [blame] | 200 | rustc_middle::mir::ConstValue, |
| Nicholas Nethercote | d28678e | 2025-02-05 12:02:04 +1100 | [diff] [blame] | 201 | rustc_type_ir::BoundConstness, |
| 202 | rustc_type_ir::PredicatePolarity, |
| Nicholas Nethercote | d282a67 | 2025-02-05 14:54:13 +1100 | [diff] [blame] | 203 | // tidy-alphabetical-end |
| Alan Egerton | 459e142 | 2023-02-10 16:14:18 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| Nicholas Nethercote | abe2a68 | 2023-09-14 12:05:05 +1000 | [diff] [blame] | 206 | // 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). |
| 209 | TrivialTypeTraversalImpls! { |
| Nicholas Nethercote | d282a67 | 2025-02-05 14:54:13 +1100 | [diff] [blame] | 210 | // tidy-alphabetical-start |
| Nicholas Nethercote | d28678e | 2025-02-05 12:02:04 +1100 | [diff] [blame] | 211 | 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 GILLOT | 0460c92 | 2025-07-03 18:41:12 +0000 | [diff] [blame] | 217 | crate::mir::ConstValue, |
| Nicholas Nethercote | d28678e | 2025-02-05 12:02:04 +1100 | [diff] [blame] | 218 | crate::mir::CoroutineSavedLocal, |
| 219 | crate::mir::FakeReadCause, |
| Mark Mansi | e957ed9 | 2019-02-05 11:20:45 -0600 | [diff] [blame] | 220 | crate::mir::Local, |
| Nicholas Nethercote | d28678e | 2025-02-05 12:02:04 +1100 | [diff] [blame] | 221 | crate::mir::MirPhase, |
| Mark Mansi | e957ed9 | 2019-02-05 11:20:45 -0600 | [diff] [blame] | 222 | crate::mir::Promoted, |
| Nicholas Nethercote | d28678e | 2025-02-05 12:02:04 +1100 | [diff] [blame] | 223 | 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 Mansi | e957ed9 | 2019-02-05 11:20:45 -0600 | [diff] [blame] | 231 | crate::ty::AdtKind, |
| Camille GILLOT | 058e021 | 2020-08-02 15:42:08 +0200 | [diff] [blame] | 232 | crate::ty::AssocItem, |
| Michael Goulet | 3bbe95c | 2022-07-24 19:33:26 +0000 | [diff] [blame] | 233 | crate::ty::AssocKind, |
| James Barford-Evans | 25c1365 | 2025-12-22 14:45:08 +0000 | [diff] [blame] | 234 | crate::ty::BoundRegion<'tcx>, |
| 235 | crate::ty::BoundTy<'tcx>, |
| Boxy Uwu | 6722805 | 2025-11-19 19:30:09 +0000 | [diff] [blame] | 236 | crate::ty::ScalarInt, |
| Nicholas Nethercote | d28678e | 2025-02-05 12:02:04 +1100 | [diff] [blame] | 237 | crate::ty::UserTypeAnnotationIndex, |
| Folkert de Vries | 1dfc840 | 2025-05-20 20:23:47 +0200 | [diff] [blame] | 238 | crate::ty::abstract_const::NotConstEvaluatable, |
| 239 | crate::ty::adjustment::AutoBorrowMutability, |
| 240 | crate::ty::adjustment::PointerCoercion, |
| Nicholas Nethercote | d28678e | 2025-02-05 12:02:04 +1100 | [diff] [blame] | 241 | 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 Scherer | 4f2b108 | 2025-02-12 10:37:49 +0000 | [diff] [blame] | 248 | rustc_hir::RangeEnd, |
| Folkert de Vries | 1dfc840 | 2025-05-20 20:23:47 +0200 | [diff] [blame] | 249 | rustc_hir::def_id::LocalDefId, |
| Nicholas Nethercote | d28678e | 2025-02-05 12:02:04 +1100 | [diff] [blame] | 250 | rustc_span::Ident, |
| 251 | rustc_span::Span, |
| 252 | rustc_span::Symbol, |
| 253 | rustc_target::asm::InlineAsmRegOrRegClass, |
| Nicholas Nethercote | d282a67 | 2025-02-05 14:54:13 +1100 | [diff] [blame] | 254 | // tidy-alphabetical-end |
| Nicholas Nethercote | abe2a68 | 2023-09-14 12:05:05 +1000 | [diff] [blame] | 255 | } |
| Nicholas Nethercote | d28678e | 2025-02-05 12:02:04 +1100 | [diff] [blame] | 256 | |
| Nicholas Nethercote | abe2a68 | 2023-09-14 12:05:05 +1000 | [diff] [blame] | 257 | // 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). |
| 261 | TrivialTypeTraversalAndLiftImpls! { |
| Nicholas Nethercote | d282a67 | 2025-02-05 14:54:13 +1100 | [diff] [blame] | 262 | // tidy-alphabetical-start |
| Camille Gillot | 1a227bd | 2025-11-09 02:57:31 +0000 | [diff] [blame] | 263 | crate::mir::RuntimeChecks, |
| Nicholas Nethercote | abe2a68 | 2023-09-14 12:05:05 +1000 | [diff] [blame] | 264 | crate::ty::ParamTy, |
| Folkert de Vries | 1dfc840 | 2025-05-20 20:23:47 +0200 | [diff] [blame] | 265 | crate::ty::instance::ReifyReason, |
| Nicholas Nethercote | d28678e | 2025-02-05 12:02:04 +1100 | [diff] [blame] | 266 | rustc_hir::def_id::DefId, |
| Nicholas Nethercote | d282a67 | 2025-02-05 14:54:13 +1100 | [diff] [blame] | 267 | // tidy-alphabetical-end |
| lcnr | 196fdf1 | 2024-10-22 23:07:51 +0200 | [diff] [blame] | 268 | } |
| 269 | |
| Niko Matsakis | 27d5872 | 2017-11-11 13:04:36 -0500 | [diff] [blame] | 270 | /////////////////////////////////////////////////////////////////////////// |
| Ariel Ben-Yehuda | 5f564fb | 2015-09-06 21:51:58 +0300 | [diff] [blame] | 271 | // Lift implementations |
| 272 | |
| Camille GILLOT | 9ff0712 | 2023-10-13 20:20:57 +0000 | [diff] [blame] | 273 | impl<'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 Goulet | 5e606c0 | 2024-05-10 14:27:48 -0400 | [diff] [blame] | 280 | impl<'tcx, T: Lift<TyCtxt<'tcx>>> Lift<TyCtxt<'tcx>> for Option<T> { |
| Eduard Burtescu | 8f72d81 | 2016-04-29 06:00:23 +0300 | [diff] [blame] | 281 | type Lifted = Option<T::Lifted>; |
| Michael Goulet | a651050 | 2024-07-16 00:03:37 -0400 | [diff] [blame] | 282 | fn lift_to_interner(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> { |
| Oli Scherer | 3e6c9e5 | 2022-09-21 08:29:19 +0000 | [diff] [blame] | 283 | Some(match self { |
| 284 | Some(x) => Some(tcx.lift(x)?), |
| 285 | None => None, |
| 286 | }) |
| Eduard Burtescu | 8f72d81 | 2016-04-29 06:00:23 +0300 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
| Michael Goulet | 5e606c0 | 2024-05-10 14:27:48 -0400 | [diff] [blame] | 290 | impl<'a, 'tcx> Lift<TyCtxt<'tcx>> for Term<'a> { |
| kadmin | 67f5667 | 2022-01-08 09:28:12 +0000 | [diff] [blame] | 291 | type Lifted = ty::Term<'tcx>; |
| Michael Goulet | a651050 | 2024-07-16 00:03:37 -0400 | [diff] [blame] | 292 | fn lift_to_interner(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> { |
| Michael Goulet | 29c3bab | 2025-05-24 12:07:32 +0000 | [diff] [blame] | 293 | match self.kind() { |
| Michael Goulet | 9fa07a4 | 2024-05-19 13:44:50 -0400 | [diff] [blame] | 294 | TermKind::Ty(ty) => tcx.lift(ty).map(Into::into), |
| 295 | TermKind::Const(c) => tcx.lift(c).map(Into::into), |
| 296 | } |
| kadmin | 67f5667 | 2022-01-08 09:28:12 +0000 | [diff] [blame] | 297 | } |
| 298 | } |
| Eduard-Mihai Burtescu | 74349fa | 2017-08-07 08:08:53 +0300 | [diff] [blame] | 299 | |
| Ariel Ben-Yehuda | 5f564fb | 2015-09-06 21:51:58 +0300 | [diff] [blame] | 300 | /////////////////////////////////////////////////////////////////////////// |
| Alan Egerton | 459e142 | 2023-02-10 16:14:18 +0000 | [diff] [blame] | 301 | // Traversal implementations. |
| Ariel Ben-Yehuda | 5f564fb | 2015-09-06 21:51:58 +0300 | [diff] [blame] | 302 | |
| Alan Egerton | 695072d | 2023-02-22 02:18:40 +0000 | [diff] [blame] | 303 | impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ty::AdtDef<'tcx> { |
| Jason Newcomb | be9b125 | 2024-02-24 17:22:28 -0500 | [diff] [blame] | 304 | fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, _visitor: &mut V) -> V::Result { |
| 305 | V::Result::output() |
| Niko Matsakis | 23837c1 | 2018-02-09 10:34:23 -0500 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
| Oli Scherer | 84acfe8 | 2023-02-02 13:57:36 +0000 | [diff] [blame] | 309 | impl<'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 Goulet | db638ab | 2024-06-18 19:13:54 -0400 | [diff] [blame] | 315 | Ok(if pat == *self { self } else { folder.cx().mk_pat(pat) }) |
| Oli Scherer | 84acfe8 | 2023-02-02 13:57:36 +0000 | [diff] [blame] | 316 | } |
| Michael Goulet | c774adc | 2025-04-14 15:10:43 +0000 | [diff] [blame] | 317 | |
| 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 Scherer | 84acfe8 | 2023-02-02 13:57:36 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | impl<'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 Egerton | 695072d | 2023-02-22 02:18:40 +0000 | [diff] [blame] | 330 | impl<'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 Nethercote | 90db033 | 2022-06-02 11:38:15 +1000 | [diff] [blame] | 335 | folder.try_fold_ty(self) |
| 336 | } |
| Michael Goulet | c774adc | 2025-04-14 15:10:43 +0000 | [diff] [blame] | 337 | |
| 338 | fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self { |
| 339 | folder.fold_ty(self) |
| 340 | } |
| Alan Egerton | f66c06f | 2022-06-17 12:09:23 +0100 | [diff] [blame] | 341 | } |
| Nicholas Nethercote | 90db033 | 2022-06-02 11:38:15 +1000 | [diff] [blame] | 342 | |
| Alan Egerton | 695072d | 2023-02-22 02:18:40 +0000 | [diff] [blame] | 343 | impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for Ty<'tcx> { |
| Jason Newcomb | be9b125 | 2024-02-24 17:22:28 -0500 | [diff] [blame] | 344 | fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result { |
| Nicholas Nethercote | 90db033 | 2022-06-02 11:38:15 +1000 | [diff] [blame] | 345 | visitor.visit_ty(*self) |
| 346 | } |
| 347 | } |
| 348 | |
| Alan Egerton | 9783fcc | 2023-02-11 09:13:27 +0000 | [diff] [blame] | 349 | impl<'tcx> TypeSuperFoldable<TyCtxt<'tcx>> for Ty<'tcx> { |
| Alan Egerton | 695072d | 2023-02-22 02:18:40 +0000 | [diff] [blame] | 350 | fn try_super_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>( |
| Alan Egerton | bfc434b | 2021-12-01 00:55:57 +0000 | [diff] [blame] | 351 | self, |
| 352 | folder: &mut F, |
| 353 | ) -> Result<Self, F::Error> { |
| lcnr | a6cbd64 | 2020-10-24 09:27:15 +0200 | [diff] [blame] | 354 | let kind = match *self.kind() { |
| Michael Goulet | 7be0dbe | 2024-03-21 17:33:10 -0400 | [diff] [blame] | 355 | ty::RawPtr(ty, mutbl) => ty::RawPtr(ty.try_fold_with(folder)?, mutbl), |
| Alan Egerton | bfc434b | 2021-12-01 00:55:57 +0000 | [diff] [blame] | 356 | 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 Dibaiee | e55583c | 2023-07-11 22:35:29 +0100 | [diff] [blame] | 358 | ty::Adt(tid, args) => ty::Adt(tid, args.try_fold_with(folder)?), |
| León Orell Valerian Liehr | 26f3337 | 2025-09-17 04:16:47 +0200 | [diff] [blame] | 359 | ty::Dynamic(trait_ty, region) => { |
| 360 | ty::Dynamic(trait_ty.try_fold_with(folder)?, region.try_fold_with(folder)?) |
| 361 | } |
| Alan Egerton | bfc434b | 2021-12-01 00:55:57 +0000 | [diff] [blame] | 362 | ty::Tuple(ts) => ty::Tuple(ts.try_fold_with(folder)?), |
| Mahdi Dibaiee | e55583c | 2023-07-11 22:35:29 +0100 | [diff] [blame] | 363 | ty::FnDef(def_id, args) => ty::FnDef(def_id, args.try_fold_with(folder)?), |
| Nicholas Nethercote | c4717cc | 2024-08-08 17:18:20 +1000 | [diff] [blame] | 364 | ty::FnPtr(sig_tys, hdr) => ty::FnPtr(sig_tys.try_fold_with(folder)?, hdr), |
| Michael Goulet | 9a1c5eb | 2024-12-21 17:05:40 +0000 | [diff] [blame] | 365 | ty::UnsafeBinder(f) => ty::UnsafeBinder(f.try_fold_with(folder)?), |
| Alan Egerton | bfc434b | 2021-12-01 00:55:57 +0000 | [diff] [blame] | 366 | ty::Ref(r, ty, mutbl) => { |
| 367 | ty::Ref(r.try_fold_with(folder)?, ty.try_fold_with(folder)?, mutbl) |
| 368 | } |
| Michael Goulet | fcb42b4 | 2023-12-21 01:52:10 +0000 | [diff] [blame] | 369 | ty::Coroutine(did, args) => ty::Coroutine(did, args.try_fold_with(folder)?), |
| Oli Scherer | 6095683 | 2023-10-19 16:06:43 +0000 | [diff] [blame] | 370 | ty::CoroutineWitness(did, args) => { |
| 371 | ty::CoroutineWitness(did, args.try_fold_with(folder)?) |
| Camille GILLOT | 1974b6b | 2022-10-01 14:56:24 +0200 | [diff] [blame] | 372 | } |
| Mahdi Dibaiee | e55583c | 2023-07-11 22:35:29 +0100 | [diff] [blame] | 373 | ty::Closure(did, args) => ty::Closure(did, args.try_fold_with(folder)?), |
| Michael Goulet | c567edd | 2024-01-24 18:01:56 +0000 | [diff] [blame] | 374 | ty::CoroutineClosure(did, args) => { |
| 375 | ty::CoroutineClosure(did, args.try_fold_with(folder)?) |
| 376 | } |
| Michael Goulet | 96cb18e | 2022-11-27 17:52:17 +0000 | [diff] [blame] | 377 | ty::Alias(kind, data) => ty::Alias(kind, data.try_fold_with(folder)?), |
| Oli Scherer | 84acfe8 | 2023-02-02 13:57:36 +0000 | [diff] [blame] | 378 | ty::Pat(ty, pat) => ty::Pat(ty.try_fold_with(folder)?, pat.try_fold_with(folder)?), |
| scalexm | 1003b7f | 2018-10-22 20:37:56 +0200 | [diff] [blame] | 379 | |
| Mark Rousskov | a06baa5 | 2019-12-22 17:42:04 -0500 | [diff] [blame] | 380 | ty::Bool |
| 381 | | ty::Char |
| 382 | | ty::Str |
| 383 | | ty::Int(_) |
| 384 | | ty::Uint(_) |
| 385 | | ty::Float(_) |
| mark | 268decb | 2020-05-05 23:02:09 -0500 | [diff] [blame] | 386 | | ty::Error(_) |
| Mark Rousskov | a06baa5 | 2019-12-22 17:42:04 -0500 | [diff] [blame] | 387 | | ty::Infer(_) |
| 388 | | ty::Param(..) |
| 389 | | ty::Bound(..) |
| 390 | | ty::Placeholder(..) |
| 391 | | ty::Never |
| LeSeulArtichaut | 6e3fa20 | 2021-05-19 13:34:54 +0200 | [diff] [blame] | 392 | | ty::Foreign(..) => return Ok(self), |
| Jeffrey Seyfried | f9808ea | 2015-11-18 09:38:57 +0000 | [diff] [blame] | 393 | }; |
| Nicholas Nethercote | 4d0618e | 2016-11-24 21:10:08 +1100 | [diff] [blame] | 394 | |
| Michael Goulet | db638ab | 2024-06-18 19:13:54 -0400 | [diff] [blame] | 395 | Ok(if *self.kind() == kind { self } else { folder.cx().mk_ty_from_kind(kind) }) |
| Jeffrey Seyfried | f9808ea | 2015-11-18 09:38:57 +0000 | [diff] [blame] | 396 | } |
| Michael Goulet | c774adc | 2025-04-14 15:10:43 +0000 | [diff] [blame] | 397 | |
| 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 Liehr | 26f3337 | 2025-09-17 04:16:47 +0200 | [diff] [blame] | 404 | ty::Dynamic(trait_ty, region) => { |
| 405 | ty::Dynamic(trait_ty.fold_with(folder), region.fold_with(folder)) |
| Michael Goulet | c774adc | 2025-04-14 15:10:43 +0000 | [diff] [blame] | 406 | } |
| 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 Egerton | f66c06f | 2022-06-17 12:09:23 +0100 | [diff] [blame] | 436 | } |
| Jeffrey Seyfried | f9808ea | 2015-11-18 09:38:57 +0000 | [diff] [blame] | 437 | |
| Alan Egerton | dea342d | 2023-02-09 19:38:07 +0000 | [diff] [blame] | 438 | impl<'tcx> TypeSuperVisitable<TyCtxt<'tcx>> for Ty<'tcx> { |
| Jason Newcomb | be9b125 | 2024-02-24 17:22:28 -0500 | [diff] [blame] | 439 | fn super_visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result { |
| LeSeulArtichaut | 3e14b68 | 2020-08-03 00:49:11 +0200 | [diff] [blame] | 440 | match self.kind() { |
| Michael Goulet | 7be0dbe | 2024-03-21 17:33:10 -0400 | [diff] [blame] | 441 | ty::RawPtr(ty, _mutbl) => ty.visit_with(visitor), |
| LeSeulArtichaut | 2c85b6f | 2020-10-21 14:22:44 +0200 | [diff] [blame] | 442 | ty::Array(typ, sz) => { |
| Jason Newcomb | be9b125 | 2024-02-24 17:22:28 -0500 | [diff] [blame] | 443 | try_visit!(typ.visit_with(visitor)); |
| LeSeulArtichaut | 2c85b6f | 2020-10-21 14:22:44 +0200 | [diff] [blame] | 444 | sz.visit_with(visitor) |
| 445 | } |
| varkor | 6f637da | 2018-08-22 01:35:02 +0100 | [diff] [blame] | 446 | ty::Slice(typ) => typ.visit_with(visitor), |
| Mahdi Dibaiee | e55583c | 2023-07-11 22:35:29 +0100 | [diff] [blame] | 447 | ty::Adt(_, args) => args.visit_with(visitor), |
| León Orell Valerian Liehr | 26f3337 | 2025-09-17 04:16:47 +0200 | [diff] [blame] | 448 | ty::Dynamic(trait_ty, reg) => { |
| Jason Newcomb | be9b125 | 2024-02-24 17:22:28 -0500 | [diff] [blame] | 449 | try_visit!(trait_ty.visit_with(visitor)); |
| LeSeulArtichaut | 2c85b6f | 2020-10-21 14:22:44 +0200 | [diff] [blame] | 450 | reg.visit_with(visitor) |
| Mark Rousskov | a06baa5 | 2019-12-22 17:42:04 -0500 | [diff] [blame] | 451 | } |
| varkor | 6f637da | 2018-08-22 01:35:02 +0100 | [diff] [blame] | 452 | ty::Tuple(ts) => ts.visit_with(visitor), |
| Mahdi Dibaiee | e55583c | 2023-07-11 22:35:29 +0100 | [diff] [blame] | 453 | ty::FnDef(_, args) => args.visit_with(visitor), |
| Michael Goulet | 3d5438ac | 2025-02-20 18:28:48 +0000 | [diff] [blame] | 454 | ty::FnPtr(sig_tys, _) => sig_tys.visit_with(visitor), |
| 455 | ty::UnsafeBinder(f) => f.visit_with(visitor), |
| LeSeulArtichaut | 2c85b6f | 2020-10-21 14:22:44 +0200 | [diff] [blame] | 456 | ty::Ref(r, ty, _) => { |
| Jason Newcomb | be9b125 | 2024-02-24 17:22:28 -0500 | [diff] [blame] | 457 | try_visit!(r.visit_with(visitor)); |
| LeSeulArtichaut | 2c85b6f | 2020-10-21 14:22:44 +0200 | [diff] [blame] | 458 | ty.visit_with(visitor) |
| 459 | } |
| Michael Goulet | 3d5438ac | 2025-02-20 18:28:48 +0000 | [diff] [blame] | 460 | 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), |
| scalexm | 1003b7f | 2018-10-22 20:37:56 +0200 | [diff] [blame] | 465 | |
| Oli Scherer | 84acfe8 | 2023-02-02 13:57:36 +0000 | [diff] [blame] | 466 | ty::Pat(ty, pat) => { |
| 467 | try_visit!(ty.visit_with(visitor)); |
| 468 | pat.visit_with(visitor) |
| 469 | } |
| 470 | |
| Oli Scherer | fb98fbb | 2024-07-16 11:31:04 +0000 | [diff] [blame] | 471 | ty::Error(guar) => guar.visit_with(visitor), |
| 472 | |
| Mark Rousskov | a06baa5 | 2019-12-22 17:42:04 -0500 | [diff] [blame] | 473 | ty::Bool |
| 474 | | ty::Char |
| 475 | | ty::Str |
| 476 | | ty::Int(_) |
| 477 | | ty::Uint(_) |
| 478 | | ty::Float(_) |
| Mark Rousskov | a06baa5 | 2019-12-22 17:42:04 -0500 | [diff] [blame] | 479 | | ty::Infer(_) |
| 480 | | ty::Bound(..) |
| 481 | | ty::Placeholder(..) |
| 482 | | ty::Param(..) |
| 483 | | ty::Never |
| Jason Newcomb | be9b125 | 2024-02-24 17:22:28 -0500 | [diff] [blame] | 484 | | ty::Foreign(..) => V::Result::output(), |
| Jeffrey Seyfried | f9808ea | 2015-11-18 09:38:57 +0000 | [diff] [blame] | 485 | } |
| 486 | } |
| Ariel Ben-Yehuda | 5f564fb | 2015-09-06 21:51:58 +0300 | [diff] [blame] | 487 | } |
| 488 | |
| Alan Egerton | 695072d | 2023-02-22 02:18:40 +0000 | [diff] [blame] | 489 | impl<'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 Egerton | bfc434b | 2021-12-01 00:55:57 +0000 | [diff] [blame] | 494 | folder.try_fold_region(self) |
| Ariel Ben-Yehuda | 5f564fb | 2015-09-06 21:51:58 +0300 | [diff] [blame] | 495 | } |
| Michael Goulet | c774adc | 2025-04-14 15:10:43 +0000 | [diff] [blame] | 496 | |
| 497 | fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self { |
| 498 | folder.fold_region(self) |
| 499 | } |
| Alan Egerton | f66c06f | 2022-06-17 12:09:23 +0100 | [diff] [blame] | 500 | } |
| Jeffrey Seyfried | f9808ea | 2015-11-18 09:38:57 +0000 | [diff] [blame] | 501 | |
| Alan Egerton | 695072d | 2023-02-22 02:18:40 +0000 | [diff] [blame] | 502 | impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ty::Region<'tcx> { |
| Jason Newcomb | be9b125 | 2024-02-24 17:22:28 -0500 | [diff] [blame] | 503 | fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result { |
| Jeffrey Seyfried | f9808ea | 2015-11-18 09:38:57 +0000 | [diff] [blame] | 504 | visitor.visit_region(*self) |
| 505 | } |
| Ariel Ben-Yehuda | 5f564fb | 2015-09-06 21:51:58 +0300 | [diff] [blame] | 506 | } |
| 507 | |
| Alan Egerton | 695072d | 2023-02-22 02:18:40 +0000 | [diff] [blame] | 508 | impl<'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 Egerton | bfc434b | 2021-12-01 00:55:57 +0000 | [diff] [blame] | 513 | folder.try_fold_predicate(self) |
| lcnr | 2140016 | 2021-07-19 12:13:25 +0200 | [diff] [blame] | 514 | } |
| Michael Goulet | c774adc | 2025-04-14 15:10:43 +0000 | [diff] [blame] | 515 | |
| 516 | fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self { |
| 517 | folder.fold_predicate(self) |
| 518 | } |
| Alan Egerton | f66c06f | 2022-06-17 12:09:23 +0100 | [diff] [blame] | 519 | } |
| lcnr | 2140016 | 2021-07-19 12:13:25 +0200 | [diff] [blame] | 520 | |
| Michael Goulet | 21226ee | 2023-06-16 06:27:41 +0000 | [diff] [blame] | 521 | // FIXME(clause): This is wonky |
| 522 | impl<'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 Goulet | 2fa796a | 2023-06-19 20:48:46 +0000 | [diff] [blame] | 527 | Ok(folder.try_fold_predicate(self.as_predicate())?.expect_clause()) |
| Michael Goulet | 21226ee | 2023-06-16 06:27:41 +0000 | [diff] [blame] | 528 | } |
| Michael Goulet | c774adc | 2025-04-14 15:10:43 +0000 | [diff] [blame] | 529 | |
| 530 | fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self { |
| 531 | folder.fold_predicate(self.as_predicate()).expect_clause() |
| 532 | } |
| Michael Goulet | 21226ee | 2023-06-16 06:27:41 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| lcnr | c56efae | 2025-05-23 14:46:38 +0000 | [diff] [blame] | 535 | impl<'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 Egerton | 695072d | 2023-02-22 02:18:40 +0000 | [diff] [blame] | 548 | impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ty::Predicate<'tcx> { |
| Jason Newcomb | be9b125 | 2024-02-24 17:22:28 -0500 | [diff] [blame] | 549 | fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result { |
| Matthew Jasper | f802ee1 | 2020-06-10 09:30:39 +0100 | [diff] [blame] | 550 | visitor.visit_predicate(*self) |
| 551 | } |
| Matthew Jasper | f802ee1 | 2020-06-10 09:30:39 +0100 | [diff] [blame] | 552 | } |
| 553 | |
| Michael Goulet | 21226ee | 2023-06-16 06:27:41 +0000 | [diff] [blame] | 554 | impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ty::Clause<'tcx> { |
| Jason Newcomb | be9b125 | 2024-02-24 17:22:28 -0500 | [diff] [blame] | 555 | fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result { |
| Michael Goulet | 21226ee | 2023-06-16 06:27:41 +0000 | [diff] [blame] | 556 | visitor.visit_predicate(self.as_predicate()) |
| 557 | } |
| 558 | } |
| 559 | |
| Alan Egerton | 9783fcc | 2023-02-11 09:13:27 +0000 | [diff] [blame] | 560 | impl<'tcx> TypeSuperFoldable<TyCtxt<'tcx>> for ty::Predicate<'tcx> { |
| Alan Egerton | 695072d | 2023-02-22 02:18:40 +0000 | [diff] [blame] | 561 | fn try_super_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>( |
| Alan Egerton | bfc434b | 2021-12-01 00:55:57 +0000 | [diff] [blame] | 562 | self, |
| 563 | folder: &mut F, |
| 564 | ) -> Result<Self, F::Error> { |
| Nicholas Nethercote | a07f717 | 2026-01-07 17:19:36 +1100 | [diff] [blame] | 565 | // 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 Nethercote | 90db033 | 2022-06-02 11:38:15 +1000 | [diff] [blame] | 571 | let new = self.kind().try_fold_with(folder)?; |
| Michael Goulet | db638ab | 2024-06-18 19:13:54 -0400 | [diff] [blame] | 572 | Ok(folder.cx().reuse_or_mk_predicate(self, new)) |
| Niko Matsakis | 5fb0f0d | 2017-05-23 04:19:47 -0400 | [diff] [blame] | 573 | } |
| Michael Goulet | c774adc | 2025-04-14 15:10:43 +0000 | [diff] [blame] | 574 | |
| 575 | fn super_fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self { |
| Nicholas Nethercote | a07f717 | 2026-01-07 17:19:36 +1100 | [diff] [blame] | 576 | // See comment in `Predicate::try_super_fold_with`. |
| Michael Goulet | c774adc | 2025-04-14 15:10:43 +0000 | [diff] [blame] | 577 | let new = self.kind().fold_with(folder); |
| 578 | folder.cx().reuse_or_mk_predicate(self, new) |
| 579 | } |
| Alan Egerton | f66c06f | 2022-06-17 12:09:23 +0100 | [diff] [blame] | 580 | } |
| Niko Matsakis | 5fb0f0d | 2017-05-23 04:19:47 -0400 | [diff] [blame] | 581 | |
| Alan Egerton | dea342d | 2023-02-09 19:38:07 +0000 | [diff] [blame] | 582 | impl<'tcx> TypeSuperVisitable<TyCtxt<'tcx>> for ty::Predicate<'tcx> { |
| Jason Newcomb | be9b125 | 2024-02-24 17:22:28 -0500 | [diff] [blame] | 583 | fn super_visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result { |
| Nicholas Nethercote | a07f717 | 2026-01-07 17:19:36 +1100 | [diff] [blame] | 584 | // See comment in `Predicate::try_super_fold_with`. |
| Nicholas Nethercote | 90db033 | 2022-06-02 11:38:15 +1000 | [diff] [blame] | 585 | self.kind().visit_with(visitor) |
| 586 | } |
| 587 | } |
| 588 | |
| Lukas Markeffsky | fcc477f | 2024-03-24 22:49:31 +0100 | [diff] [blame] | 589 | impl<'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 | |
| 595 | impl<'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 | |
| lcnr | c56efae | 2025-05-23 14:46:38 +0000 | [diff] [blame] | 601 | impl<'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 Egerton | 695072d | 2023-02-22 02:18:40 +0000 | [diff] [blame] | 614 | impl<'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 Nethercote | 90db033 | 2022-06-02 11:38:15 +1000 | [diff] [blame] | 619 | folder.try_fold_const(self) |
| 620 | } |
| Michael Goulet | c774adc | 2025-04-14 15:10:43 +0000 | [diff] [blame] | 621 | |
| 622 | fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self { |
| 623 | folder.fold_const(self) |
| 624 | } |
| Alan Egerton | f66c06f | 2022-06-17 12:09:23 +0100 | [diff] [blame] | 625 | } |
| Nicholas Nethercote | 90db033 | 2022-06-02 11:38:15 +1000 | [diff] [blame] | 626 | |
| Alan Egerton | 695072d | 2023-02-22 02:18:40 +0000 | [diff] [blame] | 627 | impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ty::Const<'tcx> { |
| Jason Newcomb | be9b125 | 2024-02-24 17:22:28 -0500 | [diff] [blame] | 628 | fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result { |
| Nicholas Nethercote | 90db033 | 2022-06-02 11:38:15 +1000 | [diff] [blame] | 629 | visitor.visit_const(*self) |
| 630 | } |
| 631 | } |
| 632 | |
| Alan Egerton | 9783fcc | 2023-02-11 09:13:27 +0000 | [diff] [blame] | 633 | impl<'tcx> TypeSuperFoldable<TyCtxt<'tcx>> for ty::Const<'tcx> { |
| Alan Egerton | 695072d | 2023-02-22 02:18:40 +0000 | [diff] [blame] | 634 | fn try_super_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>( |
| Alan Egerton | bfc434b | 2021-12-01 00:55:57 +0000 | [diff] [blame] | 635 | self, |
| 636 | folder: &mut F, |
| 637 | ) -> Result<Self, F::Error> { |
| Boxy | 62174bf | 2023-07-04 15:41:45 +0100 | [diff] [blame] | 638 | let kind = match self.kind() { |
| Boxy | 62174bf | 2023-07-04 15:41:45 +0100 | [diff] [blame] | 639 | ConstKind::Unevaluated(uv) => ConstKind::Unevaluated(uv.try_fold_with(folder)?), |
| Lukas Markeffsky | 10fc0b1 | 2025-01-27 04:30:00 +0100 | [diff] [blame] | 640 | ConstKind::Value(v) => ConstKind::Value(v.try_fold_with(folder)?), |
| Boxy | 62174bf | 2023-07-04 15:41:45 +0100 | [diff] [blame] | 641 | ConstKind::Expr(e) => ConstKind::Expr(e.try_fold_with(folder)?), |
| Nicholas Nethercote | 94cc5bb | 2025-07-30 14:29:28 +1000 | [diff] [blame] | 642 | |
| 643 | ConstKind::Param(_) |
| 644 | | ConstKind::Infer(_) |
| 645 | | ConstKind::Bound(..) |
| 646 | | ConstKind::Placeholder(_) |
| 647 | | ConstKind::Error(_) => return Ok(self), |
| Boxy | 62174bf | 2023-07-04 15:41:45 +0100 | [diff] [blame] | 648 | }; |
| Michael Goulet | db638ab | 2024-06-18 19:13:54 -0400 | [diff] [blame] | 649 | if kind != self.kind() { Ok(folder.cx().mk_ct_from_kind(kind)) } else { Ok(self) } |
| Eduard-Mihai Burtescu | 932289c | 2017-08-04 11:25:13 +0300 | [diff] [blame] | 650 | } |
| Michael Goulet | c774adc | 2025-04-14 15:10:43 +0000 | [diff] [blame] | 651 | |
| 652 | fn super_fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self { |
| 653 | let kind = match self.kind() { |
| Michael Goulet | c774adc | 2025-04-14 15:10:43 +0000 | [diff] [blame] | 654 | ConstKind::Unevaluated(uv) => ConstKind::Unevaluated(uv.fold_with(folder)), |
| 655 | ConstKind::Value(v) => ConstKind::Value(v.fold_with(folder)), |
| Michael Goulet | c774adc | 2025-04-14 15:10:43 +0000 | [diff] [blame] | 656 | ConstKind::Expr(e) => ConstKind::Expr(e.fold_with(folder)), |
| Nicholas Nethercote | 94cc5bb | 2025-07-30 14:29:28 +1000 | [diff] [blame] | 657 | |
| 658 | ConstKind::Param(_) |
| 659 | | ConstKind::Infer(_) |
| 660 | | ConstKind::Bound(..) |
| 661 | | ConstKind::Placeholder(_) |
| 662 | | ConstKind::Error(_) => return self, |
| Michael Goulet | c774adc | 2025-04-14 15:10:43 +0000 | [diff] [blame] | 663 | }; |
| 664 | if kind != self.kind() { folder.cx().mk_ct_from_kind(kind) } else { self } |
| 665 | } |
| Alan Egerton | f66c06f | 2022-06-17 12:09:23 +0100 | [diff] [blame] | 666 | } |
| Eduard-Mihai Burtescu | 932289c | 2017-08-04 11:25:13 +0300 | [diff] [blame] | 667 | |
| Alan Egerton | dea342d | 2023-02-09 19:38:07 +0000 | [diff] [blame] | 668 | impl<'tcx> TypeSuperVisitable<TyCtxt<'tcx>> for ty::Const<'tcx> { |
| Jason Newcomb | be9b125 | 2024-02-24 17:22:28 -0500 | [diff] [blame] | 669 | fn super_visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result { |
| Boxy | 62174bf | 2023-07-04 15:41:45 +0100 | [diff] [blame] | 670 | match self.kind() { |
| Boxy | 62174bf | 2023-07-04 15:41:45 +0100 | [diff] [blame] | 671 | ConstKind::Unevaluated(uv) => uv.visit_with(visitor), |
| Lukas Markeffsky | 10fc0b1 | 2025-01-27 04:30:00 +0100 | [diff] [blame] | 672 | ConstKind::Value(v) => v.visit_with(visitor), |
| Boxy | 62174bf | 2023-07-04 15:41:45 +0100 | [diff] [blame] | 673 | ConstKind::Expr(e) => e.visit_with(visitor), |
| Nicholas Nethercote | 94cc5bb | 2025-07-30 14:29:28 +1000 | [diff] [blame] | 674 | ConstKind::Error(e) => e.visit_with(visitor), |
| 675 | |
| 676 | ConstKind::Param(_) |
| 677 | | ConstKind::Infer(_) |
| 678 | | ConstKind::Bound(..) |
| 679 | | ConstKind::Placeholder(_) => V::Result::output(), |
| Boxy | 62174bf | 2023-07-04 15:41:45 +0100 | [diff] [blame] | 680 | } |
| Eduard-Mihai Burtescu | 932289c | 2017-08-04 11:25:13 +0300 | [diff] [blame] | 681 | } |
| Eduard-Mihai Burtescu | 932289c | 2017-08-04 11:25:13 +0300 | [diff] [blame] | 682 | } |
| Ariel Ben-Yehuda | 5f564fb | 2015-09-06 21:51:58 +0300 | [diff] [blame] | 683 | |
| Boxy Uwu | 6722805 | 2025-11-19 19:30:09 +0000 | [diff] [blame] | 684 | impl<'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 | |
| 691 | impl<'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 Scherer | fb98fbb | 2024-07-16 11:31:04 +0000 | [diff] [blame] | 715 | impl<'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 | |
| 721 | impl<'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 Scherer | fb98fbb | 2024-07-16 11:31:04 +0000 | [diff] [blame] | 728 | |
| Michael Goulet | c774adc | 2025-04-14 15:10:43 +0000 | [diff] [blame] | 729 | fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, _folder: &mut F) -> Self { |
| 730 | self |
| varkor | c619754 | 2019-05-06 14:05:26 +0100 | [diff] [blame] | 731 | } |
| 732 | } |
| Ellen | dec8ed4 | 2022-01-12 23:29:10 +0000 | [diff] [blame] | 733 | |
| Alan Egerton | 695072d | 2023-02-22 02:18:40 +0000 | [diff] [blame] | 734 | impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for TyAndLayout<'tcx, Ty<'tcx>> { |
| Jason Newcomb | be9b125 | 2024-02-24 17:22:28 -0500 | [diff] [blame] | 735 | fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result { |
| Ben Kimock | 5bfad5c | 2023-01-22 17:06:28 -0500 | [diff] [blame] | 736 | visitor.visit_ty(self.ty) |
| 737 | } |
| 738 | } |
| Martin Nordholts | 924ea05 | 2024-01-12 08:22:05 +0100 | [diff] [blame] | 739 | |
| 740 | impl<'tcx, T: TypeVisitable<TyCtxt<'tcx>> + Debug + Clone> TypeVisitable<TyCtxt<'tcx>> |
| 741 | for Spanned<T> |
| 742 | { |
| Jason Newcomb | be9b125 | 2024-02-24 17:22:28 -0500 | [diff] [blame] | 743 | 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 Nordholts | 924ea05 | 2024-01-12 08:22:05 +0100 | [diff] [blame] | 746 | } |
| 747 | } |
| 748 | |
| 749 | impl<'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 Nethercote | d28678e | 2025-02-05 12:02:04 +1100 | [diff] [blame] | 761 | |
| Michael Goulet | c774adc | 2025-04-14 15:10:43 +0000 | [diff] [blame] | 762 | 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 Nethercote | d28678e | 2025-02-05 12:02:04 +1100 | [diff] [blame] | 764 | } |
| 765 | } |
| 766 | |
| 767 | impl<'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 Goulet | c774adc | 2025-04-14 15:10:43 +0000 | [diff] [blame] | 774 | |
| 775 | fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, _folder: &mut F) -> Self { |
| 776 | self |
| 777 | } |
| Nicholas Nethercote | d28678e | 2025-02-05 12:02:04 +1100 | [diff] [blame] | 778 | } |
| 779 | |
| Michael Goulet | c774adc | 2025-04-14 15:10:43 +0000 | [diff] [blame] | 780 | macro_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 Nethercote | d28678e | 2025-02-05 12:02:04 +1100 | [diff] [blame] | 799 | } |
| 800 | } |
| Michael Goulet | c774adc | 2025-04-14 15:10:43 +0000 | [diff] [blame] | 801 | |
| 802 | list_fold! { |
| Michael Goulet | c774adc | 2025-04-14 15:10:43 +0000 | [diff] [blame] | 803 | &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>> : mk_poly_existential_predicates, |
| lcnr | 1acd65c | 2025-09-22 14:09:03 +0200 | [diff] [blame] | 804 | &'tcx ty::List<(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)>: mk_predefined_opaques_in_body, |
| Michael Goulet | c774adc | 2025-04-14 15:10:43 +0000 | [diff] [blame] | 805 | &'tcx ty::List<PlaceElem<'tcx>> : mk_place_elems, |
| Oli Scherer | b023856 | 2025-02-27 09:46:46 +0000 | [diff] [blame] | 806 | &'tcx ty::List<ty::Pattern<'tcx>> : mk_patterns, |
| Michael Goulet | 3634f46 | 2025-07-15 16:01:43 +0000 | [diff] [blame] | 807 | &'tcx ty::List<ty::ArgOutlivesPredicate<'tcx>> : mk_outlives, |
| Michael Goulet | c774adc | 2025-04-14 15:10:43 +0000 | [diff] [blame] | 808 | } |