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