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