]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/ty/structural_impls.rs
Rollup merge of #107519 - joboet:raw_os_error_ty, r=Amanieu
[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     u16,
205     u32,
206     u64,
207     String,
208     crate::middle::region::Scope,
209     crate::ty::FloatTy,
210     ::rustc_ast::InlineAsmOptions,
211     ::rustc_ast::InlineAsmTemplatePiece,
212     ::rustc_ast::NodeId,
213     ::rustc_span::symbol::Symbol,
214     ::rustc_hir::def::Res,
215     ::rustc_hir::def_id::DefId,
216     ::rustc_hir::def_id::LocalDefId,
217     ::rustc_hir::HirId,
218     ::rustc_hir::MatchSource,
219     ::rustc_hir::Mutability,
220     ::rustc_hir::Unsafety,
221     ::rustc_target::asm::InlineAsmRegOrRegClass,
222     ::rustc_target::spec::abi::Abi,
223     crate::mir::coverage::ExpressionOperandId,
224     crate::mir::coverage::CounterValueReference,
225     crate::mir::coverage::InjectedExpressionId,
226     crate::mir::coverage::InjectedExpressionIndex,
227     crate::mir::coverage::MappedExpressionIndex,
228     crate::mir::Local,
229     crate::mir::Promoted,
230     crate::traits::Reveal,
231     crate::ty::adjustment::AutoBorrowMutability,
232     crate::ty::AdtKind,
233     crate::ty::BoundConstness,
234     // Including `BoundRegionKind` is a *bit* dubious, but direct
235     // references to bound region appear in `ty::Error`, and aren't
236     // really meant to be folded. In general, we can only fold a fully
237     // general `Region`.
238     crate::ty::BoundRegionKind,
239     crate::ty::AssocItem,
240     crate::ty::AssocKind,
241     crate::ty::AliasKind,
242     crate::ty::Placeholder<crate::ty::BoundRegionKind>,
243     crate::ty::Placeholder<crate::ty::BoundTyKind>,
244     crate::ty::ClosureKind,
245     crate::ty::FreeRegion,
246     crate::ty::InferTy,
247     crate::ty::IntVarValue,
248     crate::ty::ParamConst,
249     crate::ty::ParamTy,
250     crate::ty::adjustment::PointerCast,
251     crate::ty::RegionVid,
252     crate::ty::UniverseIndex,
253     crate::ty::Variance,
254     ::rustc_span::Span,
255     ::rustc_errors::ErrorGuaranteed,
256     Field,
257     interpret::Scalar,
258     rustc_target::abi::Size,
259     rustc_type_ir::DebruijnIndex,
260     ty::BoundVar,
261     ty::Placeholder<ty::BoundVar>,
262 }
263
264 TrivialTypeTraversalAndLiftImpls! {
265     for<'tcx> {
266         ty::ValTree<'tcx>,
267     }
268 }
269
270 ///////////////////////////////////////////////////////////////////////////
271 // Lift implementations
272
273 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>> Lift<'tcx> for (A, B) {
274     type Lifted = (A::Lifted, B::Lifted);
275     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
276         Some((tcx.lift(self.0)?, tcx.lift(self.1)?))
277     }
278 }
279
280 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>, C: Lift<'tcx>> Lift<'tcx> for (A, B, C) {
281     type Lifted = (A::Lifted, B::Lifted, C::Lifted);
282     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
283         Some((tcx.lift(self.0)?, tcx.lift(self.1)?, tcx.lift(self.2)?))
284     }
285 }
286
287 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Option<T> {
288     type Lifted = Option<T::Lifted>;
289     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
290         Some(match self {
291             Some(x) => Some(tcx.lift(x)?),
292             None => None,
293         })
294     }
295 }
296
297 impl<'tcx, T: Lift<'tcx>, E: Lift<'tcx>> Lift<'tcx> for Result<T, E> {
298     type Lifted = Result<T::Lifted, E::Lifted>;
299     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
300         match self {
301             Ok(x) => tcx.lift(x).map(Ok),
302             Err(e) => tcx.lift(e).map(Err),
303         }
304     }
305 }
306
307 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Box<T> {
308     type Lifted = Box<T::Lifted>;
309     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
310         Some(Box::new(tcx.lift(*self)?))
311     }
312 }
313
314 impl<'tcx, T: Lift<'tcx> + Clone> Lift<'tcx> for Rc<T> {
315     type Lifted = Rc<T::Lifted>;
316     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
317         Some(Rc::new(tcx.lift(self.as_ref().clone())?))
318     }
319 }
320
321 impl<'tcx, T: Lift<'tcx> + Clone> Lift<'tcx> for Arc<T> {
322     type Lifted = Arc<T::Lifted>;
323     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
324         Some(Arc::new(tcx.lift(self.as_ref().clone())?))
325     }
326 }
327 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Vec<T> {
328     type Lifted = Vec<T::Lifted>;
329     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
330         self.into_iter().map(|v| tcx.lift(v)).collect()
331     }
332 }
333
334 impl<'tcx, I: Idx, T: Lift<'tcx>> Lift<'tcx> for IndexVec<I, T> {
335     type Lifted = IndexVec<I, T::Lifted>;
336     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
337         self.into_iter().map(|e| tcx.lift(e)).collect()
338     }
339 }
340
341 impl<'a, 'tcx> Lift<'tcx> for Term<'a> {
342     type Lifted = ty::Term<'tcx>;
343     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
344         Some(
345             match self.unpack() {
346                 TermKind::Ty(ty) => TermKind::Ty(tcx.lift(ty)?),
347                 TermKind::Const(c) => TermKind::Const(tcx.lift(c)?),
348             }
349             .pack(),
350         )
351     }
352 }
353 impl<'a, 'tcx> Lift<'tcx> for ty::ParamEnv<'a> {
354     type Lifted = ty::ParamEnv<'tcx>;
355     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
356         tcx.lift(self.caller_bounds())
357             .map(|caller_bounds| ty::ParamEnv::new(caller_bounds, self.reveal(), self.constness()))
358     }
359 }
360
361 ///////////////////////////////////////////////////////////////////////////
362 // TypeFoldable implementations.
363
364 /// AdtDefs are basically the same as a DefId.
365 impl<'tcx> TypeFoldable<'tcx> for ty::AdtDef<'tcx> {
366     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, _folder: &mut F) -> Result<Self, F::Error> {
367         Ok(self)
368     }
369 }
370
371 impl<'tcx> TypeVisitable<'tcx> for ty::AdtDef<'tcx> {
372     fn visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
373         ControlFlow::Continue(())
374     }
375 }
376
377 impl<'tcx, T: TypeFoldable<'tcx>, U: TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
378     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
379         self,
380         folder: &mut F,
381     ) -> Result<(T, U), F::Error> {
382         Ok((self.0.try_fold_with(folder)?, self.1.try_fold_with(folder)?))
383     }
384 }
385
386 impl<'tcx, T: TypeVisitable<'tcx>, U: TypeVisitable<'tcx>> TypeVisitable<'tcx> for (T, U) {
387     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
388         self.0.visit_with(visitor)?;
389         self.1.visit_with(visitor)
390     }
391 }
392
393 impl<'tcx, A: TypeFoldable<'tcx>, B: TypeFoldable<'tcx>, C: TypeFoldable<'tcx>> TypeFoldable<'tcx>
394     for (A, B, C)
395 {
396     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
397         self,
398         folder: &mut F,
399     ) -> Result<(A, B, C), F::Error> {
400         Ok((
401             self.0.try_fold_with(folder)?,
402             self.1.try_fold_with(folder)?,
403             self.2.try_fold_with(folder)?,
404         ))
405     }
406 }
407
408 impl<'tcx, A: TypeVisitable<'tcx>, B: TypeVisitable<'tcx>, C: TypeVisitable<'tcx>>
409     TypeVisitable<'tcx> for (A, B, C)
410 {
411     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
412         self.0.visit_with(visitor)?;
413         self.1.visit_with(visitor)?;
414         self.2.visit_with(visitor)
415     }
416 }
417
418 EnumTypeTraversalImpl! {
419     impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
420         (Some)(a),
421         (None),
422     } where T: TypeFoldable<'tcx>
423 }
424 EnumTypeTraversalImpl! {
425     impl<'tcx, T> TypeVisitable<'tcx> for Option<T> {
426         (Some)(a),
427         (None),
428     } where T: TypeVisitable<'tcx>
429 }
430
431 EnumTypeTraversalImpl! {
432     impl<'tcx, T, E> TypeFoldable<'tcx> for Result<T, E> {
433         (Ok)(a),
434         (Err)(a),
435     } where T: TypeFoldable<'tcx>, E: TypeFoldable<'tcx>,
436 }
437 EnumTypeTraversalImpl! {
438     impl<'tcx, T, E> TypeVisitable<'tcx> for Result<T, E> {
439         (Ok)(a),
440         (Err)(a),
441     } where T: TypeVisitable<'tcx>, E: TypeVisitable<'tcx>,
442 }
443
444 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
445     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
446         mut self,
447         folder: &mut F,
448     ) -> Result<Self, F::Error> {
449         // We merely want to replace the contained `T`, if at all possible,
450         // so that we don't needlessly allocate a new `Rc` or indeed clone
451         // the contained type.
452         unsafe {
453             // First step is to ensure that we have a unique reference to
454             // the contained type, which `Rc::make_mut` will accomplish (by
455             // allocating a new `Rc` and cloning the `T` only if required).
456             // This is done *before* casting to `Rc<ManuallyDrop<T>>` so that
457             // panicking during `make_mut` does not leak the `T`.
458             Rc::make_mut(&mut self);
459
460             // Casting to `Rc<ManuallyDrop<T>>` is safe because `ManuallyDrop`
461             // is `repr(transparent)`.
462             let ptr = Rc::into_raw(self).cast::<ManuallyDrop<T>>();
463             let mut unique = Rc::from_raw(ptr);
464
465             // Call to `Rc::make_mut` above guarantees that `unique` is the
466             // sole reference to the contained value, so we can avoid doing
467             // a checked `get_mut` here.
468             let slot = Rc::get_mut_unchecked(&mut unique);
469
470             // Semantically move the contained type out from `unique`, fold
471             // it, then move the folded value back into `unique`. Should
472             // folding fail, `ManuallyDrop` ensures that the "moved-out"
473             // value is not re-dropped.
474             let owned = ManuallyDrop::take(slot);
475             let folded = owned.try_fold_with(folder)?;
476             *slot = ManuallyDrop::new(folded);
477
478             // Cast back to `Rc<T>`.
479             Ok(Rc::from_raw(Rc::into_raw(unique).cast()))
480         }
481     }
482 }
483
484 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Rc<T> {
485     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
486         (**self).visit_with(visitor)
487     }
488 }
489
490 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Arc<T> {
491     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
492         mut self,
493         folder: &mut F,
494     ) -> Result<Self, F::Error> {
495         // We merely want to replace the contained `T`, if at all possible,
496         // so that we don't needlessly allocate a new `Arc` or indeed clone
497         // the contained type.
498         unsafe {
499             // First step is to ensure that we have a unique reference to
500             // the contained type, which `Arc::make_mut` will accomplish (by
501             // allocating a new `Arc` and cloning the `T` only if required).
502             // This is done *before* casting to `Arc<ManuallyDrop<T>>` so that
503             // panicking during `make_mut` does not leak the `T`.
504             Arc::make_mut(&mut self);
505
506             // Casting to `Arc<ManuallyDrop<T>>` is safe because `ManuallyDrop`
507             // is `repr(transparent)`.
508             let ptr = Arc::into_raw(self).cast::<ManuallyDrop<T>>();
509             let mut unique = Arc::from_raw(ptr);
510
511             // Call to `Arc::make_mut` above guarantees that `unique` is the
512             // sole reference to the contained value, so we can avoid doing
513             // a checked `get_mut` here.
514             let slot = Arc::get_mut_unchecked(&mut unique);
515
516             // Semantically move the contained type out from `unique`, fold
517             // it, then move the folded value back into `unique`. Should
518             // folding fail, `ManuallyDrop` ensures that the "moved-out"
519             // value is not re-dropped.
520             let owned = ManuallyDrop::take(slot);
521             let folded = owned.try_fold_with(folder)?;
522             *slot = ManuallyDrop::new(folded);
523
524             // Cast back to `Arc<T>`.
525             Ok(Arc::from_raw(Arc::into_raw(unique).cast()))
526         }
527     }
528 }
529
530 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Arc<T> {
531     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
532         (**self).visit_with(visitor)
533     }
534 }
535
536 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
537     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
538         self.try_map_id(|value| value.try_fold_with(folder))
539     }
540 }
541
542 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Box<T> {
543     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
544         (**self).visit_with(visitor)
545     }
546 }
547
548 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
549     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
550         self.try_map_id(|t| t.try_fold_with(folder))
551     }
552 }
553
554 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Vec<T> {
555     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
556         self.iter().try_for_each(|t| t.visit_with(visitor))
557     }
558 }
559
560 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for &[T] {
561     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
562         self.iter().try_for_each(|t| t.visit_with(visitor))
563     }
564 }
565
566 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<[T]> {
567     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
568         self.try_map_id(|t| t.try_fold_with(folder))
569     }
570 }
571
572 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Box<[T]> {
573     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
574         self.iter().try_for_each(|t| t.visit_with(visitor))
575     }
576 }
577
578 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<'tcx, T> {
579     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
580         folder.try_fold_binder(self)
581     }
582 }
583
584 impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for ty::Binder<'tcx, T> {
585     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
586         visitor.visit_binder(self)
587     }
588 }
589
590 impl<'tcx, T: TypeFoldable<'tcx>> TypeSuperFoldable<'tcx> for ty::Binder<'tcx, T> {
591     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
592         self,
593         folder: &mut F,
594     ) -> Result<Self, F::Error> {
595         self.try_map_bound(|ty| ty.try_fold_with(folder))
596     }
597 }
598
599 impl<'tcx, T: TypeVisitable<'tcx>> TypeSuperVisitable<'tcx> for ty::Binder<'tcx, T> {
600     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
601         self.as_ref().skip_binder().visit_with(visitor)
602     }
603 }
604
605 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>> {
606     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
607         ty::util::fold_list(self, folder, |tcx, v| tcx.intern_poly_existential_predicates(v))
608     }
609 }
610
611 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Const<'tcx>> {
612     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
613         ty::util::fold_list(self, folder, |tcx, v| tcx.mk_const_list(v.iter()))
614     }
615 }
616
617 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind> {
618     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
619         ty::util::fold_list(self, folder, |tcx, v| tcx.intern_projs(v))
620     }
621 }
622
623 impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
624     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
625         folder.try_fold_ty(self)
626     }
627 }
628
629 impl<'tcx> TypeVisitable<'tcx> for Ty<'tcx> {
630     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
631         visitor.visit_ty(*self)
632     }
633 }
634
635 impl<'tcx> TypeSuperFoldable<'tcx> for Ty<'tcx> {
636     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
637         self,
638         folder: &mut F,
639     ) -> Result<Self, F::Error> {
640         let kind = match *self.kind() {
641             ty::RawPtr(tm) => ty::RawPtr(tm.try_fold_with(folder)?),
642             ty::Array(typ, sz) => ty::Array(typ.try_fold_with(folder)?, sz.try_fold_with(folder)?),
643             ty::Slice(typ) => ty::Slice(typ.try_fold_with(folder)?),
644             ty::Adt(tid, substs) => ty::Adt(tid, substs.try_fold_with(folder)?),
645             ty::Dynamic(trait_ty, region, representation) => ty::Dynamic(
646                 trait_ty.try_fold_with(folder)?,
647                 region.try_fold_with(folder)?,
648                 representation,
649             ),
650             ty::Tuple(ts) => ty::Tuple(ts.try_fold_with(folder)?),
651             ty::FnDef(def_id, substs) => ty::FnDef(def_id, substs.try_fold_with(folder)?),
652             ty::FnPtr(f) => ty::FnPtr(f.try_fold_with(folder)?),
653             ty::Ref(r, ty, mutbl) => {
654                 ty::Ref(r.try_fold_with(folder)?, ty.try_fold_with(folder)?, mutbl)
655             }
656             ty::Generator(did, substs, movability) => {
657                 ty::Generator(did, substs.try_fold_with(folder)?, movability)
658             }
659             ty::GeneratorWitness(types) => ty::GeneratorWitness(types.try_fold_with(folder)?),
660             ty::GeneratorWitnessMIR(did, substs) => {
661                 ty::GeneratorWitnessMIR(did, substs.try_fold_with(folder)?)
662             }
663             ty::Closure(did, substs) => ty::Closure(did, substs.try_fold_with(folder)?),
664             ty::Alias(kind, data) => ty::Alias(kind, data.try_fold_with(folder)?),
665
666             ty::Bool
667             | ty::Char
668             | ty::Str
669             | ty::Int(_)
670             | ty::Uint(_)
671             | ty::Float(_)
672             | ty::Error(_)
673             | ty::Infer(_)
674             | ty::Param(..)
675             | ty::Bound(..)
676             | ty::Placeholder(..)
677             | ty::Never
678             | ty::Foreign(..) => return Ok(self),
679         };
680
681         Ok(if *self.kind() == kind { self } else { folder.tcx().mk_ty(kind) })
682     }
683 }
684
685 impl<'tcx> TypeSuperVisitable<'tcx> for Ty<'tcx> {
686     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
687         match self.kind() {
688             ty::RawPtr(ref tm) => tm.visit_with(visitor),
689             ty::Array(typ, sz) => {
690                 typ.visit_with(visitor)?;
691                 sz.visit_with(visitor)
692             }
693             ty::Slice(typ) => typ.visit_with(visitor),
694             ty::Adt(_, substs) => substs.visit_with(visitor),
695             ty::Dynamic(ref trait_ty, ref reg, _) => {
696                 trait_ty.visit_with(visitor)?;
697                 reg.visit_with(visitor)
698             }
699             ty::Tuple(ts) => ts.visit_with(visitor),
700             ty::FnDef(_, substs) => substs.visit_with(visitor),
701             ty::FnPtr(ref f) => f.visit_with(visitor),
702             ty::Ref(r, ty, _) => {
703                 r.visit_with(visitor)?;
704                 ty.visit_with(visitor)
705             }
706             ty::Generator(_did, ref substs, _) => substs.visit_with(visitor),
707             ty::GeneratorWitness(ref types) => types.visit_with(visitor),
708             ty::GeneratorWitnessMIR(_did, ref substs) => substs.visit_with(visitor),
709             ty::Closure(_did, ref substs) => substs.visit_with(visitor),
710             ty::Alias(_, ref data) => data.visit_with(visitor),
711
712             ty::Bool
713             | ty::Char
714             | ty::Str
715             | ty::Int(_)
716             | ty::Uint(_)
717             | ty::Float(_)
718             | ty::Error(_)
719             | ty::Infer(_)
720             | ty::Bound(..)
721             | ty::Placeholder(..)
722             | ty::Param(..)
723             | ty::Never
724             | ty::Foreign(..) => ControlFlow::Continue(()),
725         }
726     }
727 }
728
729 impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> {
730     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
731         folder.try_fold_region(self)
732     }
733 }
734
735 impl<'tcx> TypeVisitable<'tcx> for ty::Region<'tcx> {
736     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
737         visitor.visit_region(*self)
738     }
739 }
740
741 impl<'tcx> TypeSuperFoldable<'tcx> for ty::Region<'tcx> {
742     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
743         self,
744         _folder: &mut F,
745     ) -> Result<Self, F::Error> {
746         Ok(self)
747     }
748 }
749
750 impl<'tcx> TypeSuperVisitable<'tcx> for ty::Region<'tcx> {
751     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
752         ControlFlow::Continue(())
753     }
754 }
755
756 impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
757     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
758         folder.try_fold_predicate(self)
759     }
760 }
761
762 impl<'tcx> TypeVisitable<'tcx> for ty::Predicate<'tcx> {
763     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
764         visitor.visit_predicate(*self)
765     }
766
767     #[inline]
768     fn has_vars_bound_at_or_above(&self, binder: ty::DebruijnIndex) -> bool {
769         self.outer_exclusive_binder() > binder
770     }
771
772     #[inline]
773     fn has_type_flags(&self, flags: ty::TypeFlags) -> bool {
774         self.flags().intersects(flags)
775     }
776 }
777
778 impl<'tcx> TypeSuperFoldable<'tcx> for ty::Predicate<'tcx> {
779     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
780         self,
781         folder: &mut F,
782     ) -> Result<Self, F::Error> {
783         let new = self.kind().try_fold_with(folder)?;
784         Ok(folder.tcx().reuse_or_mk_predicate(self, new))
785     }
786 }
787
788 impl<'tcx> TypeSuperVisitable<'tcx> for ty::Predicate<'tcx> {
789     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
790         self.kind().visit_with(visitor)
791     }
792 }
793
794 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
795     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
796         ty::util::fold_list(self, folder, |tcx, v| tcx.intern_predicates(v))
797     }
798 }
799
800 impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
801     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
802         self.try_map_id(|x| x.try_fold_with(folder))
803     }
804 }
805
806 impl<'tcx, T: TypeVisitable<'tcx>, I: Idx> TypeVisitable<'tcx> for IndexVec<I, T> {
807     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
808         self.iter().try_for_each(|t| t.visit_with(visitor))
809     }
810 }
811
812 impl<'tcx> TypeFoldable<'tcx> for ty::Const<'tcx> {
813     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
814         folder.try_fold_const(self)
815     }
816 }
817
818 impl<'tcx> TypeVisitable<'tcx> for ty::Const<'tcx> {
819     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
820         visitor.visit_const(*self)
821     }
822 }
823
824 impl<'tcx> TypeSuperFoldable<'tcx> for ty::Const<'tcx> {
825     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
826         self,
827         folder: &mut F,
828     ) -> Result<Self, F::Error> {
829         let ty = self.ty().try_fold_with(folder)?;
830         let kind = self.kind().try_fold_with(folder)?;
831         if ty != self.ty() || kind != self.kind() {
832             Ok(folder.tcx().mk_const(kind, ty))
833         } else {
834             Ok(self)
835         }
836     }
837 }
838
839 impl<'tcx> TypeSuperVisitable<'tcx> for ty::Const<'tcx> {
840     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
841         self.ty().visit_with(visitor)?;
842         self.kind().visit_with(visitor)
843     }
844 }
845
846 impl<'tcx> TypeFoldable<'tcx> for InferConst<'tcx> {
847     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, _folder: &mut F) -> Result<Self, F::Error> {
848         Ok(self)
849     }
850 }
851
852 impl<'tcx> TypeVisitable<'tcx> for InferConst<'tcx> {
853     fn visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
854         ControlFlow::Continue(())
855     }
856 }
857
858 impl<'tcx> TypeSuperVisitable<'tcx> for ty::UnevaluatedConst<'tcx> {
859     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
860         self.substs.visit_with(visitor)
861     }
862 }
863
864 impl<'tcx> TypeVisitable<'tcx> for TyAndLayout<'tcx, Ty<'tcx>> {
865     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
866         visitor.visit_ty(self.ty)
867     }
868 }