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 (e.g.,
4 //! `BraceStructLiftImpl!`) to help with the tedium.
6 use crate::mir::ProjectionKind;
7 use crate::mir::interpret::ConstValue;
8 use crate::ty::{self, Lift, Ty, TyCtxt};
9 use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
10 use rustc_data_structures::indexed_vec::{IndexVec, Idx};
11 use smallvec::SmallVec;
12 use crate::mir::interpret;
16 ///////////////////////////////////////////////////////////////////////////
19 // For things that don't carry any arena-allocated data (and are
20 // copy...), just add them to this list.
22 CloneTypeFoldableAndLiftImpls! {
26 crate::ty::layout::VariantIdx,
29 crate::middle::region::Scope,
30 ::syntax::ast::FloatTy,
31 ::syntax::ast::NodeId,
32 ::syntax_pos::symbol::Symbol,
34 crate::hir::def_id::DefId,
35 crate::hir::InlineAsm,
36 crate::hir::MatchSource,
37 crate::hir::Mutability,
39 ::rustc_target::spec::abi::Abi,
42 crate::traits::Reveal,
43 crate::ty::adjustment::AutoBorrowMutability,
45 // Including `BoundRegion` is a *bit* dubious, but direct
46 // references to bound region appear in `ty::Error`, and aren't
47 // really meant to be folded. In general, we can only fold a fully
49 crate::ty::BoundRegion,
50 crate::ty::ClosureKind,
51 crate::ty::IntVarValue,
53 crate::ty::UniverseIndex,
58 ///////////////////////////////////////////////////////////////////////////
59 // Lift implementations
61 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>> Lift<'tcx> for (A, B) {
62 type Lifted = (A::Lifted, B::Lifted);
63 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
64 tcx.lift(&self.0).and_then(|a| tcx.lift(&self.1).map(|b| (a, b)))
68 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>, C: Lift<'tcx>> Lift<'tcx> for (A, B, C) {
69 type Lifted = (A::Lifted, B::Lifted, C::Lifted);
70 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
71 tcx.lift(&self.0).and_then(|a| {
72 tcx.lift(&self.1).and_then(|b| tcx.lift(&self.2).map(|c| (a, b, c)))
77 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Option<T> {
78 type Lifted = Option<T::Lifted>;
79 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
81 Some(ref x) => tcx.lift(x).map(Some),
87 impl<'tcx, T: Lift<'tcx>, E: Lift<'tcx>> Lift<'tcx> for Result<T, E> {
88 type Lifted = Result<T::Lifted, E::Lifted>;
89 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
91 Ok(ref x) => tcx.lift(x).map(Ok),
92 Err(ref e) => tcx.lift(e).map(Err)
97 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Box<T> {
98 type Lifted = Box<T::Lifted>;
99 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
100 tcx.lift(&**self).map(Box::new)
104 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for [T] {
105 type Lifted = Vec<T::Lifted>;
106 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
107 // type annotation needed to inform `projection_must_outlive`
108 let mut result : Vec<<T as Lift<'tcx>>::Lifted>
109 = Vec::with_capacity(self.len());
111 if let Some(value) = tcx.lift(x) {
121 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Vec<T> {
122 type Lifted = Vec<T::Lifted>;
123 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
128 impl<'tcx, I: Idx, T: Lift<'tcx>> Lift<'tcx> for IndexVec<I, T> {
129 type Lifted = IndexVec<I, T::Lifted>;
130 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
132 .map(|e| tcx.lift(e))
137 impl<'a, 'tcx> Lift<'tcx> for ty::TraitRef<'a> {
138 type Lifted = ty::TraitRef<'tcx>;
139 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
140 tcx.lift(&self.substs).map(|substs| ty::TraitRef {
147 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialTraitRef<'a> {
148 type Lifted = ty::ExistentialTraitRef<'tcx>;
149 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
150 tcx.lift(&self.substs).map(|substs| ty::ExistentialTraitRef {
157 impl<'a, 'tcx> Lift<'tcx> for ty::TraitPredicate<'a> {
158 type Lifted = ty::TraitPredicate<'tcx>;
159 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
160 -> Option<ty::TraitPredicate<'tcx>> {
161 tcx.lift(&self.trait_ref).map(|trait_ref| ty::TraitPredicate {
167 impl<'a, 'tcx> Lift<'tcx> for ty::SubtypePredicate<'a> {
168 type Lifted = ty::SubtypePredicate<'tcx>;
169 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
170 -> Option<ty::SubtypePredicate<'tcx>> {
171 tcx.lift(&(self.a, self.b)).map(|(a, b)| ty::SubtypePredicate {
172 a_is_expected: self.a_is_expected,
179 impl<'tcx, A: Copy+Lift<'tcx>, B: Copy+Lift<'tcx>> Lift<'tcx> for ty::OutlivesPredicate<A, B> {
180 type Lifted = ty::OutlivesPredicate<A::Lifted, B::Lifted>;
181 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
182 tcx.lift(&(self.0, self.1)).map(|(a, b)| ty::OutlivesPredicate(a, b))
186 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionTy<'a> {
187 type Lifted = ty::ProjectionTy<'tcx>;
188 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
189 -> Option<ty::ProjectionTy<'tcx>> {
190 tcx.lift(&self.substs).map(|substs| {
192 item_def_id: self.item_def_id,
199 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionPredicate<'a> {
200 type Lifted = ty::ProjectionPredicate<'tcx>;
201 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
202 -> Option<ty::ProjectionPredicate<'tcx>> {
203 tcx.lift(&(self.projection_ty, self.ty)).map(|(projection_ty, ty)| {
204 ty::ProjectionPredicate {
212 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialProjection<'a> {
213 type Lifted = ty::ExistentialProjection<'tcx>;
214 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
215 tcx.lift(&self.substs).map(|substs| {
216 ty::ExistentialProjection {
218 ty: tcx.lift(&self.ty).expect("type must lift when substs do"),
219 item_def_id: self.item_def_id,
225 impl<'a, 'tcx> Lift<'tcx> for ty::Predicate<'a> {
226 type Lifted = ty::Predicate<'tcx>;
227 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
229 ty::Predicate::Trait(ref binder) => {
230 tcx.lift(binder).map(ty::Predicate::Trait)
232 ty::Predicate::Subtype(ref binder) => {
233 tcx.lift(binder).map(ty::Predicate::Subtype)
235 ty::Predicate::RegionOutlives(ref binder) => {
236 tcx.lift(binder).map(ty::Predicate::RegionOutlives)
238 ty::Predicate::TypeOutlives(ref binder) => {
239 tcx.lift(binder).map(ty::Predicate::TypeOutlives)
241 ty::Predicate::Projection(ref binder) => {
242 tcx.lift(binder).map(ty::Predicate::Projection)
244 ty::Predicate::WellFormed(ty) => {
245 tcx.lift(&ty).map(ty::Predicate::WellFormed)
247 ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind) => {
248 tcx.lift(&closure_substs)
249 .map(|closure_substs| ty::Predicate::ClosureKind(closure_def_id,
253 ty::Predicate::ObjectSafe(trait_def_id) => {
254 Some(ty::Predicate::ObjectSafe(trait_def_id))
256 ty::Predicate::ConstEvaluatable(def_id, substs) => {
257 tcx.lift(&substs).map(|substs| {
258 ty::Predicate::ConstEvaluatable(def_id, substs)
265 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::Binder<T> {
266 type Lifted = ty::Binder<T::Lifted>;
267 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
268 tcx.lift(self.skip_binder()).map(ty::Binder::bind)
272 impl<'a, 'tcx> Lift<'tcx> for ty::ParamEnv<'a> {
273 type Lifted = ty::ParamEnv<'tcx>;
274 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
275 tcx.lift(&self.caller_bounds).map(|caller_bounds| {
285 impl<'a, 'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::ParamEnvAnd<'a, T> {
286 type Lifted = ty::ParamEnvAnd<'tcx, T::Lifted>;
287 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
288 tcx.lift(&self.param_env).and_then(|param_env| {
289 tcx.lift(&self.value).map(|value| {
299 impl<'a, 'tcx> Lift<'tcx> for ty::ClosureSubsts<'a> {
300 type Lifted = ty::ClosureSubsts<'tcx>;
301 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
302 tcx.lift(&self.substs).map(|substs| {
303 ty::ClosureSubsts { substs }
308 impl<'a, 'tcx> Lift<'tcx> for ty::GeneratorSubsts<'a> {
309 type Lifted = ty::GeneratorSubsts<'tcx>;
310 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
311 tcx.lift(&self.substs).map(|substs| {
312 ty::GeneratorSubsts { substs }
317 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjustment<'a> {
318 type Lifted = ty::adjustment::Adjustment<'tcx>;
319 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
320 tcx.lift(&self.kind).and_then(|kind| {
321 tcx.lift(&self.target).map(|target| {
322 ty::adjustment::Adjustment { kind, target }
328 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjust<'a> {
329 type Lifted = ty::adjustment::Adjust<'tcx>;
330 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
332 ty::adjustment::Adjust::NeverToAny =>
333 Some(ty::adjustment::Adjust::NeverToAny),
334 ty::adjustment::Adjust::ReifyFnPointer =>
335 Some(ty::adjustment::Adjust::ReifyFnPointer),
336 ty::adjustment::Adjust::UnsafeFnPointer =>
337 Some(ty::adjustment::Adjust::UnsafeFnPointer),
338 ty::adjustment::Adjust::ClosureFnPointer =>
339 Some(ty::adjustment::Adjust::ClosureFnPointer),
340 ty::adjustment::Adjust::MutToConstPointer =>
341 Some(ty::adjustment::Adjust::MutToConstPointer),
342 ty::adjustment::Adjust::Unsize =>
343 Some(ty::adjustment::Adjust::Unsize),
344 ty::adjustment::Adjust::Deref(ref overloaded) => {
345 tcx.lift(overloaded).map(ty::adjustment::Adjust::Deref)
347 ty::adjustment::Adjust::Borrow(ref autoref) => {
348 tcx.lift(autoref).map(ty::adjustment::Adjust::Borrow)
354 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::OverloadedDeref<'a> {
355 type Lifted = ty::adjustment::OverloadedDeref<'tcx>;
356 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
357 tcx.lift(&self.region).map(|region| {
358 ty::adjustment::OverloadedDeref {
366 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::AutoBorrow<'a> {
367 type Lifted = ty::adjustment::AutoBorrow<'tcx>;
368 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
370 ty::adjustment::AutoBorrow::Ref(r, m) => {
371 tcx.lift(&r).map(|r| ty::adjustment::AutoBorrow::Ref(r, m))
373 ty::adjustment::AutoBorrow::RawPtr(m) => {
374 Some(ty::adjustment::AutoBorrow::RawPtr(m))
380 impl<'a, 'tcx> Lift<'tcx> for ty::GenSig<'a> {
381 type Lifted = ty::GenSig<'tcx>;
382 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
383 tcx.lift(&(self.yield_ty, self.return_ty))
384 .map(|(yield_ty, return_ty)| {
393 impl<'a, 'tcx> Lift<'tcx> for ty::FnSig<'a> {
394 type Lifted = ty::FnSig<'tcx>;
395 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
396 tcx.lift(&self.inputs_and_output).map(|x| {
398 inputs_and_output: x,
399 variadic: self.variadic,
400 unsafety: self.unsafety,
407 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::error::ExpectedFound<T> {
408 type Lifted = ty::error::ExpectedFound<T::Lifted>;
409 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
410 tcx.lift(&self.expected).and_then(|expected| {
411 tcx.lift(&self.found).map(|found| {
412 ty::error::ExpectedFound {
421 impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> {
422 type Lifted = ty::error::TypeError<'tcx>;
423 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
424 use crate::ty::error::TypeError::*;
427 Mismatch => Mismatch,
428 UnsafetyMismatch(x) => UnsafetyMismatch(x),
429 AbiMismatch(x) => AbiMismatch(x),
430 Mutability => Mutability,
431 TupleSize(x) => TupleSize(x),
432 FixedArraySize(x) => FixedArraySize(x),
433 ArgCount => ArgCount,
434 RegionsDoesNotOutlive(a, b) => {
435 return tcx.lift(&(a, b)).map(|(a, b)| RegionsDoesNotOutlive(a, b))
437 RegionsPlaceholderMismatch => RegionsPlaceholderMismatch,
438 IntMismatch(x) => IntMismatch(x),
439 FloatMismatch(x) => FloatMismatch(x),
440 Traits(x) => Traits(x),
441 VariadicMismatch(x) => VariadicMismatch(x),
442 CyclicTy(t) => return tcx.lift(&t).map(|t| CyclicTy(t)),
443 ProjectionMismatched(x) => ProjectionMismatched(x),
444 ProjectionBoundsLength(x) => ProjectionBoundsLength(x),
445 Sorts(ref x) => return tcx.lift(x).map(Sorts),
446 ExistentialMismatch(ref x) => return tcx.lift(x).map(ExistentialMismatch)
451 impl<'a, 'tcx> Lift<'tcx> for ty::InstanceDef<'a> {
452 type Lifted = ty::InstanceDef<'tcx>;
453 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
455 ty::InstanceDef::Item(def_id) =>
456 Some(ty::InstanceDef::Item(def_id)),
457 ty::InstanceDef::VtableShim(def_id) =>
458 Some(ty::InstanceDef::VtableShim(def_id)),
459 ty::InstanceDef::Intrinsic(def_id) =>
460 Some(ty::InstanceDef::Intrinsic(def_id)),
461 ty::InstanceDef::FnPtrShim(def_id, ref ty) =>
462 Some(ty::InstanceDef::FnPtrShim(def_id, tcx.lift(ty)?)),
463 ty::InstanceDef::Virtual(def_id, n) =>
464 Some(ty::InstanceDef::Virtual(def_id, n)),
465 ty::InstanceDef::ClosureOnceShim { call_once } =>
466 Some(ty::InstanceDef::ClosureOnceShim { call_once }),
467 ty::InstanceDef::DropGlue(def_id, ref ty) =>
468 Some(ty::InstanceDef::DropGlue(def_id, tcx.lift(ty)?)),
469 ty::InstanceDef::CloneShim(def_id, ref ty) =>
470 Some(ty::InstanceDef::CloneShim(def_id, tcx.lift(ty)?)),
475 BraceStructLiftImpl! {
476 impl<'a, 'tcx> Lift<'tcx> for ty::Instance<'a> {
477 type Lifted = ty::Instance<'tcx>;
482 BraceStructLiftImpl! {
483 impl<'a, 'tcx> Lift<'tcx> for interpret::GlobalId<'a> {
484 type Lifted = interpret::GlobalId<'tcx>;
489 BraceStructLiftImpl! {
490 impl<'a, 'tcx> Lift<'tcx> for ty::Const<'a> {
491 type Lifted = ty::Const<'tcx>;
496 impl<'a, 'tcx> Lift<'tcx> for ConstValue<'a> {
497 type Lifted = ConstValue<'tcx>;
498 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
500 ConstValue::Scalar(x) => Some(ConstValue::Scalar(x)),
501 ConstValue::Slice(x, y) => Some(ConstValue::Slice(x, y)),
502 ConstValue::ByRef(x, alloc, z) => Some(ConstValue::ByRef(
503 x, alloc.lift_to_tcx(tcx)?, z,
509 ///////////////////////////////////////////////////////////////////////////
510 // TypeFoldable implementations.
512 // Ideally, each type should invoke `folder.fold_foo(self)` and
513 // nothing else. In some cases, though, we haven't gotten around to
514 // adding methods on the `folder` yet, and thus the folding is
515 // hard-coded here. This is less-flexible, because folders cannot
516 // override the behavior, but there are a lot of random types and one
517 // can easily refactor the folding into the TypeFolder trait as
520 /// AdtDefs are basically the same as a DefId.
521 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::AdtDef {
522 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, _folder: &mut F) -> Self {
526 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
531 impl<'tcx, T:TypeFoldable<'tcx>, U:TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
532 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> (T, U) {
533 (self.0.fold_with(folder), self.1.fold_with(folder))
536 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
537 self.0.visit_with(visitor) || self.1.visit_with(visitor)
541 EnumTypeFoldableImpl! {
542 impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
545 } where T: TypeFoldable<'tcx>
548 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
549 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
550 Rc::new((**self).fold_with(folder))
553 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
554 (**self).visit_with(visitor)
558 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
559 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
560 let content: T = (**self).fold_with(folder);
564 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
565 (**self).visit_with(visitor)
569 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
570 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
571 self.iter().map(|t| t.fold_with(folder)).collect()
574 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
575 self.iter().any(|t| t.visit_with(visitor))
579 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<[T]> {
580 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
581 self.iter().map(|t| t.fold_with(folder)).collect::<Vec<_>>().into_boxed_slice()
584 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
585 self.iter().any(|t| t.visit_with(visitor))
589 impl<'tcx, T:TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<T> {
590 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
591 self.map_bound_ref(|ty| ty.fold_with(folder))
594 fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
595 folder.fold_binder(self)
598 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
599 self.skip_binder().visit_with(visitor)
602 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
603 visitor.visit_binder(self)
607 BraceStructTypeFoldableImpl! {
608 impl<'tcx> TypeFoldable<'tcx> for ty::ParamEnv<'tcx> { reveal, caller_bounds, def_id }
611 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::ExistentialPredicate<'tcx>> {
612 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
613 let v = self.iter().map(|p| p.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
614 folder.tcx().intern_existential_predicates(&v)
617 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
618 self.iter().any(|p| p.visit_with(visitor))
622 EnumTypeFoldableImpl! {
623 impl<'tcx> TypeFoldable<'tcx> for ty::ExistentialPredicate<'tcx> {
624 (ty::ExistentialPredicate::Trait)(a),
625 (ty::ExistentialPredicate::Projection)(a),
626 (ty::ExistentialPredicate::AutoTrait)(a),
630 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<Ty<'tcx>> {
631 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
632 let v = self.iter().map(|t| t.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
633 folder.tcx().intern_type_list(&v)
636 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
637 self.iter().any(|t| t.visit_with(visitor))
641 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind<'tcx>> {
642 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
643 let v = self.iter().map(|t| t.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
644 folder.tcx().intern_projs(&v)
647 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
648 self.iter().any(|t| t.visit_with(visitor))
652 impl<'tcx> TypeFoldable<'tcx> for ty::instance::Instance<'tcx> {
653 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
654 use crate::ty::InstanceDef::*;
656 substs: self.substs.fold_with(folder),
657 def: match self.def {
658 Item(did) => Item(did.fold_with(folder)),
659 VtableShim(did) => VtableShim(did.fold_with(folder)),
660 Intrinsic(did) => Intrinsic(did.fold_with(folder)),
661 FnPtrShim(did, ty) => FnPtrShim(
662 did.fold_with(folder),
663 ty.fold_with(folder),
665 Virtual(did, i) => Virtual(
666 did.fold_with(folder),
669 ClosureOnceShim { call_once } => ClosureOnceShim {
670 call_once: call_once.fold_with(folder),
672 DropGlue(did, ty) => DropGlue(
673 did.fold_with(folder),
674 ty.fold_with(folder),
676 CloneShim(did, ty) => CloneShim(
677 did.fold_with(folder),
678 ty.fold_with(folder),
684 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
685 use crate::ty::InstanceDef::*;
686 self.substs.visit_with(visitor) ||
688 Item(did) | VtableShim(did) | Intrinsic(did) | Virtual(did, _) => {
689 did.visit_with(visitor)
691 FnPtrShim(did, ty) | CloneShim(did, ty) => {
692 did.visit_with(visitor) || ty.visit_with(visitor)
694 DropGlue(did, ty) => {
695 did.visit_with(visitor) || ty.visit_with(visitor)
697 ClosureOnceShim { call_once } => call_once.visit_with(visitor),
702 impl<'tcx> TypeFoldable<'tcx> for interpret::GlobalId<'tcx> {
703 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
705 instance: self.instance.fold_with(folder),
706 promoted: self.promoted
710 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
711 self.instance.visit_with(visitor)
715 impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
716 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
717 let sty = match self.sty {
718 ty::RawPtr(tm) => ty::RawPtr(tm.fold_with(folder)),
719 ty::Array(typ, sz) => ty::Array(typ.fold_with(folder), sz.fold_with(folder)),
720 ty::Slice(typ) => ty::Slice(typ.fold_with(folder)),
721 ty::Adt(tid, substs) => ty::Adt(tid, substs.fold_with(folder)),
722 ty::Dynamic(ref trait_ty, ref region) =>
723 ty::Dynamic(trait_ty.fold_with(folder), region.fold_with(folder)),
724 ty::Tuple(ts) => ty::Tuple(ts.fold_with(folder)),
725 ty::FnDef(def_id, substs) => {
726 ty::FnDef(def_id, substs.fold_with(folder))
728 ty::FnPtr(f) => ty::FnPtr(f.fold_with(folder)),
729 ty::Ref(ref r, ty, mutbl) => {
730 ty::Ref(r.fold_with(folder), ty.fold_with(folder), mutbl)
732 ty::Generator(did, substs, movability) => {
735 substs.fold_with(folder),
738 ty::GeneratorWitness(types) => ty::GeneratorWitness(types.fold_with(folder)),
739 ty::Closure(did, substs) => ty::Closure(did, substs.fold_with(folder)),
740 ty::Projection(ref data) => ty::Projection(data.fold_with(folder)),
741 ty::UnnormalizedProjection(ref data) => {
742 ty::UnnormalizedProjection(data.fold_with(folder))
744 ty::Opaque(did, substs) => ty::Opaque(did, substs.fold_with(folder)),
756 ty::Placeholder(..) |
758 ty::Foreign(..) => return self
764 folder.tcx().mk_ty(sty)
768 fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
769 folder.fold_ty(*self)
772 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
774 ty::RawPtr(ref tm) => tm.visit_with(visitor),
775 ty::Array(typ, sz) => typ.visit_with(visitor) || sz.visit_with(visitor),
776 ty::Slice(typ) => typ.visit_with(visitor),
777 ty::Adt(_, substs) => substs.visit_with(visitor),
778 ty::Dynamic(ref trait_ty, ref reg) =>
779 trait_ty.visit_with(visitor) || reg.visit_with(visitor),
780 ty::Tuple(ts) => ts.visit_with(visitor),
781 ty::FnDef(_, substs) => substs.visit_with(visitor),
782 ty::FnPtr(ref f) => f.visit_with(visitor),
783 ty::Ref(r, ty, _) => r.visit_with(visitor) || ty.visit_with(visitor),
784 ty::Generator(_did, ref substs, _) => {
785 substs.visit_with(visitor)
787 ty::GeneratorWitness(ref types) => types.visit_with(visitor),
788 ty::Closure(_did, ref substs) => substs.visit_with(visitor),
789 ty::Projection(ref data) | ty::UnnormalizedProjection(ref data) => {
790 data.visit_with(visitor)
792 ty::Opaque(_, ref substs) => substs.visit_with(visitor),
803 ty::Placeholder(..) |
806 ty::Foreign(..) => false,
810 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
811 visitor.visit_ty(self)
815 BraceStructTypeFoldableImpl! {
816 impl<'tcx> TypeFoldable<'tcx> for ty::TypeAndMut<'tcx> {
821 BraceStructTypeFoldableImpl! {
822 impl<'tcx> TypeFoldable<'tcx> for ty::GenSig<'tcx> {
827 BraceStructTypeFoldableImpl! {
828 impl<'tcx> TypeFoldable<'tcx> for ty::FnSig<'tcx> {
829 inputs_and_output, variadic, unsafety, abi
833 BraceStructTypeFoldableImpl! {
834 impl<'tcx> TypeFoldable<'tcx> for ty::TraitRef<'tcx> { def_id, substs }
837 BraceStructTypeFoldableImpl! {
838 impl<'tcx> TypeFoldable<'tcx> for ty::ExistentialTraitRef<'tcx> { def_id, substs }
841 BraceStructTypeFoldableImpl! {
842 impl<'tcx> TypeFoldable<'tcx> for ty::ImplHeader<'tcx> {
850 impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> {
851 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, _folder: &mut F) -> Self {
855 fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
856 folder.fold_region(*self)
859 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
863 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
864 visitor.visit_region(*self)
868 BraceStructTypeFoldableImpl! {
869 impl<'tcx> TypeFoldable<'tcx> for ty::ClosureSubsts<'tcx> {
874 BraceStructTypeFoldableImpl! {
875 impl<'tcx> TypeFoldable<'tcx> for ty::GeneratorSubsts<'tcx> {
880 BraceStructTypeFoldableImpl! {
881 impl<'tcx> TypeFoldable<'tcx> for ty::adjustment::Adjustment<'tcx> {
887 EnumTypeFoldableImpl! {
888 impl<'tcx> TypeFoldable<'tcx> for ty::adjustment::Adjust<'tcx> {
889 (ty::adjustment::Adjust::NeverToAny),
890 (ty::adjustment::Adjust::ReifyFnPointer),
891 (ty::adjustment::Adjust::UnsafeFnPointer),
892 (ty::adjustment::Adjust::ClosureFnPointer),
893 (ty::adjustment::Adjust::MutToConstPointer),
894 (ty::adjustment::Adjust::Unsize),
895 (ty::adjustment::Adjust::Deref)(a),
896 (ty::adjustment::Adjust::Borrow)(a),
900 BraceStructTypeFoldableImpl! {
901 impl<'tcx> TypeFoldable<'tcx> for ty::adjustment::OverloadedDeref<'tcx> {
906 EnumTypeFoldableImpl! {
907 impl<'tcx> TypeFoldable<'tcx> for ty::adjustment::AutoBorrow<'tcx> {
908 (ty::adjustment::AutoBorrow::Ref)(a, b),
909 (ty::adjustment::AutoBorrow::RawPtr)(m),
913 BraceStructTypeFoldableImpl! {
914 impl<'tcx> TypeFoldable<'tcx> for ty::GenericPredicates<'tcx> {
919 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
920 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
921 let v = self.iter().map(|p| p.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
922 folder.tcx().intern_predicates(&v)
925 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
926 self.iter().any(|p| p.visit_with(visitor))
930 EnumTypeFoldableImpl! {
931 impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
932 (ty::Predicate::Trait)(a),
933 (ty::Predicate::Subtype)(a),
934 (ty::Predicate::RegionOutlives)(a),
935 (ty::Predicate::TypeOutlives)(a),
936 (ty::Predicate::Projection)(a),
937 (ty::Predicate::WellFormed)(a),
938 (ty::Predicate::ClosureKind)(a, b, c),
939 (ty::Predicate::ObjectSafe)(a),
940 (ty::Predicate::ConstEvaluatable)(a, b),
944 BraceStructTypeFoldableImpl! {
945 impl<'tcx> TypeFoldable<'tcx> for ty::ProjectionPredicate<'tcx> {
950 BraceStructTypeFoldableImpl! {
951 impl<'tcx> TypeFoldable<'tcx> for ty::ExistentialProjection<'tcx> {
952 ty, substs, item_def_id
956 BraceStructTypeFoldableImpl! {
957 impl<'tcx> TypeFoldable<'tcx> for ty::ProjectionTy<'tcx> {
962 BraceStructTypeFoldableImpl! {
963 impl<'tcx> TypeFoldable<'tcx> for ty::InstantiatedPredicates<'tcx> {
968 BraceStructTypeFoldableImpl! {
969 impl<'tcx, T> TypeFoldable<'tcx> for ty::ParamEnvAnd<'tcx, T> {
971 } where T: TypeFoldable<'tcx>
974 BraceStructTypeFoldableImpl! {
975 impl<'tcx> TypeFoldable<'tcx> for ty::SubtypePredicate<'tcx> {
980 BraceStructTypeFoldableImpl! {
981 impl<'tcx> TypeFoldable<'tcx> for ty::TraitPredicate<'tcx> {
986 TupleStructTypeFoldableImpl! {
987 impl<'tcx,T,U> TypeFoldable<'tcx> for ty::OutlivesPredicate<T,U> {
989 } where T : TypeFoldable<'tcx>, U : TypeFoldable<'tcx>,
992 BraceStructTypeFoldableImpl! {
993 impl<'tcx> TypeFoldable<'tcx> for ty::ClosureUpvar<'tcx> {
998 BraceStructTypeFoldableImpl! {
999 impl<'tcx, T> TypeFoldable<'tcx> for ty::error::ExpectedFound<T> {
1001 } where T: TypeFoldable<'tcx>
1004 impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
1005 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
1006 self.iter().map(|x| x.fold_with(folder)).collect()
1009 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1010 self.iter().any(|t| t.visit_with(visitor))
1014 EnumTypeFoldableImpl! {
1015 impl<'tcx> TypeFoldable<'tcx> for ty::error::TypeError<'tcx> {
1016 (ty::error::TypeError::Mismatch),
1017 (ty::error::TypeError::UnsafetyMismatch)(x),
1018 (ty::error::TypeError::AbiMismatch)(x),
1019 (ty::error::TypeError::Mutability),
1020 (ty::error::TypeError::TupleSize)(x),
1021 (ty::error::TypeError::FixedArraySize)(x),
1022 (ty::error::TypeError::ArgCount),
1023 (ty::error::TypeError::RegionsDoesNotOutlive)(a, b),
1024 (ty::error::TypeError::RegionsPlaceholderMismatch),
1025 (ty::error::TypeError::IntMismatch)(x),
1026 (ty::error::TypeError::FloatMismatch)(x),
1027 (ty::error::TypeError::Traits)(x),
1028 (ty::error::TypeError::VariadicMismatch)(x),
1029 (ty::error::TypeError::CyclicTy)(t),
1030 (ty::error::TypeError::ProjectionMismatched)(x),
1031 (ty::error::TypeError::ProjectionBoundsLength)(x),
1032 (ty::error::TypeError::Sorts)(x),
1033 (ty::error::TypeError::ExistentialMismatch)(x),
1037 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::LazyConst<'tcx> {
1038 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
1039 let new = match self {
1040 ty::LazyConst::Evaluated(v) => ty::LazyConst::Evaluated(v.fold_with(folder)),
1041 ty::LazyConst::Unevaluated(def_id, substs) => {
1042 ty::LazyConst::Unevaluated(*def_id, substs.fold_with(folder))
1045 folder.tcx().mk_lazy_const(new)
1048 fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
1049 folder.fold_const(*self)
1052 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1054 ty::LazyConst::Evaluated(c) => c.visit_with(visitor),
1055 ty::LazyConst::Unevaluated(_, substs) => substs.visit_with(visitor),
1059 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1060 visitor.visit_const(self)
1064 impl<'tcx> TypeFoldable<'tcx> for ty::Const<'tcx> {
1065 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
1066 let ty = self.ty.fold_with(folder);
1067 let val = self.val.fold_with(folder);
1074 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1075 self.ty.visit_with(visitor) || self.val.visit_with(visitor)
1079 impl<'tcx> TypeFoldable<'tcx> for ConstValue<'tcx> {
1080 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, _folder: &mut F) -> Self {
1084 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {