]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/ty/structural_impls.rs
Merge commit '1480cea393d0cee195e59949eabdfbcf1230f7f9' into clippyup
[rust.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::{Field, ProjectionKind};
7 use crate::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeSuperFoldable};
8 use crate::ty::print::{with_no_trimmed_paths, FmtPrinter, Printer};
9 use crate::ty::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitor};
10 use crate::ty::{self, AliasTy, InferConst, Lift, Term, TermKind, Ty, TyCtxt};
11 use rustc_data_structures::functor::IdFunctor;
12 use rustc_hir::def::Namespace;
13 use rustc_index::vec::{Idx, IndexVec};
14 use rustc_target::abi::TyAndLayout;
15
16 use std::fmt;
17 use std::mem::ManuallyDrop;
18 use std::ops::ControlFlow;
19 use std::rc::Rc;
20 use std::sync::Arc;
21
22 impl fmt::Debug for ty::TraitDef {
23     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24         ty::tls::with(|tcx| {
25             with_no_trimmed_paths!({
26                 f.write_str(
27                     &FmtPrinter::new(tcx, Namespace::TypeNS)
28                         .print_def_path(self.def_id, &[])?
29                         .into_buffer(),
30                 )
31             })
32         })
33     }
34 }
35
36 impl<'tcx> fmt::Debug for ty::AdtDef<'tcx> {
37     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38         ty::tls::with(|tcx| {
39             with_no_trimmed_paths!({
40                 f.write_str(
41                     &FmtPrinter::new(tcx, Namespace::TypeNS)
42                         .print_def_path(self.did(), &[])?
43                         .into_buffer(),
44                 )
45             })
46         })
47     }
48 }
49
50 impl fmt::Debug for ty::UpvarId {
51     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52         let name = ty::tls::with(|tcx| tcx.hir().name(self.var_path.hir_id));
53         write!(f, "UpvarId({:?};`{}`;{:?})", self.var_path.hir_id, name, self.closure_expr_id)
54     }
55 }
56
57 impl<'tcx> fmt::Debug for ty::ExistentialTraitRef<'tcx> {
58     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59         with_no_trimmed_paths!(fmt::Display::fmt(self, f))
60     }
61 }
62
63 impl<'tcx> fmt::Debug for ty::adjustment::Adjustment<'tcx> {
64     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65         write!(f, "{:?} -> {}", self.kind, self.target)
66     }
67 }
68
69 impl fmt::Debug for ty::BoundRegionKind {
70     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71         match *self {
72             ty::BrAnon(n, span) => write!(f, "BrAnon({n:?}, {span:?})"),
73             ty::BrNamed(did, name) => {
74                 if did.is_crate_root() {
75                     write!(f, "BrNamed({})", name)
76                 } else {
77                     write!(f, "BrNamed({:?}, {})", did, name)
78                 }
79             }
80             ty::BrEnv => write!(f, "BrEnv"),
81         }
82     }
83 }
84
85 impl fmt::Debug for ty::FreeRegion {
86     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87         write!(f, "ReFree({:?}, {:?})", self.scope, self.bound_region)
88     }
89 }
90
91 impl<'tcx> fmt::Debug for ty::FnSig<'tcx> {
92     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93         write!(f, "({:?}; c_variadic: {})->{:?}", self.inputs(), self.c_variadic, self.output())
94     }
95 }
96
97 impl<'tcx> fmt::Debug for ty::ConstVid<'tcx> {
98     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99         write!(f, "_#{}c", self.index)
100     }
101 }
102
103 impl<'tcx> fmt::Debug for ty::TraitRef<'tcx> {
104     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
105         with_no_trimmed_paths!(fmt::Display::fmt(self, f))
106     }
107 }
108
109 impl<'tcx> fmt::Debug for Ty<'tcx> {
110     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
111         with_no_trimmed_paths!(fmt::Display::fmt(self, f))
112     }
113 }
114
115 impl fmt::Debug for ty::ParamTy {
116     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117         write!(f, "{}/#{}", self.name, self.index)
118     }
119 }
120
121 impl fmt::Debug for ty::ParamConst {
122     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
123         write!(f, "{}/#{}", self.name, self.index)
124     }
125 }
126
127 impl<'tcx> fmt::Debug for ty::TraitPredicate<'tcx> {
128     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
129         if let ty::BoundConstness::ConstIfConst = self.constness {
130             write!(f, "~const ")?;
131         }
132         write!(f, "TraitPredicate({:?}, polarity:{:?})", self.trait_ref, self.polarity)
133     }
134 }
135
136 impl<'tcx> fmt::Debug for ty::ProjectionPredicate<'tcx> {
137     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138         write!(f, "ProjectionPredicate({:?}, {:?})", self.projection_ty, self.term)
139     }
140 }
141
142 impl<'tcx> fmt::Debug for ty::Predicate<'tcx> {
143     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
144         write!(f, "{:?}", self.kind())
145     }
146 }
147
148 impl<'tcx> fmt::Debug for ty::Clause<'tcx> {
149     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
150         match *self {
151             ty::Clause::Trait(ref a) => a.fmt(f),
152             ty::Clause::RegionOutlives(ref pair) => pair.fmt(f),
153             ty::Clause::TypeOutlives(ref pair) => pair.fmt(f),
154             ty::Clause::Projection(ref pair) => pair.fmt(f),
155         }
156     }
157 }
158
159 impl<'tcx> fmt::Debug for ty::PredicateKind<'tcx> {
160     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
161         match *self {
162             ty::PredicateKind::Clause(ref a) => a.fmt(f),
163             ty::PredicateKind::Subtype(ref pair) => pair.fmt(f),
164             ty::PredicateKind::Coerce(ref pair) => pair.fmt(f),
165             ty::PredicateKind::WellFormed(data) => write!(f, "WellFormed({:?})", data),
166             ty::PredicateKind::ObjectSafe(trait_def_id) => {
167                 write!(f, "ObjectSafe({:?})", trait_def_id)
168             }
169             ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => {
170                 write!(f, "ClosureKind({:?}, {:?}, {:?})", closure_def_id, closure_substs, kind)
171             }
172             ty::PredicateKind::ConstEvaluatable(ct) => {
173                 write!(f, "ConstEvaluatable({ct:?})")
174             }
175             ty::PredicateKind::ConstEquate(c1, c2) => write!(f, "ConstEquate({:?}, {:?})", c1, c2),
176             ty::PredicateKind::TypeWellFormedFromEnv(ty) => {
177                 write!(f, "TypeWellFormedFromEnv({:?})", ty)
178             }
179             ty::PredicateKind::Ambiguous => write!(f, "Ambiguous"),
180         }
181     }
182 }
183
184 impl<'tcx> fmt::Debug for AliasTy<'tcx> {
185     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
186         f.debug_struct("AliasTy")
187             .field("substs", &self.substs)
188             .field("def_id", &self.def_id)
189             .finish()
190     }
191 }
192
193 ///////////////////////////////////////////////////////////////////////////
194 // Atomic structs
195 //
196 // For things that don't carry any arena-allocated data (and are
197 // copy...), just add them to this list.
198
199 TrivialTypeTraversalAndLiftImpls! {
200     (),
201     bool,
202     usize,
203     ::rustc_target::abi::VariantIdx,
204     u32,
205     u64,
206     String,
207     crate::middle::region::Scope,
208     crate::ty::FloatTy,
209     ::rustc_ast::InlineAsmOptions,
210     ::rustc_ast::InlineAsmTemplatePiece,
211     ::rustc_ast::NodeId,
212     ::rustc_span::symbol::Symbol,
213     ::rustc_hir::def::Res,
214     ::rustc_hir::def_id::DefId,
215     ::rustc_hir::def_id::LocalDefId,
216     ::rustc_hir::HirId,
217     ::rustc_hir::MatchSource,
218     ::rustc_hir::Mutability,
219     ::rustc_hir::Unsafety,
220     ::rustc_target::asm::InlineAsmRegOrRegClass,
221     ::rustc_target::spec::abi::Abi,
222     crate::mir::coverage::ExpressionOperandId,
223     crate::mir::coverage::CounterValueReference,
224     crate::mir::coverage::InjectedExpressionId,
225     crate::mir::coverage::InjectedExpressionIndex,
226     crate::mir::coverage::MappedExpressionIndex,
227     crate::mir::Local,
228     crate::mir::Promoted,
229     crate::traits::Reveal,
230     crate::ty::adjustment::AutoBorrowMutability,
231     crate::ty::AdtKind,
232     crate::ty::BoundConstness,
233     // Including `BoundRegionKind` is a *bit* dubious, but direct
234     // references to bound region appear in `ty::Error`, and aren't
235     // really meant to be folded. In general, we can only fold a fully
236     // general `Region`.
237     crate::ty::BoundRegionKind,
238     crate::ty::AssocItem,
239     crate::ty::AssocKind,
240     crate::ty::AliasKind,
241     crate::ty::Placeholder<crate::ty::BoundRegionKind>,
242     crate::ty::ClosureKind,
243     crate::ty::FreeRegion,
244     crate::ty::InferTy,
245     crate::ty::IntVarValue,
246     crate::ty::ParamConst,
247     crate::ty::ParamTy,
248     crate::ty::adjustment::PointerCast,
249     crate::ty::RegionVid,
250     crate::ty::UniverseIndex,
251     crate::ty::Variance,
252     ::rustc_span::Span,
253     ::rustc_errors::ErrorGuaranteed,
254     Field,
255     interpret::Scalar,
256     rustc_target::abi::Size,
257     rustc_type_ir::DebruijnIndex,
258     ty::BoundVar,
259     ty::Placeholder<ty::BoundVar>,
260 }
261
262 TrivialTypeTraversalAndLiftImpls! {
263     for<'tcx> {
264         ty::ValTree<'tcx>,
265     }
266 }
267
268 ///////////////////////////////////////////////////////////////////////////
269 // Lift implementations
270
271 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>> Lift<'tcx> for (A, B) {
272     type Lifted = (A::Lifted, B::Lifted);
273     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
274         Some((tcx.lift(self.0)?, tcx.lift(self.1)?))
275     }
276 }
277
278 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>, C: Lift<'tcx>> Lift<'tcx> for (A, B, C) {
279     type Lifted = (A::Lifted, B::Lifted, C::Lifted);
280     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
281         Some((tcx.lift(self.0)?, tcx.lift(self.1)?, tcx.lift(self.2)?))
282     }
283 }
284
285 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Option<T> {
286     type Lifted = Option<T::Lifted>;
287     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
288         Some(match self {
289             Some(x) => Some(tcx.lift(x)?),
290             None => None,
291         })
292     }
293 }
294
295 impl<'tcx, T: Lift<'tcx>, E: Lift<'tcx>> Lift<'tcx> for Result<T, E> {
296     type Lifted = Result<T::Lifted, E::Lifted>;
297     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
298         match self {
299             Ok(x) => tcx.lift(x).map(Ok),
300             Err(e) => tcx.lift(e).map(Err),
301         }
302     }
303 }
304
305 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Box<T> {
306     type Lifted = Box<T::Lifted>;
307     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
308         Some(Box::new(tcx.lift(*self)?))
309     }
310 }
311
312 impl<'tcx, T: Lift<'tcx> + Clone> Lift<'tcx> for Rc<T> {
313     type Lifted = Rc<T::Lifted>;
314     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
315         Some(Rc::new(tcx.lift(self.as_ref().clone())?))
316     }
317 }
318
319 impl<'tcx, T: Lift<'tcx> + Clone> Lift<'tcx> for Arc<T> {
320     type Lifted = Arc<T::Lifted>;
321     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
322         Some(Arc::new(tcx.lift(self.as_ref().clone())?))
323     }
324 }
325 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Vec<T> {
326     type Lifted = Vec<T::Lifted>;
327     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
328         self.into_iter().map(|v| tcx.lift(v)).collect()
329     }
330 }
331
332 impl<'tcx, I: Idx, T: Lift<'tcx>> Lift<'tcx> for IndexVec<I, T> {
333     type Lifted = IndexVec<I, T::Lifted>;
334     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
335         self.into_iter().map(|e| tcx.lift(e)).collect()
336     }
337 }
338
339 impl<'a, 'tcx> Lift<'tcx> for Term<'a> {
340     type Lifted = ty::Term<'tcx>;
341     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
342         Some(
343             match self.unpack() {
344                 TermKind::Ty(ty) => TermKind::Ty(tcx.lift(ty)?),
345                 TermKind::Const(c) => TermKind::Const(tcx.lift(c)?),
346             }
347             .pack(),
348         )
349     }
350 }
351 impl<'a, 'tcx> Lift<'tcx> for ty::ParamEnv<'a> {
352     type Lifted = ty::ParamEnv<'tcx>;
353     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
354         tcx.lift(self.caller_bounds())
355             .map(|caller_bounds| ty::ParamEnv::new(caller_bounds, self.reveal(), self.constness()))
356     }
357 }
358
359 ///////////////////////////////////////////////////////////////////////////
360 // TypeFoldable implementations.
361
362 /// AdtDefs are basically the same as a DefId.
363 impl<'tcx> TypeFoldable<'tcx> for ty::AdtDef<'tcx> {
364     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, _folder: &mut F) -> Result<Self, F::Error> {
365         Ok(self)
366     }
367 }
368
369 impl<'tcx> TypeVisitable<'tcx> for ty::AdtDef<'tcx> {
370     fn visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
371         ControlFlow::Continue(())
372     }
373 }
374
375 impl<'tcx, T: TypeFoldable<'tcx>, U: TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
376     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
377         self,
378         folder: &mut F,
379     ) -> Result<(T, U), F::Error> {
380         Ok((self.0.try_fold_with(folder)?, self.1.try_fold_with(folder)?))
381     }
382 }
383
384 impl<'tcx, T: TypeVisitable<'tcx>, U: TypeVisitable<'tcx>> TypeVisitable<'tcx> for (T, U) {
385     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
386         self.0.visit_with(visitor)?;
387         self.1.visit_with(visitor)
388     }
389 }
390
391 impl<'tcx, A: TypeFoldable<'tcx>, B: TypeFoldable<'tcx>, C: TypeFoldable<'tcx>> TypeFoldable<'tcx>
392     for (A, B, C)
393 {
394     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
395         self,
396         folder: &mut F,
397     ) -> Result<(A, B, C), F::Error> {
398         Ok((
399             self.0.try_fold_with(folder)?,
400             self.1.try_fold_with(folder)?,
401             self.2.try_fold_with(folder)?,
402         ))
403     }
404 }
405
406 impl<'tcx, A: TypeVisitable<'tcx>, B: TypeVisitable<'tcx>, C: TypeVisitable<'tcx>>
407     TypeVisitable<'tcx> for (A, B, C)
408 {
409     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
410         self.0.visit_with(visitor)?;
411         self.1.visit_with(visitor)?;
412         self.2.visit_with(visitor)
413     }
414 }
415
416 EnumTypeTraversalImpl! {
417     impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
418         (Some)(a),
419         (None),
420     } where T: TypeFoldable<'tcx>
421 }
422 EnumTypeTraversalImpl! {
423     impl<'tcx, T> TypeVisitable<'tcx> for Option<T> {
424         (Some)(a),
425         (None),
426     } where T: TypeVisitable<'tcx>
427 }
428
429 EnumTypeTraversalImpl! {
430     impl<'tcx, T, E> TypeFoldable<'tcx> for Result<T, E> {
431         (Ok)(a),
432         (Err)(a),
433     } where T: TypeFoldable<'tcx>, E: TypeFoldable<'tcx>,
434 }
435 EnumTypeTraversalImpl! {
436     impl<'tcx, T, E> TypeVisitable<'tcx> for Result<T, E> {
437         (Ok)(a),
438         (Err)(a),
439     } where T: TypeVisitable<'tcx>, E: TypeVisitable<'tcx>,
440 }
441
442 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
443     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
444         mut self,
445         folder: &mut F,
446     ) -> Result<Self, F::Error> {
447         // We merely want to replace the contained `T`, if at all possible,
448         // so that we don't needlessly allocate a new `Rc` or indeed clone
449         // the contained type.
450         unsafe {
451             // First step is to ensure that we have a unique reference to
452             // the contained type, which `Rc::make_mut` will accomplish (by
453             // allocating a new `Rc` and cloning the `T` only if required).
454             // This is done *before* casting to `Rc<ManuallyDrop<T>>` so that
455             // panicking during `make_mut` does not leak the `T`.
456             Rc::make_mut(&mut self);
457
458             // Casting to `Rc<ManuallyDrop<T>>` is safe because `ManuallyDrop`
459             // is `repr(transparent)`.
460             let ptr = Rc::into_raw(self).cast::<ManuallyDrop<T>>();
461             let mut unique = Rc::from_raw(ptr);
462
463             // Call to `Rc::make_mut` above guarantees that `unique` is the
464             // sole reference to the contained value, so we can avoid doing
465             // a checked `get_mut` here.
466             let slot = Rc::get_mut_unchecked(&mut unique);
467
468             // Semantically move the contained type out from `unique`, fold
469             // it, then move the folded value back into `unique`. Should
470             // folding fail, `ManuallyDrop` ensures that the "moved-out"
471             // value is not re-dropped.
472             let owned = ManuallyDrop::take(slot);
473             let folded = owned.try_fold_with(folder)?;
474             *slot = ManuallyDrop::new(folded);
475
476             // Cast back to `Rc<T>`.
477             Ok(Rc::from_raw(Rc::into_raw(unique).cast()))
478         }
479     }
480 }
481
482 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Rc<T> {
483     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
484         (**self).visit_with(visitor)
485     }
486 }
487
488 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Arc<T> {
489     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
490         mut self,
491         folder: &mut F,
492     ) -> Result<Self, F::Error> {
493         // We merely want to replace the contained `T`, if at all possible,
494         // so that we don't needlessly allocate a new `Arc` or indeed clone
495         // the contained type.
496         unsafe {
497             // First step is to ensure that we have a unique reference to
498             // the contained type, which `Arc::make_mut` will accomplish (by
499             // allocating a new `Arc` and cloning the `T` only if required).
500             // This is done *before* casting to `Arc<ManuallyDrop<T>>` so that
501             // panicking during `make_mut` does not leak the `T`.
502             Arc::make_mut(&mut self);
503
504             // Casting to `Arc<ManuallyDrop<T>>` is safe because `ManuallyDrop`
505             // is `repr(transparent)`.
506             let ptr = Arc::into_raw(self).cast::<ManuallyDrop<T>>();
507             let mut unique = Arc::from_raw(ptr);
508
509             // Call to `Arc::make_mut` above guarantees that `unique` is the
510             // sole reference to the contained value, so we can avoid doing
511             // a checked `get_mut` here.
512             let slot = Arc::get_mut_unchecked(&mut unique);
513
514             // Semantically move the contained type out from `unique`, fold
515             // it, then move the folded value back into `unique`. Should
516             // folding fail, `ManuallyDrop` ensures that the "moved-out"
517             // value is not re-dropped.
518             let owned = ManuallyDrop::take(slot);
519             let folded = owned.try_fold_with(folder)?;
520             *slot = ManuallyDrop::new(folded);
521
522             // Cast back to `Arc<T>`.
523             Ok(Arc::from_raw(Arc::into_raw(unique).cast()))
524         }
525     }
526 }
527
528 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Arc<T> {
529     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
530         (**self).visit_with(visitor)
531     }
532 }
533
534 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
535     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
536         self.try_map_id(|value| value.try_fold_with(folder))
537     }
538 }
539
540 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Box<T> {
541     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
542         (**self).visit_with(visitor)
543     }
544 }
545
546 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
547     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
548         self.try_map_id(|t| t.try_fold_with(folder))
549     }
550 }
551
552 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Vec<T> {
553     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
554         self.iter().try_for_each(|t| t.visit_with(visitor))
555     }
556 }
557
558 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for &[T] {
559     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
560         self.iter().try_for_each(|t| t.visit_with(visitor))
561     }
562 }
563
564 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<[T]> {
565     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
566         self.try_map_id(|t| t.try_fold_with(folder))
567     }
568 }
569
570 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Box<[T]> {
571     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
572         self.iter().try_for_each(|t| t.visit_with(visitor))
573     }
574 }
575
576 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<'tcx, T> {
577     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
578         folder.try_fold_binder(self)
579     }
580 }
581
582 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for ty::Binder<'tcx, T> {
583     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
584         visitor.visit_binder(self)
585     }
586 }
587
588 impl<'tcx, T: TypeFoldable<'tcx>> TypeSuperFoldable<'tcx> for ty::Binder<'tcx, T> {
589     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
590         self,
591         folder: &mut F,
592     ) -> Result<Self, F::Error> {
593         self.try_map_bound(|ty| ty.try_fold_with(folder))
594     }
595 }
596
597 impl<'tcx, T: TypeVisitable<'tcx>> TypeSuperVisitable<'tcx> for ty::Binder<'tcx, T> {
598     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
599         self.as_ref().skip_binder().visit_with(visitor)
600     }
601 }
602
603 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>> {
604     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
605         ty::util::fold_list(self, folder, |tcx, v| tcx.intern_poly_existential_predicates(v))
606     }
607 }
608
609 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Const<'tcx>> {
610     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
611         ty::util::fold_list(self, folder, |tcx, v| tcx.mk_const_list(v.iter()))
612     }
613 }
614
615 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind> {
616     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
617         ty::util::fold_list(self, folder, |tcx, v| tcx.intern_projs(v))
618     }
619 }
620
621 impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
622     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
623         folder.try_fold_ty(self)
624     }
625 }
626
627 impl<'tcx> TypeVisitable<'tcx> for Ty<'tcx> {
628     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
629         visitor.visit_ty(*self)
630     }
631 }
632
633 impl<'tcx> TypeSuperFoldable<'tcx> for Ty<'tcx> {
634     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
635         self,
636         folder: &mut F,
637     ) -> Result<Self, F::Error> {
638         let kind = match *self.kind() {
639             ty::RawPtr(tm) => ty::RawPtr(tm.try_fold_with(folder)?),
640             ty::Array(typ, sz) => ty::Array(typ.try_fold_with(folder)?, sz.try_fold_with(folder)?),
641             ty::Slice(typ) => ty::Slice(typ.try_fold_with(folder)?),
642             ty::Adt(tid, substs) => ty::Adt(tid, substs.try_fold_with(folder)?),
643             ty::Dynamic(trait_ty, region, representation) => ty::Dynamic(
644                 trait_ty.try_fold_with(folder)?,
645                 region.try_fold_with(folder)?,
646                 representation,
647             ),
648             ty::Tuple(ts) => ty::Tuple(ts.try_fold_with(folder)?),
649             ty::FnDef(def_id, substs) => ty::FnDef(def_id, substs.try_fold_with(folder)?),
650             ty::FnPtr(f) => ty::FnPtr(f.try_fold_with(folder)?),
651             ty::Ref(r, ty, mutbl) => {
652                 ty::Ref(r.try_fold_with(folder)?, ty.try_fold_with(folder)?, mutbl)
653             }
654             ty::Generator(did, substs, movability) => {
655                 ty::Generator(did, substs.try_fold_with(folder)?, movability)
656             }
657             ty::GeneratorWitness(types) => ty::GeneratorWitness(types.try_fold_with(folder)?),
658             ty::Closure(did, substs) => ty::Closure(did, substs.try_fold_with(folder)?),
659             ty::Alias(kind, data) => ty::Alias(kind, data.try_fold_with(folder)?),
660
661             ty::Bool
662             | ty::Char
663             | ty::Str
664             | ty::Int(_)
665             | ty::Uint(_)
666             | ty::Float(_)
667             | ty::Error(_)
668             | ty::Infer(_)
669             | ty::Param(..)
670             | ty::Bound(..)
671             | ty::Placeholder(..)
672             | ty::Never
673             | ty::Foreign(..) => return Ok(self),
674         };
675
676         Ok(if *self.kind() == kind { self } else { folder.tcx().mk_ty(kind) })
677     }
678 }
679
680 impl<'tcx> TypeSuperVisitable<'tcx> for Ty<'tcx> {
681     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
682         match self.kind() {
683             ty::RawPtr(ref tm) => tm.visit_with(visitor),
684             ty::Array(typ, sz) => {
685                 typ.visit_with(visitor)?;
686                 sz.visit_with(visitor)
687             }
688             ty::Slice(typ) => typ.visit_with(visitor),
689             ty::Adt(_, substs) => substs.visit_with(visitor),
690             ty::Dynamic(ref trait_ty, ref reg, _) => {
691                 trait_ty.visit_with(visitor)?;
692                 reg.visit_with(visitor)
693             }
694             ty::Tuple(ts) => ts.visit_with(visitor),
695             ty::FnDef(_, substs) => substs.visit_with(visitor),
696             ty::FnPtr(ref f) => f.visit_with(visitor),
697             ty::Ref(r, ty, _) => {
698                 r.visit_with(visitor)?;
699                 ty.visit_with(visitor)
700             }
701             ty::Generator(_did, ref substs, _) => substs.visit_with(visitor),
702             ty::GeneratorWitness(ref types) => types.visit_with(visitor),
703             ty::Closure(_did, ref substs) => substs.visit_with(visitor),
704             ty::Alias(_, ref data) => data.visit_with(visitor),
705
706             ty::Bool
707             | ty::Char
708             | ty::Str
709             | ty::Int(_)
710             | ty::Uint(_)
711             | ty::Float(_)
712             | ty::Error(_)
713             | ty::Infer(_)
714             | ty::Bound(..)
715             | ty::Placeholder(..)
716             | ty::Param(..)
717             | ty::Never
718             | ty::Foreign(..) => ControlFlow::Continue(()),
719         }
720     }
721 }
722
723 impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> {
724     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
725         folder.try_fold_region(self)
726     }
727 }
728
729 impl<'tcx> TypeVisitable<'tcx> for ty::Region<'tcx> {
730     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
731         visitor.visit_region(*self)
732     }
733 }
734
735 impl<'tcx> TypeSuperFoldable<'tcx> for ty::Region<'tcx> {
736     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
737         self,
738         _folder: &mut F,
739     ) -> Result<Self, F::Error> {
740         Ok(self)
741     }
742 }
743
744 impl<'tcx> TypeSuperVisitable<'tcx> for ty::Region<'tcx> {
745     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
746         ControlFlow::Continue(())
747     }
748 }
749
750 impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
751     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
752         folder.try_fold_predicate(self)
753     }
754 }
755
756 impl<'tcx> TypeVisitable<'tcx> for ty::Predicate<'tcx> {
757     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
758         visitor.visit_predicate(*self)
759     }
760
761     #[inline]
762     fn has_vars_bound_at_or_above(&self, binder: ty::DebruijnIndex) -> bool {
763         self.outer_exclusive_binder() > binder
764     }
765
766     #[inline]
767     fn has_type_flags(&self, flags: ty::TypeFlags) -> bool {
768         self.flags().intersects(flags)
769     }
770 }
771
772 impl<'tcx> TypeSuperFoldable<'tcx> for ty::Predicate<'tcx> {
773     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
774         self,
775         folder: &mut F,
776     ) -> Result<Self, F::Error> {
777         let new = self.kind().try_fold_with(folder)?;
778         Ok(folder.tcx().reuse_or_mk_predicate(self, new))
779     }
780 }
781
782 impl<'tcx> TypeSuperVisitable<'tcx> for ty::Predicate<'tcx> {
783     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
784         self.kind().visit_with(visitor)
785     }
786 }
787
788 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
789     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
790         ty::util::fold_list(self, folder, |tcx, v| tcx.intern_predicates(v))
791     }
792 }
793
794 impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
795     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
796         self.try_map_id(|x| x.try_fold_with(folder))
797     }
798 }
799
800 impl<'tcx, T: TypeVisitable<'tcx>, I: Idx> TypeVisitable<'tcx> for IndexVec<I, T> {
801     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
802         self.iter().try_for_each(|t| t.visit_with(visitor))
803     }
804 }
805
806 impl<'tcx> TypeFoldable<'tcx> for ty::Const<'tcx> {
807     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
808         folder.try_fold_const(self)
809     }
810 }
811
812 impl<'tcx> TypeVisitable<'tcx> for ty::Const<'tcx> {
813     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
814         visitor.visit_const(*self)
815     }
816 }
817
818 impl<'tcx> TypeSuperFoldable<'tcx> for ty::Const<'tcx> {
819     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
820         self,
821         folder: &mut F,
822     ) -> Result<Self, F::Error> {
823         let ty = self.ty().try_fold_with(folder)?;
824         let kind = self.kind().try_fold_with(folder)?;
825         if ty != self.ty() || kind != self.kind() {
826             Ok(folder.tcx().mk_const(kind, ty))
827         } else {
828             Ok(self)
829         }
830     }
831 }
832
833 impl<'tcx> TypeSuperVisitable<'tcx> for ty::Const<'tcx> {
834     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
835         self.ty().visit_with(visitor)?;
836         self.kind().visit_with(visitor)
837     }
838 }
839
840 impl<'tcx> TypeFoldable<'tcx> for InferConst<'tcx> {
841     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, _folder: &mut F) -> Result<Self, F::Error> {
842         Ok(self)
843     }
844 }
845
846 impl<'tcx> TypeVisitable<'tcx> for InferConst<'tcx> {
847     fn visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
848         ControlFlow::Continue(())
849     }
850 }
851
852 impl<'tcx> TypeSuperVisitable<'tcx> for ty::UnevaluatedConst<'tcx> {
853     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
854         self.substs.visit_with(visitor)
855     }
856 }
857
858 impl<'tcx> TypeVisitable<'tcx> for TyAndLayout<'tcx, Ty<'tcx>> {
859     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
860         visitor.visit_ty(self.ty)
861     }
862 }