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::{with_no_trimmed_paths, FmtPrinter, Printer};
9 use crate::ty::{self, InferConst, Lift, Ty, TyCtxt};
10 use rustc_data_structures::functor::IdFunctor;
12 use rustc_hir::def::Namespace;
13 use rustc_hir::def_id::CRATE_DEF_INDEX;
14 use rustc_index::vec::{Idx, IndexVec};
17 use std::ops::ControlFlow;
21 impl fmt::Debug for ty::TraitDef {
22 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24 with_no_trimmed_paths(|| {
25 FmtPrinter::new(tcx, f, Namespace::TypeNS).print_def_path(self.def_id, &[])
32 impl fmt::Debug for ty::AdtDef {
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35 with_no_trimmed_paths(|| {
36 FmtPrinter::new(tcx, f, Namespace::TypeNS).print_def_path(self.did, &[])
43 impl fmt::Debug for ty::UpvarId {
44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45 let name = ty::tls::with(|tcx| tcx.hir().name(self.var_path.hir_id));
46 write!(f, "UpvarId({:?};`{}`;{:?})", self.var_path.hir_id, name, self.closure_expr_id)
50 impl fmt::Debug for ty::UpvarBorrow<'tcx> {
51 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52 write!(f, "UpvarBorrow({:?}, {:?})", self.kind, self.region)
56 impl fmt::Debug for ty::ExistentialTraitRef<'tcx> {
57 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58 with_no_trimmed_paths(|| fmt::Display::fmt(self, f))
62 impl fmt::Debug for ty::adjustment::Adjustment<'tcx> {
63 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64 write!(f, "{:?} -> {}", self.kind, self.target)
68 impl fmt::Debug for ty::BoundRegionKind {
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71 ty::BrAnon(n) => write!(f, "BrAnon({:?})", n),
72 ty::BrNamed(did, name) => {
73 if did.index == CRATE_DEF_INDEX {
74 write!(f, "BrNamed({})", name)
76 write!(f, "BrNamed({:?}, {})", did, name)
79 ty::BrEnv => write!(f, "BrEnv"),
84 impl fmt::Debug for ty::RegionKind {
85 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87 ty::ReEarlyBound(ref data) => write!(f, "ReEarlyBound({}, {})", data.index, data.name),
89 ty::ReLateBound(binder_id, ref bound_region) => {
90 write!(f, "ReLateBound({:?}, {:?})", binder_id, bound_region)
93 ty::ReFree(ref fr) => fr.fmt(f),
95 ty::ReStatic => write!(f, "ReStatic"),
97 ty::ReVar(ref vid) => vid.fmt(f),
99 ty::RePlaceholder(placeholder) => write!(f, "RePlaceholder({:?})", placeholder),
101 ty::ReEmpty(ui) => write!(f, "ReEmpty({:?})", ui),
103 ty::ReErased => write!(f, "ReErased"),
108 impl fmt::Debug for ty::FreeRegion {
109 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110 write!(f, "ReFree({:?}, {:?})", self.scope, self.bound_region)
114 impl fmt::Debug for ty::Variance {
115 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
116 f.write_str(match *self {
117 ty::Covariant => "+",
118 ty::Contravariant => "-",
119 ty::Invariant => "o",
120 ty::Bivariant => "*",
125 impl fmt::Debug for ty::FnSig<'tcx> {
126 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127 write!(f, "({:?}; c_variadic: {})->{:?}", self.inputs(), self.c_variadic, self.output())
131 impl fmt::Debug for ty::TyVid {
132 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
133 write!(f, "_#{}t", self.index)
137 impl<'tcx> fmt::Debug for ty::ConstVid<'tcx> {
138 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
139 write!(f, "_#{}c", self.index)
143 impl fmt::Debug for ty::IntVid {
144 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
145 write!(f, "_#{}i", self.index)
149 impl fmt::Debug for ty::FloatVid {
150 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
151 write!(f, "_#{}f", self.index)
155 impl fmt::Debug for ty::RegionVid {
156 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
157 write!(f, "'_#{}r", self.index())
161 impl fmt::Debug for ty::InferTy {
162 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
164 ty::TyVar(ref v) => v.fmt(f),
165 ty::IntVar(ref v) => v.fmt(f),
166 ty::FloatVar(ref v) => v.fmt(f),
167 ty::FreshTy(v) => write!(f, "FreshTy({:?})", v),
168 ty::FreshIntTy(v) => write!(f, "FreshIntTy({:?})", v),
169 ty::FreshFloatTy(v) => write!(f, "FreshFloatTy({:?})", v),
174 impl fmt::Debug for ty::IntVarValue {
175 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
177 ty::IntType(ref v) => v.fmt(f),
178 ty::UintType(ref v) => v.fmt(f),
183 impl fmt::Debug for ty::FloatVarValue {
184 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
189 impl fmt::Debug for ty::TraitRef<'tcx> {
190 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
191 with_no_trimmed_paths(|| fmt::Display::fmt(self, f))
195 impl fmt::Debug for Ty<'tcx> {
196 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
197 with_no_trimmed_paths(|| fmt::Display::fmt(self, f))
201 impl fmt::Debug for ty::ParamTy {
202 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
203 write!(f, "{}/#{}", self.name, self.index)
207 impl fmt::Debug for ty::ParamConst {
208 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
209 write!(f, "{}/#{}", self.name, self.index)
213 impl fmt::Debug for ty::TraitPredicate<'tcx> {
214 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
215 write!(f, "TraitPredicate({:?})", self.trait_ref)
219 impl fmt::Debug for ty::ProjectionPredicate<'tcx> {
220 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
221 write!(f, "ProjectionPredicate({:?}, {:?})", self.projection_ty, self.ty)
225 impl fmt::Debug for ty::Predicate<'tcx> {
226 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
227 write!(f, "{:?}", self.kind())
231 impl fmt::Debug for ty::PredicateKind<'tcx> {
232 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
234 ty::PredicateKind::Trait(ref a, constness) => {
235 if let hir::Constness::Const = constness {
236 write!(f, "const ")?;
240 ty::PredicateKind::Subtype(ref pair) => pair.fmt(f),
241 ty::PredicateKind::RegionOutlives(ref pair) => pair.fmt(f),
242 ty::PredicateKind::TypeOutlives(ref pair) => pair.fmt(f),
243 ty::PredicateKind::Projection(ref pair) => pair.fmt(f),
244 ty::PredicateKind::WellFormed(data) => write!(f, "WellFormed({:?})", data),
245 ty::PredicateKind::ObjectSafe(trait_def_id) => {
246 write!(f, "ObjectSafe({:?})", trait_def_id)
248 ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => {
249 write!(f, "ClosureKind({:?}, {:?}, {:?})", closure_def_id, closure_substs, kind)
251 ty::PredicateKind::ConstEvaluatable(def_id, substs) => {
252 write!(f, "ConstEvaluatable({:?}, {:?})", def_id, substs)
254 ty::PredicateKind::ConstEquate(c1, c2) => write!(f, "ConstEquate({:?}, {:?})", c1, c2),
255 ty::PredicateKind::TypeWellFormedFromEnv(ty) => {
256 write!(f, "TypeWellFormedFromEnv({:?})", ty)
262 ///////////////////////////////////////////////////////////////////////////
265 // For things that don't carry any arena-allocated data (and are
266 // copy...), just add them to this list.
268 TrivialTypeFoldableAndLiftImpls! {
272 ::rustc_target::abi::VariantIdx,
276 crate::middle::region::Scope,
277 ::rustc_ast::FloatTy,
278 ::rustc_ast::InlineAsmOptions,
279 ::rustc_ast::InlineAsmTemplatePiece,
281 ::rustc_span::symbol::Symbol,
282 ::rustc_hir::def::Res,
283 ::rustc_hir::def_id::DefId,
284 ::rustc_hir::def_id::LocalDefId,
286 ::rustc_hir::LlvmInlineAsmInner,
287 ::rustc_hir::MatchSource,
288 ::rustc_hir::Mutability,
289 ::rustc_hir::Unsafety,
290 ::rustc_target::asm::InlineAsmRegOrRegClass,
291 ::rustc_target::spec::abi::Abi,
292 crate::mir::coverage::ExpressionOperandId,
293 crate::mir::coverage::CounterValueReference,
294 crate::mir::coverage::InjectedExpressionId,
295 crate::mir::coverage::InjectedExpressionIndex,
296 crate::mir::coverage::MappedExpressionIndex,
298 crate::mir::Promoted,
299 crate::traits::Reveal,
300 crate::ty::adjustment::AutoBorrowMutability,
302 // Including `BoundRegionKind` is a *bit* dubious, but direct
303 // references to bound region appear in `ty::Error`, and aren't
304 // really meant to be folded. In general, we can only fold a fully
306 crate::ty::BoundRegionKind,
307 crate::ty::AssocItem,
308 crate::ty::Placeholder<crate::ty::BoundRegionKind>,
309 crate::ty::ClosureKind,
310 crate::ty::FreeRegion,
312 crate::ty::IntVarValue,
313 crate::ty::ParamConst,
315 crate::ty::adjustment::PointerCast,
316 crate::ty::RegionVid,
317 crate::ty::UniverseIndex,
322 ///////////////////////////////////////////////////////////////////////////
323 // Lift implementations
325 // FIXME(eddyb) replace all the uses of `Option::map` with `?`.
326 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>> Lift<'tcx> for (A, B) {
327 type Lifted = (A::Lifted, B::Lifted);
328 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
329 Some((tcx.lift(self.0)?, tcx.lift(self.1)?))
333 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>, C: Lift<'tcx>> Lift<'tcx> for (A, B, C) {
334 type Lifted = (A::Lifted, B::Lifted, C::Lifted);
335 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
336 Some((tcx.lift(self.0)?, tcx.lift(self.1)?, tcx.lift(self.2)?))
340 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Option<T> {
341 type Lifted = Option<T::Lifted>;
342 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
344 Some(x) => tcx.lift(x).map(Some),
350 impl<'tcx, T: Lift<'tcx>, E: Lift<'tcx>> Lift<'tcx> for Result<T, E> {
351 type Lifted = Result<T::Lifted, E::Lifted>;
352 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
354 Ok(x) => tcx.lift(x).map(Ok),
355 Err(e) => tcx.lift(e).map(Err),
360 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Box<T> {
361 type Lifted = Box<T::Lifted>;
362 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
363 tcx.lift(*self).map(Box::new)
367 impl<'tcx, T: Lift<'tcx> + Clone> Lift<'tcx> for Rc<T> {
368 type Lifted = Rc<T::Lifted>;
369 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
370 tcx.lift(self.as_ref().clone()).map(Rc::new)
374 impl<'tcx, T: Lift<'tcx> + Clone> Lift<'tcx> for Arc<T> {
375 type Lifted = Arc<T::Lifted>;
376 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
377 tcx.lift(self.as_ref().clone()).map(Arc::new)
380 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Vec<T> {
381 type Lifted = Vec<T::Lifted>;
382 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
383 self.into_iter().map(|v| tcx.lift(v)).collect()
387 impl<'tcx, I: Idx, T: Lift<'tcx>> Lift<'tcx> for IndexVec<I, T> {
388 type Lifted = IndexVec<I, T::Lifted>;
389 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
390 self.into_iter().map(|e| tcx.lift(e)).collect()
394 impl<'a, 'tcx> Lift<'tcx> for ty::TraitRef<'a> {
395 type Lifted = ty::TraitRef<'tcx>;
396 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
397 tcx.lift(self.substs).map(|substs| ty::TraitRef { def_id: self.def_id, substs })
401 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialTraitRef<'a> {
402 type Lifted = ty::ExistentialTraitRef<'tcx>;
403 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
404 tcx.lift(self.substs).map(|substs| ty::ExistentialTraitRef { def_id: self.def_id, substs })
408 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialPredicate<'a> {
409 type Lifted = ty::ExistentialPredicate<'tcx>;
410 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
412 ty::ExistentialPredicate::Trait(x) => tcx.lift(x).map(ty::ExistentialPredicate::Trait),
413 ty::ExistentialPredicate::Projection(x) => {
414 tcx.lift(x).map(ty::ExistentialPredicate::Projection)
416 ty::ExistentialPredicate::AutoTrait(def_id) => {
417 Some(ty::ExistentialPredicate::AutoTrait(def_id))
423 impl<'a, 'tcx> Lift<'tcx> for ty::TraitPredicate<'a> {
424 type Lifted = ty::TraitPredicate<'tcx>;
425 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::TraitPredicate<'tcx>> {
426 tcx.lift(self.trait_ref).map(|trait_ref| ty::TraitPredicate { trait_ref })
430 impl<'a, 'tcx> Lift<'tcx> for ty::SubtypePredicate<'a> {
431 type Lifted = ty::SubtypePredicate<'tcx>;
432 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::SubtypePredicate<'tcx>> {
433 tcx.lift((self.a, self.b)).map(|(a, b)| ty::SubtypePredicate {
434 a_is_expected: self.a_is_expected,
441 impl<'tcx, A: Copy + Lift<'tcx>, B: Copy + Lift<'tcx>> Lift<'tcx> for ty::OutlivesPredicate<A, B> {
442 type Lifted = ty::OutlivesPredicate<A::Lifted, B::Lifted>;
443 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
444 tcx.lift((self.0, self.1)).map(|(a, b)| ty::OutlivesPredicate(a, b))
448 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionTy<'a> {
449 type Lifted = ty::ProjectionTy<'tcx>;
450 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::ProjectionTy<'tcx>> {
451 tcx.lift(self.substs)
452 .map(|substs| ty::ProjectionTy { item_def_id: self.item_def_id, substs })
456 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionPredicate<'a> {
457 type Lifted = ty::ProjectionPredicate<'tcx>;
458 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::ProjectionPredicate<'tcx>> {
459 tcx.lift((self.projection_ty, self.ty))
460 .map(|(projection_ty, ty)| ty::ProjectionPredicate { projection_ty, ty })
464 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialProjection<'a> {
465 type Lifted = ty::ExistentialProjection<'tcx>;
466 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
467 tcx.lift(self.substs).map(|substs| ty::ExistentialProjection {
469 ty: tcx.lift(self.ty).expect("type must lift when substs do"),
470 item_def_id: self.item_def_id,
475 impl<'a, 'tcx> Lift<'tcx> for ty::PredicateKind<'a> {
476 type Lifted = ty::PredicateKind<'tcx>;
477 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
479 ty::PredicateKind::Trait(data, constness) => {
480 tcx.lift(data).map(|data| ty::PredicateKind::Trait(data, constness))
482 ty::PredicateKind::Subtype(data) => tcx.lift(data).map(ty::PredicateKind::Subtype),
483 ty::PredicateKind::RegionOutlives(data) => {
484 tcx.lift(data).map(ty::PredicateKind::RegionOutlives)
486 ty::PredicateKind::TypeOutlives(data) => {
487 tcx.lift(data).map(ty::PredicateKind::TypeOutlives)
489 ty::PredicateKind::Projection(data) => {
490 tcx.lift(data).map(ty::PredicateKind::Projection)
492 ty::PredicateKind::WellFormed(ty) => tcx.lift(ty).map(ty::PredicateKind::WellFormed),
493 ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => {
494 tcx.lift(closure_substs).map(|closure_substs| {
495 ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind)
498 ty::PredicateKind::ObjectSafe(trait_def_id) => {
499 Some(ty::PredicateKind::ObjectSafe(trait_def_id))
501 ty::PredicateKind::ConstEvaluatable(def_id, substs) => {
502 tcx.lift(substs).map(|substs| ty::PredicateKind::ConstEvaluatable(def_id, substs))
504 ty::PredicateKind::ConstEquate(c1, c2) => {
505 tcx.lift((c1, c2)).map(|(c1, c2)| ty::PredicateKind::ConstEquate(c1, c2))
507 ty::PredicateKind::TypeWellFormedFromEnv(ty) => {
508 tcx.lift(ty).map(ty::PredicateKind::TypeWellFormedFromEnv)
514 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::Binder<T> {
515 type Lifted = ty::Binder<T::Lifted>;
516 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
517 self.map_bound(|v| tcx.lift(v)).transpose()
521 impl<'a, 'tcx> Lift<'tcx> for ty::ParamEnv<'a> {
522 type Lifted = ty::ParamEnv<'tcx>;
523 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
524 tcx.lift(self.caller_bounds())
525 .map(|caller_bounds| ty::ParamEnv::new(caller_bounds, self.reveal()))
529 impl<'a, 'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::ParamEnvAnd<'a, T> {
530 type Lifted = ty::ParamEnvAnd<'tcx, T::Lifted>;
531 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
532 tcx.lift(self.param_env).and_then(|param_env| {
533 tcx.lift(self.value).map(|value| ty::ParamEnvAnd { param_env, value })
538 impl<'a, 'tcx> Lift<'tcx> for ty::ClosureSubsts<'a> {
539 type Lifted = ty::ClosureSubsts<'tcx>;
540 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
541 tcx.lift(self.substs).map(|substs| ty::ClosureSubsts { substs })
545 impl<'a, 'tcx> Lift<'tcx> for ty::GeneratorSubsts<'a> {
546 type Lifted = ty::GeneratorSubsts<'tcx>;
547 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
548 tcx.lift(self.substs).map(|substs| ty::GeneratorSubsts { substs })
552 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjustment<'a> {
553 type Lifted = ty::adjustment::Adjustment<'tcx>;
554 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
555 let ty::adjustment::Adjustment { kind, target } = self;
556 tcx.lift(kind).and_then(|kind| {
557 tcx.lift(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(overloaded) => {
569 tcx.lift(overloaded).map(ty::adjustment::Adjust::Deref)
571 ty::adjustment::Adjust::Borrow(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).map(|region| ty::adjustment::OverloadedDeref {
589 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::AutoBorrow<'a> {
590 type Lifted = ty::adjustment::AutoBorrow<'tcx>;
591 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
593 ty::adjustment::AutoBorrow::Ref(r, m) => {
594 tcx.lift(r).map(|r| ty::adjustment::AutoBorrow::Ref(r, m))
596 ty::adjustment::AutoBorrow::RawPtr(m) => Some(ty::adjustment::AutoBorrow::RawPtr(m)),
601 impl<'a, 'tcx> Lift<'tcx> for ty::GenSig<'a> {
602 type Lifted = ty::GenSig<'tcx>;
603 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
604 tcx.lift((self.resume_ty, self.yield_ty, self.return_ty))
605 .map(|(resume_ty, yield_ty, return_ty)| ty::GenSig { resume_ty, yield_ty, return_ty })
609 impl<'a, 'tcx> Lift<'tcx> for ty::FnSig<'a> {
610 type Lifted = ty::FnSig<'tcx>;
611 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
612 tcx.lift(self.inputs_and_output).map(|x| ty::FnSig {
613 inputs_and_output: x,
614 c_variadic: self.c_variadic,
615 unsafety: self.unsafety,
621 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::error::ExpectedFound<T> {
622 type Lifted = ty::error::ExpectedFound<T::Lifted>;
623 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
624 let ty::error::ExpectedFound { expected, found } = self;
625 tcx.lift(expected).and_then(|expected| {
626 tcx.lift(found).map(|found| ty::error::ExpectedFound { expected, found })
631 impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> {
632 type Lifted = ty::error::TypeError<'tcx>;
633 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
634 use crate::ty::error::TypeError::*;
637 Mismatch => Mismatch,
638 UnsafetyMismatch(x) => UnsafetyMismatch(x),
639 AbiMismatch(x) => AbiMismatch(x),
640 Mutability => Mutability,
641 TupleSize(x) => TupleSize(x),
642 FixedArraySize(x) => FixedArraySize(x),
643 ArgCount => ArgCount,
644 RegionsDoesNotOutlive(a, b) => {
645 return tcx.lift((a, b)).map(|(a, b)| RegionsDoesNotOutlive(a, b));
647 RegionsInsufficientlyPolymorphic(a, b) => {
648 return tcx.lift(b).map(|b| RegionsInsufficientlyPolymorphic(a, b));
650 RegionsOverlyPolymorphic(a, b) => {
651 return tcx.lift(b).map(|b| RegionsOverlyPolymorphic(a, b));
653 RegionsPlaceholderMismatch => RegionsPlaceholderMismatch,
654 IntMismatch(x) => IntMismatch(x),
655 FloatMismatch(x) => FloatMismatch(x),
656 Traits(x) => Traits(x),
657 VariadicMismatch(x) => VariadicMismatch(x),
658 CyclicTy(t) => return tcx.lift(t).map(|t| CyclicTy(t)),
659 CyclicConst(ct) => return tcx.lift(ct).map(|ct| CyclicConst(ct)),
660 ProjectionMismatched(x) => ProjectionMismatched(x),
661 Sorts(x) => return tcx.lift(x).map(Sorts),
662 ExistentialMismatch(x) => return tcx.lift(x).map(ExistentialMismatch),
663 ConstMismatch(x) => return tcx.lift(x).map(ConstMismatch),
664 IntrinsicCast => IntrinsicCast,
665 TargetFeatureCast(x) => TargetFeatureCast(x),
666 ObjectUnsafeCoercion(x) => return tcx.lift(x).map(ObjectUnsafeCoercion),
671 impl<'a, 'tcx> Lift<'tcx> for ty::InstanceDef<'a> {
672 type Lifted = ty::InstanceDef<'tcx>;
673 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
675 ty::InstanceDef::Item(def_id) => Some(ty::InstanceDef::Item(def_id)),
676 ty::InstanceDef::VtableShim(def_id) => Some(ty::InstanceDef::VtableShim(def_id)),
677 ty::InstanceDef::ReifyShim(def_id) => Some(ty::InstanceDef::ReifyShim(def_id)),
678 ty::InstanceDef::Intrinsic(def_id) => Some(ty::InstanceDef::Intrinsic(def_id)),
679 ty::InstanceDef::FnPtrShim(def_id, ty) => {
680 Some(ty::InstanceDef::FnPtrShim(def_id, tcx.lift(ty)?))
682 ty::InstanceDef::Virtual(def_id, n) => Some(ty::InstanceDef::Virtual(def_id, n)),
683 ty::InstanceDef::ClosureOnceShim { call_once } => {
684 Some(ty::InstanceDef::ClosureOnceShim { call_once })
686 ty::InstanceDef::DropGlue(def_id, ty) => {
687 Some(ty::InstanceDef::DropGlue(def_id, tcx.lift(ty)?))
689 ty::InstanceDef::CloneShim(def_id, ty) => {
690 Some(ty::InstanceDef::CloneShim(def_id, tcx.lift(ty)?))
696 ///////////////////////////////////////////////////////////////////////////
697 // TypeFoldable implementations.
699 // Ideally, each type should invoke `folder.fold_foo(self)` and
700 // nothing else. In some cases, though, we haven't gotten around to
701 // adding methods on the `folder` yet, and thus the folding is
702 // hard-coded here. This is less-flexible, because folders cannot
703 // override the behavior, but there are a lot of random types and one
704 // can easily refactor the folding into the TypeFolder trait as
707 /// AdtDefs are basically the same as a DefId.
708 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::AdtDef {
709 fn super_fold_with<F: TypeFolder<'tcx>>(self, _folder: &mut F) -> Self {
713 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
714 ControlFlow::CONTINUE
718 impl<'tcx, T: TypeFoldable<'tcx>, U: TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
719 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> (T, U) {
720 (self.0.fold_with(folder), self.1.fold_with(folder))
723 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
724 self.0.visit_with(visitor)?;
725 self.1.visit_with(visitor)
729 impl<'tcx, A: TypeFoldable<'tcx>, B: TypeFoldable<'tcx>, C: TypeFoldable<'tcx>> TypeFoldable<'tcx>
732 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> (A, B, C) {
733 (self.0.fold_with(folder), self.1.fold_with(folder), self.2.fold_with(folder))
736 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
737 self.0.visit_with(visitor)?;
738 self.1.visit_with(visitor)?;
739 self.2.visit_with(visitor)
743 EnumTypeFoldableImpl! {
744 impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
747 } where T: TypeFoldable<'tcx>
750 EnumTypeFoldableImpl! {
751 impl<'tcx, T, E> TypeFoldable<'tcx> for Result<T, E> {
754 } where T: TypeFoldable<'tcx>, E: TypeFoldable<'tcx>,
757 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
758 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
759 // FIXME: Reuse the `Rc` here.
760 Rc::new((*self).clone().fold_with(folder))
763 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
764 (**self).visit_with(visitor)
768 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Arc<T> {
769 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
770 // FIXME: Reuse the `Arc` here.
771 Arc::new((*self).clone().fold_with(folder))
774 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
775 (**self).visit_with(visitor)
779 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
780 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
781 self.map_id(|value| value.fold_with(folder))
784 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
785 (**self).visit_with(visitor)
789 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
790 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
791 self.map_id(|t| t.fold_with(folder))
794 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
795 self.iter().try_for_each(|t| t.visit_with(visitor))
799 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<[T]> {
800 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
801 self.map_id(|t| t.fold_with(folder))
804 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
805 self.iter().try_for_each(|t| t.visit_with(visitor))
809 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<T> {
810 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
811 self.map_bound(|ty| ty.fold_with(folder))
814 fn fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
815 folder.fold_binder(self)
818 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
819 self.as_ref().skip_binder().visit_with(visitor)
822 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
823 visitor.visit_binder(self)
827 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Binder<ty::ExistentialPredicate<'tcx>>> {
828 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
829 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_poly_existential_predicates(v))
832 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
833 self.iter().try_for_each(|p| p.visit_with(visitor))
837 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<Ty<'tcx>> {
838 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
839 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_type_list(v))
842 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
843 self.iter().try_for_each(|t| t.visit_with(visitor))
847 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind> {
848 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
849 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_projs(v))
852 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
853 self.iter().try_for_each(|t| t.visit_with(visitor))
857 impl<'tcx> TypeFoldable<'tcx> for ty::instance::Instance<'tcx> {
858 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
859 use crate::ty::InstanceDef::*;
861 substs: self.substs.fold_with(folder),
862 def: match self.def {
863 Item(def) => Item(def.fold_with(folder)),
864 VtableShim(did) => VtableShim(did.fold_with(folder)),
865 ReifyShim(did) => ReifyShim(did.fold_with(folder)),
866 Intrinsic(did) => Intrinsic(did.fold_with(folder)),
867 FnPtrShim(did, ty) => FnPtrShim(did.fold_with(folder), ty.fold_with(folder)),
868 Virtual(did, i) => Virtual(did.fold_with(folder), i),
869 ClosureOnceShim { call_once } => {
870 ClosureOnceShim { call_once: call_once.fold_with(folder) }
872 DropGlue(did, ty) => DropGlue(did.fold_with(folder), ty.fold_with(folder)),
873 CloneShim(did, ty) => CloneShim(did.fold_with(folder), ty.fold_with(folder)),
878 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
879 use crate::ty::InstanceDef::*;
880 self.substs.visit_with(visitor)?;
882 Item(def) => def.visit_with(visitor),
883 VtableShim(did) | ReifyShim(did) | Intrinsic(did) | Virtual(did, _) => {
884 did.visit_with(visitor)
886 FnPtrShim(did, ty) | CloneShim(did, ty) => {
887 did.visit_with(visitor)?;
888 ty.visit_with(visitor)
890 DropGlue(did, ty) => {
891 did.visit_with(visitor)?;
892 ty.visit_with(visitor)
894 ClosureOnceShim { call_once } => call_once.visit_with(visitor),
899 impl<'tcx> TypeFoldable<'tcx> for interpret::GlobalId<'tcx> {
900 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
901 Self { instance: self.instance.fold_with(folder), promoted: self.promoted }
904 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
905 self.instance.visit_with(visitor)
909 impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
910 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
911 let kind = match *self.kind() {
912 ty::RawPtr(tm) => ty::RawPtr(tm.fold_with(folder)),
913 ty::Array(typ, sz) => ty::Array(typ.fold_with(folder), sz.fold_with(folder)),
914 ty::Slice(typ) => ty::Slice(typ.fold_with(folder)),
915 ty::Adt(tid, substs) => ty::Adt(tid, substs.fold_with(folder)),
916 ty::Dynamic(trait_ty, region) => {
917 ty::Dynamic(trait_ty.fold_with(folder), region.fold_with(folder))
919 ty::Tuple(ts) => ty::Tuple(ts.fold_with(folder)),
920 ty::FnDef(def_id, substs) => ty::FnDef(def_id, substs.fold_with(folder)),
921 ty::FnPtr(f) => ty::FnPtr(f.fold_with(folder)),
922 ty::Ref(r, ty, mutbl) => ty::Ref(r.fold_with(folder), ty.fold_with(folder), mutbl),
923 ty::Generator(did, substs, movability) => {
924 ty::Generator(did, substs.fold_with(folder), movability)
926 ty::GeneratorWitness(types) => ty::GeneratorWitness(types.fold_with(folder)),
927 ty::Closure(did, substs) => ty::Closure(did, substs.fold_with(folder)),
928 ty::Projection(data) => ty::Projection(data.fold_with(folder)),
929 ty::Opaque(did, substs) => ty::Opaque(did, substs.fold_with(folder)),
941 | ty::Placeholder(..)
943 | ty::Foreign(..) => return self,
946 if *self.kind() == kind { self } else { folder.tcx().mk_ty(kind) }
949 fn fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
953 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
955 ty::RawPtr(ref tm) => tm.visit_with(visitor),
956 ty::Array(typ, sz) => {
957 typ.visit_with(visitor)?;
958 sz.visit_with(visitor)
960 ty::Slice(typ) => typ.visit_with(visitor),
961 ty::Adt(_, substs) => substs.visit_with(visitor),
962 ty::Dynamic(ref trait_ty, ref reg) => {
963 trait_ty.visit_with(visitor)?;
964 reg.visit_with(visitor)
966 ty::Tuple(ts) => ts.visit_with(visitor),
967 ty::FnDef(_, substs) => substs.visit_with(visitor),
968 ty::FnPtr(ref f) => f.visit_with(visitor),
969 ty::Ref(r, ty, _) => {
970 r.visit_with(visitor)?;
971 ty.visit_with(visitor)
973 ty::Generator(_did, ref substs, _) => substs.visit_with(visitor),
974 ty::GeneratorWitness(ref types) => types.visit_with(visitor),
975 ty::Closure(_did, ref substs) => substs.visit_with(visitor),
976 ty::Projection(ref data) => data.visit_with(visitor),
977 ty::Opaque(_, ref substs) => substs.visit_with(visitor),
988 | ty::Placeholder(..)
991 | ty::Foreign(..) => ControlFlow::CONTINUE,
995 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
996 visitor.visit_ty(self)
1000 impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> {
1001 fn super_fold_with<F: TypeFolder<'tcx>>(self, _folder: &mut F) -> Self {
1005 fn fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
1006 folder.fold_region(self)
1009 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
1010 ControlFlow::CONTINUE
1013 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1014 visitor.visit_region(*self)
1018 impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
1019 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
1020 let new = self.inner.kind.fold_with(folder);
1021 folder.tcx().reuse_or_mk_predicate(self, new)
1024 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1025 self.inner.kind.visit_with(visitor)
1028 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1029 visitor.visit_predicate(*self)
1032 fn has_vars_bound_at_or_above(&self, binder: ty::DebruijnIndex) -> bool {
1033 self.inner.outer_exclusive_binder > binder
1036 fn has_type_flags(&self, flags: ty::TypeFlags) -> bool {
1037 self.inner.flags.intersects(flags)
1041 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
1042 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
1043 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_predicates(v))
1046 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1047 self.iter().try_for_each(|p| p.visit_with(visitor))
1051 impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
1052 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
1053 self.map_id(|x| x.fold_with(folder))
1056 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1057 self.iter().try_for_each(|t| t.visit_with(visitor))
1061 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Const<'tcx> {
1062 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
1063 let ty = self.ty.fold_with(folder);
1064 let val = self.val.fold_with(folder);
1065 if ty != self.ty || val != self.val {
1066 folder.tcx().mk_const(ty::Const { ty, val })
1072 fn fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
1073 folder.fold_const(self)
1076 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1077 self.ty.visit_with(visitor)?;
1078 self.val.visit_with(visitor)
1081 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1082 visitor.visit_const(self)
1086 impl<'tcx> TypeFoldable<'tcx> for ty::ConstKind<'tcx> {
1087 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
1089 ty::ConstKind::Infer(ic) => ty::ConstKind::Infer(ic.fold_with(folder)),
1090 ty::ConstKind::Param(p) => ty::ConstKind::Param(p.fold_with(folder)),
1091 ty::ConstKind::Unevaluated(did, substs, promoted) => {
1092 ty::ConstKind::Unevaluated(did, substs.fold_with(folder), promoted)
1094 ty::ConstKind::Value(_)
1095 | ty::ConstKind::Bound(..)
1096 | ty::ConstKind::Placeholder(..)
1097 | ty::ConstKind::Error(_) => self,
1101 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1103 ty::ConstKind::Infer(ic) => ic.visit_with(visitor),
1104 ty::ConstKind::Param(p) => p.visit_with(visitor),
1105 ty::ConstKind::Unevaluated(_, substs, _) => substs.visit_with(visitor),
1106 ty::ConstKind::Value(_)
1107 | ty::ConstKind::Bound(..)
1108 | ty::ConstKind::Placeholder(_)
1109 | ty::ConstKind::Error(_) => ControlFlow::CONTINUE,
1114 impl<'tcx> TypeFoldable<'tcx> for InferConst<'tcx> {
1115 fn super_fold_with<F: TypeFolder<'tcx>>(self, _folder: &mut F) -> Self {
1119 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
1120 ControlFlow::CONTINUE