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::FnSig<'tcx> {
115 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
116 write!(f, "({:?}; c_variadic: {})->{:?}", self.inputs(), self.c_variadic, self.output())
120 impl<'tcx> fmt::Debug for ty::ConstVid<'tcx> {
121 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
122 write!(f, "_#{}c", self.index)
126 impl fmt::Debug for ty::RegionVid {
127 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
128 write!(f, "'_#{}r", self.index())
132 impl fmt::Debug for ty::TraitRef<'tcx> {
133 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
134 with_no_trimmed_paths(|| fmt::Display::fmt(self, f))
138 impl fmt::Debug for Ty<'tcx> {
139 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
140 with_no_trimmed_paths(|| fmt::Display::fmt(self, f))
144 impl fmt::Debug for ty::ParamTy {
145 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
146 write!(f, "{}/#{}", self.name, self.index)
150 impl fmt::Debug for ty::ParamConst {
151 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152 write!(f, "{}/#{}", self.name, self.index)
156 impl fmt::Debug for ty::TraitPredicate<'tcx> {
157 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
158 write!(f, "TraitPredicate({:?})", self.trait_ref)
162 impl fmt::Debug for ty::ProjectionPredicate<'tcx> {
163 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
164 write!(f, "ProjectionPredicate({:?}, {:?})", self.projection_ty, self.ty)
168 impl fmt::Debug for ty::Predicate<'tcx> {
169 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
170 write!(f, "{:?}", self.kind())
174 impl fmt::Debug for ty::PredicateKind<'tcx> {
175 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
177 ty::PredicateKind::Trait(ref a, constness) => {
178 if let hir::Constness::Const = constness {
179 write!(f, "const ")?;
183 ty::PredicateKind::Subtype(ref pair) => pair.fmt(f),
184 ty::PredicateKind::RegionOutlives(ref pair) => pair.fmt(f),
185 ty::PredicateKind::TypeOutlives(ref pair) => pair.fmt(f),
186 ty::PredicateKind::Projection(ref pair) => pair.fmt(f),
187 ty::PredicateKind::WellFormed(data) => write!(f, "WellFormed({:?})", data),
188 ty::PredicateKind::ObjectSafe(trait_def_id) => {
189 write!(f, "ObjectSafe({:?})", trait_def_id)
191 ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => {
192 write!(f, "ClosureKind({:?}, {:?}, {:?})", closure_def_id, closure_substs, kind)
194 ty::PredicateKind::ConstEvaluatable(def_id, substs) => {
195 write!(f, "ConstEvaluatable({:?}, {:?})", def_id, substs)
197 ty::PredicateKind::ConstEquate(c1, c2) => write!(f, "ConstEquate({:?}, {:?})", c1, c2),
198 ty::PredicateKind::TypeWellFormedFromEnv(ty) => {
199 write!(f, "TypeWellFormedFromEnv({:?})", ty)
205 ///////////////////////////////////////////////////////////////////////////
208 // For things that don't carry any arena-allocated data (and are
209 // copy...), just add them to this list.
211 TrivialTypeFoldableAndLiftImpls! {
215 ::rustc_target::abi::VariantIdx,
219 crate::middle::region::Scope,
221 ::rustc_ast::InlineAsmOptions,
222 ::rustc_ast::InlineAsmTemplatePiece,
224 ::rustc_span::symbol::Symbol,
225 ::rustc_hir::def::Res,
226 ::rustc_hir::def_id::DefId,
227 ::rustc_hir::def_id::LocalDefId,
229 ::rustc_hir::LlvmInlineAsmInner,
230 ::rustc_hir::MatchSource,
231 ::rustc_hir::Mutability,
232 ::rustc_hir::Unsafety,
233 ::rustc_target::asm::InlineAsmRegOrRegClass,
234 ::rustc_target::spec::abi::Abi,
235 crate::mir::coverage::ExpressionOperandId,
236 crate::mir::coverage::CounterValueReference,
237 crate::mir::coverage::InjectedExpressionId,
238 crate::mir::coverage::InjectedExpressionIndex,
239 crate::mir::coverage::MappedExpressionIndex,
241 crate::mir::Promoted,
242 crate::traits::Reveal,
243 crate::ty::adjustment::AutoBorrowMutability,
245 // Including `BoundRegionKind` is a *bit* dubious, but direct
246 // references to bound region appear in `ty::Error`, and aren't
247 // really meant to be folded. In general, we can only fold a fully
249 crate::ty::BoundRegionKind,
250 crate::ty::AssocItem,
251 crate::ty::Placeholder<crate::ty::BoundRegionKind>,
252 crate::ty::ClosureKind,
253 crate::ty::FreeRegion,
255 crate::ty::IntVarValue,
256 crate::ty::ParamConst,
258 crate::ty::adjustment::PointerCast,
259 crate::ty::RegionVid,
260 crate::ty::UniverseIndex,
265 ///////////////////////////////////////////////////////////////////////////
266 // Lift implementations
268 // FIXME(eddyb) replace all the uses of `Option::map` with `?`.
269 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>> Lift<'tcx> for (A, B) {
270 type Lifted = (A::Lifted, B::Lifted);
271 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
272 Some((tcx.lift(self.0)?, tcx.lift(self.1)?))
276 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>, C: Lift<'tcx>> Lift<'tcx> for (A, B, C) {
277 type Lifted = (A::Lifted, B::Lifted, C::Lifted);
278 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
279 Some((tcx.lift(self.0)?, tcx.lift(self.1)?, tcx.lift(self.2)?))
283 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Option<T> {
284 type Lifted = Option<T::Lifted>;
285 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
287 Some(x) => tcx.lift(x).map(Some),
293 impl<'tcx, T: Lift<'tcx>, E: Lift<'tcx>> Lift<'tcx> for Result<T, E> {
294 type Lifted = Result<T::Lifted, E::Lifted>;
295 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
297 Ok(x) => tcx.lift(x).map(Ok),
298 Err(e) => tcx.lift(e).map(Err),
303 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Box<T> {
304 type Lifted = Box<T::Lifted>;
305 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
306 tcx.lift(*self).map(Box::new)
310 impl<'tcx, T: Lift<'tcx> + Clone> Lift<'tcx> for Rc<T> {
311 type Lifted = Rc<T::Lifted>;
312 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
313 tcx.lift(self.as_ref().clone()).map(Rc::new)
317 impl<'tcx, T: Lift<'tcx> + Clone> Lift<'tcx> for Arc<T> {
318 type Lifted = Arc<T::Lifted>;
319 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
320 tcx.lift(self.as_ref().clone()).map(Arc::new)
323 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Vec<T> {
324 type Lifted = Vec<T::Lifted>;
325 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
326 self.into_iter().map(|v| tcx.lift(v)).collect()
330 impl<'tcx, I: Idx, T: Lift<'tcx>> Lift<'tcx> for IndexVec<I, T> {
331 type Lifted = IndexVec<I, T::Lifted>;
332 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
333 self.into_iter().map(|e| tcx.lift(e)).collect()
337 impl<'a, 'tcx> Lift<'tcx> for ty::TraitRef<'a> {
338 type Lifted = ty::TraitRef<'tcx>;
339 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
340 tcx.lift(self.substs).map(|substs| ty::TraitRef { def_id: self.def_id, substs })
344 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialTraitRef<'a> {
345 type Lifted = ty::ExistentialTraitRef<'tcx>;
346 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
347 tcx.lift(self.substs).map(|substs| ty::ExistentialTraitRef { def_id: self.def_id, substs })
351 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialPredicate<'a> {
352 type Lifted = ty::ExistentialPredicate<'tcx>;
353 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
355 ty::ExistentialPredicate::Trait(x) => tcx.lift(x).map(ty::ExistentialPredicate::Trait),
356 ty::ExistentialPredicate::Projection(x) => {
357 tcx.lift(x).map(ty::ExistentialPredicate::Projection)
359 ty::ExistentialPredicate::AutoTrait(def_id) => {
360 Some(ty::ExistentialPredicate::AutoTrait(def_id))
366 impl<'a, 'tcx> Lift<'tcx> for ty::TraitPredicate<'a> {
367 type Lifted = ty::TraitPredicate<'tcx>;
368 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::TraitPredicate<'tcx>> {
369 tcx.lift(self.trait_ref).map(|trait_ref| ty::TraitPredicate { trait_ref })
373 impl<'a, 'tcx> Lift<'tcx> for ty::SubtypePredicate<'a> {
374 type Lifted = ty::SubtypePredicate<'tcx>;
375 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::SubtypePredicate<'tcx>> {
376 tcx.lift((self.a, self.b)).map(|(a, b)| ty::SubtypePredicate {
377 a_is_expected: self.a_is_expected,
384 impl<'tcx, A: Copy + Lift<'tcx>, B: Copy + Lift<'tcx>> Lift<'tcx> for ty::OutlivesPredicate<A, B> {
385 type Lifted = ty::OutlivesPredicate<A::Lifted, B::Lifted>;
386 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
387 tcx.lift((self.0, self.1)).map(|(a, b)| ty::OutlivesPredicate(a, b))
391 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionTy<'a> {
392 type Lifted = ty::ProjectionTy<'tcx>;
393 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::ProjectionTy<'tcx>> {
394 tcx.lift(self.substs)
395 .map(|substs| ty::ProjectionTy { item_def_id: self.item_def_id, substs })
399 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionPredicate<'a> {
400 type Lifted = ty::ProjectionPredicate<'tcx>;
401 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::ProjectionPredicate<'tcx>> {
402 tcx.lift((self.projection_ty, self.ty))
403 .map(|(projection_ty, ty)| ty::ProjectionPredicate { projection_ty, ty })
407 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialProjection<'a> {
408 type Lifted = ty::ExistentialProjection<'tcx>;
409 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
410 tcx.lift(self.substs).map(|substs| ty::ExistentialProjection {
412 ty: tcx.lift(self.ty).expect("type must lift when substs do"),
413 item_def_id: self.item_def_id,
418 impl<'a, 'tcx> Lift<'tcx> for ty::PredicateKind<'a> {
419 type Lifted = ty::PredicateKind<'tcx>;
420 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
422 ty::PredicateKind::Trait(data, constness) => {
423 tcx.lift(data).map(|data| ty::PredicateKind::Trait(data, constness))
425 ty::PredicateKind::Subtype(data) => tcx.lift(data).map(ty::PredicateKind::Subtype),
426 ty::PredicateKind::RegionOutlives(data) => {
427 tcx.lift(data).map(ty::PredicateKind::RegionOutlives)
429 ty::PredicateKind::TypeOutlives(data) => {
430 tcx.lift(data).map(ty::PredicateKind::TypeOutlives)
432 ty::PredicateKind::Projection(data) => {
433 tcx.lift(data).map(ty::PredicateKind::Projection)
435 ty::PredicateKind::WellFormed(ty) => tcx.lift(ty).map(ty::PredicateKind::WellFormed),
436 ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => {
437 tcx.lift(closure_substs).map(|closure_substs| {
438 ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind)
441 ty::PredicateKind::ObjectSafe(trait_def_id) => {
442 Some(ty::PredicateKind::ObjectSafe(trait_def_id))
444 ty::PredicateKind::ConstEvaluatable(def_id, substs) => {
445 tcx.lift(substs).map(|substs| ty::PredicateKind::ConstEvaluatable(def_id, substs))
447 ty::PredicateKind::ConstEquate(c1, c2) => {
448 tcx.lift((c1, c2)).map(|(c1, c2)| ty::PredicateKind::ConstEquate(c1, c2))
450 ty::PredicateKind::TypeWellFormedFromEnv(ty) => {
451 tcx.lift(ty).map(ty::PredicateKind::TypeWellFormedFromEnv)
457 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::Binder<T> {
458 type Lifted = ty::Binder<T::Lifted>;
459 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
460 self.map_bound(|v| tcx.lift(v)).transpose()
464 impl<'a, 'tcx> Lift<'tcx> for ty::ParamEnv<'a> {
465 type Lifted = ty::ParamEnv<'tcx>;
466 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
467 tcx.lift(self.caller_bounds())
468 .map(|caller_bounds| ty::ParamEnv::new(caller_bounds, self.reveal()))
472 impl<'a, 'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::ParamEnvAnd<'a, T> {
473 type Lifted = ty::ParamEnvAnd<'tcx, T::Lifted>;
474 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
475 tcx.lift(self.param_env).and_then(|param_env| {
476 tcx.lift(self.value).map(|value| ty::ParamEnvAnd { param_env, value })
481 impl<'a, 'tcx> Lift<'tcx> for ty::ClosureSubsts<'a> {
482 type Lifted = ty::ClosureSubsts<'tcx>;
483 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
484 tcx.lift(self.substs).map(|substs| ty::ClosureSubsts { substs })
488 impl<'a, 'tcx> Lift<'tcx> for ty::GeneratorSubsts<'a> {
489 type Lifted = ty::GeneratorSubsts<'tcx>;
490 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
491 tcx.lift(self.substs).map(|substs| ty::GeneratorSubsts { substs })
495 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjustment<'a> {
496 type Lifted = ty::adjustment::Adjustment<'tcx>;
497 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
498 let ty::adjustment::Adjustment { kind, target } = self;
499 tcx.lift(kind).and_then(|kind| {
500 tcx.lift(target).map(|target| ty::adjustment::Adjustment { kind, target })
505 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjust<'a> {
506 type Lifted = ty::adjustment::Adjust<'tcx>;
507 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
509 ty::adjustment::Adjust::NeverToAny => Some(ty::adjustment::Adjust::NeverToAny),
510 ty::adjustment::Adjust::Pointer(ptr) => Some(ty::adjustment::Adjust::Pointer(ptr)),
511 ty::adjustment::Adjust::Deref(overloaded) => {
512 tcx.lift(overloaded).map(ty::adjustment::Adjust::Deref)
514 ty::adjustment::Adjust::Borrow(autoref) => {
515 tcx.lift(autoref).map(ty::adjustment::Adjust::Borrow)
521 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::OverloadedDeref<'a> {
522 type Lifted = ty::adjustment::OverloadedDeref<'tcx>;
523 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
524 tcx.lift(self.region).map(|region| ty::adjustment::OverloadedDeref {
532 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::AutoBorrow<'a> {
533 type Lifted = ty::adjustment::AutoBorrow<'tcx>;
534 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
536 ty::adjustment::AutoBorrow::Ref(r, m) => {
537 tcx.lift(r).map(|r| ty::adjustment::AutoBorrow::Ref(r, m))
539 ty::adjustment::AutoBorrow::RawPtr(m) => Some(ty::adjustment::AutoBorrow::RawPtr(m)),
544 impl<'a, 'tcx> Lift<'tcx> for ty::GenSig<'a> {
545 type Lifted = ty::GenSig<'tcx>;
546 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
547 tcx.lift((self.resume_ty, self.yield_ty, self.return_ty))
548 .map(|(resume_ty, yield_ty, return_ty)| ty::GenSig { resume_ty, yield_ty, return_ty })
552 impl<'a, 'tcx> Lift<'tcx> for ty::FnSig<'a> {
553 type Lifted = ty::FnSig<'tcx>;
554 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
555 tcx.lift(self.inputs_and_output).map(|x| ty::FnSig {
556 inputs_and_output: x,
557 c_variadic: self.c_variadic,
558 unsafety: self.unsafety,
564 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::error::ExpectedFound<T> {
565 type Lifted = ty::error::ExpectedFound<T::Lifted>;
566 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
567 let ty::error::ExpectedFound { expected, found } = self;
568 tcx.lift(expected).and_then(|expected| {
569 tcx.lift(found).map(|found| ty::error::ExpectedFound { expected, found })
574 impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> {
575 type Lifted = ty::error::TypeError<'tcx>;
576 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
577 use crate::ty::error::TypeError::*;
580 Mismatch => Mismatch,
581 UnsafetyMismatch(x) => UnsafetyMismatch(x),
582 AbiMismatch(x) => AbiMismatch(x),
583 Mutability => Mutability,
584 TupleSize(x) => TupleSize(x),
585 FixedArraySize(x) => FixedArraySize(x),
586 ArgCount => ArgCount,
587 RegionsDoesNotOutlive(a, b) => {
588 return tcx.lift((a, b)).map(|(a, b)| RegionsDoesNotOutlive(a, b));
590 RegionsInsufficientlyPolymorphic(a, b) => {
591 return tcx.lift(b).map(|b| RegionsInsufficientlyPolymorphic(a, b));
593 RegionsOverlyPolymorphic(a, b) => {
594 return tcx.lift(b).map(|b| RegionsOverlyPolymorphic(a, b));
596 RegionsPlaceholderMismatch => RegionsPlaceholderMismatch,
597 IntMismatch(x) => IntMismatch(x),
598 FloatMismatch(x) => FloatMismatch(x),
599 Traits(x) => Traits(x),
600 VariadicMismatch(x) => VariadicMismatch(x),
601 CyclicTy(t) => return tcx.lift(t).map(|t| CyclicTy(t)),
602 CyclicConst(ct) => return tcx.lift(ct).map(|ct| CyclicConst(ct)),
603 ProjectionMismatched(x) => ProjectionMismatched(x),
604 Sorts(x) => return tcx.lift(x).map(Sorts),
605 ExistentialMismatch(x) => return tcx.lift(x).map(ExistentialMismatch),
606 ConstMismatch(x) => return tcx.lift(x).map(ConstMismatch),
607 IntrinsicCast => IntrinsicCast,
608 TargetFeatureCast(x) => TargetFeatureCast(x),
609 ObjectUnsafeCoercion(x) => return tcx.lift(x).map(ObjectUnsafeCoercion),
614 impl<'a, 'tcx> Lift<'tcx> for ty::InstanceDef<'a> {
615 type Lifted = ty::InstanceDef<'tcx>;
616 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
618 ty::InstanceDef::Item(def_id) => Some(ty::InstanceDef::Item(def_id)),
619 ty::InstanceDef::VtableShim(def_id) => Some(ty::InstanceDef::VtableShim(def_id)),
620 ty::InstanceDef::ReifyShim(def_id) => Some(ty::InstanceDef::ReifyShim(def_id)),
621 ty::InstanceDef::Intrinsic(def_id) => Some(ty::InstanceDef::Intrinsic(def_id)),
622 ty::InstanceDef::FnPtrShim(def_id, ty) => {
623 Some(ty::InstanceDef::FnPtrShim(def_id, tcx.lift(ty)?))
625 ty::InstanceDef::Virtual(def_id, n) => Some(ty::InstanceDef::Virtual(def_id, n)),
626 ty::InstanceDef::ClosureOnceShim { call_once } => {
627 Some(ty::InstanceDef::ClosureOnceShim { call_once })
629 ty::InstanceDef::DropGlue(def_id, ty) => {
630 Some(ty::InstanceDef::DropGlue(def_id, tcx.lift(ty)?))
632 ty::InstanceDef::CloneShim(def_id, ty) => {
633 Some(ty::InstanceDef::CloneShim(def_id, tcx.lift(ty)?))
639 ///////////////////////////////////////////////////////////////////////////
640 // TypeFoldable implementations.
642 // Ideally, each type should invoke `folder.fold_foo(self)` and
643 // nothing else. In some cases, though, we haven't gotten around to
644 // adding methods on the `folder` yet, and thus the folding is
645 // hard-coded here. This is less-flexible, because folders cannot
646 // override the behavior, but there are a lot of random types and one
647 // can easily refactor the folding into the TypeFolder trait as
650 /// AdtDefs are basically the same as a DefId.
651 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::AdtDef {
652 fn super_fold_with<F: TypeFolder<'tcx>>(self, _folder: &mut F) -> Self {
656 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
657 ControlFlow::CONTINUE
661 impl<'tcx, T: TypeFoldable<'tcx>, U: TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
662 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> (T, U) {
663 (self.0.fold_with(folder), self.1.fold_with(folder))
666 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
667 self.0.visit_with(visitor)?;
668 self.1.visit_with(visitor)
672 impl<'tcx, A: TypeFoldable<'tcx>, B: TypeFoldable<'tcx>, C: TypeFoldable<'tcx>> TypeFoldable<'tcx>
675 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> (A, B, C) {
676 (self.0.fold_with(folder), self.1.fold_with(folder), self.2.fold_with(folder))
679 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
680 self.0.visit_with(visitor)?;
681 self.1.visit_with(visitor)?;
682 self.2.visit_with(visitor)
686 EnumTypeFoldableImpl! {
687 impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
690 } where T: TypeFoldable<'tcx>
693 EnumTypeFoldableImpl! {
694 impl<'tcx, T, E> TypeFoldable<'tcx> for Result<T, E> {
697 } where T: TypeFoldable<'tcx>, E: TypeFoldable<'tcx>,
700 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
701 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
702 // FIXME: Reuse the `Rc` here.
703 Rc::new((*self).clone().fold_with(folder))
706 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
707 (**self).visit_with(visitor)
711 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Arc<T> {
712 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
713 // FIXME: Reuse the `Arc` here.
714 Arc::new((*self).clone().fold_with(folder))
717 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
718 (**self).visit_with(visitor)
722 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
723 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
724 self.map_id(|value| value.fold_with(folder))
727 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
728 (**self).visit_with(visitor)
732 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
733 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
734 self.map_id(|t| t.fold_with(folder))
737 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
738 self.iter().try_for_each(|t| t.visit_with(visitor))
742 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<[T]> {
743 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
744 self.map_id(|t| t.fold_with(folder))
747 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
748 self.iter().try_for_each(|t| t.visit_with(visitor))
752 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<T> {
753 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
754 self.map_bound(|ty| ty.fold_with(folder))
757 fn fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
758 folder.fold_binder(self)
761 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
762 self.as_ref().skip_binder().visit_with(visitor)
765 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
766 visitor.visit_binder(self)
770 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Binder<ty::ExistentialPredicate<'tcx>>> {
771 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
772 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_poly_existential_predicates(v))
775 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
776 self.iter().try_for_each(|p| p.visit_with(visitor))
780 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<Ty<'tcx>> {
781 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
782 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_type_list(v))
785 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
786 self.iter().try_for_each(|t| t.visit_with(visitor))
790 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind> {
791 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
792 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_projs(v))
795 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
796 self.iter().try_for_each(|t| t.visit_with(visitor))
800 impl<'tcx> TypeFoldable<'tcx> for ty::instance::Instance<'tcx> {
801 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
802 use crate::ty::InstanceDef::*;
804 substs: self.substs.fold_with(folder),
805 def: match self.def {
806 Item(def) => Item(def.fold_with(folder)),
807 VtableShim(did) => VtableShim(did.fold_with(folder)),
808 ReifyShim(did) => ReifyShim(did.fold_with(folder)),
809 Intrinsic(did) => Intrinsic(did.fold_with(folder)),
810 FnPtrShim(did, ty) => FnPtrShim(did.fold_with(folder), ty.fold_with(folder)),
811 Virtual(did, i) => Virtual(did.fold_with(folder), i),
812 ClosureOnceShim { call_once } => {
813 ClosureOnceShim { call_once: call_once.fold_with(folder) }
815 DropGlue(did, ty) => DropGlue(did.fold_with(folder), ty.fold_with(folder)),
816 CloneShim(did, ty) => CloneShim(did.fold_with(folder), ty.fold_with(folder)),
821 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
822 use crate::ty::InstanceDef::*;
823 self.substs.visit_with(visitor)?;
825 Item(def) => def.visit_with(visitor),
826 VtableShim(did) | ReifyShim(did) | Intrinsic(did) | Virtual(did, _) => {
827 did.visit_with(visitor)
829 FnPtrShim(did, ty) | CloneShim(did, ty) => {
830 did.visit_with(visitor)?;
831 ty.visit_with(visitor)
833 DropGlue(did, ty) => {
834 did.visit_with(visitor)?;
835 ty.visit_with(visitor)
837 ClosureOnceShim { call_once } => call_once.visit_with(visitor),
842 impl<'tcx> TypeFoldable<'tcx> for interpret::GlobalId<'tcx> {
843 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
844 Self { instance: self.instance.fold_with(folder), promoted: self.promoted }
847 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
848 self.instance.visit_with(visitor)
852 impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
853 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
854 let kind = match *self.kind() {
855 ty::RawPtr(tm) => ty::RawPtr(tm.fold_with(folder)),
856 ty::Array(typ, sz) => ty::Array(typ.fold_with(folder), sz.fold_with(folder)),
857 ty::Slice(typ) => ty::Slice(typ.fold_with(folder)),
858 ty::Adt(tid, substs) => ty::Adt(tid, substs.fold_with(folder)),
859 ty::Dynamic(trait_ty, region) => {
860 ty::Dynamic(trait_ty.fold_with(folder), region.fold_with(folder))
862 ty::Tuple(ts) => ty::Tuple(ts.fold_with(folder)),
863 ty::FnDef(def_id, substs) => ty::FnDef(def_id, substs.fold_with(folder)),
864 ty::FnPtr(f) => ty::FnPtr(f.fold_with(folder)),
865 ty::Ref(r, ty, mutbl) => ty::Ref(r.fold_with(folder), ty.fold_with(folder), mutbl),
866 ty::Generator(did, substs, movability) => {
867 ty::Generator(did, substs.fold_with(folder), movability)
869 ty::GeneratorWitness(types) => ty::GeneratorWitness(types.fold_with(folder)),
870 ty::Closure(did, substs) => ty::Closure(did, substs.fold_with(folder)),
871 ty::Projection(data) => ty::Projection(data.fold_with(folder)),
872 ty::Opaque(did, substs) => ty::Opaque(did, substs.fold_with(folder)),
884 | ty::Placeholder(..)
886 | ty::Foreign(..) => return self,
889 if *self.kind() == kind { self } else { folder.tcx().mk_ty(kind) }
892 fn fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
896 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
898 ty::RawPtr(ref tm) => tm.visit_with(visitor),
899 ty::Array(typ, sz) => {
900 typ.visit_with(visitor)?;
901 sz.visit_with(visitor)
903 ty::Slice(typ) => typ.visit_with(visitor),
904 ty::Adt(_, substs) => substs.visit_with(visitor),
905 ty::Dynamic(ref trait_ty, ref reg) => {
906 trait_ty.visit_with(visitor)?;
907 reg.visit_with(visitor)
909 ty::Tuple(ts) => ts.visit_with(visitor),
910 ty::FnDef(_, substs) => substs.visit_with(visitor),
911 ty::FnPtr(ref f) => f.visit_with(visitor),
912 ty::Ref(r, ty, _) => {
913 r.visit_with(visitor)?;
914 ty.visit_with(visitor)
916 ty::Generator(_did, ref substs, _) => substs.visit_with(visitor),
917 ty::GeneratorWitness(ref types) => types.visit_with(visitor),
918 ty::Closure(_did, ref substs) => substs.visit_with(visitor),
919 ty::Projection(ref data) => data.visit_with(visitor),
920 ty::Opaque(_, ref substs) => substs.visit_with(visitor),
931 | ty::Placeholder(..)
934 | ty::Foreign(..) => ControlFlow::CONTINUE,
938 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
939 visitor.visit_ty(self)
943 impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> {
944 fn super_fold_with<F: TypeFolder<'tcx>>(self, _folder: &mut F) -> Self {
948 fn fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
949 folder.fold_region(self)
952 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
953 ControlFlow::CONTINUE
956 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
957 visitor.visit_region(*self)
961 impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
962 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
963 let new = self.inner.kind.fold_with(folder);
964 folder.tcx().reuse_or_mk_predicate(self, new)
967 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
968 self.inner.kind.visit_with(visitor)
971 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
972 visitor.visit_predicate(*self)
975 fn has_vars_bound_at_or_above(&self, binder: ty::DebruijnIndex) -> bool {
976 self.inner.outer_exclusive_binder > binder
979 fn has_type_flags(&self, flags: ty::TypeFlags) -> bool {
980 self.inner.flags.intersects(flags)
984 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
985 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
986 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_predicates(v))
989 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
990 self.iter().try_for_each(|p| p.visit_with(visitor))
994 impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
995 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
996 self.map_id(|x| x.fold_with(folder))
999 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1000 self.iter().try_for_each(|t| t.visit_with(visitor))
1004 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Const<'tcx> {
1005 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
1006 let ty = self.ty.fold_with(folder);
1007 let val = self.val.fold_with(folder);
1008 if ty != self.ty || val != self.val {
1009 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) -> ControlFlow<V::BreakTy> {
1020 self.ty.visit_with(visitor)?;
1021 self.val.visit_with(visitor)
1024 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1025 visitor.visit_const(self)
1029 impl<'tcx> TypeFoldable<'tcx> for ty::ConstKind<'tcx> {
1030 fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
1032 ty::ConstKind::Infer(ic) => ty::ConstKind::Infer(ic.fold_with(folder)),
1033 ty::ConstKind::Param(p) => ty::ConstKind::Param(p.fold_with(folder)),
1034 ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted }) => {
1035 ty::ConstKind::Unevaluated(ty::Unevaluated {
1037 substs: substs.fold_with(folder),
1041 ty::ConstKind::Value(_)
1042 | ty::ConstKind::Bound(..)
1043 | ty::ConstKind::Placeholder(..)
1044 | ty::ConstKind::Error(_) => self,
1048 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1050 ty::ConstKind::Infer(ic) => ic.visit_with(visitor),
1051 ty::ConstKind::Param(p) => p.visit_with(visitor),
1052 ty::ConstKind::Unevaluated(ct) => ct.substs.visit_with(visitor),
1053 ty::ConstKind::Value(_)
1054 | ty::ConstKind::Bound(..)
1055 | ty::ConstKind::Placeholder(_)
1056 | ty::ConstKind::Error(_) => ControlFlow::CONTINUE,
1061 impl<'tcx> TypeFoldable<'tcx> for InferConst<'tcx> {
1062 fn super_fold_with<F: TypeFolder<'tcx>>(self, _folder: &mut F) -> Self {
1066 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
1067 ControlFlow::CONTINUE