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::{FallibleTypeFolder, TypeFoldable, TypeVisitor};
8 use crate::ty::print::{with_no_trimmed_paths, FmtPrinter, Printer};
9 use crate::ty::{self, InferConst, Lift, Term, 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::mem::ManuallyDrop;
18 use std::ops::ControlFlow;
22 impl fmt::Debug for ty::TraitDef {
23 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25 with_no_trimmed_paths(|| {
26 FmtPrinter::new(tcx, f, Namespace::TypeNS).print_def_path(self.def_id, &[])
33 impl fmt::Debug for ty::AdtDef {
34 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36 with_no_trimmed_paths(|| {
37 FmtPrinter::new(tcx, f, Namespace::TypeNS).print_def_path(self.did, &[])
44 impl fmt::Debug for ty::UpvarId {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 let name = ty::tls::with(|tcx| tcx.hir().name(self.var_path.hir_id));
47 write!(f, "UpvarId({:?};`{}`;{:?})", self.var_path.hir_id, name, self.closure_expr_id)
51 impl<'tcx> fmt::Debug for ty::ExistentialTraitRef<'tcx> {
52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53 with_no_trimmed_paths(|| fmt::Display::fmt(self, f))
57 impl<'tcx> fmt::Debug for ty::adjustment::Adjustment<'tcx> {
58 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59 write!(f, "{:?} -> {}", self.kind, self.target)
63 impl fmt::Debug for ty::BoundRegionKind {
64 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66 ty::BrAnon(n) => write!(f, "BrAnon({:?})", n),
67 ty::BrNamed(did, name) => {
68 if did.index == CRATE_DEF_INDEX {
69 write!(f, "BrNamed({})", name)
71 write!(f, "BrNamed({:?}, {})", did, name)
74 ty::BrEnv => write!(f, "BrEnv"),
79 impl fmt::Debug for ty::RegionKind {
80 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82 ty::ReEarlyBound(ref data) => write!(f, "ReEarlyBound({}, {})", data.index, data.name),
84 ty::ReLateBound(binder_id, ref bound_region) => {
85 write!(f, "ReLateBound({:?}, {:?})", binder_id, bound_region)
88 ty::ReFree(ref fr) => fr.fmt(f),
90 ty::ReStatic => write!(f, "ReStatic"),
92 ty::ReVar(ref vid) => vid.fmt(f),
94 ty::RePlaceholder(placeholder) => write!(f, "RePlaceholder({:?})", placeholder),
96 ty::ReEmpty(ui) => write!(f, "ReEmpty({:?})", ui),
98 ty::ReErased => write!(f, "ReErased"),
103 impl fmt::Debug for ty::FreeRegion {
104 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
105 write!(f, "ReFree({:?}, {:?})", self.scope, self.bound_region)
109 impl<'tcx> fmt::Debug for ty::FnSig<'tcx> {
110 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
111 write!(f, "({:?}; c_variadic: {})->{:?}", self.inputs(), self.c_variadic, self.output())
115 impl<'tcx> fmt::Debug for ty::ConstVid<'tcx> {
116 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117 write!(f, "_#{}c", self.index)
121 impl fmt::Debug for ty::RegionVid {
122 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
123 write!(f, "'_#{}r", self.index())
127 impl<'tcx> fmt::Debug for ty::TraitRef<'tcx> {
128 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
129 with_no_trimmed_paths(|| fmt::Display::fmt(self, f))
133 impl<'tcx> fmt::Debug for Ty<'tcx> {
134 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
135 with_no_trimmed_paths(|| fmt::Display::fmt(self, f))
139 impl fmt::Debug for ty::ParamTy {
140 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
141 write!(f, "{}/#{}", self.name, self.index)
145 impl fmt::Debug for ty::ParamConst {
146 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
147 write!(f, "{}/#{}", self.name, self.index)
151 impl<'tcx> fmt::Debug for ty::TraitPredicate<'tcx> {
152 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
153 if let ty::BoundConstness::ConstIfConst = self.constness {
154 write!(f, "~const ")?;
156 write!(f, "TraitPredicate({:?}, polarity:{:?})", self.trait_ref, self.polarity)
160 impl<'tcx> fmt::Debug for ty::ProjectionPredicate<'tcx> {
161 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
162 write!(f, "ProjectionPredicate({:?}, {:?})", self.projection_ty, self.term)
166 impl<'tcx> fmt::Debug for ty::Predicate<'tcx> {
167 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
168 write!(f, "{:?}", self.kind())
172 impl<'tcx> fmt::Debug for ty::PredicateKind<'tcx> {
173 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
175 ty::PredicateKind::Trait(ref a) => a.fmt(f),
176 ty::PredicateKind::Subtype(ref pair) => pair.fmt(f),
177 ty::PredicateKind::Coerce(ref pair) => pair.fmt(f),
178 ty::PredicateKind::RegionOutlives(ref pair) => pair.fmt(f),
179 ty::PredicateKind::TypeOutlives(ref pair) => pair.fmt(f),
180 ty::PredicateKind::Projection(ref pair) => pair.fmt(f),
181 ty::PredicateKind::WellFormed(data) => write!(f, "WellFormed({:?})", data),
182 ty::PredicateKind::ObjectSafe(trait_def_id) => {
183 write!(f, "ObjectSafe({:?})", trait_def_id)
185 ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => {
186 write!(f, "ClosureKind({:?}, {:?}, {:?})", closure_def_id, closure_substs, kind)
188 ty::PredicateKind::ConstEvaluatable(uv) => {
189 write!(f, "ConstEvaluatable({:?}, {:?})", uv.def, uv.substs)
191 ty::PredicateKind::ConstEquate(c1, c2) => write!(f, "ConstEquate({:?}, {:?})", c1, c2),
192 ty::PredicateKind::TypeWellFormedFromEnv(ty) => {
193 write!(f, "TypeWellFormedFromEnv({:?})", ty)
199 ///////////////////////////////////////////////////////////////////////////
202 // For things that don't carry any arena-allocated data (and are
203 // copy...), just add them to this list.
205 TrivialTypeFoldableAndLiftImpls! {
209 ::rustc_target::abi::VariantIdx,
213 crate::middle::region::Scope,
215 ::rustc_ast::InlineAsmOptions,
216 ::rustc_ast::InlineAsmTemplatePiece,
218 ::rustc_span::symbol::Symbol,
219 ::rustc_hir::def::Res,
220 ::rustc_hir::def_id::DefId,
221 ::rustc_hir::def_id::LocalDefId,
223 ::rustc_hir::MatchSource,
224 ::rustc_hir::Mutability,
225 ::rustc_hir::Unsafety,
226 ::rustc_target::asm::InlineAsmRegOrRegClass,
227 ::rustc_target::spec::abi::Abi,
228 crate::mir::coverage::ExpressionOperandId,
229 crate::mir::coverage::CounterValueReference,
230 crate::mir::coverage::InjectedExpressionId,
231 crate::mir::coverage::InjectedExpressionIndex,
232 crate::mir::coverage::MappedExpressionIndex,
234 crate::mir::Promoted,
235 crate::traits::Reveal,
236 crate::ty::adjustment::AutoBorrowMutability,
238 crate::ty::BoundConstness,
239 // Including `BoundRegionKind` is a *bit* dubious, but direct
240 // references to bound region appear in `ty::Error`, and aren't
241 // really meant to be folded. In general, we can only fold a fully
243 crate::ty::BoundRegionKind,
244 crate::ty::AssocItem,
245 crate::ty::Placeholder<crate::ty::BoundRegionKind>,
246 crate::ty::ClosureKind,
247 crate::ty::FreeRegion,
249 crate::ty::IntVarValue,
250 crate::ty::ParamConst,
252 crate::ty::adjustment::PointerCast,
253 crate::ty::RegionVid,
254 crate::ty::UniverseIndex,
257 ::rustc_errors::ErrorReported,
260 ///////////////////////////////////////////////////////////////////////////
261 // Lift implementations
263 // FIXME(eddyb) replace all the uses of `Option::map` with `?`.
264 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>> Lift<'tcx> for (A, B) {
265 type Lifted = (A::Lifted, B::Lifted);
266 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
267 Some((tcx.lift(self.0)?, tcx.lift(self.1)?))
271 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>, C: Lift<'tcx>> Lift<'tcx> for (A, B, C) {
272 type Lifted = (A::Lifted, B::Lifted, C::Lifted);
273 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
274 Some((tcx.lift(self.0)?, tcx.lift(self.1)?, tcx.lift(self.2)?))
278 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Option<T> {
279 type Lifted = Option<T::Lifted>;
280 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
282 Some(x) => tcx.lift(x).map(Some),
288 impl<'tcx, T: Lift<'tcx>, E: Lift<'tcx>> Lift<'tcx> for Result<T, E> {
289 type Lifted = Result<T::Lifted, E::Lifted>;
290 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
292 Ok(x) => tcx.lift(x).map(Ok),
293 Err(e) => tcx.lift(e).map(Err),
298 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Box<T> {
299 type Lifted = Box<T::Lifted>;
300 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
301 tcx.lift(*self).map(Box::new)
305 impl<'tcx, T: Lift<'tcx> + Clone> Lift<'tcx> for Rc<T> {
306 type Lifted = Rc<T::Lifted>;
307 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
308 tcx.lift(self.as_ref().clone()).map(Rc::new)
312 impl<'tcx, T: Lift<'tcx> + Clone> Lift<'tcx> for Arc<T> {
313 type Lifted = Arc<T::Lifted>;
314 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
315 tcx.lift(self.as_ref().clone()).map(Arc::new)
318 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Vec<T> {
319 type Lifted = Vec<T::Lifted>;
320 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
321 self.into_iter().map(|v| tcx.lift(v)).collect()
325 impl<'tcx, I: Idx, T: Lift<'tcx>> Lift<'tcx> for IndexVec<I, T> {
326 type Lifted = IndexVec<I, T::Lifted>;
327 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
328 self.into_iter().map(|e| tcx.lift(e)).collect()
332 impl<'a, 'tcx> Lift<'tcx> for ty::TraitRef<'a> {
333 type Lifted = ty::TraitRef<'tcx>;
334 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
335 tcx.lift(self.substs).map(|substs| ty::TraitRef { def_id: self.def_id, substs })
339 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialTraitRef<'a> {
340 type Lifted = ty::ExistentialTraitRef<'tcx>;
341 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
342 tcx.lift(self.substs).map(|substs| ty::ExistentialTraitRef { def_id: self.def_id, substs })
346 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialPredicate<'a> {
347 type Lifted = ty::ExistentialPredicate<'tcx>;
348 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
350 ty::ExistentialPredicate::Trait(x) => tcx.lift(x).map(ty::ExistentialPredicate::Trait),
351 ty::ExistentialPredicate::Projection(x) => {
352 tcx.lift(x).map(ty::ExistentialPredicate::Projection)
354 ty::ExistentialPredicate::AutoTrait(def_id) => {
355 Some(ty::ExistentialPredicate::AutoTrait(def_id))
361 impl<'a, 'tcx> Lift<'tcx> for Term<'a> {
362 type Lifted = ty::Term<'tcx>;
363 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
365 Term::Ty(ty) => Term::Ty(tcx.lift(ty)?),
366 Term::Const(c) => Term::Const(tcx.lift(c)?),
371 impl<'a, 'tcx> Lift<'tcx> for ty::TraitPredicate<'a> {
372 type Lifted = ty::TraitPredicate<'tcx>;
373 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::TraitPredicate<'tcx>> {
374 tcx.lift(self.trait_ref).map(|trait_ref| ty::TraitPredicate {
376 constness: self.constness,
377 polarity: self.polarity,
382 impl<'a, 'tcx> Lift<'tcx> for ty::SubtypePredicate<'a> {
383 type Lifted = ty::SubtypePredicate<'tcx>;
384 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::SubtypePredicate<'tcx>> {
385 tcx.lift((self.a, self.b)).map(|(a, b)| ty::SubtypePredicate {
386 a_is_expected: self.a_is_expected,
393 impl<'a, 'tcx> Lift<'tcx> for ty::CoercePredicate<'a> {
394 type Lifted = ty::CoercePredicate<'tcx>;
395 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::CoercePredicate<'tcx>> {
396 tcx.lift((self.a, self.b)).map(|(a, b)| ty::CoercePredicate { a, b })
400 impl<'tcx, A: Copy + Lift<'tcx>, B: Copy + Lift<'tcx>> Lift<'tcx> for ty::OutlivesPredicate<A, B> {
401 type Lifted = ty::OutlivesPredicate<A::Lifted, B::Lifted>;
402 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
403 tcx.lift((self.0, self.1)).map(|(a, b)| ty::OutlivesPredicate(a, b))
407 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionTy<'a> {
408 type Lifted = ty::ProjectionTy<'tcx>;
409 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::ProjectionTy<'tcx>> {
410 tcx.lift(self.substs)
411 .map(|substs| ty::ProjectionTy { item_def_id: self.item_def_id, substs })
415 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionPredicate<'a> {
416 type Lifted = ty::ProjectionPredicate<'tcx>;
417 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::ProjectionPredicate<'tcx>> {
418 tcx.lift((self.projection_ty, self.term))
419 .map(|(projection_ty, term)| ty::ProjectionPredicate { projection_ty, term })
423 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialProjection<'a> {
424 type Lifted = ty::ExistentialProjection<'tcx>;
425 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
426 tcx.lift(self.substs).map(|substs| ty::ExistentialProjection {
428 term: tcx.lift(self.term).expect("type must lift when substs do"),
429 item_def_id: self.item_def_id,
434 impl<'a, 'tcx> Lift<'tcx> for ty::PredicateKind<'a> {
435 type Lifted = ty::PredicateKind<'tcx>;
436 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
438 ty::PredicateKind::Trait(data) => tcx.lift(data).map(ty::PredicateKind::Trait),
439 ty::PredicateKind::Subtype(data) => tcx.lift(data).map(ty::PredicateKind::Subtype),
440 ty::PredicateKind::Coerce(data) => tcx.lift(data).map(ty::PredicateKind::Coerce),
441 ty::PredicateKind::RegionOutlives(data) => {
442 tcx.lift(data).map(ty::PredicateKind::RegionOutlives)
444 ty::PredicateKind::TypeOutlives(data) => {
445 tcx.lift(data).map(ty::PredicateKind::TypeOutlives)
447 ty::PredicateKind::Projection(data) => {
448 tcx.lift(data).map(ty::PredicateKind::Projection)
450 ty::PredicateKind::WellFormed(ty) => tcx.lift(ty).map(ty::PredicateKind::WellFormed),
451 ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => {
452 tcx.lift(closure_substs).map(|closure_substs| {
453 ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind)
456 ty::PredicateKind::ObjectSafe(trait_def_id) => {
457 Some(ty::PredicateKind::ObjectSafe(trait_def_id))
459 ty::PredicateKind::ConstEvaluatable(uv) => {
460 tcx.lift(uv).map(|uv| ty::PredicateKind::ConstEvaluatable(uv))
462 ty::PredicateKind::ConstEquate(c1, c2) => {
463 tcx.lift((c1, c2)).map(|(c1, c2)| ty::PredicateKind::ConstEquate(c1, c2))
465 ty::PredicateKind::TypeWellFormedFromEnv(ty) => {
466 tcx.lift(ty).map(ty::PredicateKind::TypeWellFormedFromEnv)
472 impl<'a, 'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::Binder<'a, T>
474 <T as Lift<'tcx>>::Lifted: TypeFoldable<'tcx>,
476 type Lifted = ty::Binder<'tcx, T::Lifted>;
477 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
478 let bound_vars = tcx.lift(self.bound_vars());
479 tcx.lift(self.skip_binder())
481 .map(|(value, vars)| ty::Binder::bind_with_vars(value, vars))
485 impl<'a, 'tcx> Lift<'tcx> for ty::ParamEnv<'a> {
486 type Lifted = ty::ParamEnv<'tcx>;
487 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
488 tcx.lift(self.caller_bounds())
489 .map(|caller_bounds| ty::ParamEnv::new(caller_bounds, self.reveal(), self.constness()))
493 impl<'a, 'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::ParamEnvAnd<'a, T> {
494 type Lifted = ty::ParamEnvAnd<'tcx, T::Lifted>;
495 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
496 tcx.lift(self.param_env).and_then(|param_env| {
497 tcx.lift(self.value).map(|value| ty::ParamEnvAnd { param_env, value })
502 impl<'a, 'tcx> Lift<'tcx> for ty::ClosureSubsts<'a> {
503 type Lifted = ty::ClosureSubsts<'tcx>;
504 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
505 tcx.lift(self.substs).map(|substs| ty::ClosureSubsts { substs })
509 impl<'a, 'tcx> Lift<'tcx> for ty::GeneratorSubsts<'a> {
510 type Lifted = ty::GeneratorSubsts<'tcx>;
511 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
512 tcx.lift(self.substs).map(|substs| ty::GeneratorSubsts { substs })
516 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjustment<'a> {
517 type Lifted = ty::adjustment::Adjustment<'tcx>;
518 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
519 let ty::adjustment::Adjustment { kind, target } = self;
520 tcx.lift(kind).and_then(|kind| {
521 tcx.lift(target).map(|target| ty::adjustment::Adjustment { kind, target })
526 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjust<'a> {
527 type Lifted = ty::adjustment::Adjust<'tcx>;
528 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
530 ty::adjustment::Adjust::NeverToAny => Some(ty::adjustment::Adjust::NeverToAny),
531 ty::adjustment::Adjust::Pointer(ptr) => Some(ty::adjustment::Adjust::Pointer(ptr)),
532 ty::adjustment::Adjust::Deref(overloaded) => {
533 tcx.lift(overloaded).map(ty::adjustment::Adjust::Deref)
535 ty::adjustment::Adjust::Borrow(autoref) => {
536 tcx.lift(autoref).map(ty::adjustment::Adjust::Borrow)
542 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::OverloadedDeref<'a> {
543 type Lifted = ty::adjustment::OverloadedDeref<'tcx>;
544 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
545 tcx.lift(self.region).map(|region| ty::adjustment::OverloadedDeref {
553 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::AutoBorrow<'a> {
554 type Lifted = ty::adjustment::AutoBorrow<'tcx>;
555 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
557 ty::adjustment::AutoBorrow::Ref(r, m) => {
558 tcx.lift(r).map(|r| ty::adjustment::AutoBorrow::Ref(r, m))
560 ty::adjustment::AutoBorrow::RawPtr(m) => Some(ty::adjustment::AutoBorrow::RawPtr(m)),
565 impl<'a, 'tcx> Lift<'tcx> for ty::GenSig<'a> {
566 type Lifted = ty::GenSig<'tcx>;
567 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
568 tcx.lift((self.resume_ty, self.yield_ty, self.return_ty))
569 .map(|(resume_ty, yield_ty, return_ty)| ty::GenSig { resume_ty, yield_ty, return_ty })
573 impl<'a, 'tcx> Lift<'tcx> for ty::FnSig<'a> {
574 type Lifted = ty::FnSig<'tcx>;
575 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
576 tcx.lift(self.inputs_and_output).map(|x| ty::FnSig {
577 inputs_and_output: x,
578 c_variadic: self.c_variadic,
579 unsafety: self.unsafety,
585 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::error::ExpectedFound<T> {
586 type Lifted = ty::error::ExpectedFound<T::Lifted>;
587 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
588 let ty::error::ExpectedFound { expected, found } = self;
589 tcx.lift(expected).and_then(|expected| {
590 tcx.lift(found).map(|found| ty::error::ExpectedFound { expected, found })
595 impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> {
596 type Lifted = ty::error::TypeError<'tcx>;
597 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
598 use crate::ty::error::TypeError::*;
601 Mismatch => Mismatch,
602 ConstnessMismatch(x) => ConstnessMismatch(x),
603 PolarityMismatch(x) => PolarityMismatch(x),
604 UnsafetyMismatch(x) => UnsafetyMismatch(x),
605 AbiMismatch(x) => AbiMismatch(x),
606 Mutability => Mutability,
607 ArgumentMutability(i) => ArgumentMutability(i),
608 TupleSize(x) => TupleSize(x),
609 FixedArraySize(x) => FixedArraySize(x),
610 ArgCount => ArgCount,
611 FieldMisMatch(x, y) => FieldMisMatch(x, y),
612 RegionsDoesNotOutlive(a, b) => {
613 return tcx.lift((a, b)).map(|(a, b)| RegionsDoesNotOutlive(a, b));
615 RegionsInsufficientlyPolymorphic(a, b) => {
616 return tcx.lift(b).map(|b| RegionsInsufficientlyPolymorphic(a, b));
618 RegionsOverlyPolymorphic(a, b) => {
619 return tcx.lift(b).map(|b| RegionsOverlyPolymorphic(a, b));
621 RegionsPlaceholderMismatch => RegionsPlaceholderMismatch,
622 IntMismatch(x) => IntMismatch(x),
623 FloatMismatch(x) => FloatMismatch(x),
624 Traits(x) => Traits(x),
625 VariadicMismatch(x) => VariadicMismatch(x),
626 CyclicTy(t) => return tcx.lift(t).map(|t| CyclicTy(t)),
627 CyclicConst(ct) => return tcx.lift(ct).map(|ct| CyclicConst(ct)),
628 ProjectionMismatched(x) => ProjectionMismatched(x),
629 ArgumentSorts(x, i) => return tcx.lift(x).map(|x| ArgumentSorts(x, i)),
630 Sorts(x) => return tcx.lift(x).map(Sorts),
631 ExistentialMismatch(x) => return tcx.lift(x).map(ExistentialMismatch),
632 ConstMismatch(x) => return tcx.lift(x).map(ConstMismatch),
633 IntrinsicCast => IntrinsicCast,
634 TargetFeatureCast(x) => TargetFeatureCast(x),
635 ObjectUnsafeCoercion(x) => return tcx.lift(x).map(ObjectUnsafeCoercion),
640 impl<'a, 'tcx> Lift<'tcx> for ty::InstanceDef<'a> {
641 type Lifted = ty::InstanceDef<'tcx>;
642 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
644 ty::InstanceDef::Item(def_id) => Some(ty::InstanceDef::Item(def_id)),
645 ty::InstanceDef::VtableShim(def_id) => Some(ty::InstanceDef::VtableShim(def_id)),
646 ty::InstanceDef::ReifyShim(def_id) => Some(ty::InstanceDef::ReifyShim(def_id)),
647 ty::InstanceDef::Intrinsic(def_id) => Some(ty::InstanceDef::Intrinsic(def_id)),
648 ty::InstanceDef::FnPtrShim(def_id, ty) => {
649 Some(ty::InstanceDef::FnPtrShim(def_id, tcx.lift(ty)?))
651 ty::InstanceDef::Virtual(def_id, n) => Some(ty::InstanceDef::Virtual(def_id, n)),
652 ty::InstanceDef::ClosureOnceShim { call_once, track_caller } => {
653 Some(ty::InstanceDef::ClosureOnceShim { call_once, track_caller })
655 ty::InstanceDef::DropGlue(def_id, ty) => {
656 Some(ty::InstanceDef::DropGlue(def_id, tcx.lift(ty)?))
658 ty::InstanceDef::CloneShim(def_id, ty) => {
659 Some(ty::InstanceDef::CloneShim(def_id, tcx.lift(ty)?))
665 ///////////////////////////////////////////////////////////////////////////
666 // TypeFoldable implementations.
668 /// AdtDefs are basically the same as a DefId.
669 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::AdtDef {
670 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
673 ) -> Result<Self, F::Error> {
677 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
678 ControlFlow::CONTINUE
682 impl<'tcx, T: TypeFoldable<'tcx>, U: TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
683 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
686 ) -> Result<(T, U), F::Error> {
687 Ok((self.0.try_fold_with(folder)?, self.1.try_fold_with(folder)?))
690 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
691 self.0.visit_with(visitor)?;
692 self.1.visit_with(visitor)
696 impl<'tcx, A: TypeFoldable<'tcx>, B: TypeFoldable<'tcx>, C: TypeFoldable<'tcx>> TypeFoldable<'tcx>
699 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
702 ) -> Result<(A, B, C), F::Error> {
704 self.0.try_fold_with(folder)?,
705 self.1.try_fold_with(folder)?,
706 self.2.try_fold_with(folder)?,
710 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
711 self.0.visit_with(visitor)?;
712 self.1.visit_with(visitor)?;
713 self.2.visit_with(visitor)
717 EnumTypeFoldableImpl! {
718 impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
721 } where T: TypeFoldable<'tcx>
724 EnumTypeFoldableImpl! {
725 impl<'tcx, T, E> TypeFoldable<'tcx> for Result<T, E> {
728 } where T: TypeFoldable<'tcx>, E: TypeFoldable<'tcx>,
731 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
732 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
735 ) -> Result<Self, F::Error> {
736 // We merely want to replace the contained `T`, if at all possible,
737 // so that we don't needlessly allocate a new `Rc` or indeed clone
738 // the contained type.
740 // First step is to ensure that we have a unique reference to
741 // the contained type, which `Rc::make_mut` will accomplish (by
742 // allocating a new `Rc` and cloning the `T` only if required).
743 // This is done *before* casting to `Rc<ManuallyDrop<T>>` so that
744 // panicking during `make_mut` does not leak the `T`.
745 Rc::make_mut(&mut self);
747 // Casting to `Rc<ManuallyDrop<T>>` is safe because `ManuallyDrop`
748 // is `repr(transparent)`.
749 let ptr = Rc::into_raw(self).cast::<ManuallyDrop<T>>();
750 let mut unique = Rc::from_raw(ptr);
752 // Call to `Rc::make_mut` above guarantees that `unique` is the
753 // sole reference to the contained value, so we can avoid doing
754 // a checked `get_mut` here.
755 let slot = Rc::get_mut_unchecked(&mut unique);
757 // Semantically move the contained type out from `unique`, fold
758 // it, then move the folded value back into `unique`. Should
759 // folding fail, `ManuallyDrop` ensures that the "moved-out"
760 // value is not re-dropped.
761 let owned = ManuallyDrop::take(slot);
762 let folded = owned.try_fold_with(folder)?;
763 *slot = ManuallyDrop::new(folded);
765 // Cast back to `Rc<T>`.
766 Ok(Rc::from_raw(Rc::into_raw(unique).cast()))
770 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
771 (**self).visit_with(visitor)
775 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Arc<T> {
776 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
779 ) -> Result<Self, F::Error> {
780 // We merely want to replace the contained `T`, if at all possible,
781 // so that we don't needlessly allocate a new `Arc` or indeed clone
782 // the contained type.
784 // First step is to ensure that we have a unique reference to
785 // the contained type, which `Arc::make_mut` will accomplish (by
786 // allocating a new `Arc` and cloning the `T` only if required).
787 // This is done *before* casting to `Arc<ManuallyDrop<T>>` so that
788 // panicking during `make_mut` does not leak the `T`.
789 Arc::make_mut(&mut self);
791 // Casting to `Arc<ManuallyDrop<T>>` is safe because `ManuallyDrop`
792 // is `repr(transparent)`.
793 let ptr = Arc::into_raw(self).cast::<ManuallyDrop<T>>();
794 let mut unique = Arc::from_raw(ptr);
796 // Call to `Arc::make_mut` above guarantees that `unique` is the
797 // sole reference to the contained value, so we can avoid doing
798 // a checked `get_mut` here.
799 let slot = Arc::get_mut_unchecked(&mut unique);
801 // Semantically move the contained type out from `unique`, fold
802 // it, then move the folded value back into `unique`. Should
803 // folding fail, `ManuallyDrop` ensures that the "moved-out"
804 // value is not re-dropped.
805 let owned = ManuallyDrop::take(slot);
806 let folded = owned.try_fold_with(folder)?;
807 *slot = ManuallyDrop::new(folded);
809 // Cast back to `Arc<T>`.
810 Ok(Arc::from_raw(Arc::into_raw(unique).cast()))
814 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
815 (**self).visit_with(visitor)
819 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
820 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
823 ) -> Result<Self, F::Error> {
824 self.try_map_id(|value| value.try_fold_with(folder))
827 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
828 (**self).visit_with(visitor)
832 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
833 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
836 ) -> Result<Self, F::Error> {
837 self.try_map_id(|t| t.try_fold_with(folder))
840 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
841 self.iter().try_for_each(|t| t.visit_with(visitor))
845 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<[T]> {
846 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
849 ) -> Result<Self, F::Error> {
850 self.try_map_id(|t| t.try_fold_with(folder))
853 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
854 self.iter().try_for_each(|t| t.visit_with(visitor))
858 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<'tcx, T> {
859 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
862 ) -> Result<Self, F::Error> {
863 self.try_map_bound(|ty| ty.try_fold_with(folder))
866 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
867 folder.try_fold_binder(self)
870 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
871 self.as_ref().skip_binder().visit_with(visitor)
874 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
875 visitor.visit_binder(self)
879 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>> {
880 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
883 ) -> Result<Self, F::Error> {
884 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_poly_existential_predicates(v))
887 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
888 self.iter().try_for_each(|p| p.visit_with(visitor))
892 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<Ty<'tcx>> {
893 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
896 ) -> Result<Self, F::Error> {
897 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_type_list(v))
900 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
901 self.iter().try_for_each(|t| t.visit_with(visitor))
905 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind> {
906 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
909 ) -> Result<Self, F::Error> {
910 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_projs(v))
913 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
914 self.iter().try_for_each(|t| t.visit_with(visitor))
918 impl<'tcx> TypeFoldable<'tcx> for ty::instance::Instance<'tcx> {
919 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
922 ) -> Result<Self, F::Error> {
923 use crate::ty::InstanceDef::*;
925 substs: self.substs.try_fold_with(folder)?,
926 def: match self.def {
927 Item(def) => Item(def.try_fold_with(folder)?),
928 VtableShim(did) => VtableShim(did.try_fold_with(folder)?),
929 ReifyShim(did) => ReifyShim(did.try_fold_with(folder)?),
930 Intrinsic(did) => Intrinsic(did.try_fold_with(folder)?),
931 FnPtrShim(did, ty) => {
932 FnPtrShim(did.try_fold_with(folder)?, ty.try_fold_with(folder)?)
934 Virtual(did, i) => Virtual(did.try_fold_with(folder)?, i),
935 ClosureOnceShim { call_once, track_caller } => {
936 ClosureOnceShim { call_once: call_once.try_fold_with(folder)?, track_caller }
938 DropGlue(did, ty) => {
939 DropGlue(did.try_fold_with(folder)?, ty.try_fold_with(folder)?)
941 CloneShim(did, ty) => {
942 CloneShim(did.try_fold_with(folder)?, ty.try_fold_with(folder)?)
948 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
949 use crate::ty::InstanceDef::*;
950 self.substs.visit_with(visitor)?;
952 Item(def) => def.visit_with(visitor),
953 VtableShim(did) | ReifyShim(did) | Intrinsic(did) | Virtual(did, _) => {
954 did.visit_with(visitor)
956 FnPtrShim(did, ty) | CloneShim(did, ty) => {
957 did.visit_with(visitor)?;
958 ty.visit_with(visitor)
960 DropGlue(did, ty) => {
961 did.visit_with(visitor)?;
962 ty.visit_with(visitor)
964 ClosureOnceShim { call_once, track_caller: _ } => call_once.visit_with(visitor),
969 impl<'tcx> TypeFoldable<'tcx> for interpret::GlobalId<'tcx> {
970 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
973 ) -> Result<Self, F::Error> {
974 Ok(Self { instance: self.instance.try_fold_with(folder)?, promoted: self.promoted })
977 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
978 self.instance.visit_with(visitor)
982 impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
983 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
986 ) -> Result<Self, F::Error> {
987 let kind = match *self.kind() {
988 ty::RawPtr(tm) => ty::RawPtr(tm.try_fold_with(folder)?),
989 ty::Array(typ, sz) => ty::Array(typ.try_fold_with(folder)?, sz.try_fold_with(folder)?),
990 ty::Slice(typ) => ty::Slice(typ.try_fold_with(folder)?),
991 ty::Adt(tid, substs) => ty::Adt(tid, substs.try_fold_with(folder)?),
992 ty::Dynamic(trait_ty, region) => {
993 ty::Dynamic(trait_ty.try_fold_with(folder)?, region.try_fold_with(folder)?)
995 ty::Tuple(ts) => ty::Tuple(ts.try_fold_with(folder)?),
996 ty::FnDef(def_id, substs) => ty::FnDef(def_id, substs.try_fold_with(folder)?),
997 ty::FnPtr(f) => ty::FnPtr(f.try_fold_with(folder)?),
998 ty::Ref(r, ty, mutbl) => {
999 ty::Ref(r.try_fold_with(folder)?, ty.try_fold_with(folder)?, mutbl)
1001 ty::Generator(did, substs, movability) => {
1002 ty::Generator(did, substs.try_fold_with(folder)?, movability)
1004 ty::GeneratorWitness(types) => ty::GeneratorWitness(types.try_fold_with(folder)?),
1005 ty::Closure(did, substs) => ty::Closure(did, substs.try_fold_with(folder)?),
1006 ty::Projection(data) => ty::Projection(data.try_fold_with(folder)?),
1007 ty::Opaque(did, substs) => ty::Opaque(did, substs.try_fold_with(folder)?),
1019 | ty::Placeholder(..)
1021 | ty::Foreign(..) => return Ok(self),
1024 Ok(if *self.kind() == kind { self } else { folder.tcx().mk_ty(kind) })
1027 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
1028 folder.try_fold_ty(self)
1031 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1033 ty::RawPtr(ref tm) => tm.visit_with(visitor),
1034 ty::Array(typ, sz) => {
1035 typ.visit_with(visitor)?;
1036 sz.visit_with(visitor)
1038 ty::Slice(typ) => typ.visit_with(visitor),
1039 ty::Adt(_, substs) => substs.visit_with(visitor),
1040 ty::Dynamic(ref trait_ty, ref reg) => {
1041 trait_ty.visit_with(visitor)?;
1042 reg.visit_with(visitor)
1044 ty::Tuple(ts) => ts.visit_with(visitor),
1045 ty::FnDef(_, substs) => substs.visit_with(visitor),
1046 ty::FnPtr(ref f) => f.visit_with(visitor),
1047 ty::Ref(r, ty, _) => {
1048 r.visit_with(visitor)?;
1049 ty.visit_with(visitor)
1051 ty::Generator(_did, ref substs, _) => substs.visit_with(visitor),
1052 ty::GeneratorWitness(ref types) => types.visit_with(visitor),
1053 ty::Closure(_did, ref substs) => substs.visit_with(visitor),
1054 ty::Projection(ref data) => data.visit_with(visitor),
1055 ty::Opaque(_, ref substs) => substs.visit_with(visitor),
1066 | ty::Placeholder(..)
1069 | ty::Foreign(..) => ControlFlow::CONTINUE,
1073 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1074 visitor.visit_ty(*self)
1078 impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> {
1079 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1082 ) -> Result<Self, F::Error> {
1086 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
1087 folder.try_fold_region(self)
1090 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
1091 ControlFlow::CONTINUE
1094 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1095 visitor.visit_region(*self)
1099 impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
1100 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
1101 folder.try_fold_predicate(self)
1104 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1107 ) -> Result<Self, F::Error> {
1108 let new = self.kind().try_fold_with(folder)?;
1109 Ok(folder.tcx().reuse_or_mk_predicate(self, new))
1112 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1113 self.kind().visit_with(visitor)
1116 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1117 visitor.visit_predicate(*self)
1120 fn has_vars_bound_at_or_above(&self, binder: ty::DebruijnIndex) -> bool {
1121 self.outer_exclusive_binder() > binder
1124 fn has_type_flags(&self, flags: ty::TypeFlags) -> bool {
1125 self.flags().intersects(flags)
1129 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
1130 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1133 ) -> Result<Self, F::Error> {
1134 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_predicates(v))
1137 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1138 self.iter().try_for_each(|p| p.visit_with(visitor))
1142 impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
1143 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1146 ) -> Result<Self, F::Error> {
1147 self.try_map_id(|x| x.try_fold_with(folder))
1150 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1151 self.iter().try_for_each(|t| t.visit_with(visitor))
1155 impl<'tcx> TypeFoldable<'tcx> for ty::Const<'tcx> {
1156 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1159 ) -> Result<Self, F::Error> {
1160 let ty = self.ty().try_fold_with(folder)?;
1161 let val = self.val().try_fold_with(folder)?;
1162 if ty != self.ty() || val != self.val() {
1163 Ok(folder.tcx().mk_const(ty::ConstS { ty, val }))
1169 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
1170 folder.try_fold_const(self)
1173 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1174 self.ty().visit_with(visitor)?;
1175 self.val().visit_with(visitor)
1178 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1179 visitor.visit_const(*self)
1183 impl<'tcx> TypeFoldable<'tcx> for ty::ConstKind<'tcx> {
1184 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1187 ) -> Result<Self, F::Error> {
1189 ty::ConstKind::Infer(ic) => ty::ConstKind::Infer(ic.try_fold_with(folder)?),
1190 ty::ConstKind::Param(p) => ty::ConstKind::Param(p.try_fold_with(folder)?),
1191 ty::ConstKind::Unevaluated(uv) => ty::ConstKind::Unevaluated(uv.try_fold_with(folder)?),
1192 ty::ConstKind::Value(_)
1193 | ty::ConstKind::Bound(..)
1194 | ty::ConstKind::Placeholder(..)
1195 | ty::ConstKind::Error(_) => self,
1199 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1201 ty::ConstKind::Infer(ic) => ic.visit_with(visitor),
1202 ty::ConstKind::Param(p) => p.visit_with(visitor),
1203 ty::ConstKind::Unevaluated(uv) => uv.visit_with(visitor),
1204 ty::ConstKind::Value(_)
1205 | ty::ConstKind::Bound(..)
1206 | ty::ConstKind::Placeholder(_)
1207 | ty::ConstKind::Error(_) => ControlFlow::CONTINUE,
1212 impl<'tcx> TypeFoldable<'tcx> for InferConst<'tcx> {
1213 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1216 ) -> Result<Self, F::Error> {
1220 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
1221 ControlFlow::CONTINUE
1225 impl<'tcx> TypeFoldable<'tcx> for ty::Unevaluated<'tcx> {
1226 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1229 ) -> Result<Self, F::Error> {
1230 Ok(ty::Unevaluated {
1232 substs: self.substs.try_fold_with(folder)?,
1233 promoted: self.promoted,
1237 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1238 visitor.visit_unevaluated_const(*self)
1241 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1242 self.substs.visit_with(visitor)
1246 impl<'tcx> TypeFoldable<'tcx> for ty::Unevaluated<'tcx, ()> {
1247 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1250 ) -> Result<Self, F::Error> {
1251 Ok(ty::Unevaluated {
1253 substs: self.substs.try_fold_with(folder)?,
1254 promoted: self.promoted,
1258 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1259 visitor.visit_unevaluated_const(self.expand())
1262 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1263 self.substs.visit_with(visitor)
1267 impl<'tcx> TypeFoldable<'tcx> for hir::Constness {
1268 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(self, _: &mut F) -> Result<Self, F::Error> {
1272 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<V::BreakTy> {
1273 ControlFlow::CONTINUE