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.
5 use crate::hir::def::Namespace;
6 use crate::mir::ProjectionKind;
7 use crate::mir::interpret;
8 use crate::ty::{self, Lift, Ty, TyCtxt, InferConst};
9 use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
10 use crate::ty::print::{FmtPrinter, Printer};
12 use rustc_index::vec::{IndexVec, Idx};
13 use smallvec::SmallVec;
19 impl fmt::Debug for ty::GenericParamDef {
20 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21 let type_name = match self.kind {
22 ty::GenericParamDefKind::Lifetime => "Lifetime",
23 ty::GenericParamDefKind::Type {..} => "Type",
24 ty::GenericParamDefKind::Const => "Const",
26 write!(f, "{}({}, {:?}, {})",
34 impl fmt::Debug for ty::TraitDef {
35 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37 FmtPrinter::new(tcx, f, Namespace::TypeNS)
38 .print_def_path(self.def_id, &[])?;
44 impl fmt::Debug for ty::AdtDef {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47 FmtPrinter::new(tcx, f, Namespace::TypeNS)
48 .print_def_path(self.did, &[])?;
54 impl fmt::Debug for ty::ClosureUpvar<'tcx> {
55 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56 write!(f, "ClosureUpvar({:?},{:?})",
62 impl fmt::Debug for ty::UpvarId {
63 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64 let name = ty::tls::with(|tcx| {
65 tcx.hir().name(self.var_path.hir_id)
67 write!(f, "UpvarId({:?};`{}`;{:?})",
74 impl fmt::Debug for ty::UpvarBorrow<'tcx> {
75 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76 write!(f, "UpvarBorrow({:?}, {:?})",
77 self.kind, self.region)
81 impl fmt::Debug for ty::ExistentialTraitRef<'tcx> {
82 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83 fmt::Display::fmt(self, f)
87 impl fmt::Debug for ty::adjustment::Adjustment<'tcx> {
88 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89 write!(f, "{:?} -> {}", self.kind, self.target)
93 impl fmt::Debug for ty::BoundRegion {
94 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
96 ty::BrAnon(n) => write!(f, "BrAnon({:?})", n),
97 ty::BrNamed(did, name) => {
98 write!(f, "BrNamed({:?}:{:?}, {})",
99 did.krate, did.index, name)
101 ty::BrEnv => write!(f, "BrEnv"),
106 impl fmt::Debug for ty::RegionKind {
107 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109 ty::ReEarlyBound(ref data) => {
110 write!(f, "ReEarlyBound({}, {})",
115 ty::ReClosureBound(ref vid) => {
116 write!(f, "ReClosureBound({:?})", vid)
119 ty::ReLateBound(binder_id, ref bound_region) => {
120 write!(f, "ReLateBound({:?}, {:?})", binder_id, bound_region)
123 ty::ReFree(ref fr) => fr.fmt(f),
125 ty::ReScope(id) => write!(f, "ReScope({:?})", id),
127 ty::ReStatic => write!(f, "ReStatic"),
129 ty::ReVar(ref vid) => vid.fmt(f),
131 ty::RePlaceholder(placeholder) => {
132 write!(f, "RePlaceholder({:?})", placeholder)
135 ty::ReEmpty => write!(f, "ReEmpty"),
137 ty::ReErased => write!(f, "ReErased"),
142 impl fmt::Debug for ty::FreeRegion {
143 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
144 write!(f, "ReFree({:?}, {:?})", self.scope, self.bound_region)
148 impl fmt::Debug for ty::Variance {
149 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
150 f.write_str(match *self {
151 ty::Covariant => "+",
152 ty::Contravariant => "-",
153 ty::Invariant => "o",
154 ty::Bivariant => "*",
159 impl fmt::Debug for ty::FnSig<'tcx> {
160 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
161 write!(f, "({:?}; c_variadic: {})->{:?}",
162 self.inputs(), self.c_variadic, self.output())
166 impl fmt::Debug for ty::TyVid {
167 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
168 write!(f, "_#{}t", self.index)
172 impl<'tcx> fmt::Debug for ty::ConstVid<'tcx> {
173 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
174 write!(f, "_#{}c", self.index)
178 impl fmt::Debug for ty::IntVid {
179 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
180 write!(f, "_#{}i", self.index)
184 impl fmt::Debug for ty::FloatVid {
185 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
186 write!(f, "_#{}f", self.index)
190 impl fmt::Debug for ty::RegionVid {
191 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
192 write!(f, "'_#{}r", self.index())
196 impl fmt::Debug for ty::InferTy {
197 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
199 ty::TyVar(ref v) => v.fmt(f),
200 ty::IntVar(ref v) => v.fmt(f),
201 ty::FloatVar(ref v) => v.fmt(f),
202 ty::FreshTy(v) => write!(f, "FreshTy({:?})", v),
203 ty::FreshIntTy(v) => write!(f, "FreshIntTy({:?})", v),
204 ty::FreshFloatTy(v) => write!(f, "FreshFloatTy({:?})", v),
209 impl fmt::Debug for ty::IntVarValue {
210 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
212 ty::IntType(ref v) => v.fmt(f),
213 ty::UintType(ref v) => v.fmt(f),
218 impl fmt::Debug for ty::FloatVarValue {
219 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
224 impl fmt::Debug for ty::TraitRef<'tcx> {
225 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
226 // FIXME(#59188) this is used across the compiler to print
227 // a `TraitRef` qualified (with the Self type explicit),
228 // instead of having a different way to make that choice.
229 write!(f, "<{} as {}>", self.self_ty(), self)
233 impl fmt::Debug for Ty<'tcx> {
234 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
235 fmt::Display::fmt(self, f)
239 impl fmt::Debug for ty::ParamTy {
240 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
241 write!(f, "{}/#{}", self.name, self.index)
245 impl fmt::Debug for ty::ParamConst {
246 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
247 write!(f, "{}/#{}", self.name, self.index)
251 impl fmt::Debug for ty::TraitPredicate<'tcx> {
252 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
253 write!(f, "TraitPredicate({:?})", self.trait_ref)
257 impl fmt::Debug for ty::ProjectionPredicate<'tcx> {
258 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
259 write!(f, "ProjectionPredicate({:?}, {:?})", self.projection_ty, self.ty)
263 impl fmt::Debug for ty::Predicate<'tcx> {
264 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
266 ty::Predicate::Trait(ref a) => a.fmt(f),
267 ty::Predicate::Subtype(ref pair) => pair.fmt(f),
268 ty::Predicate::RegionOutlives(ref pair) => pair.fmt(f),
269 ty::Predicate::TypeOutlives(ref pair) => pair.fmt(f),
270 ty::Predicate::Projection(ref pair) => pair.fmt(f),
271 ty::Predicate::WellFormed(ty) => write!(f, "WellFormed({:?})", ty),
272 ty::Predicate::ObjectSafe(trait_def_id) => {
273 write!(f, "ObjectSafe({:?})", trait_def_id)
275 ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind) => {
276 write!(f, "ClosureKind({:?}, {:?}, {:?})",
277 closure_def_id, closure_substs, kind)
279 ty::Predicate::ConstEvaluatable(def_id, substs) => {
280 write!(f, "ConstEvaluatable({:?}, {:?})", def_id, substs)
286 ///////////////////////////////////////////////////////////////////////////
289 // For things that don't carry any arena-allocated data (and are
290 // copy...), just add them to this list.
292 CloneTypeFoldableAndLiftImpls! {
296 crate::ty::layout::VariantIdx,
299 crate::middle::region::Scope,
300 ::syntax::ast::FloatTy,
301 ::syntax::ast::NodeId,
302 ::syntax_pos::symbol::Symbol,
303 crate::hir::def::Res,
304 crate::hir::def_id::DefId,
305 crate::hir::InlineAsmInner,
306 crate::hir::MatchSource,
307 crate::hir::Mutability,
308 crate::hir::Unsafety,
309 ::rustc_target::spec::abi::Abi,
311 crate::mir::Promoted,
312 crate::traits::Reveal,
313 crate::ty::adjustment::AutoBorrowMutability,
315 // Including `BoundRegion` is a *bit* dubious, but direct
316 // references to bound region appear in `ty::Error`, and aren't
317 // really meant to be folded. In general, we can only fold a fully
319 crate::ty::BoundRegion,
320 crate::ty::Placeholder<crate::ty::BoundRegion>,
321 crate::ty::ClosureKind,
322 crate::ty::FreeRegion,
324 crate::ty::IntVarValue,
325 crate::ty::ParamConst,
327 crate::ty::adjustment::PointerCast,
328 crate::ty::RegionVid,
329 crate::ty::UniverseIndex,
334 ///////////////////////////////////////////////////////////////////////////
335 // Lift implementations
337 // FIXME(eddyb) replace all the uses of `Option::map` with `?`.
338 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>> Lift<'tcx> for (A, B) {
339 type Lifted = (A::Lifted, B::Lifted);
340 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
341 tcx.lift(&self.0).and_then(|a| tcx.lift(&self.1).map(|b| (a, b)))
345 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>, C: Lift<'tcx>> Lift<'tcx> for (A, B, C) {
346 type Lifted = (A::Lifted, B::Lifted, C::Lifted);
347 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
348 tcx.lift(&self.0).and_then(|a| {
349 tcx.lift(&self.1).and_then(|b| tcx.lift(&self.2).map(|c| (a, b, c)))
354 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Option<T> {
355 type Lifted = Option<T::Lifted>;
356 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
358 Some(ref x) => tcx.lift(x).map(Some),
364 impl<'tcx, T: Lift<'tcx>, E: Lift<'tcx>> Lift<'tcx> for Result<T, E> {
365 type Lifted = Result<T::Lifted, E::Lifted>;
366 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
368 Ok(ref x) => tcx.lift(x).map(Ok),
369 Err(ref e) => tcx.lift(e).map(Err)
374 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Box<T> {
375 type Lifted = Box<T::Lifted>;
376 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
377 tcx.lift(&**self).map(Box::new)
381 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Rc<T> {
382 type Lifted = Rc<T::Lifted>;
383 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
384 tcx.lift(&**self).map(Rc::new)
388 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Arc<T> {
389 type Lifted = Arc<T::Lifted>;
390 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
391 tcx.lift(&**self).map(Arc::new)
395 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for [T] {
396 type Lifted = Vec<T::Lifted>;
397 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
398 // type annotation needed to inform `projection_must_outlive`
399 let mut result : Vec<<T as Lift<'tcx>>::Lifted>
400 = Vec::with_capacity(self.len());
402 if let Some(value) = tcx.lift(x) {
412 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Vec<T> {
413 type Lifted = Vec<T::Lifted>;
414 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
419 impl<'tcx, I: Idx, T: Lift<'tcx>> Lift<'tcx> for IndexVec<I, T> {
420 type Lifted = IndexVec<I, T::Lifted>;
421 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
423 .map(|e| tcx.lift(e))
428 impl<'a, 'tcx> Lift<'tcx> for ty::TraitRef<'a> {
429 type Lifted = ty::TraitRef<'tcx>;
430 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
431 tcx.lift(&self.substs).map(|substs| ty::TraitRef {
438 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialTraitRef<'a> {
439 type Lifted = ty::ExistentialTraitRef<'tcx>;
440 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
441 tcx.lift(&self.substs).map(|substs| ty::ExistentialTraitRef {
448 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialPredicate<'a> {
449 type Lifted = ty::ExistentialPredicate<'tcx>;
450 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
452 ty::ExistentialPredicate::Trait(x) => {
453 tcx.lift(x).map(ty::ExistentialPredicate::Trait)
455 ty::ExistentialPredicate::Projection(x) => {
456 tcx.lift(x).map(ty::ExistentialPredicate::Projection)
458 ty::ExistentialPredicate::AutoTrait(def_id) => {
459 Some(ty::ExistentialPredicate::AutoTrait(*def_id))
465 impl<'a, 'tcx> Lift<'tcx> for ty::TraitPredicate<'a> {
466 type Lifted = ty::TraitPredicate<'tcx>;
467 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<ty::TraitPredicate<'tcx>> {
468 tcx.lift(&self.trait_ref).map(|trait_ref| ty::TraitPredicate {
474 impl<'a, 'tcx> Lift<'tcx> for ty::SubtypePredicate<'a> {
475 type Lifted = ty::SubtypePredicate<'tcx>;
476 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<ty::SubtypePredicate<'tcx>> {
477 tcx.lift(&(self.a, self.b)).map(|(a, b)| ty::SubtypePredicate {
478 a_is_expected: self.a_is_expected,
485 impl<'tcx, A: Copy + Lift<'tcx>, B: Copy + Lift<'tcx>> Lift<'tcx> for ty::OutlivesPredicate<A, B> {
486 type Lifted = ty::OutlivesPredicate<A::Lifted, B::Lifted>;
487 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
488 tcx.lift(&(self.0, self.1)).map(|(a, b)| ty::OutlivesPredicate(a, b))
492 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionTy<'a> {
493 type Lifted = ty::ProjectionTy<'tcx>;
494 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<ty::ProjectionTy<'tcx>> {
495 tcx.lift(&self.substs).map(|substs| {
497 item_def_id: self.item_def_id,
504 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionPredicate<'a> {
505 type Lifted = ty::ProjectionPredicate<'tcx>;
506 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<ty::ProjectionPredicate<'tcx>> {
507 tcx.lift(&(self.projection_ty, self.ty)).map(|(projection_ty, ty)| {
508 ty::ProjectionPredicate {
516 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialProjection<'a> {
517 type Lifted = ty::ExistentialProjection<'tcx>;
518 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
519 tcx.lift(&self.substs).map(|substs| {
520 ty::ExistentialProjection {
522 ty: tcx.lift(&self.ty).expect("type must lift when substs do"),
523 item_def_id: self.item_def_id,
529 impl<'a, 'tcx> Lift<'tcx> for ty::Predicate<'a> {
530 type Lifted = ty::Predicate<'tcx>;
531 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
533 ty::Predicate::Trait(ref binder) => {
534 tcx.lift(binder).map(ty::Predicate::Trait)
536 ty::Predicate::Subtype(ref binder) => {
537 tcx.lift(binder).map(ty::Predicate::Subtype)
539 ty::Predicate::RegionOutlives(ref binder) => {
540 tcx.lift(binder).map(ty::Predicate::RegionOutlives)
542 ty::Predicate::TypeOutlives(ref binder) => {
543 tcx.lift(binder).map(ty::Predicate::TypeOutlives)
545 ty::Predicate::Projection(ref binder) => {
546 tcx.lift(binder).map(ty::Predicate::Projection)
548 ty::Predicate::WellFormed(ty) => {
549 tcx.lift(&ty).map(ty::Predicate::WellFormed)
551 ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind) => {
552 tcx.lift(&closure_substs)
553 .map(|closure_substs| ty::Predicate::ClosureKind(closure_def_id,
557 ty::Predicate::ObjectSafe(trait_def_id) => {
558 Some(ty::Predicate::ObjectSafe(trait_def_id))
560 ty::Predicate::ConstEvaluatable(def_id, substs) => {
561 tcx.lift(&substs).map(|substs| {
562 ty::Predicate::ConstEvaluatable(def_id, substs)
569 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::Binder<T> {
570 type Lifted = ty::Binder<T::Lifted>;
571 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
572 tcx.lift(self.skip_binder()).map(ty::Binder::bind)
576 impl<'a, 'tcx> Lift<'tcx> for ty::ParamEnv<'a> {
577 type Lifted = ty::ParamEnv<'tcx>;
578 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
579 tcx.lift(&self.caller_bounds).map(|caller_bounds| {
589 impl<'a, 'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::ParamEnvAnd<'a, T> {
590 type Lifted = ty::ParamEnvAnd<'tcx, T::Lifted>;
591 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
592 tcx.lift(&self.param_env).and_then(|param_env| {
593 tcx.lift(&self.value).map(|value| {
603 impl<'a, 'tcx> Lift<'tcx> for ty::ClosureSubsts<'a> {
604 type Lifted = ty::ClosureSubsts<'tcx>;
605 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
606 tcx.lift(&self.substs).map(|substs| {
607 ty::ClosureSubsts { substs }
612 impl<'a, 'tcx> Lift<'tcx> for ty::GeneratorSubsts<'a> {
613 type Lifted = ty::GeneratorSubsts<'tcx>;
614 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
615 tcx.lift(&self.substs).map(|substs| {
616 ty::GeneratorSubsts { substs }
621 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjustment<'a> {
622 type Lifted = ty::adjustment::Adjustment<'tcx>;
623 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
624 tcx.lift(&self.kind).and_then(|kind| {
625 tcx.lift(&self.target).map(|target| {
626 ty::adjustment::Adjustment { kind, target }
632 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjust<'a> {
633 type Lifted = ty::adjustment::Adjust<'tcx>;
634 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
636 ty::adjustment::Adjust::NeverToAny =>
637 Some(ty::adjustment::Adjust::NeverToAny),
638 ty::adjustment::Adjust::Pointer(ptr) =>
639 Some(ty::adjustment::Adjust::Pointer(ptr)),
640 ty::adjustment::Adjust::Deref(ref overloaded) => {
641 tcx.lift(overloaded).map(ty::adjustment::Adjust::Deref)
643 ty::adjustment::Adjust::Borrow(ref autoref) => {
644 tcx.lift(autoref).map(ty::adjustment::Adjust::Borrow)
650 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::OverloadedDeref<'a> {
651 type Lifted = ty::adjustment::OverloadedDeref<'tcx>;
652 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
653 tcx.lift(&self.region).map(|region| {
654 ty::adjustment::OverloadedDeref {
662 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::AutoBorrow<'a> {
663 type Lifted = ty::adjustment::AutoBorrow<'tcx>;
664 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
666 ty::adjustment::AutoBorrow::Ref(r, m) => {
667 tcx.lift(&r).map(|r| ty::adjustment::AutoBorrow::Ref(r, m))
669 ty::adjustment::AutoBorrow::RawPtr(m) => {
670 Some(ty::adjustment::AutoBorrow::RawPtr(m))
676 impl<'a, 'tcx> Lift<'tcx> for ty::GenSig<'a> {
677 type Lifted = ty::GenSig<'tcx>;
678 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
679 tcx.lift(&(self.yield_ty, self.return_ty))
680 .map(|(yield_ty, return_ty)| {
689 impl<'a, 'tcx> Lift<'tcx> for ty::FnSig<'a> {
690 type Lifted = ty::FnSig<'tcx>;
691 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
692 tcx.lift(&self.inputs_and_output).map(|x| {
694 inputs_and_output: x,
695 c_variadic: self.c_variadic,
696 unsafety: self.unsafety,
703 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::error::ExpectedFound<T> {
704 type Lifted = ty::error::ExpectedFound<T::Lifted>;
705 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
706 tcx.lift(&self.expected).and_then(|expected| {
707 tcx.lift(&self.found).map(|found| {
708 ty::error::ExpectedFound {
717 impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> {
718 type Lifted = ty::error::TypeError<'tcx>;
719 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
720 use crate::ty::error::TypeError::*;
723 Mismatch => Mismatch,
724 UnsafetyMismatch(x) => UnsafetyMismatch(x),
725 AbiMismatch(x) => AbiMismatch(x),
726 Mutability => Mutability,
727 TupleSize(x) => TupleSize(x),
728 FixedArraySize(x) => FixedArraySize(x),
729 ArgCount => ArgCount,
730 RegionsDoesNotOutlive(a, b) => {
731 return tcx.lift(&(a, b)).map(|(a, b)| RegionsDoesNotOutlive(a, b))
733 RegionsInsufficientlyPolymorphic(a, b) => {
734 return tcx.lift(&b).map(|b| RegionsInsufficientlyPolymorphic(a, b))
736 RegionsOverlyPolymorphic(a, b) => {
737 return tcx.lift(&b).map(|b| RegionsOverlyPolymorphic(a, b))
739 RegionsPlaceholderMismatch => RegionsPlaceholderMismatch,
740 IntMismatch(x) => IntMismatch(x),
741 FloatMismatch(x) => FloatMismatch(x),
742 Traits(x) => Traits(x),
743 VariadicMismatch(x) => VariadicMismatch(x),
744 CyclicTy(t) => return tcx.lift(&t).map(|t| CyclicTy(t)),
745 ProjectionMismatched(x) => ProjectionMismatched(x),
746 ProjectionBoundsLength(x) => ProjectionBoundsLength(x),
747 Sorts(ref x) => return tcx.lift(x).map(Sorts),
748 ExistentialMismatch(ref x) => return tcx.lift(x).map(ExistentialMismatch),
749 ConstMismatch(ref x) => return tcx.lift(x).map(ConstMismatch),
750 IntrinsicCast => IntrinsicCast,
751 ObjectUnsafeCoercion(ref x) => return tcx.lift(x).map(ObjectUnsafeCoercion),
756 impl<'a, 'tcx> Lift<'tcx> for ty::InstanceDef<'a> {
757 type Lifted = ty::InstanceDef<'tcx>;
758 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
760 ty::InstanceDef::Item(def_id) =>
761 Some(ty::InstanceDef::Item(def_id)),
762 ty::InstanceDef::VtableShim(def_id) =>
763 Some(ty::InstanceDef::VtableShim(def_id)),
764 ty::InstanceDef::ReifyShim(def_id) =>
765 Some(ty::InstanceDef::ReifyShim(def_id)),
766 ty::InstanceDef::Intrinsic(def_id) =>
767 Some(ty::InstanceDef::Intrinsic(def_id)),
768 ty::InstanceDef::FnPtrShim(def_id, ref ty) =>
769 Some(ty::InstanceDef::FnPtrShim(def_id, tcx.lift(ty)?)),
770 ty::InstanceDef::Virtual(def_id, n) =>
771 Some(ty::InstanceDef::Virtual(def_id, n)),
772 ty::InstanceDef::ClosureOnceShim { call_once } =>
773 Some(ty::InstanceDef::ClosureOnceShim { call_once }),
774 ty::InstanceDef::DropGlue(def_id, ref ty) =>
775 Some(ty::InstanceDef::DropGlue(def_id, tcx.lift(ty)?)),
776 ty::InstanceDef::CloneShim(def_id, ref ty) =>
777 Some(ty::InstanceDef::CloneShim(def_id, tcx.lift(ty)?)),
782 ///////////////////////////////////////////////////////////////////////////
783 // TypeFoldable implementations.
785 // Ideally, each type should invoke `folder.fold_foo(self)` and
786 // nothing else. In some cases, though, we haven't gotten around to
787 // adding methods on the `folder` yet, and thus the folding is
788 // hard-coded here. This is less-flexible, because folders cannot
789 // override the behavior, but there are a lot of random types and one
790 // can easily refactor the folding into the TypeFolder trait as
793 /// AdtDefs are basically the same as a DefId.
794 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::AdtDef {
795 fn super_fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> Self {
799 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
804 impl<'tcx, T: TypeFoldable<'tcx>, U: TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
805 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> (T, U) {
806 (self.0.fold_with(folder), self.1.fold_with(folder))
809 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
810 self.0.visit_with(visitor) || self.1.visit_with(visitor)
814 EnumTypeFoldableImpl! {
815 impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
818 } where T: TypeFoldable<'tcx>
821 EnumTypeFoldableImpl! {
822 impl<'tcx, T, E> TypeFoldable<'tcx> for Result<T, E> {
825 } where T: TypeFoldable<'tcx>, E: TypeFoldable<'tcx>,
828 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
829 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
830 Rc::new((**self).fold_with(folder))
833 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
834 (**self).visit_with(visitor)
838 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Arc<T> {
839 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
840 Arc::new((**self).fold_with(folder))
843 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
844 (**self).visit_with(visitor)
848 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
849 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
850 let content: T = (**self).fold_with(folder);
854 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
855 (**self).visit_with(visitor)
859 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
860 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
861 self.iter().map(|t| t.fold_with(folder)).collect()
864 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
865 self.iter().any(|t| t.visit_with(visitor))
869 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<[T]> {
870 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
871 self.iter().map(|t| t.fold_with(folder)).collect::<Vec<_>>().into_boxed_slice()
874 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
875 self.iter().any(|t| t.visit_with(visitor))
879 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<T> {
880 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
881 self.map_bound_ref(|ty| ty.fold_with(folder))
884 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
885 folder.fold_binder(self)
888 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
889 self.skip_binder().visit_with(visitor)
892 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
893 visitor.visit_binder(self)
897 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::ExistentialPredicate<'tcx>> {
898 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
899 let v = self.iter().map(|p| p.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
900 folder.tcx().intern_existential_predicates(&v)
903 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
904 self.iter().any(|p| p.visit_with(visitor))
908 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<Ty<'tcx>> {
909 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
910 let v = self.iter().map(|t| t.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
911 folder.tcx().intern_type_list(&v)
914 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
915 self.iter().any(|t| t.visit_with(visitor))
919 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind> {
920 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
921 let v = self.iter().map(|t| t.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
922 folder.tcx().intern_projs(&v)
925 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
926 self.iter().any(|t| t.visit_with(visitor))
930 impl<'tcx> TypeFoldable<'tcx> for ty::instance::Instance<'tcx> {
931 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
932 use crate::ty::InstanceDef::*;
934 substs: self.substs.fold_with(folder),
935 def: match self.def {
936 Item(did) => Item(did.fold_with(folder)),
937 VtableShim(did) => VtableShim(did.fold_with(folder)),
938 ReifyShim(did) => ReifyShim(did.fold_with(folder)),
939 Intrinsic(did) => Intrinsic(did.fold_with(folder)),
940 FnPtrShim(did, ty) => FnPtrShim(
941 did.fold_with(folder),
942 ty.fold_with(folder),
944 Virtual(did, i) => Virtual(
945 did.fold_with(folder),
948 ClosureOnceShim { call_once } => ClosureOnceShim {
949 call_once: call_once.fold_with(folder),
951 DropGlue(did, ty) => DropGlue(
952 did.fold_with(folder),
953 ty.fold_with(folder),
955 CloneShim(did, ty) => CloneShim(
956 did.fold_with(folder),
957 ty.fold_with(folder),
963 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
964 use crate::ty::InstanceDef::*;
965 self.substs.visit_with(visitor) ||
967 Item(did) | VtableShim(did) | ReifyShim(did) | Intrinsic(did) | Virtual(did, _) => {
968 did.visit_with(visitor)
970 FnPtrShim(did, ty) | CloneShim(did, ty) => {
971 did.visit_with(visitor) || ty.visit_with(visitor)
973 DropGlue(did, ty) => {
974 did.visit_with(visitor) || ty.visit_with(visitor)
976 ClosureOnceShim { call_once } => call_once.visit_with(visitor),
981 impl<'tcx> TypeFoldable<'tcx> for interpret::GlobalId<'tcx> {
982 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
984 instance: self.instance.fold_with(folder),
985 promoted: self.promoted
989 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
990 self.instance.visit_with(visitor)
994 impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
995 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
996 let kind = match self.kind {
997 ty::RawPtr(tm) => ty::RawPtr(tm.fold_with(folder)),
998 ty::Array(typ, sz) => ty::Array(typ.fold_with(folder), sz.fold_with(folder)),
999 ty::Slice(typ) => ty::Slice(typ.fold_with(folder)),
1000 ty::Adt(tid, substs) => ty::Adt(tid, substs.fold_with(folder)),
1001 ty::Dynamic(ref trait_ty, ref region) =>
1002 ty::Dynamic(trait_ty.fold_with(folder), region.fold_with(folder)),
1003 ty::Tuple(ts) => ty::Tuple(ts.fold_with(folder)),
1004 ty::FnDef(def_id, substs) => {
1005 ty::FnDef(def_id, substs.fold_with(folder))
1007 ty::FnPtr(f) => ty::FnPtr(f.fold_with(folder)),
1008 ty::Ref(ref r, ty, mutbl) => {
1009 ty::Ref(r.fold_with(folder), ty.fold_with(folder), mutbl)
1011 ty::Generator(did, substs, movability) => {
1014 substs.fold_with(folder),
1017 ty::GeneratorWitness(types) => ty::GeneratorWitness(types.fold_with(folder)),
1018 ty::Closure(did, substs) => ty::Closure(did, substs.fold_with(folder)),
1019 ty::Projection(ref data) => ty::Projection(data.fold_with(folder)),
1020 ty::UnnormalizedProjection(ref data) => {
1021 ty::UnnormalizedProjection(data.fold_with(folder))
1023 ty::Opaque(did, substs) => ty::Opaque(did, substs.fold_with(folder)),
1035 ty::Placeholder(..) |
1037 ty::Foreign(..) => return self,
1040 if self.kind == kind {
1043 folder.tcx().mk_ty(kind)
1047 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1048 folder.fold_ty(*self)
1051 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1053 ty::RawPtr(ref tm) => tm.visit_with(visitor),
1054 ty::Array(typ, sz) => typ.visit_with(visitor) || sz.visit_with(visitor),
1055 ty::Slice(typ) => typ.visit_with(visitor),
1056 ty::Adt(_, substs) => substs.visit_with(visitor),
1057 ty::Dynamic(ref trait_ty, ref reg) =>
1058 trait_ty.visit_with(visitor) || reg.visit_with(visitor),
1059 ty::Tuple(ts) => ts.visit_with(visitor),
1060 ty::FnDef(_, substs) => substs.visit_with(visitor),
1061 ty::FnPtr(ref f) => f.visit_with(visitor),
1062 ty::Ref(r, ty, _) => r.visit_with(visitor) || ty.visit_with(visitor),
1063 ty::Generator(_did, ref substs, _) => {
1064 substs.visit_with(visitor)
1066 ty::GeneratorWitness(ref types) => types.visit_with(visitor),
1067 ty::Closure(_did, ref substs) => substs.visit_with(visitor),
1068 ty::Projection(ref data) | ty::UnnormalizedProjection(ref data) => {
1069 data.visit_with(visitor)
1071 ty::Opaque(_, ref substs) => substs.visit_with(visitor),
1082 ty::Placeholder(..) |
1085 ty::Foreign(..) => false,
1089 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1090 visitor.visit_ty(self)
1094 impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> {
1095 fn super_fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> Self {
1099 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1100 folder.fold_region(*self)
1103 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
1107 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1108 visitor.visit_region(*self)
1112 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
1113 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1114 // This code is hot enough that it's worth specializing for a list of
1115 // length 0. (No other length is common enough to be worth singling
1117 if self.len() == 0 {
1120 // Don't bother interning if nothing changed, which is the common
1122 let v = self.iter().map(|p| p.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
1123 if v[..] == self[..] {
1126 folder.tcx().intern_predicates(&v)
1131 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1132 self.iter().any(|p| p.visit_with(visitor))
1136 impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
1137 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1138 self.iter().map(|x| x.fold_with(folder)).collect()
1141 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1142 self.iter().any(|t| t.visit_with(visitor))
1146 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Const<'tcx> {
1147 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1148 let ty = self.ty.fold_with(folder);
1149 let val = self.val.fold_with(folder);
1150 folder.tcx().mk_const(ty::Const {
1156 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1157 folder.fold_const(*self)
1160 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1161 self.ty.visit_with(visitor) || self.val.visit_with(visitor)
1164 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1165 visitor.visit_const(self)
1169 impl<'tcx> TypeFoldable<'tcx> for ty::ConstKind<'tcx> {
1170 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1172 ty::ConstKind::Infer(ic) => ty::ConstKind::Infer(ic.fold_with(folder)),
1173 ty::ConstKind::Param(p) => ty::ConstKind::Param(p.fold_with(folder)),
1174 ty::ConstKind::Unevaluated(did, substs)
1175 => ty::ConstKind::Unevaluated(did, substs.fold_with(folder)),
1176 ty::ConstKind::Value(_) | ty::ConstKind::Bound(..)
1177 | ty::ConstKind::Placeholder(..) => *self,
1181 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1183 ty::ConstKind::Infer(ic) => ic.visit_with(visitor),
1184 ty::ConstKind::Param(p) => p.visit_with(visitor),
1185 ty::ConstKind::Unevaluated(_, substs) => substs.visit_with(visitor),
1186 ty::ConstKind::Value(_) | ty::ConstKind::Bound(..)
1187 | ty::ConstKind::Placeholder(_) => false,
1192 impl<'tcx> TypeFoldable<'tcx> for InferConst<'tcx> {
1193 fn super_fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> Self {
1197 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {