]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_middle/src/ty/structural_impls.rs
New upstream version 1.58.1+dfsg1
[rustc.git] / compiler / rustc_middle / src / ty / structural_impls.rs
CommitLineData
ff7c6d11
XL
1//! This module contains implements of the `Lift` and `TypeFoldable`
2//! traits for various types in the Rust compiler. Most are written by
60c5eb7d 3//! hand, though we've recently added some macros and proc-macros to help with the tedium.
ff7c6d11 4
60c5eb7d 5use crate::mir::interpret;
dfeec247 6use crate::mir::ProjectionKind;
9fa01778 7use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
1b1a35ee 8use crate::ty::print::{with_no_trimmed_paths, FmtPrinter, Printer};
dfeec247 9use crate::ty::{self, InferConst, Lift, Ty, TyCtxt};
fc512014 10use rustc_data_structures::functor::IdFunctor;
dfeec247
XL
11use rustc_hir::def::Namespace;
12use rustc_hir::def_id::CRATE_DEF_INDEX;
13use rustc_index::vec::{Idx, IndexVec};
60c5eb7d 14
532ac7d7 15use std::fmt;
29967ef6 16use std::ops::ControlFlow;
e9174d1e 17use std::rc::Rc;
dc9dc135 18use std::sync::Arc;
532ac7d7
XL
19
20impl fmt::Debug for ty::TraitDef {
21 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22 ty::tls::with(|tcx| {
1b1a35ee
XL
23 with_no_trimmed_paths(|| {
24 FmtPrinter::new(tcx, f, Namespace::TypeNS).print_def_path(self.def_id, &[])
25 })?;
532ac7d7
XL
26 Ok(())
27 })
28 }
29}
30
31impl fmt::Debug for ty::AdtDef {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 ty::tls::with(|tcx| {
1b1a35ee
XL
34 with_no_trimmed_paths(|| {
35 FmtPrinter::new(tcx, f, Namespace::TypeNS).print_def_path(self.did, &[])
36 })?;
532ac7d7
XL
37 Ok(())
38 })
39 }
40}
41
532ac7d7
XL
42impl fmt::Debug for ty::UpvarId {
43 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
dfeec247
XL
44 let name = ty::tls::with(|tcx| tcx.hir().name(self.var_path.hir_id));
45 write!(f, "UpvarId({:?};`{}`;{:?})", self.var_path.hir_id, name, self.closure_expr_id)
532ac7d7
XL
46 }
47}
48
49impl fmt::Debug for ty::UpvarBorrow<'tcx> {
50 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
dfeec247 51 write!(f, "UpvarBorrow({:?}, {:?})", self.kind, self.region)
532ac7d7
XL
52 }
53}
54
55impl fmt::Debug for ty::ExistentialTraitRef<'tcx> {
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1b1a35ee 57 with_no_trimmed_paths(|| fmt::Display::fmt(self, f))
532ac7d7
XL
58 }
59}
60
61impl fmt::Debug for ty::adjustment::Adjustment<'tcx> {
62 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63 write!(f, "{:?} -> {}", self.kind, self.target)
64 }
65}
66
fc512014 67impl fmt::Debug for ty::BoundRegionKind {
532ac7d7
XL
68 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69 match *self {
70 ty::BrAnon(n) => write!(f, "BrAnon({:?})", n),
532ac7d7 71 ty::BrNamed(did, name) => {
60c5eb7d
XL
72 if did.index == CRATE_DEF_INDEX {
73 write!(f, "BrNamed({})", name)
74 } else {
75 write!(f, "BrNamed({:?}, {})", did, name)
76 }
532ac7d7
XL
77 }
78 ty::BrEnv => write!(f, "BrEnv"),
79 }
80 }
81}
82
83impl fmt::Debug for ty::RegionKind {
84 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85 match *self {
dfeec247 86 ty::ReEarlyBound(ref data) => write!(f, "ReEarlyBound({}, {})", data.index, data.name),
532ac7d7 87
532ac7d7
XL
88 ty::ReLateBound(binder_id, ref bound_region) => {
89 write!(f, "ReLateBound({:?}, {:?})", binder_id, bound_region)
90 }
91
92 ty::ReFree(ref fr) => fr.fmt(f),
93
532ac7d7
XL
94 ty::ReStatic => write!(f, "ReStatic"),
95
96 ty::ReVar(ref vid) => vid.fmt(f),
97
dfeec247 98 ty::RePlaceholder(placeholder) => write!(f, "RePlaceholder({:?})", placeholder),
532ac7d7 99
74b04a01 100 ty::ReEmpty(ui) => write!(f, "ReEmpty({:?})", ui),
532ac7d7
XL
101
102 ty::ReErased => write!(f, "ReErased"),
103 }
104 }
105}
106
107impl fmt::Debug for ty::FreeRegion {
108 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109 write!(f, "ReFree({:?}, {:?})", self.scope, self.bound_region)
110 }
111}
112
532ac7d7
XL
113impl fmt::Debug for ty::FnSig<'tcx> {
114 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
dfeec247 115 write!(f, "({:?}; c_variadic: {})->{:?}", self.inputs(), self.c_variadic, self.output())
532ac7d7
XL
116 }
117}
118
532ac7d7
XL
119impl<'tcx> fmt::Debug for ty::ConstVid<'tcx> {
120 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
121 write!(f, "_#{}c", self.index)
122 }
123}
124
532ac7d7
XL
125impl fmt::Debug for ty::RegionVid {
126 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127 write!(f, "'_#{}r", self.index())
128 }
129}
130
532ac7d7
XL
131impl fmt::Debug for ty::TraitRef<'tcx> {
132 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1b1a35ee 133 with_no_trimmed_paths(|| fmt::Display::fmt(self, f))
532ac7d7
XL
134 }
135}
136
137impl fmt::Debug for Ty<'tcx> {
138 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1b1a35ee 139 with_no_trimmed_paths(|| fmt::Display::fmt(self, f))
532ac7d7
XL
140 }
141}
142
143impl fmt::Debug for ty::ParamTy {
144 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48663c56 145 write!(f, "{}/#{}", self.name, self.index)
532ac7d7
XL
146 }
147}
148
149impl fmt::Debug for ty::ParamConst {
150 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
151 write!(f, "{}/#{}", self.name, self.index)
152 }
153}
154
155impl fmt::Debug for ty::TraitPredicate<'tcx> {
156 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94222f64
XL
157 if let ty::BoundConstness::ConstIfConst = self.constness {
158 write!(f, "~const ")?;
159 }
3c0e092e 160 write!(f, "TraitPredicate({:?}, polarity:{:?})", self.trait_ref, self.polarity)
532ac7d7
XL
161 }
162}
163
164impl fmt::Debug for ty::ProjectionPredicate<'tcx> {
165 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
166 write!(f, "ProjectionPredicate({:?}, {:?})", self.projection_ty, self.ty)
167 }
168}
169
170impl fmt::Debug for ty::Predicate<'tcx> {
f9f354fc
XL
171 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
172 write!(f, "{:?}", self.kind())
173 }
174}
175
176impl fmt::Debug for ty::PredicateKind<'tcx> {
532ac7d7
XL
177 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
178 match *self {
94222f64 179 ty::PredicateKind::Trait(ref a) => a.fmt(f),
5869c6ff 180 ty::PredicateKind::Subtype(ref pair) => pair.fmt(f),
94222f64 181 ty::PredicateKind::Coerce(ref pair) => pair.fmt(f),
5869c6ff
XL
182 ty::PredicateKind::RegionOutlives(ref pair) => pair.fmt(f),
183 ty::PredicateKind::TypeOutlives(ref pair) => pair.fmt(f),
184 ty::PredicateKind::Projection(ref pair) => pair.fmt(f),
185 ty::PredicateKind::WellFormed(data) => write!(f, "WellFormed({:?})", data),
186 ty::PredicateKind::ObjectSafe(trait_def_id) => {
f9f354fc
XL
187 write!(f, "ObjectSafe({:?})", trait_def_id)
188 }
5869c6ff 189 ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => {
dfeec247 190 write!(f, "ClosureKind({:?}, {:?}, {:?})", closure_def_id, closure_substs, kind)
532ac7d7 191 }
94222f64
XL
192 ty::PredicateKind::ConstEvaluatable(uv) => {
193 write!(f, "ConstEvaluatable({:?}, {:?})", uv.def, uv.substs_)
532ac7d7 194 }
5869c6ff
XL
195 ty::PredicateKind::ConstEquate(c1, c2) => write!(f, "ConstEquate({:?}, {:?})", c1, c2),
196 ty::PredicateKind::TypeWellFormedFromEnv(ty) => {
1b1a35ee
XL
197 write!(f, "TypeWellFormedFromEnv({:?})", ty)
198 }
532ac7d7
XL
199 }
200 }
201}
202
ff7c6d11
XL
203///////////////////////////////////////////////////////////////////////////
204// Atomic structs
205//
206// For things that don't carry any arena-allocated data (and are
207// copy...), just add them to this list.
208
fc512014 209TrivialTypeFoldableAndLiftImpls! {
ff7c6d11 210 (),
0531ce1d
XL
211 bool,
212 usize,
ba9703b0 213 ::rustc_target::abi::VariantIdx,
3dfed10e 214 u32,
0531ce1d 215 u64,
0731742a 216 String,
9fa01778 217 crate::middle::region::Scope,
5869c6ff 218 crate::ty::FloatTy,
3dfed10e
XL
219 ::rustc_ast::InlineAsmOptions,
220 ::rustc_ast::InlineAsmTemplatePiece,
221 ::rustc_ast::NodeId,
dfeec247
XL
222 ::rustc_span::symbol::Symbol,
223 ::rustc_hir::def::Res,
224 ::rustc_hir::def_id::DefId,
3dfed10e
XL
225 ::rustc_hir::def_id::LocalDefId,
226 ::rustc_hir::HirId,
ba9703b0 227 ::rustc_hir::LlvmInlineAsmInner,
dfeec247
XL
228 ::rustc_hir::MatchSource,
229 ::rustc_hir::Mutability,
230 ::rustc_hir::Unsafety,
f9f354fc 231 ::rustc_target::asm::InlineAsmRegOrRegClass,
83c7162d 232 ::rustc_target::spec::abi::Abi,
3dfed10e
XL
233 crate::mir::coverage::ExpressionOperandId,
234 crate::mir::coverage::CounterValueReference,
29967ef6 235 crate::mir::coverage::InjectedExpressionId,
3dfed10e
XL
236 crate::mir::coverage::InjectedExpressionIndex,
237 crate::mir::coverage::MappedExpressionIndex,
9fa01778
XL
238 crate::mir::Local,
239 crate::mir::Promoted,
240 crate::traits::Reveal,
241 crate::ty::adjustment::AutoBorrowMutability,
242 crate::ty::AdtKind,
94222f64 243 crate::ty::BoundConstness,
fc512014 244 // Including `BoundRegionKind` is a *bit* dubious, but direct
0531ce1d
XL
245 // references to bound region appear in `ty::Error`, and aren't
246 // really meant to be folded. In general, we can only fold a fully
247 // general `Region`.
fc512014 248 crate::ty::BoundRegionKind,
3dfed10e 249 crate::ty::AssocItem,
fc512014 250 crate::ty::Placeholder<crate::ty::BoundRegionKind>,
9fa01778 251 crate::ty::ClosureKind,
532ac7d7
XL
252 crate::ty::FreeRegion,
253 crate::ty::InferTy,
9fa01778 254 crate::ty::IntVarValue,
532ac7d7 255 crate::ty::ParamConst,
9fa01778 256 crate::ty::ParamTy,
48663c56 257 crate::ty::adjustment::PointerCast,
532ac7d7 258 crate::ty::RegionVid,
9fa01778
XL
259 crate::ty::UniverseIndex,
260 crate::ty::Variance,
dfeec247 261 ::rustc_span::Span,
ff7c6d11
XL
262}
263
e9174d1e
SL
264///////////////////////////////////////////////////////////////////////////
265// Lift implementations
266
532ac7d7 267// FIXME(eddyb) replace all the uses of `Option::map` with `?`.
e9174d1e
SL
268impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>> Lift<'tcx> for (A, B) {
269 type Lifted = (A::Lifted, B::Lifted);
29967ef6
XL
270 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
271 Some((tcx.lift(self.0)?, tcx.lift(self.1)?))
e9174d1e
SL
272 }
273}
274
ea8adc8c
XL
275impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>, C: Lift<'tcx>> Lift<'tcx> for (A, B, C) {
276 type Lifted = (A::Lifted, B::Lifted, C::Lifted);
29967ef6
XL
277 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
278 Some((tcx.lift(self.0)?, tcx.lift(self.1)?, tcx.lift(self.2)?))
dfeec247 279 }
ea8adc8c
XL
280}
281
a7813a04
XL
282impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Option<T> {
283 type Lifted = Option<T::Lifted>;
29967ef6
XL
284 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
285 match self {
286 Some(x) => tcx.lift(x).map(Some),
dfeec247 287 None => Some(None),
a7813a04
XL
288 }
289 }
290}
291
292impl<'tcx, T: Lift<'tcx>, E: Lift<'tcx>> Lift<'tcx> for Result<T, E> {
293 type Lifted = Result<T::Lifted, E::Lifted>;
29967ef6
XL
294 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
295 match self {
296 Ok(x) => tcx.lift(x).map(Ok),
297 Err(e) => tcx.lift(e).map(Err),
a7813a04
XL
298 }
299 }
300}
301
ea8adc8c
XL
302impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Box<T> {
303 type Lifted = Box<T::Lifted>;
29967ef6
XL
304 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
305 tcx.lift(*self).map(Box::new)
ea8adc8c
XL
306 }
307}
308
29967ef6 309impl<'tcx, T: Lift<'tcx> + Clone> Lift<'tcx> for Rc<T> {
dc9dc135 310 type Lifted = Rc<T::Lifted>;
29967ef6
XL
311 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
312 tcx.lift(self.as_ref().clone()).map(Rc::new)
dc9dc135
XL
313 }
314}
315
29967ef6 316impl<'tcx, T: Lift<'tcx> + Clone> Lift<'tcx> for Arc<T> {
dc9dc135 317 type Lifted = Arc<T::Lifted>;
29967ef6
XL
318 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
319 tcx.lift(self.as_ref().clone()).map(Arc::new)
e9174d1e
SL
320 }
321}
a7813a04
XL
322impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Vec<T> {
323 type Lifted = Vec<T::Lifted>;
29967ef6
XL
324 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
325 self.into_iter().map(|v| tcx.lift(v)).collect()
a7813a04
XL
326 }
327}
328
ff7c6d11
XL
329impl<'tcx, I: Idx, T: Lift<'tcx>> Lift<'tcx> for IndexVec<I, T> {
330 type Lifted = IndexVec<I, T::Lifted>;
29967ef6
XL
331 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
332 self.into_iter().map(|e| tcx.lift(e)).collect()
ff7c6d11
XL
333 }
334}
335
9e0c209e
SL
336impl<'a, 'tcx> Lift<'tcx> for ty::TraitRef<'a> {
337 type Lifted = ty::TraitRef<'tcx>;
29967ef6
XL
338 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
339 tcx.lift(self.substs).map(|substs| ty::TraitRef { def_id: self.def_id, substs })
e9174d1e
SL
340 }
341}
342
9e0c209e
SL
343impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialTraitRef<'a> {
344 type Lifted = ty::ExistentialTraitRef<'tcx>;
29967ef6
XL
345 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
346 tcx.lift(self.substs).map(|substs| ty::ExistentialTraitRef { def_id: self.def_id, substs })
e9174d1e
SL
347 }
348}
349
532ac7d7
XL
350impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialPredicate<'a> {
351 type Lifted = ty::ExistentialPredicate<'tcx>;
29967ef6 352 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
532ac7d7 353 match self {
dfeec247 354 ty::ExistentialPredicate::Trait(x) => tcx.lift(x).map(ty::ExistentialPredicate::Trait),
532ac7d7
XL
355 ty::ExistentialPredicate::Projection(x) => {
356 tcx.lift(x).map(ty::ExistentialPredicate::Projection)
357 }
358 ty::ExistentialPredicate::AutoTrait(def_id) => {
29967ef6 359 Some(ty::ExistentialPredicate::AutoTrait(def_id))
532ac7d7
XL
360 }
361 }
362 }
363}
364
e9174d1e
SL
365impl<'a, 'tcx> Lift<'tcx> for ty::TraitPredicate<'a> {
366 type Lifted = ty::TraitPredicate<'tcx>;
29967ef6 367 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::TraitPredicate<'tcx>> {
3c0e092e
XL
368 tcx.lift(self.trait_ref).map(|trait_ref| ty::TraitPredicate {
369 trait_ref,
370 constness: self.constness,
371 polarity: self.polarity,
372 })
e9174d1e
SL
373 }
374}
375
cc61c64b
XL
376impl<'a, 'tcx> Lift<'tcx> for ty::SubtypePredicate<'a> {
377 type Lifted = ty::SubtypePredicate<'tcx>;
29967ef6
XL
378 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::SubtypePredicate<'tcx>> {
379 tcx.lift((self.a, self.b)).map(|(a, b)| ty::SubtypePredicate {
cc61c64b 380 a_is_expected: self.a_is_expected,
041b39d2
XL
381 a,
382 b,
cc61c64b
XL
383 })
384 }
385}
386
94222f64
XL
387impl<'a, 'tcx> Lift<'tcx> for ty::CoercePredicate<'a> {
388 type Lifted = ty::CoercePredicate<'tcx>;
389 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::CoercePredicate<'tcx>> {
390 tcx.lift((self.a, self.b)).map(|(a, b)| ty::CoercePredicate { a, b })
391 }
392}
393
dc9dc135 394impl<'tcx, A: Copy + Lift<'tcx>, B: Copy + Lift<'tcx>> Lift<'tcx> for ty::OutlivesPredicate<A, B> {
e9174d1e 395 type Lifted = ty::OutlivesPredicate<A::Lifted, B::Lifted>;
29967ef6
XL
396 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
397 tcx.lift((self.0, self.1)).map(|(a, b)| ty::OutlivesPredicate(a, b))
e9174d1e
SL
398 }
399}
400
5bcae85e
SL
401impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionTy<'a> {
402 type Lifted = ty::ProjectionTy<'tcx>;
29967ef6
XL
403 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::ProjectionTy<'tcx>> {
404 tcx.lift(self.substs)
dfeec247 405 .map(|substs| ty::ProjectionTy { item_def_id: self.item_def_id, substs })
5bcae85e
SL
406 }
407}
408
e9174d1e
SL
409impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionPredicate<'a> {
410 type Lifted = ty::ProjectionPredicate<'tcx>;
29967ef6
XL
411 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::ProjectionPredicate<'tcx>> {
412 tcx.lift((self.projection_ty, self.ty))
dfeec247 413 .map(|(projection_ty, ty)| ty::ProjectionPredicate { projection_ty, ty })
e9174d1e
SL
414 }
415}
416
9e0c209e
SL
417impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialProjection<'a> {
418 type Lifted = ty::ExistentialProjection<'tcx>;
29967ef6
XL
419 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
420 tcx.lift(self.substs).map(|substs| ty::ExistentialProjection {
dfeec247 421 substs,
29967ef6 422 ty: tcx.lift(self.ty).expect("type must lift when substs do"),
dfeec247 423 item_def_id: self.item_def_id,
9e0c209e
SL
424 })
425 }
426}
427
f9f354fc
XL
428impl<'a, 'tcx> Lift<'tcx> for ty::PredicateKind<'a> {
429 type Lifted = ty::PredicateKind<'tcx>;
29967ef6 430 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
3dfed10e 431 match self {
94222f64 432 ty::PredicateKind::Trait(data) => tcx.lift(data).map(ty::PredicateKind::Trait),
5869c6ff 433 ty::PredicateKind::Subtype(data) => tcx.lift(data).map(ty::PredicateKind::Subtype),
94222f64 434 ty::PredicateKind::Coerce(data) => tcx.lift(data).map(ty::PredicateKind::Coerce),
5869c6ff
XL
435 ty::PredicateKind::RegionOutlives(data) => {
436 tcx.lift(data).map(ty::PredicateKind::RegionOutlives)
a7813a04 437 }
5869c6ff
XL
438 ty::PredicateKind::TypeOutlives(data) => {
439 tcx.lift(data).map(ty::PredicateKind::TypeOutlives)
a7813a04 440 }
5869c6ff
XL
441 ty::PredicateKind::Projection(data) => {
442 tcx.lift(data).map(ty::PredicateKind::Projection)
a7813a04 443 }
5869c6ff
XL
444 ty::PredicateKind::WellFormed(ty) => tcx.lift(ty).map(ty::PredicateKind::WellFormed),
445 ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => {
29967ef6 446 tcx.lift(closure_substs).map(|closure_substs| {
5869c6ff 447 ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind)
dfeec247 448 })
a7813a04 449 }
5869c6ff
XL
450 ty::PredicateKind::ObjectSafe(trait_def_id) => {
451 Some(ty::PredicateKind::ObjectSafe(trait_def_id))
a7813a04 452 }
94222f64
XL
453 ty::PredicateKind::ConstEvaluatable(uv) => {
454 tcx.lift(uv).map(|uv| ty::PredicateKind::ConstEvaluatable(uv))
f9f354fc 455 }
5869c6ff
XL
456 ty::PredicateKind::ConstEquate(c1, c2) => {
457 tcx.lift((c1, c2)).map(|(c1, c2)| ty::PredicateKind::ConstEquate(c1, c2))
ea8adc8c 458 }
5869c6ff
XL
459 ty::PredicateKind::TypeWellFormedFromEnv(ty) => {
460 tcx.lift(ty).map(ty::PredicateKind::TypeWellFormedFromEnv)
1b1a35ee 461 }
a7813a04
XL
462 }
463 }
464}
465
cdc7bbd5
XL
466impl<'a, 'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::Binder<'a, T>
467where
468 <T as Lift<'tcx>>::Lifted: TypeFoldable<'tcx>,
469{
470 type Lifted = ty::Binder<'tcx, T::Lifted>;
29967ef6 471 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
cdc7bbd5
XL
472 let bound_vars = tcx.lift(self.bound_vars());
473 tcx.lift(self.skip_binder())
474 .zip(bound_vars)
475 .map(|(value, vars)| ty::Binder::bind_with_vars(value, vars))
e9174d1e
SL
476 }
477}
478
ea8adc8c
XL
479impl<'a, 'tcx> Lift<'tcx> for ty::ParamEnv<'a> {
480 type Lifted = ty::ParamEnv<'tcx>;
29967ef6
XL
481 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
482 tcx.lift(self.caller_bounds())
1b1a35ee 483 .map(|caller_bounds| ty::ParamEnv::new(caller_bounds, self.reveal()))
ea8adc8c
XL
484 }
485}
486
487impl<'a, 'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::ParamEnvAnd<'a, T> {
488 type Lifted = ty::ParamEnvAnd<'tcx, T::Lifted>;
29967ef6
XL
489 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
490 tcx.lift(self.param_env).and_then(|param_env| {
491 tcx.lift(self.value).map(|value| ty::ParamEnvAnd { param_env, value })
ea8adc8c
XL
492 })
493 }
494}
495
a7813a04
XL
496impl<'a, 'tcx> Lift<'tcx> for ty::ClosureSubsts<'a> {
497 type Lifted = ty::ClosureSubsts<'tcx>;
29967ef6
XL
498 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
499 tcx.lift(self.substs).map(|substs| ty::ClosureSubsts { substs })
a7813a04
XL
500 }
501}
502
94b46f34
XL
503impl<'a, 'tcx> Lift<'tcx> for ty::GeneratorSubsts<'a> {
504 type Lifted = ty::GeneratorSubsts<'tcx>;
29967ef6
XL
505 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
506 tcx.lift(self.substs).map(|substs| ty::GeneratorSubsts { substs })
ea8adc8c
XL
507 }
508}
509
7cac9316
XL
510impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjustment<'a> {
511 type Lifted = ty::adjustment::Adjustment<'tcx>;
29967ef6
XL
512 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
513 let ty::adjustment::Adjustment { kind, target } = self;
514 tcx.lift(kind).and_then(|kind| {
515 tcx.lift(target).map(|target| ty::adjustment::Adjustment { kind, target })
7cac9316
XL
516 })
517 }
518}
519
520impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjust<'a> {
521 type Lifted = ty::adjustment::Adjust<'tcx>;
29967ef6
XL
522 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
523 match self {
dfeec247
XL
524 ty::adjustment::Adjust::NeverToAny => Some(ty::adjustment::Adjust::NeverToAny),
525 ty::adjustment::Adjust::Pointer(ptr) => Some(ty::adjustment::Adjust::Pointer(ptr)),
29967ef6 526 ty::adjustment::Adjust::Deref(overloaded) => {
7cac9316
XL
527 tcx.lift(overloaded).map(ty::adjustment::Adjust::Deref)
528 }
29967ef6 529 ty::adjustment::Adjust::Borrow(autoref) => {
7cac9316
XL
530 tcx.lift(autoref).map(ty::adjustment::Adjust::Borrow)
531 }
532 }
533 }
534}
535
536impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::OverloadedDeref<'a> {
537 type Lifted = ty::adjustment::OverloadedDeref<'tcx>;
29967ef6
XL
538 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
539 tcx.lift(self.region).map(|region| ty::adjustment::OverloadedDeref {
1b1a35ee
XL
540 region,
541 mutbl: self.mutbl,
542 span: self.span,
543 })
a7813a04
XL
544 }
545}
546
c30ab7b3
SL
547impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::AutoBorrow<'a> {
548 type Lifted = ty::adjustment::AutoBorrow<'tcx>;
29967ef6
XL
549 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
550 match self {
c30ab7b3 551 ty::adjustment::AutoBorrow::Ref(r, m) => {
29967ef6 552 tcx.lift(r).map(|r| ty::adjustment::AutoBorrow::Ref(r, m))
a7813a04 553 }
dfeec247 554 ty::adjustment::AutoBorrow::RawPtr(m) => Some(ty::adjustment::AutoBorrow::RawPtr(m)),
a7813a04
XL
555 }
556 }
557}
558
ea8adc8c
XL
559impl<'a, 'tcx> Lift<'tcx> for ty::GenSig<'a> {
560 type Lifted = ty::GenSig<'tcx>;
29967ef6
XL
561 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
562 tcx.lift((self.resume_ty, self.yield_ty, self.return_ty))
74b04a01 563 .map(|(resume_ty, yield_ty, return_ty)| ty::GenSig { resume_ty, yield_ty, return_ty })
ea8adc8c
XL
564 }
565}
566
a7813a04
XL
567impl<'a, 'tcx> Lift<'tcx> for ty::FnSig<'a> {
568 type Lifted = ty::FnSig<'tcx>;
29967ef6
XL
569 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
570 tcx.lift(self.inputs_and_output).map(|x| ty::FnSig {
dfeec247
XL
571 inputs_and_output: x,
572 c_variadic: self.c_variadic,
573 unsafety: self.unsafety,
574 abi: self.abi,
a7813a04
XL
575 })
576 }
577}
578
579impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::error::ExpectedFound<T> {
580 type Lifted = ty::error::ExpectedFound<T::Lifted>;
29967ef6
XL
581 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
582 let ty::error::ExpectedFound { expected, found } = self;
583 tcx.lift(expected).and_then(|expected| {
584 tcx.lift(found).map(|found| ty::error::ExpectedFound { expected, found })
a7813a04
XL
585 })
586 }
587}
588
a7813a04
XL
589impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> {
590 type Lifted = ty::error::TypeError<'tcx>;
29967ef6 591 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
9fa01778 592 use crate::ty::error::TypeError::*;
a7813a04 593
29967ef6 594 Some(match self {
a7813a04 595 Mismatch => Mismatch,
94222f64 596 ConstnessMismatch(x) => ConstnessMismatch(x),
3c0e092e 597 PolarityMismatch(x) => PolarityMismatch(x),
a7813a04
XL
598 UnsafetyMismatch(x) => UnsafetyMismatch(x),
599 AbiMismatch(x) => AbiMismatch(x),
600 Mutability => Mutability,
cdc7bbd5 601 ArgumentMutability(i) => ArgumentMutability(i),
a7813a04
XL
602 TupleSize(x) => TupleSize(x),
603 FixedArraySize(x) => FixedArraySize(x),
a7813a04 604 ArgCount => ArgCount,
3c0e092e 605 FieldMisMatch(x, y) => FieldMisMatch(x, y),
9e0c209e 606 RegionsDoesNotOutlive(a, b) => {
29967ef6 607 return tcx.lift((a, b)).map(|(a, b)| RegionsDoesNotOutlive(a, b));
9e0c209e 608 }
ea8adc8c 609 RegionsInsufficientlyPolymorphic(a, b) => {
29967ef6 610 return tcx.lift(b).map(|b| RegionsInsufficientlyPolymorphic(a, b));
9e0c209e 611 }
ea8adc8c 612 RegionsOverlyPolymorphic(a, b) => {
29967ef6 613 return tcx.lift(b).map(|b| RegionsOverlyPolymorphic(a, b));
a7813a04 614 }
0731742a 615 RegionsPlaceholderMismatch => RegionsPlaceholderMismatch,
a7813a04
XL
616 IntMismatch(x) => IntMismatch(x),
617 FloatMismatch(x) => FloatMismatch(x),
618 Traits(x) => Traits(x),
a7813a04 619 VariadicMismatch(x) => VariadicMismatch(x),
29967ef6
XL
620 CyclicTy(t) => return tcx.lift(t).map(|t| CyclicTy(t)),
621 CyclicConst(ct) => return tcx.lift(ct).map(|ct| CyclicConst(ct)),
041b39d2 622 ProjectionMismatched(x) => ProjectionMismatched(x),
cdc7bbd5 623 ArgumentSorts(x, i) => return tcx.lift(x).map(|x| ArgumentSorts(x, i)),
29967ef6
XL
624 Sorts(x) => return tcx.lift(x).map(Sorts),
625 ExistentialMismatch(x) => return tcx.lift(x).map(ExistentialMismatch),
626 ConstMismatch(x) => return tcx.lift(x).map(ConstMismatch),
e1599b0c 627 IntrinsicCast => IntrinsicCast,
29967ef6
XL
628 TargetFeatureCast(x) => TargetFeatureCast(x),
629 ObjectUnsafeCoercion(x) => return tcx.lift(x).map(ObjectUnsafeCoercion),
a7813a04
XL
630 })
631 }
632}
633
0531ce1d
XL
634impl<'a, 'tcx> Lift<'tcx> for ty::InstanceDef<'a> {
635 type Lifted = ty::InstanceDef<'tcx>;
29967ef6
XL
636 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
637 match self {
dfeec247
XL
638 ty::InstanceDef::Item(def_id) => Some(ty::InstanceDef::Item(def_id)),
639 ty::InstanceDef::VtableShim(def_id) => Some(ty::InstanceDef::VtableShim(def_id)),
640 ty::InstanceDef::ReifyShim(def_id) => Some(ty::InstanceDef::ReifyShim(def_id)),
641 ty::InstanceDef::Intrinsic(def_id) => Some(ty::InstanceDef::Intrinsic(def_id)),
29967ef6 642 ty::InstanceDef::FnPtrShim(def_id, ty) => {
dfeec247
XL
643 Some(ty::InstanceDef::FnPtrShim(def_id, tcx.lift(ty)?))
644 }
645 ty::InstanceDef::Virtual(def_id, n) => Some(ty::InstanceDef::Virtual(def_id, n)),
c295e0f8
XL
646 ty::InstanceDef::ClosureOnceShim { call_once, track_caller } => {
647 Some(ty::InstanceDef::ClosureOnceShim { call_once, track_caller })
dfeec247 648 }
29967ef6 649 ty::InstanceDef::DropGlue(def_id, ty) => {
dfeec247
XL
650 Some(ty::InstanceDef::DropGlue(def_id, tcx.lift(ty)?))
651 }
29967ef6 652 ty::InstanceDef::CloneShim(def_id, ty) => {
dfeec247
XL
653 Some(ty::InstanceDef::CloneShim(def_id, tcx.lift(ty)?))
654 }
0531ce1d
XL
655 }
656 }
657}
658
e9174d1e
SL
659///////////////////////////////////////////////////////////////////////////
660// TypeFoldable implementations.
661//
662// Ideally, each type should invoke `folder.fold_foo(self)` and
663// nothing else. In some cases, though, we haven't gotten around to
664// adding methods on the `folder` yet, and thus the folding is
665// hard-coded here. This is less-flexible, because folders cannot
666// override the behavior, but there are a lot of random types and one
667// can easily refactor the folding into the TypeFolder trait as
668// needed.
669
0531ce1d
XL
670/// AdtDefs are basically the same as a DefId.
671impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::AdtDef {
fc512014
XL
672 fn super_fold_with<F: TypeFolder<'tcx>>(self, _folder: &mut F) -> Self {
673 self
0531ce1d
XL
674 }
675
fc512014 676 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
29967ef6 677 ControlFlow::CONTINUE
0531ce1d
XL
678 }
679}
680
dc9dc135 681impl<'tcx, T: TypeFoldable<'tcx>, U: TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
fc512014 682 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> (T, U) {
e9174d1e
SL
683 (self.0.fold_with(folder), self.1.fold_with(folder))
684 }
9cc50fc6 685
fc512014 686 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
29967ef6
XL
687 self.0.visit_with(visitor)?;
688 self.1.visit_with(visitor)
9cc50fc6 689 }
e9174d1e
SL
690}
691
3dfed10e
XL
692impl<'tcx, A: TypeFoldable<'tcx>, B: TypeFoldable<'tcx>, C: TypeFoldable<'tcx>> TypeFoldable<'tcx>
693 for (A, B, C)
694{
fc512014 695 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> (A, B, C) {
3dfed10e
XL
696 (self.0.fold_with(folder), self.1.fold_with(folder), self.2.fold_with(folder))
697 }
698
fc512014 699 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
29967ef6
XL
700 self.0.visit_with(visitor)?;
701 self.1.visit_with(visitor)?;
702 self.2.visit_with(visitor)
3dfed10e
XL
703 }
704}
705
0531ce1d
XL
706EnumTypeFoldableImpl! {
707 impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
708 (Some)(a),
709 (None),
710 } where T: TypeFoldable<'tcx>
e9174d1e
SL
711}
712
dc9dc135
XL
713EnumTypeFoldableImpl! {
714 impl<'tcx, T, E> TypeFoldable<'tcx> for Result<T, E> {
715 (Ok)(a),
716 (Err)(a),
717 } where T: TypeFoldable<'tcx>, E: TypeFoldable<'tcx>,
718}
719
e9174d1e 720impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
fc512014
XL
721 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
722 // FIXME: Reuse the `Rc` here.
723 Rc::new((*self).clone().fold_with(folder))
e9174d1e 724 }
9cc50fc6 725
fc512014 726 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
9cc50fc6
SL
727 (**self).visit_with(visitor)
728 }
e9174d1e
SL
729}
730
dc9dc135 731impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Arc<T> {
fc512014
XL
732 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
733 // FIXME: Reuse the `Arc` here.
734 Arc::new((*self).clone().fold_with(folder))
dc9dc135
XL
735 }
736
fc512014 737 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
dc9dc135
XL
738 (**self).visit_with(visitor)
739 }
740}
741
e9174d1e 742impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
fc512014
XL
743 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
744 self.map_id(|value| value.fold_with(folder))
e9174d1e 745 }
9cc50fc6 746
fc512014 747 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
9cc50fc6
SL
748 (**self).visit_with(visitor)
749 }
e9174d1e
SL
750}
751
752impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
fc512014
XL
753 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
754 self.map_id(|t| t.fold_with(folder))
e9174d1e 755 }
9cc50fc6 756
fc512014 757 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
29967ef6 758 self.iter().try_for_each(|t| t.visit_with(visitor))
9cc50fc6 759 }
e9174d1e
SL
760}
761
0bf4aa26 762impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<[T]> {
fc512014
XL
763 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
764 self.map_id(|t| t.fold_with(folder))
0bf4aa26
XL
765 }
766
fc512014 767 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
29967ef6 768 self.iter().try_for_each(|t| t.visit_with(visitor))
0bf4aa26
XL
769 }
770}
771
cdc7bbd5 772impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<'tcx, T> {
fc512014
XL
773 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
774 self.map_bound(|ty| ty.fold_with(folder))
9cc50fc6
SL
775 }
776
fc512014 777 fn fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
e9174d1e
SL
778 folder.fold_binder(self)
779 }
9cc50fc6 780
fc512014 781 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
f035d41b 782 self.as_ref().skip_binder().visit_with(visitor)
54a0048b
SL
783 }
784
fc512014 785 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
54a0048b 786 visitor.visit_binder(self)
9cc50fc6 787 }
e9174d1e
SL
788}
789
cdc7bbd5 790impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>> {
fc512014
XL
791 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
792 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_poly_existential_predicates(v))
476ff2be
SL
793 }
794
fc512014 795 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
29967ef6 796 self.iter().try_for_each(|p| p.visit_with(visitor))
476ff2be
SL
797 }
798}
799
b7449926 800impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<Ty<'tcx>> {
fc512014
XL
801 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
802 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_type_list(v))
0531ce1d
XL
803 }
804
fc512014 805 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
29967ef6 806 self.iter().try_for_each(|t| t.visit_with(visitor))
0531ce1d
XL
807 }
808}
809
532ac7d7 810impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind> {
fc512014
XL
811 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
812 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_projs(v))
0bf4aa26
XL
813 }
814
fc512014 815 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
29967ef6 816 self.iter().try_for_each(|t| t.visit_with(visitor))
0bf4aa26
XL
817 }
818}
819
0531ce1d 820impl<'tcx> TypeFoldable<'tcx> for ty::instance::Instance<'tcx> {
fc512014 821 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
9fa01778 822 use crate::ty::InstanceDef::*;
0531ce1d
XL
823 Self {
824 substs: self.substs.fold_with(folder),
825 def: match self.def {
3dfed10e 826 Item(def) => Item(def.fold_with(folder)),
a1dfa0c6 827 VtableShim(did) => VtableShim(did.fold_with(folder)),
e74abb32 828 ReifyShim(did) => ReifyShim(did.fold_with(folder)),
0531ce1d 829 Intrinsic(did) => Intrinsic(did.fold_with(folder)),
dfeec247
XL
830 FnPtrShim(did, ty) => FnPtrShim(did.fold_with(folder), ty.fold_with(folder)),
831 Virtual(did, i) => Virtual(did.fold_with(folder), i),
c295e0f8
XL
832 ClosureOnceShim { call_once, track_caller } => {
833 ClosureOnceShim { call_once: call_once.fold_with(folder), track_caller }
dfeec247
XL
834 }
835 DropGlue(did, ty) => DropGlue(did.fold_with(folder), ty.fold_with(folder)),
836 CloneShim(did, ty) => CloneShim(did.fold_with(folder), ty.fold_with(folder)),
0531ce1d 837 },
9cc50fc6
SL
838 }
839 }
840
fc512014 841 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
9fa01778 842 use crate::ty::InstanceDef::*;
29967ef6
XL
843 self.substs.visit_with(visitor)?;
844 match self.def {
845 Item(def) => def.visit_with(visitor),
846 VtableShim(did) | ReifyShim(did) | Intrinsic(did) | Virtual(did, _) => {
847 did.visit_with(visitor)
848 }
849 FnPtrShim(did, ty) | CloneShim(did, ty) => {
850 did.visit_with(visitor)?;
851 ty.visit_with(visitor)
dfeec247 852 }
29967ef6
XL
853 DropGlue(did, ty) => {
854 did.visit_with(visitor)?;
855 ty.visit_with(visitor)
856 }
c295e0f8 857 ClosureOnceShim { call_once, track_caller: _ } => call_once.visit_with(visitor),
29967ef6 858 }
9cc50fc6 859 }
e9174d1e
SL
860}
861
0531ce1d 862impl<'tcx> TypeFoldable<'tcx> for interpret::GlobalId<'tcx> {
fc512014 863 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
dfeec247 864 Self { instance: self.instance.fold_with(folder), promoted: self.promoted }
a7813a04
XL
865 }
866
fc512014 867 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
0531ce1d 868 self.instance.visit_with(visitor)
a7813a04
XL
869 }
870}
871
e9174d1e 872impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
fc512014
XL
873 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
874 let kind = match *self.kind() {
b7449926
XL
875 ty::RawPtr(tm) => ty::RawPtr(tm.fold_with(folder)),
876 ty::Array(typ, sz) => ty::Array(typ.fold_with(folder), sz.fold_with(folder)),
877 ty::Slice(typ) => ty::Slice(typ.fold_with(folder)),
fc512014
XL
878 ty::Adt(tid, substs) => ty::Adt(tid, substs.fold_with(folder)),
879 ty::Dynamic(trait_ty, region) => {
dfeec247 880 ty::Dynamic(trait_ty.fold_with(folder), region.fold_with(folder))
9cc50fc6 881 }
dfeec247 882 ty::Tuple(ts) => ty::Tuple(ts.fold_with(folder)),
fc512014 883 ty::FnDef(def_id, substs) => ty::FnDef(def_id, substs.fold_with(folder)),
b7449926 884 ty::FnPtr(f) => ty::FnPtr(f.fold_with(folder)),
fc512014 885 ty::Ref(r, ty, mutbl) => ty::Ref(r.fold_with(folder), ty.fold_with(folder), mutbl),
b7449926 886 ty::Generator(did, substs, movability) => {
fc512014 887 ty::Generator(did, substs.fold_with(folder), movability)
ea8adc8c 888 }
b7449926 889 ty::GeneratorWitness(types) => ty::GeneratorWitness(types.fold_with(folder)),
fc512014
XL
890 ty::Closure(did, substs) => ty::Closure(did, substs.fold_with(folder)),
891 ty::Projection(data) => ty::Projection(data.fold_with(folder)),
892 ty::Opaque(did, substs) => ty::Opaque(did, substs.fold_with(folder)),
a1dfa0c6 893
dfeec247
XL
894 ty::Bool
895 | ty::Char
896 | ty::Str
897 | ty::Int(_)
898 | ty::Uint(_)
899 | ty::Float(_)
f035d41b 900 | ty::Error(_)
dfeec247
XL
901 | ty::Infer(_)
902 | ty::Param(..)
903 | ty::Bound(..)
904 | ty::Placeholder(..)
905 | ty::Never
906 | ty::Foreign(..) => return self,
9cc50fc6 907 };
476ff2be 908
1b1a35ee 909 if *self.kind() == kind { self } else { folder.tcx().mk_ty(kind) }
9cc50fc6
SL
910 }
911
fc512014
XL
912 fn fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
913 folder.fold_ty(self)
e9174d1e 914 }
9cc50fc6 915
fc512014 916 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1b1a35ee 917 match self.kind() {
b7449926 918 ty::RawPtr(ref tm) => tm.visit_with(visitor),
29967ef6
XL
919 ty::Array(typ, sz) => {
920 typ.visit_with(visitor)?;
921 sz.visit_with(visitor)
922 }
b7449926
XL
923 ty::Slice(typ) => typ.visit_with(visitor),
924 ty::Adt(_, substs) => substs.visit_with(visitor),
dfeec247 925 ty::Dynamic(ref trait_ty, ref reg) => {
29967ef6
XL
926 trait_ty.visit_with(visitor)?;
927 reg.visit_with(visitor)
dfeec247 928 }
b7449926
XL
929 ty::Tuple(ts) => ts.visit_with(visitor),
930 ty::FnDef(_, substs) => substs.visit_with(visitor),
931 ty::FnPtr(ref f) => f.visit_with(visitor),
29967ef6
XL
932 ty::Ref(r, ty, _) => {
933 r.visit_with(visitor)?;
934 ty.visit_with(visitor)
935 }
dfeec247 936 ty::Generator(_did, ref substs, _) => substs.visit_with(visitor),
b7449926
XL
937 ty::GeneratorWitness(ref types) => types.visit_with(visitor),
938 ty::Closure(_did, ref substs) => substs.visit_with(visitor),
f9f354fc 939 ty::Projection(ref data) => data.visit_with(visitor),
b7449926 940 ty::Opaque(_, ref substs) => substs.visit_with(visitor),
a1dfa0c6 941
dfeec247
XL
942 ty::Bool
943 | ty::Char
944 | ty::Str
945 | ty::Int(_)
946 | ty::Uint(_)
947 | ty::Float(_)
f035d41b 948 | ty::Error(_)
dfeec247
XL
949 | ty::Infer(_)
950 | ty::Bound(..)
951 | ty::Placeholder(..)
952 | ty::Param(..)
953 | ty::Never
29967ef6 954 | ty::Foreign(..) => ControlFlow::CONTINUE,
9cc50fc6
SL
955 }
956 }
957
fc512014 958 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
9cc50fc6
SL
959 visitor.visit_ty(self)
960 }
e9174d1e
SL
961}
962
7cac9316 963impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> {
fc512014
XL
964 fn super_fold_with<F: TypeFolder<'tcx>>(self, _folder: &mut F) -> Self {
965 self
9cc50fc6
SL
966 }
967
fc512014
XL
968 fn fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
969 folder.fold_region(self)
e9174d1e 970 }
9cc50fc6 971
fc512014 972 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
29967ef6 973 ControlFlow::CONTINUE
9cc50fc6
SL
974 }
975
fc512014 976 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
9cc50fc6
SL
977 visitor.visit_region(*self)
978 }
e9174d1e
SL
979}
980
f9f354fc 981impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
94222f64
XL
982 fn fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
983 folder.fold_predicate(self)
984 }
985
fc512014 986 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
5869c6ff 987 let new = self.inner.kind.fold_with(folder);
fc512014 988 folder.tcx().reuse_or_mk_predicate(self, new)
f9f354fc
XL
989 }
990
fc512014 991 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
5869c6ff 992 self.inner.kind.visit_with(visitor)
f035d41b
XL
993 }
994
fc512014 995 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
f035d41b
XL
996 visitor.visit_predicate(*self)
997 }
998
999 fn has_vars_bound_at_or_above(&self, binder: ty::DebruijnIndex) -> bool {
1000 self.inner.outer_exclusive_binder > binder
1001 }
1002
1003 fn has_type_flags(&self, flags: ty::TypeFlags) -> bool {
1004 self.inner.flags.intersects(flags)
1005 }
1006}
1007
b7449926 1008impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
fc512014
XL
1009 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
1010 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_predicates(v))
7cac9316
XL
1011 }
1012
fc512014 1013 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
29967ef6 1014 self.iter().try_for_each(|p| p.visit_with(visitor))
7cac9316
XL
1015 }
1016}
1017
8bb4bdeb 1018impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
fc512014
XL
1019 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
1020 self.map_id(|x| x.fold_with(folder))
8bb4bdeb
XL
1021 }
1022
fc512014 1023 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
29967ef6 1024 self.iter().try_for_each(|t| t.visit_with(visitor))
8bb4bdeb
XL
1025 }
1026}
041b39d2 1027
532ac7d7 1028impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Const<'tcx> {
fc512014 1029 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
532ac7d7
XL
1030 let ty = self.ty.fold_with(folder);
1031 let val = self.val.fold_with(folder);
f9f354fc
XL
1032 if ty != self.ty || val != self.val {
1033 folder.tcx().mk_const(ty::Const { ty, val })
1034 } else {
fc512014 1035 self
f9f354fc 1036 }
0731742a
XL
1037 }
1038
fc512014
XL
1039 fn fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
1040 folder.fold_const(self)
ea8adc8c
XL
1041 }
1042
fc512014 1043 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
29967ef6
XL
1044 self.ty.visit_with(visitor)?;
1045 self.val.visit_with(visitor)
ea8adc8c 1046 }
0731742a 1047
fc512014 1048 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
0731742a
XL
1049 visitor.visit_const(self)
1050 }
ea8adc8c
XL
1051}
1052
60c5eb7d 1053impl<'tcx> TypeFoldable<'tcx> for ty::ConstKind<'tcx> {
fc512014
XL
1054 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
1055 match self {
60c5eb7d
XL
1056 ty::ConstKind::Infer(ic) => ty::ConstKind::Infer(ic.fold_with(folder)),
1057 ty::ConstKind::Param(p) => ty::ConstKind::Param(p.fold_with(folder)),
94222f64 1058 ty::ConstKind::Unevaluated(uv) => ty::ConstKind::Unevaluated(uv.fold_with(folder)),
ba9703b0
XL
1059 ty::ConstKind::Value(_)
1060 | ty::ConstKind::Bound(..)
1061 | ty::ConstKind::Placeholder(..)
fc512014 1062 | ty::ConstKind::Error(_) => self,
0731742a 1063 }
ea8adc8c
XL
1064 }
1065
fc512014 1066 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
532ac7d7 1067 match *self {
60c5eb7d
XL
1068 ty::ConstKind::Infer(ic) => ic.visit_with(visitor),
1069 ty::ConstKind::Param(p) => p.visit_with(visitor),
94222f64 1070 ty::ConstKind::Unevaluated(uv) => uv.visit_with(visitor),
ba9703b0
XL
1071 ty::ConstKind::Value(_)
1072 | ty::ConstKind::Bound(..)
1073 | ty::ConstKind::Placeholder(_)
29967ef6 1074 | ty::ConstKind::Error(_) => ControlFlow::CONTINUE,
532ac7d7 1075 }
ea8adc8c
XL
1076 }
1077}
48663c56
XL
1078
1079impl<'tcx> TypeFoldable<'tcx> for InferConst<'tcx> {
fc512014
XL
1080 fn super_fold_with<F: TypeFolder<'tcx>>(self, _folder: &mut F) -> Self {
1081 self
48663c56
XL
1082 }
1083
fc512014 1084 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
29967ef6 1085 ControlFlow::CONTINUE
48663c56
XL
1086 }
1087}
94222f64
XL
1088
1089impl<'tcx> TypeFoldable<'tcx> for ty::Unevaluated<'tcx> {
1090 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
1091 ty::Unevaluated {
1092 def: self.def,
1093 substs_: Some(self.substs(folder.tcx()).fold_with(folder)),
1094 promoted: self.promoted,
1095 }
1096 }
1097
1098 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1099 visitor.visit_unevaluated_const(*self)
1100 }
1101
1102 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1103 if let Some(tcx) = visitor.tcx_for_anon_const_substs() {
1104 self.substs(tcx).visit_with(visitor)
1105 } else if let Some(substs) = self.substs_ {
1106 substs.visit_with(visitor)
1107 } else {
1108 debug!("ignoring default substs of `{:?}`", self.def);
1109 ControlFlow::CONTINUE
1110 }
1111 }
1112}
1113
1114impl<'tcx> TypeFoldable<'tcx> for ty::Unevaluated<'tcx, ()> {
1115 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
1116 ty::Unevaluated {
1117 def: self.def,
1118 substs_: Some(self.substs(folder.tcx()).fold_with(folder)),
1119 promoted: self.promoted,
1120 }
1121 }
1122
1123 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1124 visitor.visit_unevaluated_const(self.expand())
1125 }
1126
1127 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1128 if let Some(tcx) = visitor.tcx_for_anon_const_substs() {
1129 self.substs(tcx).visit_with(visitor)
1130 } else if let Some(substs) = self.substs_ {
1131 substs.visit_with(visitor)
1132 } else {
1133 debug!("ignoring default substs of `{:?}`", self.def);
1134 ControlFlow::CONTINUE
1135 }
1136 }
1137}