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