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;
20 impl fmt::Debug for ty::GenericParamDef {
21 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22 let type_name = match self.kind {
23 ty::GenericParamDefKind::Lifetime => "Lifetime",
24 ty::GenericParamDefKind::Type { .. } => "Type",
25 ty::GenericParamDefKind::Const => "Const",
27 write!(f, "{}({}, {:?}, {})", type_name, self.name, self.def_id, self.index)
31 impl fmt::Debug for ty::TraitDef {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 FmtPrinter::new(tcx, f, Namespace::TypeNS).print_def_path(self.def_id, &[])?;
40 impl fmt::Debug for ty::AdtDef {
41 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43 FmtPrinter::new(tcx, f, Namespace::TypeNS).print_def_path(self.did, &[])?;
49 impl fmt::Debug for ty::UpvarId {
50 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51 let name = ty::tls::with(|tcx| tcx.hir().name(self.var_path.hir_id));
52 write!(f, "UpvarId({:?};`{}`;{:?})", self.var_path.hir_id, name, self.closure_expr_id)
56 impl fmt::Debug for ty::UpvarBorrow<'tcx> {
57 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58 write!(f, "UpvarBorrow({:?}, {:?})", self.kind, self.region)
62 impl fmt::Debug for ty::ExistentialTraitRef<'tcx> {
63 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64 fmt::Display::fmt(self, f)
68 impl fmt::Debug for ty::adjustment::Adjustment<'tcx> {
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70 write!(f, "{:?} -> {}", self.kind, self.target)
74 impl fmt::Debug for ty::BoundRegion {
75 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77 ty::BrAnon(n) => write!(f, "BrAnon({:?})", n),
78 ty::BrNamed(did, name) => {
79 if did.index == CRATE_DEF_INDEX {
80 write!(f, "BrNamed({})", name)
82 write!(f, "BrNamed({:?}, {})", did, name)
85 ty::BrEnv => write!(f, "BrEnv"),
90 impl fmt::Debug for ty::RegionKind {
91 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93 ty::ReEarlyBound(ref data) => write!(f, "ReEarlyBound({}, {})", data.index, data.name),
95 ty::ReClosureBound(ref vid) => write!(f, "ReClosureBound({:?})", vid),
97 ty::ReLateBound(binder_id, ref bound_region) => {
98 write!(f, "ReLateBound({:?}, {:?})", binder_id, bound_region)
101 ty::ReFree(ref fr) => fr.fmt(f),
103 ty::ReScope(id) => write!(f, "ReScope({:?})", id),
105 ty::ReStatic => write!(f, "ReStatic"),
107 ty::ReVar(ref vid) => vid.fmt(f),
109 ty::RePlaceholder(placeholder) => write!(f, "RePlaceholder({:?})", placeholder),
111 ty::ReEmpty => write!(f, "ReEmpty"),
113 ty::ReErased => write!(f, "ReErased"),
118 impl fmt::Debug for ty::FreeRegion {
119 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
120 write!(f, "ReFree({:?}, {:?})", self.scope, self.bound_region)
124 impl fmt::Debug for ty::Variance {
125 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
126 f.write_str(match *self {
127 ty::Covariant => "+",
128 ty::Contravariant => "-",
129 ty::Invariant => "o",
130 ty::Bivariant => "*",
135 impl fmt::Debug for ty::FnSig<'tcx> {
136 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
137 write!(f, "({:?}; c_variadic: {})->{:?}", self.inputs(), self.c_variadic, self.output())
141 impl fmt::Debug for ty::TyVid {
142 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
143 write!(f, "_#{}t", self.index)
147 impl<'tcx> fmt::Debug for ty::ConstVid<'tcx> {
148 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
149 write!(f, "_#{}c", self.index)
153 impl fmt::Debug for ty::IntVid {
154 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
155 write!(f, "_#{}i", self.index)
159 impl fmt::Debug for ty::FloatVid {
160 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
161 write!(f, "_#{}f", self.index)
165 impl fmt::Debug for ty::RegionVid {
166 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
167 write!(f, "'_#{}r", self.index())
171 impl fmt::Debug for ty::InferTy {
172 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
174 ty::TyVar(ref v) => v.fmt(f),
175 ty::IntVar(ref v) => v.fmt(f),
176 ty::FloatVar(ref v) => v.fmt(f),
177 ty::FreshTy(v) => write!(f, "FreshTy({:?})", v),
178 ty::FreshIntTy(v) => write!(f, "FreshIntTy({:?})", v),
179 ty::FreshFloatTy(v) => write!(f, "FreshFloatTy({:?})", v),
184 impl fmt::Debug for ty::IntVarValue {
185 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
187 ty::IntType(ref v) => v.fmt(f),
188 ty::UintType(ref v) => v.fmt(f),
193 impl fmt::Debug for ty::FloatVarValue {
194 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
199 impl fmt::Debug for ty::TraitRef<'tcx> {
200 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
201 fmt::Display::fmt(self, f)
205 impl fmt::Debug for Ty<'tcx> {
206 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
207 fmt::Display::fmt(self, f)
211 impl fmt::Debug for ty::ParamTy {
212 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
213 write!(f, "{}/#{}", self.name, self.index)
217 impl fmt::Debug for ty::ParamConst {
218 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
219 write!(f, "{}/#{}", self.name, self.index)
223 impl fmt::Debug for ty::TraitPredicate<'tcx> {
224 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
225 write!(f, "TraitPredicate({:?})", self.trait_ref)
229 impl fmt::Debug for ty::ProjectionPredicate<'tcx> {
230 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
231 write!(f, "ProjectionPredicate({:?}, {:?})", self.projection_ty, self.ty)
235 impl fmt::Debug for ty::Predicate<'tcx> {
236 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
238 ty::Predicate::Trait(ref a, constness) => {
239 if let ast::Constness::Const = constness {
240 write!(f, "const ")?;
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, constness) => {
484 tcx.lift(binder).map(|binder| ty::Predicate::Trait(binder, constness))
486 ty::Predicate::Subtype(ref binder) => tcx.lift(binder).map(ty::Predicate::Subtype),
487 ty::Predicate::RegionOutlives(ref binder) => {
488 tcx.lift(binder).map(ty::Predicate::RegionOutlives)
490 ty::Predicate::TypeOutlives(ref binder) => {
491 tcx.lift(binder).map(ty::Predicate::TypeOutlives)
493 ty::Predicate::Projection(ref binder) => {
494 tcx.lift(binder).map(ty::Predicate::Projection)
496 ty::Predicate::WellFormed(ty) => tcx.lift(&ty).map(ty::Predicate::WellFormed),
497 ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind) => {
498 tcx.lift(&closure_substs).map(|closure_substs| {
499 ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind)
502 ty::Predicate::ObjectSafe(trait_def_id) => {
503 Some(ty::Predicate::ObjectSafe(trait_def_id))
505 ty::Predicate::ConstEvaluatable(def_id, substs) => {
506 tcx.lift(&substs).map(|substs| ty::Predicate::ConstEvaluatable(def_id, substs))
512 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::Binder<T> {
513 type Lifted = ty::Binder<T::Lifted>;
514 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
515 tcx.lift(self.skip_binder()).map(ty::Binder::bind)
519 impl<'a, 'tcx> Lift<'tcx> for ty::ParamEnv<'a> {
520 type Lifted = ty::ParamEnv<'tcx>;
521 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
522 tcx.lift(&self.caller_bounds).map(|caller_bounds| ty::ParamEnv {
530 impl<'a, 'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::ParamEnvAnd<'a, T> {
531 type Lifted = ty::ParamEnvAnd<'tcx, T::Lifted>;
532 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
533 tcx.lift(&self.param_env).and_then(|param_env| {
534 tcx.lift(&self.value).map(|value| ty::ParamEnvAnd { param_env, value })
539 impl<'a, 'tcx> Lift<'tcx> for ty::ClosureSubsts<'a> {
540 type Lifted = ty::ClosureSubsts<'tcx>;
541 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
542 tcx.lift(&self.substs).map(|substs| ty::ClosureSubsts { substs })
546 impl<'a, 'tcx> Lift<'tcx> for ty::GeneratorSubsts<'a> {
547 type Lifted = ty::GeneratorSubsts<'tcx>;
548 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
549 tcx.lift(&self.substs).map(|substs| ty::GeneratorSubsts { substs })
553 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjustment<'a> {
554 type Lifted = ty::adjustment::Adjustment<'tcx>;
555 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
556 tcx.lift(&self.kind).and_then(|kind| {
557 tcx.lift(&self.target).map(|target| ty::adjustment::Adjustment { kind, target })
562 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjust<'a> {
563 type Lifted = ty::adjustment::Adjust<'tcx>;
564 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
566 ty::adjustment::Adjust::NeverToAny => Some(ty::adjustment::Adjust::NeverToAny),
567 ty::adjustment::Adjust::Pointer(ptr) => Some(ty::adjustment::Adjust::Pointer(ptr)),
568 ty::adjustment::Adjust::Deref(ref overloaded) => {
569 tcx.lift(overloaded).map(ty::adjustment::Adjust::Deref)
571 ty::adjustment::Adjust::Borrow(ref autoref) => {
572 tcx.lift(autoref).map(ty::adjustment::Adjust::Borrow)
578 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::OverloadedDeref<'a> {
579 type Lifted = ty::adjustment::OverloadedDeref<'tcx>;
580 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
581 tcx.lift(&self.region)
582 .map(|region| ty::adjustment::OverloadedDeref { region, mutbl: self.mutbl })
586 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::AutoBorrow<'a> {
587 type Lifted = ty::adjustment::AutoBorrow<'tcx>;
588 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
590 ty::adjustment::AutoBorrow::Ref(r, m) => {
591 tcx.lift(&r).map(|r| ty::adjustment::AutoBorrow::Ref(r, m))
593 ty::adjustment::AutoBorrow::RawPtr(m) => Some(ty::adjustment::AutoBorrow::RawPtr(m)),
598 impl<'a, 'tcx> Lift<'tcx> for ty::GenSig<'a> {
599 type Lifted = ty::GenSig<'tcx>;
600 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
601 tcx.lift(&(self.yield_ty, self.return_ty))
602 .map(|(yield_ty, return_ty)| ty::GenSig { yield_ty, return_ty })
606 impl<'a, 'tcx> Lift<'tcx> for ty::FnSig<'a> {
607 type Lifted = ty::FnSig<'tcx>;
608 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
609 tcx.lift(&self.inputs_and_output).map(|x| ty::FnSig {
610 inputs_and_output: x,
611 c_variadic: self.c_variadic,
612 unsafety: self.unsafety,
618 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::error::ExpectedFound<T> {
619 type Lifted = ty::error::ExpectedFound<T::Lifted>;
620 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
621 tcx.lift(&self.expected).and_then(|expected| {
622 tcx.lift(&self.found).map(|found| ty::error::ExpectedFound { expected, found })
627 impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> {
628 type Lifted = ty::error::TypeError<'tcx>;
629 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
630 use crate::ty::error::TypeError::*;
633 Mismatch => Mismatch,
634 UnsafetyMismatch(x) => UnsafetyMismatch(x),
635 AbiMismatch(x) => AbiMismatch(x),
636 Mutability => Mutability,
637 TupleSize(x) => TupleSize(x),
638 FixedArraySize(x) => FixedArraySize(x),
639 ArgCount => ArgCount,
640 RegionsDoesNotOutlive(a, b) => {
641 return tcx.lift(&(a, b)).map(|(a, b)| RegionsDoesNotOutlive(a, b));
643 RegionsInsufficientlyPolymorphic(a, b) => {
644 return tcx.lift(&b).map(|b| RegionsInsufficientlyPolymorphic(a, b));
646 RegionsOverlyPolymorphic(a, b) => {
647 return tcx.lift(&b).map(|b| RegionsOverlyPolymorphic(a, b));
649 RegionsPlaceholderMismatch => RegionsPlaceholderMismatch,
650 IntMismatch(x) => IntMismatch(x),
651 FloatMismatch(x) => FloatMismatch(x),
652 Traits(x) => Traits(x),
653 VariadicMismatch(x) => VariadicMismatch(x),
654 CyclicTy(t) => return tcx.lift(&t).map(|t| CyclicTy(t)),
655 ProjectionMismatched(x) => ProjectionMismatched(x),
656 ProjectionBoundsLength(x) => ProjectionBoundsLength(x),
657 Sorts(ref x) => return tcx.lift(x).map(Sorts),
658 ExistentialMismatch(ref x) => return tcx.lift(x).map(ExistentialMismatch),
659 ConstMismatch(ref x) => return tcx.lift(x).map(ConstMismatch),
660 IntrinsicCast => IntrinsicCast,
661 ObjectUnsafeCoercion(ref x) => return tcx.lift(x).map(ObjectUnsafeCoercion),
666 impl<'a, 'tcx> Lift<'tcx> for ty::InstanceDef<'a> {
667 type Lifted = ty::InstanceDef<'tcx>;
668 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
670 ty::InstanceDef::Item(def_id) => Some(ty::InstanceDef::Item(def_id)),
671 ty::InstanceDef::VtableShim(def_id) => Some(ty::InstanceDef::VtableShim(def_id)),
672 ty::InstanceDef::ReifyShim(def_id) => Some(ty::InstanceDef::ReifyShim(def_id)),
673 ty::InstanceDef::Intrinsic(def_id) => Some(ty::InstanceDef::Intrinsic(def_id)),
674 ty::InstanceDef::FnPtrShim(def_id, ref ty) => {
675 Some(ty::InstanceDef::FnPtrShim(def_id, tcx.lift(ty)?))
677 ty::InstanceDef::Virtual(def_id, n) => Some(ty::InstanceDef::Virtual(def_id, n)),
678 ty::InstanceDef::ClosureOnceShim { call_once } => {
679 Some(ty::InstanceDef::ClosureOnceShim { call_once })
681 ty::InstanceDef::DropGlue(def_id, ref ty) => {
682 Some(ty::InstanceDef::DropGlue(def_id, tcx.lift(ty)?))
684 ty::InstanceDef::CloneShim(def_id, ref ty) => {
685 Some(ty::InstanceDef::CloneShim(def_id, tcx.lift(ty)?))
691 ///////////////////////////////////////////////////////////////////////////
692 // TypeFoldable implementations.
694 // Ideally, each type should invoke `folder.fold_foo(self)` and
695 // nothing else. In some cases, though, we haven't gotten around to
696 // adding methods on the `folder` yet, and thus the folding is
697 // hard-coded here. This is less-flexible, because folders cannot
698 // override the behavior, but there are a lot of random types and one
699 // can easily refactor the folding into the TypeFolder trait as
702 /// AdtDefs are basically the same as a DefId.
703 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::AdtDef {
704 fn super_fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> Self {
708 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
713 impl<'tcx, T: TypeFoldable<'tcx>, U: TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
714 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> (T, U) {
715 (self.0.fold_with(folder), self.1.fold_with(folder))
718 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
719 self.0.visit_with(visitor) || self.1.visit_with(visitor)
723 EnumTypeFoldableImpl! {
724 impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
727 } where T: TypeFoldable<'tcx>
730 EnumTypeFoldableImpl! {
731 impl<'tcx, T, E> TypeFoldable<'tcx> for Result<T, E> {
734 } where T: TypeFoldable<'tcx>, E: TypeFoldable<'tcx>,
737 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
738 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
739 Rc::new((**self).fold_with(folder))
742 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
743 (**self).visit_with(visitor)
747 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Arc<T> {
748 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
749 Arc::new((**self).fold_with(folder))
752 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
753 (**self).visit_with(visitor)
757 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
758 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
759 let content: T = (**self).fold_with(folder);
763 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
764 (**self).visit_with(visitor)
768 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
769 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
770 self.iter().map(|t| t.fold_with(folder)).collect()
773 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
774 self.iter().any(|t| t.visit_with(visitor))
778 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<[T]> {
779 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
780 self.iter().map(|t| t.fold_with(folder)).collect::<Vec<_>>().into_boxed_slice()
783 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
784 self.iter().any(|t| t.visit_with(visitor))
788 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<T> {
789 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
790 self.map_bound_ref(|ty| ty.fold_with(folder))
793 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
794 folder.fold_binder(self)
797 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
798 self.skip_binder().visit_with(visitor)
801 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
802 visitor.visit_binder(self)
806 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::ExistentialPredicate<'tcx>> {
807 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
808 let v = self.iter().map(|p| p.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
809 folder.tcx().intern_existential_predicates(&v)
812 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
813 self.iter().any(|p| p.visit_with(visitor))
817 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<Ty<'tcx>> {
818 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
819 let v = self.iter().map(|t| t.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
820 folder.tcx().intern_type_list(&v)
823 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
824 self.iter().any(|t| t.visit_with(visitor))
828 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind> {
829 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
830 let v = self.iter().map(|t| t.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
831 folder.tcx().intern_projs(&v)
834 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
835 self.iter().any(|t| t.visit_with(visitor))
839 impl<'tcx> TypeFoldable<'tcx> for ty::instance::Instance<'tcx> {
840 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
841 use crate::ty::InstanceDef::*;
843 substs: self.substs.fold_with(folder),
844 def: match self.def {
845 Item(did) => Item(did.fold_with(folder)),
846 VtableShim(did) => VtableShim(did.fold_with(folder)),
847 ReifyShim(did) => ReifyShim(did.fold_with(folder)),
848 Intrinsic(did) => Intrinsic(did.fold_with(folder)),
849 FnPtrShim(did, ty) => FnPtrShim(did.fold_with(folder), ty.fold_with(folder)),
850 Virtual(did, i) => Virtual(did.fold_with(folder), i),
851 ClosureOnceShim { call_once } => {
852 ClosureOnceShim { call_once: call_once.fold_with(folder) }
854 DropGlue(did, ty) => DropGlue(did.fold_with(folder), ty.fold_with(folder)),
855 CloneShim(did, ty) => CloneShim(did.fold_with(folder), ty.fold_with(folder)),
860 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
861 use crate::ty::InstanceDef::*;
862 self.substs.visit_with(visitor)
864 Item(did) | VtableShim(did) | ReifyShim(did) | Intrinsic(did) | Virtual(did, _) => {
865 did.visit_with(visitor)
867 FnPtrShim(did, ty) | CloneShim(did, ty) => {
868 did.visit_with(visitor) || ty.visit_with(visitor)
870 DropGlue(did, ty) => did.visit_with(visitor) || ty.visit_with(visitor),
871 ClosureOnceShim { call_once } => call_once.visit_with(visitor),
876 impl<'tcx> TypeFoldable<'tcx> for interpret::GlobalId<'tcx> {
877 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
878 Self { instance: self.instance.fold_with(folder), promoted: self.promoted }
881 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
882 self.instance.visit_with(visitor)
886 impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
887 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
888 let kind = match self.kind {
889 ty::RawPtr(tm) => ty::RawPtr(tm.fold_with(folder)),
890 ty::Array(typ, sz) => ty::Array(typ.fold_with(folder), sz.fold_with(folder)),
891 ty::Slice(typ) => ty::Slice(typ.fold_with(folder)),
892 ty::Adt(tid, substs) => ty::Adt(tid, substs.fold_with(folder)),
893 ty::Dynamic(ref trait_ty, ref region) => {
894 ty::Dynamic(trait_ty.fold_with(folder), region.fold_with(folder))
896 ty::Tuple(ts) => ty::Tuple(ts.fold_with(folder)),
897 ty::FnDef(def_id, substs) => ty::FnDef(def_id, substs.fold_with(folder)),
898 ty::FnPtr(f) => ty::FnPtr(f.fold_with(folder)),
899 ty::Ref(ref r, ty, mutbl) => ty::Ref(r.fold_with(folder), ty.fold_with(folder), mutbl),
900 ty::Generator(did, substs, movability) => {
901 ty::Generator(did, substs.fold_with(folder), movability)
903 ty::GeneratorWitness(types) => ty::GeneratorWitness(types.fold_with(folder)),
904 ty::Closure(did, substs) => ty::Closure(did, substs.fold_with(folder)),
905 ty::Projection(ref data) => ty::Projection(data.fold_with(folder)),
906 ty::UnnormalizedProjection(ref data) => {
907 ty::UnnormalizedProjection(data.fold_with(folder))
909 ty::Opaque(did, substs) => ty::Opaque(did, substs.fold_with(folder)),
921 | ty::Placeholder(..)
923 | ty::Foreign(..) => return self,
926 if self.kind == kind { self } else { folder.tcx().mk_ty(kind) }
929 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
930 folder.fold_ty(*self)
933 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
935 ty::RawPtr(ref tm) => tm.visit_with(visitor),
936 ty::Array(typ, sz) => typ.visit_with(visitor) || sz.visit_with(visitor),
937 ty::Slice(typ) => typ.visit_with(visitor),
938 ty::Adt(_, substs) => substs.visit_with(visitor),
939 ty::Dynamic(ref trait_ty, ref reg) => {
940 trait_ty.visit_with(visitor) || reg.visit_with(visitor)
942 ty::Tuple(ts) => ts.visit_with(visitor),
943 ty::FnDef(_, substs) => substs.visit_with(visitor),
944 ty::FnPtr(ref f) => f.visit_with(visitor),
945 ty::Ref(r, ty, _) => r.visit_with(visitor) || ty.visit_with(visitor),
946 ty::Generator(_did, ref substs, _) => substs.visit_with(visitor),
947 ty::GeneratorWitness(ref types) => types.visit_with(visitor),
948 ty::Closure(_did, ref substs) => substs.visit_with(visitor),
949 ty::Projection(ref data) | ty::UnnormalizedProjection(ref data) => {
950 data.visit_with(visitor)
952 ty::Opaque(_, ref substs) => substs.visit_with(visitor),
963 | ty::Placeholder(..)
966 | ty::Foreign(..) => false,
970 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
971 visitor.visit_ty(self)
975 impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> {
976 fn super_fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> Self {
980 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
981 folder.fold_region(*self)
984 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
988 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
989 visitor.visit_region(*self)
993 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
994 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
995 // This code is hot enough that it's worth specializing for a list of
996 // length 0. (No other length is common enough to be worth singling
1001 // Don't bother interning if nothing changed, which is the common
1003 let v = self.iter().map(|p| p.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
1004 if v[..] == self[..] { self } else { folder.tcx().intern_predicates(&v) }
1008 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1009 self.iter().any(|p| p.visit_with(visitor))
1013 impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
1014 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1015 self.iter().map(|x| x.fold_with(folder)).collect()
1018 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1019 self.iter().any(|t| t.visit_with(visitor))
1023 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Const<'tcx> {
1024 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1025 let ty = self.ty.fold_with(folder);
1026 let val = self.val.fold_with(folder);
1027 folder.tcx().mk_const(ty::Const { ty, val })
1030 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1031 folder.fold_const(*self)
1034 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1035 self.ty.visit_with(visitor) || self.val.visit_with(visitor)
1038 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1039 visitor.visit_const(self)
1043 impl<'tcx> TypeFoldable<'tcx> for ty::ConstKind<'tcx> {
1044 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1046 ty::ConstKind::Infer(ic) => ty::ConstKind::Infer(ic.fold_with(folder)),
1047 ty::ConstKind::Param(p) => ty::ConstKind::Param(p.fold_with(folder)),
1048 ty::ConstKind::Unevaluated(did, substs, promoted) => {
1049 ty::ConstKind::Unevaluated(did, substs.fold_with(folder), promoted)
1051 ty::ConstKind::Value(_) | ty::ConstKind::Bound(..) | ty::ConstKind::Placeholder(..) => {
1057 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1059 ty::ConstKind::Infer(ic) => ic.visit_with(visitor),
1060 ty::ConstKind::Param(p) => p.visit_with(visitor),
1061 ty::ConstKind::Unevaluated(_, substs, _) => substs.visit_with(visitor),
1062 ty::ConstKind::Value(_) | ty::ConstKind::Bound(..) | ty::ConstKind::Placeholder(_) => {
1069 impl<'tcx> TypeFoldable<'tcx> for InferConst<'tcx> {
1070 fn super_fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> Self {
1074 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {