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::mir::interpret;
6 use crate::mir::ProjectionKind;
7 use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
8 use crate::ty::print::{FmtPrinter, Printer};
9 use crate::ty::{self, InferConst, Lift, Ty, TyCtxt};
10 use rustc_hir::def::Namespace;
11 use rustc_hir::def_id::CRATE_DEF_INDEX;
12 use rustc_index::vec::{Idx, IndexVec};
14 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, "{}({}, {:?}, {})", type_name, self.name, self.def_id, self.index)
30 impl fmt::Debug for ty::TraitDef {
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 FmtPrinter::new(tcx, f, Namespace::TypeNS).print_def_path(self.def_id, &[])?;
39 impl fmt::Debug for ty::AdtDef {
40 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42 FmtPrinter::new(tcx, f, Namespace::TypeNS).print_def_path(self.did, &[])?;
48 impl fmt::Debug for ty::ClosureUpvar<'tcx> {
49 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50 write!(f, "ClosureUpvar({:?},{:?})", self.res, self.ty)
54 impl fmt::Debug for ty::UpvarId {
55 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56 let name = ty::tls::with(|tcx| tcx.hir().name(self.var_path.hir_id));
57 write!(f, "UpvarId({:?};`{}`;{:?})", self.var_path.hir_id, name, self.closure_expr_id)
61 impl fmt::Debug for ty::UpvarBorrow<'tcx> {
62 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63 write!(f, "UpvarBorrow({:?}, {:?})", self.kind, self.region)
67 impl fmt::Debug for ty::ExistentialTraitRef<'tcx> {
68 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69 fmt::Display::fmt(self, f)
73 impl fmt::Debug for ty::adjustment::Adjustment<'tcx> {
74 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75 write!(f, "{:?} -> {}", self.kind, self.target)
79 impl fmt::Debug for ty::BoundRegion {
80 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82 ty::BrAnon(n) => write!(f, "BrAnon({:?})", n),
83 ty::BrNamed(did, name) => {
84 if did.index == CRATE_DEF_INDEX {
85 write!(f, "BrNamed({})", name)
87 write!(f, "BrNamed({:?}, {})", did, name)
90 ty::BrEnv => write!(f, "BrEnv"),
95 impl fmt::Debug for ty::RegionKind {
96 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98 ty::ReEarlyBound(ref data) => write!(f, "ReEarlyBound({}, {})", data.index, data.name),
100 ty::ReClosureBound(ref vid) => write!(f, "ReClosureBound({:?})", vid),
102 ty::ReLateBound(binder_id, ref bound_region) => {
103 write!(f, "ReLateBound({:?}, {:?})", binder_id, bound_region)
106 ty::ReFree(ref fr) => fr.fmt(f),
108 ty::ReScope(id) => write!(f, "ReScope({:?})", id),
110 ty::ReStatic => write!(f, "ReStatic"),
112 ty::ReVar(ref vid) => vid.fmt(f),
114 ty::RePlaceholder(placeholder) => write!(f, "RePlaceholder({:?})", placeholder),
116 ty::ReEmpty => write!(f, "ReEmpty"),
118 ty::ReErased => write!(f, "ReErased"),
123 impl fmt::Debug for ty::FreeRegion {
124 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
125 write!(f, "ReFree({:?}, {:?})", self.scope, self.bound_region)
129 impl fmt::Debug for ty::Variance {
130 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
131 f.write_str(match *self {
132 ty::Covariant => "+",
133 ty::Contravariant => "-",
134 ty::Invariant => "o",
135 ty::Bivariant => "*",
140 impl fmt::Debug for ty::FnSig<'tcx> {
141 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
142 write!(f, "({:?}; c_variadic: {})->{:?}", self.inputs(), self.c_variadic, self.output())
146 impl fmt::Debug for ty::TyVid {
147 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
148 write!(f, "_#{}t", self.index)
152 impl<'tcx> fmt::Debug for ty::ConstVid<'tcx> {
153 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
154 write!(f, "_#{}c", self.index)
158 impl fmt::Debug for ty::IntVid {
159 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
160 write!(f, "_#{}i", self.index)
164 impl fmt::Debug for ty::FloatVid {
165 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
166 write!(f, "_#{}f", self.index)
170 impl fmt::Debug for ty::RegionVid {
171 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
172 write!(f, "'_#{}r", self.index())
176 impl fmt::Debug for ty::InferTy {
177 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
179 ty::TyVar(ref v) => v.fmt(f),
180 ty::IntVar(ref v) => v.fmt(f),
181 ty::FloatVar(ref v) => v.fmt(f),
182 ty::FreshTy(v) => write!(f, "FreshTy({:?})", v),
183 ty::FreshIntTy(v) => write!(f, "FreshIntTy({:?})", v),
184 ty::FreshFloatTy(v) => write!(f, "FreshFloatTy({:?})", v),
189 impl fmt::Debug for ty::IntVarValue {
190 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
192 ty::IntType(ref v) => v.fmt(f),
193 ty::UintType(ref v) => v.fmt(f),
198 impl fmt::Debug for ty::FloatVarValue {
199 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
204 impl fmt::Debug for ty::TraitRef<'tcx> {
205 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
206 fmt::Display::fmt(self, f)
210 impl fmt::Debug for Ty<'tcx> {
211 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
212 fmt::Display::fmt(self, f)
216 impl fmt::Debug for ty::ParamTy {
217 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
218 write!(f, "{}/#{}", self.name, self.index)
222 impl fmt::Debug for ty::ParamConst {
223 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
224 write!(f, "{}/#{}", self.name, self.index)
228 impl fmt::Debug for ty::TraitPredicate<'tcx> {
229 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
230 write!(f, "TraitPredicate({:?})", self.trait_ref)
234 impl fmt::Debug for ty::ProjectionPredicate<'tcx> {
235 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
236 write!(f, "ProjectionPredicate({:?}, {:?})", self.projection_ty, self.ty)
240 impl fmt::Debug for ty::Predicate<'tcx> {
241 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
243 ty::Predicate::Trait(ref a) => a.fmt(f),
244 ty::Predicate::Subtype(ref pair) => pair.fmt(f),
245 ty::Predicate::RegionOutlives(ref pair) => pair.fmt(f),
246 ty::Predicate::TypeOutlives(ref pair) => pair.fmt(f),
247 ty::Predicate::Projection(ref pair) => pair.fmt(f),
248 ty::Predicate::WellFormed(ty) => write!(f, "WellFormed({:?})", ty),
249 ty::Predicate::ObjectSafe(trait_def_id) => write!(f, "ObjectSafe({:?})", trait_def_id),
250 ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind) => {
251 write!(f, "ClosureKind({:?}, {:?}, {:?})", closure_def_id, closure_substs, kind)
253 ty::Predicate::ConstEvaluatable(def_id, substs) => {
254 write!(f, "ConstEvaluatable({:?}, {:?})", def_id, substs)
260 ///////////////////////////////////////////////////////////////////////////
263 // For things that don't carry any arena-allocated data (and are
264 // copy...), just add them to this list.
266 CloneTypeFoldableAndLiftImpls! {
270 crate::ty::layout::VariantIdx,
273 crate::middle::region::Scope,
274 ::syntax::ast::FloatTy,
275 ::syntax::ast::NodeId,
276 ::rustc_span::symbol::Symbol,
277 ::rustc_hir::def::Res,
278 ::rustc_hir::def_id::DefId,
279 ::rustc_hir::InlineAsmInner,
280 ::rustc_hir::MatchSource,
281 ::rustc_hir::Mutability,
282 ::rustc_hir::Unsafety,
283 ::rustc_target::spec::abi::Abi,
285 crate::mir::Promoted,
286 crate::traits::Reveal,
287 crate::ty::adjustment::AutoBorrowMutability,
289 // Including `BoundRegion` is a *bit* dubious, but direct
290 // references to bound region appear in `ty::Error`, and aren't
291 // really meant to be folded. In general, we can only fold a fully
293 crate::ty::BoundRegion,
294 crate::ty::Placeholder<crate::ty::BoundRegion>,
295 crate::ty::ClosureKind,
296 crate::ty::FreeRegion,
298 crate::ty::IntVarValue,
299 crate::ty::ParamConst,
301 crate::ty::adjustment::PointerCast,
302 crate::ty::RegionVid,
303 crate::ty::UniverseIndex,
308 ///////////////////////////////////////////////////////////////////////////
309 // Lift implementations
311 // FIXME(eddyb) replace all the uses of `Option::map` with `?`.
312 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>> Lift<'tcx> for (A, B) {
313 type Lifted = (A::Lifted, B::Lifted);
314 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
315 tcx.lift(&self.0).and_then(|a| tcx.lift(&self.1).map(|b| (a, b)))
319 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>, C: Lift<'tcx>> Lift<'tcx> for (A, B, C) {
320 type Lifted = (A::Lifted, B::Lifted, C::Lifted);
321 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
323 .and_then(|a| tcx.lift(&self.1).and_then(|b| tcx.lift(&self.2).map(|c| (a, b, c))))
327 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Option<T> {
328 type Lifted = Option<T::Lifted>;
329 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
331 Some(ref x) => tcx.lift(x).map(Some),
337 impl<'tcx, T: Lift<'tcx>, E: Lift<'tcx>> Lift<'tcx> for Result<T, E> {
338 type Lifted = Result<T::Lifted, E::Lifted>;
339 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
341 Ok(ref x) => tcx.lift(x).map(Ok),
342 Err(ref e) => tcx.lift(e).map(Err),
347 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Box<T> {
348 type Lifted = Box<T::Lifted>;
349 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
350 tcx.lift(&**self).map(Box::new)
354 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Rc<T> {
355 type Lifted = Rc<T::Lifted>;
356 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
357 tcx.lift(&**self).map(Rc::new)
361 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Arc<T> {
362 type Lifted = Arc<T::Lifted>;
363 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
364 tcx.lift(&**self).map(Arc::new)
368 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for [T] {
369 type Lifted = Vec<T::Lifted>;
370 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
371 // type annotation needed to inform `projection_must_outlive`
372 let mut result: Vec<<T as Lift<'tcx>>::Lifted> = Vec::with_capacity(self.len());
374 if let Some(value) = tcx.lift(x) {
384 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Vec<T> {
385 type Lifted = Vec<T::Lifted>;
386 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
391 impl<'tcx, I: Idx, T: Lift<'tcx>> Lift<'tcx> for IndexVec<I, T> {
392 type Lifted = IndexVec<I, T::Lifted>;
393 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
394 self.iter().map(|e| tcx.lift(e)).collect()
398 impl<'a, 'tcx> Lift<'tcx> for ty::TraitRef<'a> {
399 type Lifted = ty::TraitRef<'tcx>;
400 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
401 tcx.lift(&self.substs).map(|substs| ty::TraitRef { def_id: self.def_id, substs })
405 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialTraitRef<'a> {
406 type Lifted = ty::ExistentialTraitRef<'tcx>;
407 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
408 tcx.lift(&self.substs).map(|substs| ty::ExistentialTraitRef { def_id: self.def_id, substs })
412 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialPredicate<'a> {
413 type Lifted = ty::ExistentialPredicate<'tcx>;
414 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
416 ty::ExistentialPredicate::Trait(x) => tcx.lift(x).map(ty::ExistentialPredicate::Trait),
417 ty::ExistentialPredicate::Projection(x) => {
418 tcx.lift(x).map(ty::ExistentialPredicate::Projection)
420 ty::ExistentialPredicate::AutoTrait(def_id) => {
421 Some(ty::ExistentialPredicate::AutoTrait(*def_id))
427 impl<'a, 'tcx> Lift<'tcx> for ty::TraitPredicate<'a> {
428 type Lifted = ty::TraitPredicate<'tcx>;
429 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<ty::TraitPredicate<'tcx>> {
430 tcx.lift(&self.trait_ref).map(|trait_ref| ty::TraitPredicate { trait_ref })
434 impl<'a, 'tcx> Lift<'tcx> for ty::SubtypePredicate<'a> {
435 type Lifted = ty::SubtypePredicate<'tcx>;
436 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<ty::SubtypePredicate<'tcx>> {
437 tcx.lift(&(self.a, self.b)).map(|(a, b)| ty::SubtypePredicate {
438 a_is_expected: self.a_is_expected,
445 impl<'tcx, A: Copy + Lift<'tcx>, B: Copy + Lift<'tcx>> Lift<'tcx> for ty::OutlivesPredicate<A, B> {
446 type Lifted = ty::OutlivesPredicate<A::Lifted, B::Lifted>;
447 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
448 tcx.lift(&(self.0, self.1)).map(|(a, b)| ty::OutlivesPredicate(a, b))
452 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionTy<'a> {
453 type Lifted = ty::ProjectionTy<'tcx>;
454 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<ty::ProjectionTy<'tcx>> {
455 tcx.lift(&self.substs)
456 .map(|substs| ty::ProjectionTy { item_def_id: self.item_def_id, substs })
460 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionPredicate<'a> {
461 type Lifted = ty::ProjectionPredicate<'tcx>;
462 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<ty::ProjectionPredicate<'tcx>> {
463 tcx.lift(&(self.projection_ty, self.ty))
464 .map(|(projection_ty, ty)| ty::ProjectionPredicate { projection_ty, ty })
468 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialProjection<'a> {
469 type Lifted = ty::ExistentialProjection<'tcx>;
470 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
471 tcx.lift(&self.substs).map(|substs| ty::ExistentialProjection {
473 ty: tcx.lift(&self.ty).expect("type must lift when substs do"),
474 item_def_id: self.item_def_id,
479 impl<'a, 'tcx> Lift<'tcx> for ty::Predicate<'a> {
480 type Lifted = ty::Predicate<'tcx>;
481 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
483 ty::Predicate::Trait(ref binder) => tcx.lift(binder).map(ty::Predicate::Trait),
484 ty::Predicate::Subtype(ref binder) => tcx.lift(binder).map(ty::Predicate::Subtype),
485 ty::Predicate::RegionOutlives(ref binder) => {
486 tcx.lift(binder).map(ty::Predicate::RegionOutlives)
488 ty::Predicate::TypeOutlives(ref binder) => {
489 tcx.lift(binder).map(ty::Predicate::TypeOutlives)
491 ty::Predicate::Projection(ref binder) => {
492 tcx.lift(binder).map(ty::Predicate::Projection)
494 ty::Predicate::WellFormed(ty) => tcx.lift(&ty).map(ty::Predicate::WellFormed),
495 ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind) => {
496 tcx.lift(&closure_substs).map(|closure_substs| {
497 ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind)
500 ty::Predicate::ObjectSafe(trait_def_id) => {
501 Some(ty::Predicate::ObjectSafe(trait_def_id))
503 ty::Predicate::ConstEvaluatable(def_id, substs) => {
504 tcx.lift(&substs).map(|substs| ty::Predicate::ConstEvaluatable(def_id, substs))
510 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::Binder<T> {
511 type Lifted = ty::Binder<T::Lifted>;
512 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
513 tcx.lift(self.skip_binder()).map(ty::Binder::bind)
517 impl<'a, 'tcx> Lift<'tcx> for ty::ParamEnv<'a> {
518 type Lifted = ty::ParamEnv<'tcx>;
519 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
520 tcx.lift(&self.caller_bounds).map(|caller_bounds| ty::ParamEnv {
528 impl<'a, 'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::ParamEnvAnd<'a, T> {
529 type Lifted = ty::ParamEnvAnd<'tcx, T::Lifted>;
530 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
531 tcx.lift(&self.param_env).and_then(|param_env| {
532 tcx.lift(&self.value).map(|value| ty::ParamEnvAnd { param_env, value })
537 impl<'a, 'tcx> Lift<'tcx> for ty::ClosureSubsts<'a> {
538 type Lifted = ty::ClosureSubsts<'tcx>;
539 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
540 tcx.lift(&self.substs).map(|substs| ty::ClosureSubsts { substs })
544 impl<'a, 'tcx> Lift<'tcx> for ty::GeneratorSubsts<'a> {
545 type Lifted = ty::GeneratorSubsts<'tcx>;
546 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
547 tcx.lift(&self.substs).map(|substs| ty::GeneratorSubsts { substs })
551 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjustment<'a> {
552 type Lifted = ty::adjustment::Adjustment<'tcx>;
553 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
554 tcx.lift(&self.kind).and_then(|kind| {
555 tcx.lift(&self.target).map(|target| ty::adjustment::Adjustment { kind, target })
560 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjust<'a> {
561 type Lifted = ty::adjustment::Adjust<'tcx>;
562 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
564 ty::adjustment::Adjust::NeverToAny => Some(ty::adjustment::Adjust::NeverToAny),
565 ty::adjustment::Adjust::Pointer(ptr) => Some(ty::adjustment::Adjust::Pointer(ptr)),
566 ty::adjustment::Adjust::Deref(ref overloaded) => {
567 tcx.lift(overloaded).map(ty::adjustment::Adjust::Deref)
569 ty::adjustment::Adjust::Borrow(ref autoref) => {
570 tcx.lift(autoref).map(ty::adjustment::Adjust::Borrow)
576 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::OverloadedDeref<'a> {
577 type Lifted = ty::adjustment::OverloadedDeref<'tcx>;
578 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
579 tcx.lift(&self.region)
580 .map(|region| ty::adjustment::OverloadedDeref { region, mutbl: self.mutbl })
584 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::AutoBorrow<'a> {
585 type Lifted = ty::adjustment::AutoBorrow<'tcx>;
586 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
588 ty::adjustment::AutoBorrow::Ref(r, m) => {
589 tcx.lift(&r).map(|r| ty::adjustment::AutoBorrow::Ref(r, m))
591 ty::adjustment::AutoBorrow::RawPtr(m) => Some(ty::adjustment::AutoBorrow::RawPtr(m)),
596 impl<'a, 'tcx> Lift<'tcx> for ty::GenSig<'a> {
597 type Lifted = ty::GenSig<'tcx>;
598 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
599 tcx.lift(&(self.yield_ty, self.return_ty))
600 .map(|(yield_ty, return_ty)| ty::GenSig { yield_ty, return_ty })
604 impl<'a, 'tcx> Lift<'tcx> for ty::FnSig<'a> {
605 type Lifted = ty::FnSig<'tcx>;
606 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
607 tcx.lift(&self.inputs_and_output).map(|x| ty::FnSig {
608 inputs_and_output: x,
609 c_variadic: self.c_variadic,
610 unsafety: self.unsafety,
616 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::error::ExpectedFound<T> {
617 type Lifted = ty::error::ExpectedFound<T::Lifted>;
618 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
619 tcx.lift(&self.expected).and_then(|expected| {
620 tcx.lift(&self.found).map(|found| ty::error::ExpectedFound { expected, found })
625 impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> {
626 type Lifted = ty::error::TypeError<'tcx>;
627 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
628 use crate::ty::error::TypeError::*;
631 Mismatch => Mismatch,
632 UnsafetyMismatch(x) => UnsafetyMismatch(x),
633 AbiMismatch(x) => AbiMismatch(x),
634 Mutability => Mutability,
635 TupleSize(x) => TupleSize(x),
636 FixedArraySize(x) => FixedArraySize(x),
637 ArgCount => ArgCount,
638 RegionsDoesNotOutlive(a, b) => {
639 return tcx.lift(&(a, b)).map(|(a, b)| RegionsDoesNotOutlive(a, b));
641 RegionsInsufficientlyPolymorphic(a, b) => {
642 return tcx.lift(&b).map(|b| RegionsInsufficientlyPolymorphic(a, b));
644 RegionsOverlyPolymorphic(a, b) => {
645 return tcx.lift(&b).map(|b| RegionsOverlyPolymorphic(a, b));
647 RegionsPlaceholderMismatch => RegionsPlaceholderMismatch,
648 IntMismatch(x) => IntMismatch(x),
649 FloatMismatch(x) => FloatMismatch(x),
650 Traits(x) => Traits(x),
651 VariadicMismatch(x) => VariadicMismatch(x),
652 CyclicTy(t) => return tcx.lift(&t).map(|t| CyclicTy(t)),
653 ProjectionMismatched(x) => ProjectionMismatched(x),
654 ProjectionBoundsLength(x) => ProjectionBoundsLength(x),
655 Sorts(ref x) => return tcx.lift(x).map(Sorts),
656 ExistentialMismatch(ref x) => return tcx.lift(x).map(ExistentialMismatch),
657 ConstMismatch(ref x) => return tcx.lift(x).map(ConstMismatch),
658 IntrinsicCast => IntrinsicCast,
659 ObjectUnsafeCoercion(ref x) => return tcx.lift(x).map(ObjectUnsafeCoercion),
664 impl<'a, 'tcx> Lift<'tcx> for ty::InstanceDef<'a> {
665 type Lifted = ty::InstanceDef<'tcx>;
666 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
668 ty::InstanceDef::Item(def_id) => Some(ty::InstanceDef::Item(def_id)),
669 ty::InstanceDef::VtableShim(def_id) => Some(ty::InstanceDef::VtableShim(def_id)),
670 ty::InstanceDef::ReifyShim(def_id) => Some(ty::InstanceDef::ReifyShim(def_id)),
671 ty::InstanceDef::Intrinsic(def_id) => Some(ty::InstanceDef::Intrinsic(def_id)),
672 ty::InstanceDef::FnPtrShim(def_id, ref ty) => {
673 Some(ty::InstanceDef::FnPtrShim(def_id, tcx.lift(ty)?))
675 ty::InstanceDef::Virtual(def_id, n) => Some(ty::InstanceDef::Virtual(def_id, n)),
676 ty::InstanceDef::ClosureOnceShim { call_once } => {
677 Some(ty::InstanceDef::ClosureOnceShim { call_once })
679 ty::InstanceDef::DropGlue(def_id, ref ty) => {
680 Some(ty::InstanceDef::DropGlue(def_id, tcx.lift(ty)?))
682 ty::InstanceDef::CloneShim(def_id, ref ty) => {
683 Some(ty::InstanceDef::CloneShim(def_id, tcx.lift(ty)?))
689 ///////////////////////////////////////////////////////////////////////////
690 // TypeFoldable implementations.
692 // Ideally, each type should invoke `folder.fold_foo(self)` and
693 // nothing else. In some cases, though, we haven't gotten around to
694 // adding methods on the `folder` yet, and thus the folding is
695 // hard-coded here. This is less-flexible, because folders cannot
696 // override the behavior, but there are a lot of random types and one
697 // can easily refactor the folding into the TypeFolder trait as
700 /// AdtDefs are basically the same as a DefId.
701 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::AdtDef {
702 fn super_fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> Self {
706 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
711 impl<'tcx, T: TypeFoldable<'tcx>, U: TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
712 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> (T, U) {
713 (self.0.fold_with(folder), self.1.fold_with(folder))
716 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
717 self.0.visit_with(visitor) || self.1.visit_with(visitor)
721 EnumTypeFoldableImpl! {
722 impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
725 } where T: TypeFoldable<'tcx>
728 EnumTypeFoldableImpl! {
729 impl<'tcx, T, E> TypeFoldable<'tcx> for Result<T, E> {
732 } where T: TypeFoldable<'tcx>, E: TypeFoldable<'tcx>,
735 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
736 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
737 Rc::new((**self).fold_with(folder))
740 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
741 (**self).visit_with(visitor)
745 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Arc<T> {
746 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
747 Arc::new((**self).fold_with(folder))
750 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
751 (**self).visit_with(visitor)
755 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
756 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
757 let content: T = (**self).fold_with(folder);
761 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
762 (**self).visit_with(visitor)
766 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
767 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
768 self.iter().map(|t| t.fold_with(folder)).collect()
771 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
772 self.iter().any(|t| t.visit_with(visitor))
776 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<[T]> {
777 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
778 self.iter().map(|t| t.fold_with(folder)).collect::<Vec<_>>().into_boxed_slice()
781 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
782 self.iter().any(|t| t.visit_with(visitor))
786 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<T> {
787 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
788 self.map_bound_ref(|ty| ty.fold_with(folder))
791 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
792 folder.fold_binder(self)
795 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
796 self.skip_binder().visit_with(visitor)
799 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
800 visitor.visit_binder(self)
804 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::ExistentialPredicate<'tcx>> {
805 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
806 fold_list(*self, folder, |tcx, v| tcx.intern_existential_predicates(v))
809 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
810 self.iter().any(|p| p.visit_with(visitor))
814 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<Ty<'tcx>> {
815 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
816 fold_list(*self, folder, |tcx, v| tcx.intern_type_list(v))
819 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
820 self.iter().any(|t| t.visit_with(visitor))
824 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind> {
825 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
826 fold_list(*self, folder, |tcx, v| tcx.intern_projs(v))
829 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
830 self.iter().any(|t| t.visit_with(visitor))
834 impl<'tcx> TypeFoldable<'tcx> for ty::instance::Instance<'tcx> {
835 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
836 use crate::ty::InstanceDef::*;
838 substs: self.substs.fold_with(folder),
839 def: match self.def {
840 Item(did) => Item(did.fold_with(folder)),
841 VtableShim(did) => VtableShim(did.fold_with(folder)),
842 ReifyShim(did) => ReifyShim(did.fold_with(folder)),
843 Intrinsic(did) => Intrinsic(did.fold_with(folder)),
844 FnPtrShim(did, ty) => FnPtrShim(did.fold_with(folder), ty.fold_with(folder)),
845 Virtual(did, i) => Virtual(did.fold_with(folder), i),
846 ClosureOnceShim { call_once } => {
847 ClosureOnceShim { call_once: call_once.fold_with(folder) }
849 DropGlue(did, ty) => DropGlue(did.fold_with(folder), ty.fold_with(folder)),
850 CloneShim(did, ty) => CloneShim(did.fold_with(folder), ty.fold_with(folder)),
855 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
856 use crate::ty::InstanceDef::*;
857 self.substs.visit_with(visitor)
859 Item(did) | VtableShim(did) | ReifyShim(did) | Intrinsic(did) | Virtual(did, _) => {
860 did.visit_with(visitor)
862 FnPtrShim(did, ty) | CloneShim(did, ty) => {
863 did.visit_with(visitor) || ty.visit_with(visitor)
865 DropGlue(did, ty) => did.visit_with(visitor) || ty.visit_with(visitor),
866 ClosureOnceShim { call_once } => call_once.visit_with(visitor),
871 impl<'tcx> TypeFoldable<'tcx> for interpret::GlobalId<'tcx> {
872 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
873 Self { instance: self.instance.fold_with(folder), promoted: self.promoted }
876 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
877 self.instance.visit_with(visitor)
881 impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
882 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
883 let kind = match self.kind {
884 ty::RawPtr(tm) => ty::RawPtr(tm.fold_with(folder)),
885 ty::Array(typ, sz) => ty::Array(typ.fold_with(folder), sz.fold_with(folder)),
886 ty::Slice(typ) => ty::Slice(typ.fold_with(folder)),
887 ty::Adt(tid, substs) => ty::Adt(tid, substs.fold_with(folder)),
888 ty::Dynamic(ref trait_ty, ref region) => {
889 ty::Dynamic(trait_ty.fold_with(folder), region.fold_with(folder))
891 ty::Tuple(ts) => ty::Tuple(ts.fold_with(folder)),
892 ty::FnDef(def_id, substs) => ty::FnDef(def_id, substs.fold_with(folder)),
893 ty::FnPtr(f) => ty::FnPtr(f.fold_with(folder)),
894 ty::Ref(ref r, ty, mutbl) => ty::Ref(r.fold_with(folder), ty.fold_with(folder), mutbl),
895 ty::Generator(did, substs, movability) => {
896 ty::Generator(did, substs.fold_with(folder), movability)
898 ty::GeneratorWitness(types) => ty::GeneratorWitness(types.fold_with(folder)),
899 ty::Closure(did, substs) => ty::Closure(did, substs.fold_with(folder)),
900 ty::Projection(ref data) => ty::Projection(data.fold_with(folder)),
901 ty::UnnormalizedProjection(ref data) => {
902 ty::UnnormalizedProjection(data.fold_with(folder))
904 ty::Opaque(did, substs) => ty::Opaque(did, substs.fold_with(folder)),
916 | ty::Placeholder(..)
918 | ty::Foreign(..) => return self,
921 if self.kind == kind { self } else { folder.tcx().mk_ty(kind) }
924 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
925 folder.fold_ty(*self)
928 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
930 ty::RawPtr(ref tm) => tm.visit_with(visitor),
931 ty::Array(typ, sz) => typ.visit_with(visitor) || sz.visit_with(visitor),
932 ty::Slice(typ) => typ.visit_with(visitor),
933 ty::Adt(_, substs) => substs.visit_with(visitor),
934 ty::Dynamic(ref trait_ty, ref reg) => {
935 trait_ty.visit_with(visitor) || reg.visit_with(visitor)
937 ty::Tuple(ts) => ts.visit_with(visitor),
938 ty::FnDef(_, substs) => substs.visit_with(visitor),
939 ty::FnPtr(ref f) => f.visit_with(visitor),
940 ty::Ref(r, ty, _) => r.visit_with(visitor) || ty.visit_with(visitor),
941 ty::Generator(_did, ref substs, _) => substs.visit_with(visitor),
942 ty::GeneratorWitness(ref types) => types.visit_with(visitor),
943 ty::Closure(_did, ref substs) => substs.visit_with(visitor),
944 ty::Projection(ref data) | ty::UnnormalizedProjection(ref data) => {
945 data.visit_with(visitor)
947 ty::Opaque(_, ref substs) => substs.visit_with(visitor),
958 | ty::Placeholder(..)
961 | ty::Foreign(..) => false,
965 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
966 visitor.visit_ty(self)
970 impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> {
971 fn super_fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> Self {
975 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
976 folder.fold_region(*self)
979 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
983 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
984 visitor.visit_region(*self)
988 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
989 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
990 fold_list(*self, folder, |tcx, v| tcx.intern_predicates(v))
993 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
994 self.iter().any(|p| p.visit_with(visitor))
998 impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
999 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1000 self.iter().map(|x| x.fold_with(folder)).collect()
1003 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1004 self.iter().any(|t| t.visit_with(visitor))
1008 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Const<'tcx> {
1009 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1010 let ty = self.ty.fold_with(folder);
1011 let val = self.val.fold_with(folder);
1012 folder.tcx().mk_const(ty::Const { ty, val })
1015 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1016 folder.fold_const(*self)
1019 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1020 self.ty.visit_with(visitor) || self.val.visit_with(visitor)
1023 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1024 visitor.visit_const(self)
1028 impl<'tcx> TypeFoldable<'tcx> for ty::ConstKind<'tcx> {
1029 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1031 ty::ConstKind::Infer(ic) => ty::ConstKind::Infer(ic.fold_with(folder)),
1032 ty::ConstKind::Param(p) => ty::ConstKind::Param(p.fold_with(folder)),
1033 ty::ConstKind::Unevaluated(did, substs) => {
1034 ty::ConstKind::Unevaluated(did, substs.fold_with(folder))
1036 ty::ConstKind::Value(_) | ty::ConstKind::Bound(..) | ty::ConstKind::Placeholder(..) => {
1042 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1044 ty::ConstKind::Infer(ic) => ic.visit_with(visitor),
1045 ty::ConstKind::Param(p) => p.visit_with(visitor),
1046 ty::ConstKind::Unevaluated(_, substs) => substs.visit_with(visitor),
1047 ty::ConstKind::Value(_) | ty::ConstKind::Bound(..) | ty::ConstKind::Placeholder(_) => {
1054 impl<'tcx> TypeFoldable<'tcx> for InferConst<'tcx> {
1055 fn super_fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> Self {
1059 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
1064 fn fold_list<'tcx, F, T>(
1065 list: &'tcx ty::List<T>,
1067 intern: impl FnOnce(TyCtxt<'tcx>, &[T]) -> &'tcx ty::List<T>,
1068 ) -> &'tcx ty::List<T>
1070 F: TypeFolder<'tcx>,
1071 T: TypeFoldable<'tcx> + PartialEq + Copy,
1073 let mut iter = list.iter();
1074 // Look for the first element that changed
1075 if let Some((i, new_t)) = iter.by_ref().enumerate().find_map(|(i, t)| {
1076 let new_t = t.fold_with(folder);
1077 if new_t == *t { None } else { Some((i, new_t)) }
1079 // An element changed, prepare to intern the resulting list
1080 let mut new_list = SmallVec::<[_; 8]>::with_capacity(list.len());
1081 new_list.extend_from_slice(&list[..i]);
1082 new_list.push(new_t);
1083 new_list.extend(iter.map(|t| t.fold_with(folder)));
1084 intern(folder.tcx(), &new_list)