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, Ty, TyCtxt};
10 use rustc_data_structures::functor::IdFunctor;
11 use rustc_hir::def::Namespace;
12 use rustc_hir::def_id::CRATE_DEF_INDEX;
13 use rustc_index::vec::{Idx, IndexVec};
16 use std::mem::ManuallyDrop;
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<'tcx> fmt::Debug for ty::ExistentialTraitRef<'tcx> {
51 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52 with_no_trimmed_paths(|| fmt::Display::fmt(self, f))
56 impl<'tcx> fmt::Debug for ty::adjustment::Adjustment<'tcx> {
57 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58 write!(f, "{:?} -> {}", self.kind, self.target)
62 impl fmt::Debug for ty::BoundRegionKind {
63 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65 ty::BrAnon(n) => write!(f, "BrAnon({:?})", n),
66 ty::BrNamed(did, name) => {
67 if did.index == CRATE_DEF_INDEX {
68 write!(f, "BrNamed({})", name)
70 write!(f, "BrNamed({:?}, {})", did, name)
73 ty::BrEnv => write!(f, "BrEnv"),
78 impl fmt::Debug for ty::RegionKind {
79 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81 ty::ReEarlyBound(ref data) => write!(f, "ReEarlyBound({}, {})", data.index, data.name),
83 ty::ReLateBound(binder_id, ref bound_region) => {
84 write!(f, "ReLateBound({:?}, {:?})", binder_id, bound_region)
87 ty::ReFree(ref fr) => fr.fmt(f),
89 ty::ReStatic => write!(f, "ReStatic"),
91 ty::ReVar(ref vid) => vid.fmt(f),
93 ty::RePlaceholder(placeholder) => write!(f, "RePlaceholder({:?})", placeholder),
95 ty::ReEmpty(ui) => write!(f, "ReEmpty({:?})", ui),
97 ty::ReErased => write!(f, "ReErased"),
102 impl fmt::Debug for ty::FreeRegion {
103 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104 write!(f, "ReFree({:?}, {:?})", self.scope, self.bound_region)
108 impl<'tcx> fmt::Debug for ty::FnSig<'tcx> {
109 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110 write!(f, "({:?}; c_variadic: {})->{:?}", self.inputs(), self.c_variadic, self.output())
114 impl<'tcx> fmt::Debug for ty::ConstVid<'tcx> {
115 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
116 write!(f, "_#{}c", self.index)
120 impl fmt::Debug for ty::RegionVid {
121 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
122 write!(f, "'_#{}r", self.index())
126 impl<'tcx> fmt::Debug for ty::TraitRef<'tcx> {
127 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
128 with_no_trimmed_paths(|| fmt::Display::fmt(self, f))
132 impl<'tcx> fmt::Debug for Ty<'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::ParamTy {
139 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
140 write!(f, "{}/#{}", self.name, self.index)
144 impl fmt::Debug for ty::ParamConst {
145 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
146 write!(f, "{}/#{}", self.name, self.index)
150 impl<'tcx> fmt::Debug for ty::TraitPredicate<'tcx> {
151 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152 if let ty::BoundConstness::ConstIfConst = self.constness {
153 write!(f, "~const ")?;
155 write!(f, "TraitPredicate({:?}, polarity:{:?})", self.trait_ref, self.polarity)
159 impl<'tcx> fmt::Debug for ty::ProjectionPredicate<'tcx> {
160 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
161 write!(f, "ProjectionPredicate({:?}, {:?})", self.projection_ty, self.ty)
165 impl<'tcx> fmt::Debug for ty::Predicate<'tcx> {
166 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
167 write!(f, "{:?}", self.kind())
171 impl<'tcx> fmt::Debug for ty::PredicateKind<'tcx> {
172 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
174 ty::PredicateKind::Trait(ref a) => a.fmt(f),
175 ty::PredicateKind::Subtype(ref pair) => pair.fmt(f),
176 ty::PredicateKind::Coerce(ref pair) => pair.fmt(f),
177 ty::PredicateKind::RegionOutlives(ref pair) => pair.fmt(f),
178 ty::PredicateKind::TypeOutlives(ref pair) => pair.fmt(f),
179 ty::PredicateKind::Projection(ref pair) => pair.fmt(f),
180 ty::PredicateKind::WellFormed(data) => write!(f, "WellFormed({:?})", data),
181 ty::PredicateKind::ObjectSafe(trait_def_id) => {
182 write!(f, "ObjectSafe({:?})", trait_def_id)
184 ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => {
185 write!(f, "ClosureKind({:?}, {:?}, {:?})", closure_def_id, closure_substs, kind)
187 ty::PredicateKind::ConstEvaluatable(uv) => {
188 write!(f, "ConstEvaluatable({:?}, {:?})", uv.def, uv.substs)
190 ty::PredicateKind::ConstEquate(c1, c2) => write!(f, "ConstEquate({:?}, {:?})", c1, c2),
191 ty::PredicateKind::TypeWellFormedFromEnv(ty) => {
192 write!(f, "TypeWellFormedFromEnv({:?})", ty)
198 ///////////////////////////////////////////////////////////////////////////
201 // For things that don't carry any arena-allocated data (and are
202 // copy...), just add them to this list.
204 TrivialTypeFoldableAndLiftImpls! {
208 ::rustc_target::abi::VariantIdx,
212 crate::middle::region::Scope,
214 ::rustc_ast::InlineAsmOptions,
215 ::rustc_ast::InlineAsmTemplatePiece,
217 ::rustc_span::symbol::Symbol,
218 ::rustc_hir::def::Res,
219 ::rustc_hir::def_id::DefId,
220 ::rustc_hir::def_id::LocalDefId,
222 ::rustc_hir::MatchSource,
223 ::rustc_hir::Mutability,
224 ::rustc_hir::Unsafety,
225 ::rustc_target::asm::InlineAsmRegOrRegClass,
226 ::rustc_target::spec::abi::Abi,
227 crate::mir::coverage::ExpressionOperandId,
228 crate::mir::coverage::CounterValueReference,
229 crate::mir::coverage::InjectedExpressionId,
230 crate::mir::coverage::InjectedExpressionIndex,
231 crate::mir::coverage::MappedExpressionIndex,
233 crate::mir::Promoted,
234 crate::traits::Reveal,
235 crate::ty::adjustment::AutoBorrowMutability,
237 crate::ty::BoundConstness,
238 // Including `BoundRegionKind` is a *bit* dubious, but direct
239 // references to bound region appear in `ty::Error`, and aren't
240 // really meant to be folded. In general, we can only fold a fully
242 crate::ty::BoundRegionKind,
243 crate::ty::AssocItem,
244 crate::ty::Placeholder<crate::ty::BoundRegionKind>,
245 crate::ty::ClosureKind,
246 crate::ty::FreeRegion,
248 crate::ty::IntVarValue,
249 crate::ty::ParamConst,
251 crate::ty::adjustment::PointerCast,
252 crate::ty::RegionVid,
253 crate::ty::UniverseIndex,
258 ///////////////////////////////////////////////////////////////////////////
259 // Lift implementations
261 // FIXME(eddyb) replace all the uses of `Option::map` with `?`.
262 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>> Lift<'tcx> for (A, B) {
263 type Lifted = (A::Lifted, B::Lifted);
264 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
265 Some((tcx.lift(self.0)?, tcx.lift(self.1)?))
269 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>, C: Lift<'tcx>> Lift<'tcx> for (A, B, C) {
270 type Lifted = (A::Lifted, B::Lifted, C::Lifted);
271 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
272 Some((tcx.lift(self.0)?, tcx.lift(self.1)?, tcx.lift(self.2)?))
276 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Option<T> {
277 type Lifted = Option<T::Lifted>;
278 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
280 Some(x) => tcx.lift(x).map(Some),
286 impl<'tcx, T: Lift<'tcx>, E: Lift<'tcx>> Lift<'tcx> for Result<T, E> {
287 type Lifted = Result<T::Lifted, E::Lifted>;
288 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
290 Ok(x) => tcx.lift(x).map(Ok),
291 Err(e) => tcx.lift(e).map(Err),
296 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Box<T> {
297 type Lifted = Box<T::Lifted>;
298 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
299 tcx.lift(*self).map(Box::new)
303 impl<'tcx, T: Lift<'tcx> + Clone> Lift<'tcx> for Rc<T> {
304 type Lifted = Rc<T::Lifted>;
305 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
306 tcx.lift(self.as_ref().clone()).map(Rc::new)
310 impl<'tcx, T: Lift<'tcx> + Clone> Lift<'tcx> for Arc<T> {
311 type Lifted = Arc<T::Lifted>;
312 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
313 tcx.lift(self.as_ref().clone()).map(Arc::new)
316 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Vec<T> {
317 type Lifted = Vec<T::Lifted>;
318 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
319 self.into_iter().map(|v| tcx.lift(v)).collect()
323 impl<'tcx, I: Idx, T: Lift<'tcx>> Lift<'tcx> for IndexVec<I, T> {
324 type Lifted = IndexVec<I, T::Lifted>;
325 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
326 self.into_iter().map(|e| tcx.lift(e)).collect()
330 impl<'a, 'tcx> Lift<'tcx> for ty::TraitRef<'a> {
331 type Lifted = ty::TraitRef<'tcx>;
332 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
333 tcx.lift(self.substs).map(|substs| ty::TraitRef { def_id: self.def_id, substs })
337 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialTraitRef<'a> {
338 type Lifted = ty::ExistentialTraitRef<'tcx>;
339 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
340 tcx.lift(self.substs).map(|substs| ty::ExistentialTraitRef { def_id: self.def_id, substs })
344 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialPredicate<'a> {
345 type Lifted = ty::ExistentialPredicate<'tcx>;
346 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
348 ty::ExistentialPredicate::Trait(x) => tcx.lift(x).map(ty::ExistentialPredicate::Trait),
349 ty::ExistentialPredicate::Projection(x) => {
350 tcx.lift(x).map(ty::ExistentialPredicate::Projection)
352 ty::ExistentialPredicate::AutoTrait(def_id) => {
353 Some(ty::ExistentialPredicate::AutoTrait(def_id))
359 impl<'a, 'tcx> Lift<'tcx> for ty::TraitPredicate<'a> {
360 type Lifted = ty::TraitPredicate<'tcx>;
361 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::TraitPredicate<'tcx>> {
362 tcx.lift(self.trait_ref).map(|trait_ref| ty::TraitPredicate {
364 constness: self.constness,
365 polarity: self.polarity,
370 impl<'a, 'tcx> Lift<'tcx> for ty::SubtypePredicate<'a> {
371 type Lifted = ty::SubtypePredicate<'tcx>;
372 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::SubtypePredicate<'tcx>> {
373 tcx.lift((self.a, self.b)).map(|(a, b)| ty::SubtypePredicate {
374 a_is_expected: self.a_is_expected,
381 impl<'a, 'tcx> Lift<'tcx> for ty::CoercePredicate<'a> {
382 type Lifted = ty::CoercePredicate<'tcx>;
383 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::CoercePredicate<'tcx>> {
384 tcx.lift((self.a, self.b)).map(|(a, b)| ty::CoercePredicate { a, b })
388 impl<'tcx, A: Copy + Lift<'tcx>, B: Copy + Lift<'tcx>> Lift<'tcx> for ty::OutlivesPredicate<A, B> {
389 type Lifted = ty::OutlivesPredicate<A::Lifted, B::Lifted>;
390 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
391 tcx.lift((self.0, self.1)).map(|(a, b)| ty::OutlivesPredicate(a, b))
395 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionTy<'a> {
396 type Lifted = ty::ProjectionTy<'tcx>;
397 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::ProjectionTy<'tcx>> {
398 tcx.lift(self.substs)
399 .map(|substs| ty::ProjectionTy { item_def_id: self.item_def_id, substs })
403 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionPredicate<'a> {
404 type Lifted = ty::ProjectionPredicate<'tcx>;
405 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::ProjectionPredicate<'tcx>> {
406 tcx.lift((self.projection_ty, self.ty))
407 .map(|(projection_ty, ty)| ty::ProjectionPredicate { projection_ty, ty })
411 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialProjection<'a> {
412 type Lifted = ty::ExistentialProjection<'tcx>;
413 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
414 tcx.lift(self.substs).map(|substs| ty::ExistentialProjection {
416 ty: tcx.lift(self.ty).expect("type must lift when substs do"),
417 item_def_id: self.item_def_id,
422 impl<'a, 'tcx> Lift<'tcx> for ty::PredicateKind<'a> {
423 type Lifted = ty::PredicateKind<'tcx>;
424 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
426 ty::PredicateKind::Trait(data) => tcx.lift(data).map(ty::PredicateKind::Trait),
427 ty::PredicateKind::Subtype(data) => tcx.lift(data).map(ty::PredicateKind::Subtype),
428 ty::PredicateKind::Coerce(data) => tcx.lift(data).map(ty::PredicateKind::Coerce),
429 ty::PredicateKind::RegionOutlives(data) => {
430 tcx.lift(data).map(ty::PredicateKind::RegionOutlives)
432 ty::PredicateKind::TypeOutlives(data) => {
433 tcx.lift(data).map(ty::PredicateKind::TypeOutlives)
435 ty::PredicateKind::Projection(data) => {
436 tcx.lift(data).map(ty::PredicateKind::Projection)
438 ty::PredicateKind::WellFormed(ty) => tcx.lift(ty).map(ty::PredicateKind::WellFormed),
439 ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => {
440 tcx.lift(closure_substs).map(|closure_substs| {
441 ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind)
444 ty::PredicateKind::ObjectSafe(trait_def_id) => {
445 Some(ty::PredicateKind::ObjectSafe(trait_def_id))
447 ty::PredicateKind::ConstEvaluatable(uv) => {
448 tcx.lift(uv).map(|uv| ty::PredicateKind::ConstEvaluatable(uv))
450 ty::PredicateKind::ConstEquate(c1, c2) => {
451 tcx.lift((c1, c2)).map(|(c1, c2)| ty::PredicateKind::ConstEquate(c1, c2))
453 ty::PredicateKind::TypeWellFormedFromEnv(ty) => {
454 tcx.lift(ty).map(ty::PredicateKind::TypeWellFormedFromEnv)
460 impl<'a, 'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::Binder<'a, T>
462 <T as Lift<'tcx>>::Lifted: TypeFoldable<'tcx>,
464 type Lifted = ty::Binder<'tcx, T::Lifted>;
465 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
466 let bound_vars = tcx.lift(self.bound_vars());
467 tcx.lift(self.skip_binder())
469 .map(|(value, vars)| ty::Binder::bind_with_vars(value, vars))
473 impl<'a, 'tcx> Lift<'tcx> for ty::ParamEnv<'a> {
474 type Lifted = ty::ParamEnv<'tcx>;
475 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
476 tcx.lift(self.caller_bounds())
477 .map(|caller_bounds| ty::ParamEnv::new(caller_bounds, self.reveal(), self.constness()))
481 impl<'a, 'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::ParamEnvAnd<'a, T> {
482 type Lifted = ty::ParamEnvAnd<'tcx, T::Lifted>;
483 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
484 tcx.lift(self.param_env).and_then(|param_env| {
485 tcx.lift(self.value).map(|value| ty::ParamEnvAnd { param_env, value })
490 impl<'a, 'tcx> Lift<'tcx> for ty::ClosureSubsts<'a> {
491 type Lifted = ty::ClosureSubsts<'tcx>;
492 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
493 tcx.lift(self.substs).map(|substs| ty::ClosureSubsts { substs })
497 impl<'a, 'tcx> Lift<'tcx> for ty::GeneratorSubsts<'a> {
498 type Lifted = ty::GeneratorSubsts<'tcx>;
499 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
500 tcx.lift(self.substs).map(|substs| ty::GeneratorSubsts { substs })
504 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjustment<'a> {
505 type Lifted = ty::adjustment::Adjustment<'tcx>;
506 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
507 let ty::adjustment::Adjustment { kind, target } = self;
508 tcx.lift(kind).and_then(|kind| {
509 tcx.lift(target).map(|target| ty::adjustment::Adjustment { kind, target })
514 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjust<'a> {
515 type Lifted = ty::adjustment::Adjust<'tcx>;
516 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
518 ty::adjustment::Adjust::NeverToAny => Some(ty::adjustment::Adjust::NeverToAny),
519 ty::adjustment::Adjust::Pointer(ptr) => Some(ty::adjustment::Adjust::Pointer(ptr)),
520 ty::adjustment::Adjust::Deref(overloaded) => {
521 tcx.lift(overloaded).map(ty::adjustment::Adjust::Deref)
523 ty::adjustment::Adjust::Borrow(autoref) => {
524 tcx.lift(autoref).map(ty::adjustment::Adjust::Borrow)
530 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::OverloadedDeref<'a> {
531 type Lifted = ty::adjustment::OverloadedDeref<'tcx>;
532 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
533 tcx.lift(self.region).map(|region| ty::adjustment::OverloadedDeref {
541 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::AutoBorrow<'a> {
542 type Lifted = ty::adjustment::AutoBorrow<'tcx>;
543 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
545 ty::adjustment::AutoBorrow::Ref(r, m) => {
546 tcx.lift(r).map(|r| ty::adjustment::AutoBorrow::Ref(r, m))
548 ty::adjustment::AutoBorrow::RawPtr(m) => Some(ty::adjustment::AutoBorrow::RawPtr(m)),
553 impl<'a, 'tcx> Lift<'tcx> for ty::GenSig<'a> {
554 type Lifted = ty::GenSig<'tcx>;
555 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
556 tcx.lift((self.resume_ty, self.yield_ty, self.return_ty))
557 .map(|(resume_ty, yield_ty, return_ty)| ty::GenSig { resume_ty, yield_ty, return_ty })
561 impl<'a, 'tcx> Lift<'tcx> for ty::FnSig<'a> {
562 type Lifted = ty::FnSig<'tcx>;
563 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
564 tcx.lift(self.inputs_and_output).map(|x| ty::FnSig {
565 inputs_and_output: x,
566 c_variadic: self.c_variadic,
567 unsafety: self.unsafety,
573 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::error::ExpectedFound<T> {
574 type Lifted = ty::error::ExpectedFound<T::Lifted>;
575 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
576 let ty::error::ExpectedFound { expected, found } = self;
577 tcx.lift(expected).and_then(|expected| {
578 tcx.lift(found).map(|found| ty::error::ExpectedFound { expected, found })
583 impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> {
584 type Lifted = ty::error::TypeError<'tcx>;
585 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
586 use crate::ty::error::TypeError::*;
589 Mismatch => Mismatch,
590 ConstnessMismatch(x) => ConstnessMismatch(x),
591 PolarityMismatch(x) => PolarityMismatch(x),
592 UnsafetyMismatch(x) => UnsafetyMismatch(x),
593 AbiMismatch(x) => AbiMismatch(x),
594 Mutability => Mutability,
595 ArgumentMutability(i) => ArgumentMutability(i),
596 TupleSize(x) => TupleSize(x),
597 FixedArraySize(x) => FixedArraySize(x),
598 ArgCount => ArgCount,
599 FieldMisMatch(x, y) => FieldMisMatch(x, y),
600 RegionsDoesNotOutlive(a, b) => {
601 return tcx.lift((a, b)).map(|(a, b)| RegionsDoesNotOutlive(a, b));
603 RegionsInsufficientlyPolymorphic(a, b) => {
604 return tcx.lift(b).map(|b| RegionsInsufficientlyPolymorphic(a, b));
606 RegionsOverlyPolymorphic(a, b) => {
607 return tcx.lift(b).map(|b| RegionsOverlyPolymorphic(a, b));
609 RegionsPlaceholderMismatch => RegionsPlaceholderMismatch,
610 IntMismatch(x) => IntMismatch(x),
611 FloatMismatch(x) => FloatMismatch(x),
612 Traits(x) => Traits(x),
613 VariadicMismatch(x) => VariadicMismatch(x),
614 CyclicTy(t) => return tcx.lift(t).map(|t| CyclicTy(t)),
615 CyclicConst(ct) => return tcx.lift(ct).map(|ct| CyclicConst(ct)),
616 ProjectionMismatched(x) => ProjectionMismatched(x),
617 ArgumentSorts(x, i) => return tcx.lift(x).map(|x| ArgumentSorts(x, i)),
618 Sorts(x) => return tcx.lift(x).map(Sorts),
619 ExistentialMismatch(x) => return tcx.lift(x).map(ExistentialMismatch),
620 ConstMismatch(x) => return tcx.lift(x).map(ConstMismatch),
621 IntrinsicCast => IntrinsicCast,
622 TargetFeatureCast(x) => TargetFeatureCast(x),
623 ObjectUnsafeCoercion(x) => return tcx.lift(x).map(ObjectUnsafeCoercion),
628 impl<'a, 'tcx> Lift<'tcx> for ty::InstanceDef<'a> {
629 type Lifted = ty::InstanceDef<'tcx>;
630 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
632 ty::InstanceDef::Item(def_id) => Some(ty::InstanceDef::Item(def_id)),
633 ty::InstanceDef::VtableShim(def_id) => Some(ty::InstanceDef::VtableShim(def_id)),
634 ty::InstanceDef::ReifyShim(def_id) => Some(ty::InstanceDef::ReifyShim(def_id)),
635 ty::InstanceDef::Intrinsic(def_id) => Some(ty::InstanceDef::Intrinsic(def_id)),
636 ty::InstanceDef::FnPtrShim(def_id, ty) => {
637 Some(ty::InstanceDef::FnPtrShim(def_id, tcx.lift(ty)?))
639 ty::InstanceDef::Virtual(def_id, n) => Some(ty::InstanceDef::Virtual(def_id, n)),
640 ty::InstanceDef::ClosureOnceShim { call_once, track_caller } => {
641 Some(ty::InstanceDef::ClosureOnceShim { call_once, track_caller })
643 ty::InstanceDef::DropGlue(def_id, ty) => {
644 Some(ty::InstanceDef::DropGlue(def_id, tcx.lift(ty)?))
646 ty::InstanceDef::CloneShim(def_id, ty) => {
647 Some(ty::InstanceDef::CloneShim(def_id, tcx.lift(ty)?))
653 ///////////////////////////////////////////////////////////////////////////
654 // TypeFoldable implementations.
656 // Ideally, each type should invoke `folder.fold_foo(self)` and
657 // nothing else. In some cases, though, we haven't gotten around to
658 // adding methods on the `folder` yet, and thus the folding is
659 // hard-coded here. This is less-flexible, because folders cannot
660 // override the behavior, but there are a lot of random types and one
661 // can easily refactor the folding into the TypeFolder trait as
664 /// AdtDefs are basically the same as a DefId.
665 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::AdtDef {
666 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
669 ) -> Result<Self, F::Error> {
673 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
674 ControlFlow::CONTINUE
678 impl<'tcx, T: TypeFoldable<'tcx>, U: TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
679 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
682 ) -> Result<(T, U), F::Error> {
683 Ok((self.0.try_fold_with(folder)?, self.1.try_fold_with(folder)?))
686 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
687 self.0.visit_with(visitor)?;
688 self.1.visit_with(visitor)
692 impl<'tcx, A: TypeFoldable<'tcx>, B: TypeFoldable<'tcx>, C: TypeFoldable<'tcx>> TypeFoldable<'tcx>
695 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
698 ) -> Result<(A, B, C), F::Error> {
700 self.0.try_fold_with(folder)?,
701 self.1.try_fold_with(folder)?,
702 self.2.try_fold_with(folder)?,
706 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
707 self.0.visit_with(visitor)?;
708 self.1.visit_with(visitor)?;
709 self.2.visit_with(visitor)
713 EnumTypeFoldableImpl! {
714 impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
717 } where T: TypeFoldable<'tcx>
720 EnumTypeFoldableImpl! {
721 impl<'tcx, T, E> TypeFoldable<'tcx> for Result<T, E> {
724 } where T: TypeFoldable<'tcx>, E: TypeFoldable<'tcx>,
727 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
728 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
731 ) -> Result<Self, F::Error> {
732 // We merely want to replace the contained `T`, if at all possible,
733 // so that we don't needlessly allocate a new `Rc` or indeed clone
734 // the contained type.
736 // First step is to ensure that we have a unique reference to
737 // the contained type, which `Rc::make_mut` will accomplish (by
738 // allocating a new `Rc` and cloning the `T` only if required).
739 // This is done *before* casting to `Rc<ManuallyDrop<T>>` so that
740 // panicking during `make_mut` does not leak the `T`.
741 Rc::make_mut(&mut self);
743 // Casting to `Rc<ManuallyDrop<T>>` is safe because `ManuallyDrop`
744 // is `repr(transparent)`.
745 let ptr = Rc::into_raw(self).cast::<ManuallyDrop<T>>();
746 let mut unique = Rc::from_raw(ptr);
748 // Call to `Rc::make_mut` above guarantees that `unique` is the
749 // sole reference to the contained value, so we can avoid doing
750 // a checked `get_mut` here.
751 let slot = Rc::get_mut_unchecked(&mut unique);
753 // Semantically move the contained type out from `unique`, fold
754 // it, then move the folded value back into `unique`. Should
755 // folding fail, `ManuallyDrop` ensures that the "moved-out"
756 // value is not re-dropped.
757 let owned = ManuallyDrop::take(slot);
758 let folded = owned.try_fold_with(folder)?;
759 *slot = ManuallyDrop::new(folded);
761 // Cast back to `Rc<T>`.
762 Ok(Rc::from_raw(Rc::into_raw(unique).cast()))
766 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
767 (**self).visit_with(visitor)
771 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Arc<T> {
772 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
775 ) -> Result<Self, F::Error> {
776 // We merely want to replace the contained `T`, if at all possible,
777 // so that we don't needlessly allocate a new `Arc` or indeed clone
778 // the contained type.
780 // First step is to ensure that we have a unique reference to
781 // the contained type, which `Arc::make_mut` will accomplish (by
782 // allocating a new `Arc` and cloning the `T` only if required).
783 // This is done *before* casting to `Arc<ManuallyDrop<T>>` so that
784 // panicking during `make_mut` does not leak the `T`.
785 Arc::make_mut(&mut self);
787 // Casting to `Arc<ManuallyDrop<T>>` is safe because `ManuallyDrop`
788 // is `repr(transparent)`.
789 let ptr = Arc::into_raw(self).cast::<ManuallyDrop<T>>();
790 let mut unique = Arc::from_raw(ptr);
792 // Call to `Arc::make_mut` above guarantees that `unique` is the
793 // sole reference to the contained value, so we can avoid doing
794 // a checked `get_mut` here.
795 let slot = Arc::get_mut_unchecked(&mut unique);
797 // Semantically move the contained type out from `unique`, fold
798 // it, then move the folded value back into `unique`. Should
799 // folding fail, `ManuallyDrop` ensures that the "moved-out"
800 // value is not re-dropped.
801 let owned = ManuallyDrop::take(slot);
802 let folded = owned.try_fold_with(folder)?;
803 *slot = ManuallyDrop::new(folded);
805 // Cast back to `Arc<T>`.
806 Ok(Arc::from_raw(Arc::into_raw(unique).cast()))
810 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
811 (**self).visit_with(visitor)
815 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
816 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
819 ) -> Result<Self, F::Error> {
820 self.try_map_id(|value| value.try_fold_with(folder))
823 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
824 (**self).visit_with(visitor)
828 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
829 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
832 ) -> Result<Self, F::Error> {
833 self.try_map_id(|t| t.try_fold_with(folder))
836 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
837 self.iter().try_for_each(|t| t.visit_with(visitor))
841 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<[T]> {
842 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
845 ) -> Result<Self, F::Error> {
846 self.try_map_id(|t| t.try_fold_with(folder))
849 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
850 self.iter().try_for_each(|t| t.visit_with(visitor))
854 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<'tcx, T> {
855 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
858 ) -> Result<Self, F::Error> {
859 self.try_map_bound(|ty| ty.try_fold_with(folder))
862 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
863 folder.try_fold_binder(self)
866 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
867 self.as_ref().skip_binder().visit_with(visitor)
870 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
871 visitor.visit_binder(self)
875 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>> {
876 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
879 ) -> Result<Self, F::Error> {
880 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_poly_existential_predicates(v))
883 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
884 self.iter().try_for_each(|p| p.visit_with(visitor))
888 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<Ty<'tcx>> {
889 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
892 ) -> Result<Self, F::Error> {
893 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_type_list(v))
896 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
897 self.iter().try_for_each(|t| t.visit_with(visitor))
901 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind> {
902 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
905 ) -> Result<Self, F::Error> {
906 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_projs(v))
909 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
910 self.iter().try_for_each(|t| t.visit_with(visitor))
914 impl<'tcx> TypeFoldable<'tcx> for ty::instance::Instance<'tcx> {
915 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
918 ) -> Result<Self, F::Error> {
919 use crate::ty::InstanceDef::*;
921 substs: self.substs.try_fold_with(folder)?,
922 def: match self.def {
923 Item(def) => Item(def.try_fold_with(folder)?),
924 VtableShim(did) => VtableShim(did.try_fold_with(folder)?),
925 ReifyShim(did) => ReifyShim(did.try_fold_with(folder)?),
926 Intrinsic(did) => Intrinsic(did.try_fold_with(folder)?),
927 FnPtrShim(did, ty) => {
928 FnPtrShim(did.try_fold_with(folder)?, ty.try_fold_with(folder)?)
930 Virtual(did, i) => Virtual(did.try_fold_with(folder)?, i),
931 ClosureOnceShim { call_once, track_caller } => {
932 ClosureOnceShim { call_once: call_once.try_fold_with(folder)?, track_caller }
934 DropGlue(did, ty) => {
935 DropGlue(did.try_fold_with(folder)?, ty.try_fold_with(folder)?)
937 CloneShim(did, ty) => {
938 CloneShim(did.try_fold_with(folder)?, ty.try_fold_with(folder)?)
944 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
945 use crate::ty::InstanceDef::*;
946 self.substs.visit_with(visitor)?;
948 Item(def) => def.visit_with(visitor),
949 VtableShim(did) | ReifyShim(did) | Intrinsic(did) | Virtual(did, _) => {
950 did.visit_with(visitor)
952 FnPtrShim(did, ty) | CloneShim(did, ty) => {
953 did.visit_with(visitor)?;
954 ty.visit_with(visitor)
956 DropGlue(did, ty) => {
957 did.visit_with(visitor)?;
958 ty.visit_with(visitor)
960 ClosureOnceShim { call_once, track_caller: _ } => call_once.visit_with(visitor),
965 impl<'tcx> TypeFoldable<'tcx> for interpret::GlobalId<'tcx> {
966 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
969 ) -> Result<Self, F::Error> {
970 Ok(Self { instance: self.instance.try_fold_with(folder)?, promoted: self.promoted })
973 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
974 self.instance.visit_with(visitor)
978 impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
979 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
982 ) -> Result<Self, F::Error> {
983 let kind = match *self.kind() {
984 ty::RawPtr(tm) => ty::RawPtr(tm.try_fold_with(folder)?),
985 ty::Array(typ, sz) => ty::Array(typ.try_fold_with(folder)?, sz.try_fold_with(folder)?),
986 ty::Slice(typ) => ty::Slice(typ.try_fold_with(folder)?),
987 ty::Adt(tid, substs) => ty::Adt(tid, substs.try_fold_with(folder)?),
988 ty::Dynamic(trait_ty, region) => {
989 ty::Dynamic(trait_ty.try_fold_with(folder)?, region.try_fold_with(folder)?)
991 ty::Tuple(ts) => ty::Tuple(ts.try_fold_with(folder)?),
992 ty::FnDef(def_id, substs) => ty::FnDef(def_id, substs.try_fold_with(folder)?),
993 ty::FnPtr(f) => ty::FnPtr(f.try_fold_with(folder)?),
994 ty::Ref(r, ty, mutbl) => {
995 ty::Ref(r.try_fold_with(folder)?, ty.try_fold_with(folder)?, mutbl)
997 ty::Generator(did, substs, movability) => {
998 ty::Generator(did, substs.try_fold_with(folder)?, movability)
1000 ty::GeneratorWitness(types) => ty::GeneratorWitness(types.try_fold_with(folder)?),
1001 ty::Closure(did, substs) => ty::Closure(did, substs.try_fold_with(folder)?),
1002 ty::Projection(data) => ty::Projection(data.try_fold_with(folder)?),
1003 ty::Opaque(did, substs) => ty::Opaque(did, substs.try_fold_with(folder)?),
1015 | ty::Placeholder(..)
1017 | ty::Foreign(..) => return Ok(self),
1020 Ok(if *self.kind() == kind { self } else { folder.tcx().mk_ty(kind) })
1023 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
1024 folder.try_fold_ty(self)
1027 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1029 ty::RawPtr(ref tm) => tm.visit_with(visitor),
1030 ty::Array(typ, sz) => {
1031 typ.visit_with(visitor)?;
1032 sz.visit_with(visitor)
1034 ty::Slice(typ) => typ.visit_with(visitor),
1035 ty::Adt(_, substs) => substs.visit_with(visitor),
1036 ty::Dynamic(ref trait_ty, ref reg) => {
1037 trait_ty.visit_with(visitor)?;
1038 reg.visit_with(visitor)
1040 ty::Tuple(ts) => ts.visit_with(visitor),
1041 ty::FnDef(_, substs) => substs.visit_with(visitor),
1042 ty::FnPtr(ref f) => f.visit_with(visitor),
1043 ty::Ref(r, ty, _) => {
1044 r.visit_with(visitor)?;
1045 ty.visit_with(visitor)
1047 ty::Generator(_did, ref substs, _) => substs.visit_with(visitor),
1048 ty::GeneratorWitness(ref types) => types.visit_with(visitor),
1049 ty::Closure(_did, ref substs) => substs.visit_with(visitor),
1050 ty::Projection(ref data) => data.visit_with(visitor),
1051 ty::Opaque(_, ref substs) => substs.visit_with(visitor),
1062 | ty::Placeholder(..)
1065 | ty::Foreign(..) => ControlFlow::CONTINUE,
1069 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1070 visitor.visit_ty(self)
1074 impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> {
1075 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1078 ) -> Result<Self, F::Error> {
1082 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
1083 folder.try_fold_region(self)
1086 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
1087 ControlFlow::CONTINUE
1090 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1091 visitor.visit_region(*self)
1095 impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
1096 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
1097 folder.try_fold_predicate(self)
1100 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1103 ) -> Result<Self, F::Error> {
1104 let new = self.inner.kind.try_fold_with(folder)?;
1105 Ok(folder.tcx().reuse_or_mk_predicate(self, new))
1108 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1109 self.inner.kind.visit_with(visitor)
1112 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1113 visitor.visit_predicate(*self)
1116 fn has_vars_bound_at_or_above(&self, binder: ty::DebruijnIndex) -> bool {
1117 self.inner.outer_exclusive_binder > binder
1120 fn has_type_flags(&self, flags: ty::TypeFlags) -> bool {
1121 self.inner.flags.intersects(flags)
1125 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
1126 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1129 ) -> Result<Self, F::Error> {
1130 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_predicates(v))
1133 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1134 self.iter().try_for_each(|p| p.visit_with(visitor))
1138 impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
1139 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1142 ) -> Result<Self, F::Error> {
1143 self.try_map_id(|x| x.try_fold_with(folder))
1146 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1147 self.iter().try_for_each(|t| t.visit_with(visitor))
1151 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Const<'tcx> {
1152 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1155 ) -> Result<Self, F::Error> {
1156 let ty = self.ty.try_fold_with(folder)?;
1157 let val = self.val.try_fold_with(folder)?;
1158 if ty != self.ty || val != self.val {
1159 Ok(folder.tcx().mk_const(ty::Const { ty, val }))
1165 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
1166 folder.try_fold_const(self)
1169 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1170 self.ty.visit_with(visitor)?;
1171 self.val.visit_with(visitor)
1174 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1175 visitor.visit_const(self)
1179 impl<'tcx> TypeFoldable<'tcx> for ty::ConstKind<'tcx> {
1180 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1183 ) -> Result<Self, F::Error> {
1185 ty::ConstKind::Infer(ic) => ty::ConstKind::Infer(ic.try_fold_with(folder)?),
1186 ty::ConstKind::Param(p) => ty::ConstKind::Param(p.try_fold_with(folder)?),
1187 ty::ConstKind::Unevaluated(uv) => ty::ConstKind::Unevaluated(uv.try_fold_with(folder)?),
1188 ty::ConstKind::Value(_)
1189 | ty::ConstKind::Bound(..)
1190 | ty::ConstKind::Placeholder(..)
1191 | ty::ConstKind::Error(_) => self,
1195 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1197 ty::ConstKind::Infer(ic) => ic.visit_with(visitor),
1198 ty::ConstKind::Param(p) => p.visit_with(visitor),
1199 ty::ConstKind::Unevaluated(uv) => uv.visit_with(visitor),
1200 ty::ConstKind::Value(_)
1201 | ty::ConstKind::Bound(..)
1202 | ty::ConstKind::Placeholder(_)
1203 | ty::ConstKind::Error(_) => ControlFlow::CONTINUE,
1208 impl<'tcx> TypeFoldable<'tcx> for InferConst<'tcx> {
1209 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1212 ) -> Result<Self, F::Error> {
1216 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
1217 ControlFlow::CONTINUE
1221 impl<'tcx> TypeFoldable<'tcx> for ty::Unevaluated<'tcx> {
1222 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1225 ) -> Result<Self, F::Error> {
1226 Ok(ty::Unevaluated {
1228 substs: self.substs.try_fold_with(folder)?,
1229 promoted: self.promoted,
1233 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1234 visitor.visit_unevaluated_const(*self)
1237 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1238 self.substs.visit_with(visitor)
1242 impl<'tcx> TypeFoldable<'tcx> for ty::Unevaluated<'tcx, ()> {
1243 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1246 ) -> Result<Self, F::Error> {
1247 Ok(ty::Unevaluated {
1249 substs: self.substs.try_fold_with(folder)?,
1250 promoted: self.promoted,
1254 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1255 visitor.visit_unevaluated_const(self.expand())
1258 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1259 self.substs.visit_with(visitor)