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_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!({
26 &FmtPrinter::new(tcx, Namespace::TypeNS)
27 .print_def_path(self.def_id, &[])?
35 impl<'tcx> fmt::Debug for ty::AdtDef<'tcx> {
36 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38 with_no_trimmed_paths!({
40 &FmtPrinter::new(tcx, Namespace::TypeNS)
41 .print_def_path(self.did(), &[])?
49 impl fmt::Debug for ty::UpvarId {
50 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51 let name = ty::tls::with(|tcx| tcx.hir().name(self.var_path.hir_id));
52 write!(f, "UpvarId({:?};`{}`;{:?})", self.var_path.hir_id, name, self.closure_expr_id)
56 impl<'tcx> 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<'tcx> 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.is_crate_root() {
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<'tcx> 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<'tcx> 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<'tcx> 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<'tcx> fmt::Debug for ty::TraitPredicate<'tcx> {
157 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
158 if let ty::BoundConstness::ConstIfConst = self.constness {
159 write!(f, "~const ")?;
161 write!(f, "TraitPredicate({:?}, polarity:{:?})", self.trait_ref, self.polarity)
165 impl<'tcx> fmt::Debug for ty::ProjectionPredicate<'tcx> {
166 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
167 write!(f, "ProjectionPredicate({:?}, {:?})", self.projection_ty, self.term)
171 impl<'tcx> fmt::Debug for ty::Predicate<'tcx> {
172 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
173 write!(f, "{:?}", self.kind())
177 impl<'tcx> fmt::Debug for ty::PredicateKind<'tcx> {
178 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
180 ty::PredicateKind::Trait(ref a) => a.fmt(f),
181 ty::PredicateKind::Subtype(ref pair) => pair.fmt(f),
182 ty::PredicateKind::Coerce(ref pair) => pair.fmt(f),
183 ty::PredicateKind::RegionOutlives(ref pair) => pair.fmt(f),
184 ty::PredicateKind::TypeOutlives(ref pair) => pair.fmt(f),
185 ty::PredicateKind::Projection(ref pair) => pair.fmt(f),
186 ty::PredicateKind::WellFormed(data) => write!(f, "WellFormed({:?})", data),
187 ty::PredicateKind::ObjectSafe(trait_def_id) => {
188 write!(f, "ObjectSafe({:?})", trait_def_id)
190 ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => {
191 write!(f, "ClosureKind({:?}, {:?}, {:?})", closure_def_id, closure_substs, kind)
193 ty::PredicateKind::ConstEvaluatable(uv) => {
194 write!(f, "ConstEvaluatable({:?}, {:?})", uv.def, uv.substs)
196 ty::PredicateKind::ConstEquate(c1, c2) => write!(f, "ConstEquate({:?}, {:?})", c1, c2),
197 ty::PredicateKind::TypeWellFormedFromEnv(ty) => {
198 write!(f, "TypeWellFormedFromEnv({:?})", ty)
204 ///////////////////////////////////////////////////////////////////////////
207 // For things that don't carry any arena-allocated data (and are
208 // copy...), just add them to this list.
210 TrivialTypeFoldableAndLiftImpls! {
214 ::rustc_target::abi::VariantIdx,
218 crate::middle::region::Scope,
220 ::rustc_ast::InlineAsmOptions,
221 ::rustc_ast::InlineAsmTemplatePiece,
223 ::rustc_span::symbol::Symbol,
224 ::rustc_hir::def::Res,
225 ::rustc_hir::def_id::DefId,
226 ::rustc_hir::def_id::LocalDefId,
228 ::rustc_hir::MatchSource,
229 ::rustc_hir::Mutability,
230 ::rustc_hir::Unsafety,
231 ::rustc_target::asm::InlineAsmRegOrRegClass,
232 ::rustc_target::spec::abi::Abi,
233 crate::mir::coverage::ExpressionOperandId,
234 crate::mir::coverage::CounterValueReference,
235 crate::mir::coverage::InjectedExpressionId,
236 crate::mir::coverage::InjectedExpressionIndex,
237 crate::mir::coverage::MappedExpressionIndex,
239 crate::mir::Promoted,
240 crate::traits::Reveal,
241 crate::ty::adjustment::AutoBorrowMutability,
243 crate::ty::BoundConstness,
244 // Including `BoundRegionKind` is a *bit* dubious, but direct
245 // references to bound region appear in `ty::Error`, and aren't
246 // really meant to be folded. In general, we can only fold a fully
248 crate::ty::BoundRegionKind,
249 crate::ty::AssocItem,
250 crate::ty::Placeholder<crate::ty::BoundRegionKind>,
251 crate::ty::ClosureKind,
252 crate::ty::FreeRegion,
254 crate::ty::IntVarValue,
255 crate::ty::ParamConst,
257 crate::ty::adjustment::PointerCast,
258 crate::ty::RegionVid,
259 crate::ty::UniverseIndex,
262 ::rustc_errors::ErrorGuaranteed,
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 Term<'a> {
367 type Lifted = ty::Term<'tcx>;
368 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
370 Term::Ty(ty) => Term::Ty(tcx.lift(ty)?),
371 Term::Const(c) => Term::Const(tcx.lift(c)?),
376 impl<'a, 'tcx> Lift<'tcx> for ty::TraitPredicate<'a> {
377 type Lifted = ty::TraitPredicate<'tcx>;
378 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::TraitPredicate<'tcx>> {
379 tcx.lift(self.trait_ref).map(|trait_ref| ty::TraitPredicate {
381 constness: self.constness,
382 polarity: self.polarity,
387 impl<'a, 'tcx> Lift<'tcx> for ty::SubtypePredicate<'a> {
388 type Lifted = ty::SubtypePredicate<'tcx>;
389 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::SubtypePredicate<'tcx>> {
390 tcx.lift((self.a, self.b)).map(|(a, b)| ty::SubtypePredicate {
391 a_is_expected: self.a_is_expected,
398 impl<'a, 'tcx> Lift<'tcx> for ty::CoercePredicate<'a> {
399 type Lifted = ty::CoercePredicate<'tcx>;
400 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::CoercePredicate<'tcx>> {
401 tcx.lift((self.a, self.b)).map(|(a, b)| ty::CoercePredicate { a, b })
405 impl<'tcx, A: Copy + Lift<'tcx>, B: Copy + Lift<'tcx>> Lift<'tcx> for ty::OutlivesPredicate<A, B> {
406 type Lifted = ty::OutlivesPredicate<A::Lifted, B::Lifted>;
407 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
408 tcx.lift((self.0, self.1)).map(|(a, b)| ty::OutlivesPredicate(a, b))
412 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionTy<'a> {
413 type Lifted = ty::ProjectionTy<'tcx>;
414 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::ProjectionTy<'tcx>> {
415 tcx.lift(self.substs)
416 .map(|substs| ty::ProjectionTy { item_def_id: self.item_def_id, substs })
420 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionPredicate<'a> {
421 type Lifted = ty::ProjectionPredicate<'tcx>;
422 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::ProjectionPredicate<'tcx>> {
423 tcx.lift((self.projection_ty, self.term))
424 .map(|(projection_ty, term)| ty::ProjectionPredicate { projection_ty, term })
428 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialProjection<'a> {
429 type Lifted = ty::ExistentialProjection<'tcx>;
430 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
431 tcx.lift(self.substs).map(|substs| ty::ExistentialProjection {
433 term: tcx.lift(self.term).expect("type must lift when substs do"),
434 item_def_id: self.item_def_id,
439 impl<'a, 'tcx> Lift<'tcx> for ty::PredicateKind<'a> {
440 type Lifted = ty::PredicateKind<'tcx>;
441 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
443 ty::PredicateKind::Trait(data) => tcx.lift(data).map(ty::PredicateKind::Trait),
444 ty::PredicateKind::Subtype(data) => tcx.lift(data).map(ty::PredicateKind::Subtype),
445 ty::PredicateKind::Coerce(data) => tcx.lift(data).map(ty::PredicateKind::Coerce),
446 ty::PredicateKind::RegionOutlives(data) => {
447 tcx.lift(data).map(ty::PredicateKind::RegionOutlives)
449 ty::PredicateKind::TypeOutlives(data) => {
450 tcx.lift(data).map(ty::PredicateKind::TypeOutlives)
452 ty::PredicateKind::Projection(data) => {
453 tcx.lift(data).map(ty::PredicateKind::Projection)
455 ty::PredicateKind::WellFormed(ty) => tcx.lift(ty).map(ty::PredicateKind::WellFormed),
456 ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => {
457 tcx.lift(closure_substs).map(|closure_substs| {
458 ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind)
461 ty::PredicateKind::ObjectSafe(trait_def_id) => {
462 Some(ty::PredicateKind::ObjectSafe(trait_def_id))
464 ty::PredicateKind::ConstEvaluatable(uv) => {
465 tcx.lift(uv).map(|uv| ty::PredicateKind::ConstEvaluatable(uv))
467 ty::PredicateKind::ConstEquate(c1, c2) => {
468 tcx.lift((c1, c2)).map(|(c1, c2)| ty::PredicateKind::ConstEquate(c1, c2))
470 ty::PredicateKind::TypeWellFormedFromEnv(ty) => {
471 tcx.lift(ty).map(ty::PredicateKind::TypeWellFormedFromEnv)
477 impl<'a, 'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::Binder<'a, T>
479 <T as Lift<'tcx>>::Lifted: TypeFoldable<'tcx>,
481 type Lifted = ty::Binder<'tcx, T::Lifted>;
482 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
483 let bound_vars = tcx.lift(self.bound_vars());
484 tcx.lift(self.skip_binder())
486 .map(|(value, vars)| ty::Binder::bind_with_vars(value, vars))
490 impl<'a, 'tcx> Lift<'tcx> for ty::ParamEnv<'a> {
491 type Lifted = ty::ParamEnv<'tcx>;
492 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
493 tcx.lift(self.caller_bounds())
494 .map(|caller_bounds| ty::ParamEnv::new(caller_bounds, self.reveal(), self.constness()))
498 impl<'a, 'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::ParamEnvAnd<'a, T> {
499 type Lifted = ty::ParamEnvAnd<'tcx, T::Lifted>;
500 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
501 tcx.lift(self.param_env).and_then(|param_env| {
502 tcx.lift(self.value).map(|value| ty::ParamEnvAnd { param_env, value })
507 impl<'a, 'tcx> Lift<'tcx> for ty::ClosureSubsts<'a> {
508 type Lifted = ty::ClosureSubsts<'tcx>;
509 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
510 tcx.lift(self.substs).map(|substs| ty::ClosureSubsts { substs })
514 impl<'a, 'tcx> Lift<'tcx> for ty::GeneratorSubsts<'a> {
515 type Lifted = ty::GeneratorSubsts<'tcx>;
516 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
517 tcx.lift(self.substs).map(|substs| ty::GeneratorSubsts { substs })
521 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjustment<'a> {
522 type Lifted = ty::adjustment::Adjustment<'tcx>;
523 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
524 let ty::adjustment::Adjustment { kind, target } = self;
525 tcx.lift(kind).and_then(|kind| {
526 tcx.lift(target).map(|target| ty::adjustment::Adjustment { kind, target })
531 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjust<'a> {
532 type Lifted = ty::adjustment::Adjust<'tcx>;
533 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
535 ty::adjustment::Adjust::NeverToAny => Some(ty::adjustment::Adjust::NeverToAny),
536 ty::adjustment::Adjust::Pointer(ptr) => Some(ty::adjustment::Adjust::Pointer(ptr)),
537 ty::adjustment::Adjust::Deref(overloaded) => {
538 tcx.lift(overloaded).map(ty::adjustment::Adjust::Deref)
540 ty::adjustment::Adjust::Borrow(autoref) => {
541 tcx.lift(autoref).map(ty::adjustment::Adjust::Borrow)
547 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::OverloadedDeref<'a> {
548 type Lifted = ty::adjustment::OverloadedDeref<'tcx>;
549 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
550 tcx.lift(self.region).map(|region| ty::adjustment::OverloadedDeref {
558 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::AutoBorrow<'a> {
559 type Lifted = ty::adjustment::AutoBorrow<'tcx>;
560 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
562 ty::adjustment::AutoBorrow::Ref(r, m) => {
563 tcx.lift(r).map(|r| ty::adjustment::AutoBorrow::Ref(r, m))
565 ty::adjustment::AutoBorrow::RawPtr(m) => Some(ty::adjustment::AutoBorrow::RawPtr(m)),
570 impl<'a, 'tcx> Lift<'tcx> for ty::GenSig<'a> {
571 type Lifted = ty::GenSig<'tcx>;
572 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
573 tcx.lift((self.resume_ty, self.yield_ty, self.return_ty))
574 .map(|(resume_ty, yield_ty, return_ty)| ty::GenSig { resume_ty, yield_ty, return_ty })
578 impl<'a, 'tcx> Lift<'tcx> for ty::FnSig<'a> {
579 type Lifted = ty::FnSig<'tcx>;
580 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
581 tcx.lift(self.inputs_and_output).map(|x| ty::FnSig {
582 inputs_and_output: x,
583 c_variadic: self.c_variadic,
584 unsafety: self.unsafety,
590 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::error::ExpectedFound<T> {
591 type Lifted = ty::error::ExpectedFound<T::Lifted>;
592 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
593 let ty::error::ExpectedFound { expected, found } = self;
594 tcx.lift(expected).and_then(|expected| {
595 tcx.lift(found).map(|found| ty::error::ExpectedFound { expected, found })
600 impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> {
601 type Lifted = ty::error::TypeError<'tcx>;
602 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
603 use crate::ty::error::TypeError::*;
606 Mismatch => Mismatch,
607 ConstnessMismatch(x) => ConstnessMismatch(x),
608 PolarityMismatch(x) => PolarityMismatch(x),
609 UnsafetyMismatch(x) => UnsafetyMismatch(x),
610 AbiMismatch(x) => AbiMismatch(x),
611 Mutability => Mutability,
612 ArgumentMutability(i) => ArgumentMutability(i),
613 TupleSize(x) => TupleSize(x),
614 FixedArraySize(x) => FixedArraySize(x),
615 ArgCount => ArgCount,
616 FieldMisMatch(x, y) => FieldMisMatch(x, y),
617 RegionsDoesNotOutlive(a, b) => {
618 return tcx.lift((a, b)).map(|(a, b)| RegionsDoesNotOutlive(a, b));
620 RegionsInsufficientlyPolymorphic(a, b) => {
621 return tcx.lift(b).map(|b| RegionsInsufficientlyPolymorphic(a, b));
623 RegionsOverlyPolymorphic(a, b) => {
624 return tcx.lift(b).map(|b| RegionsOverlyPolymorphic(a, b));
626 RegionsPlaceholderMismatch => RegionsPlaceholderMismatch,
627 IntMismatch(x) => IntMismatch(x),
628 FloatMismatch(x) => FloatMismatch(x),
629 Traits(x) => Traits(x),
630 VariadicMismatch(x) => VariadicMismatch(x),
631 CyclicTy(t) => return tcx.lift(t).map(|t| CyclicTy(t)),
632 CyclicConst(ct) => return tcx.lift(ct).map(|ct| CyclicConst(ct)),
633 ProjectionMismatched(x) => ProjectionMismatched(x),
634 ArgumentSorts(x, i) => return tcx.lift(x).map(|x| ArgumentSorts(x, i)),
635 Sorts(x) => return tcx.lift(x).map(Sorts),
636 ExistentialMismatch(x) => return tcx.lift(x).map(ExistentialMismatch),
637 ConstMismatch(x) => return tcx.lift(x).map(ConstMismatch),
638 IntrinsicCast => IntrinsicCast,
639 TargetFeatureCast(x) => TargetFeatureCast(x),
640 ObjectUnsafeCoercion(x) => return tcx.lift(x).map(ObjectUnsafeCoercion),
645 impl<'a, 'tcx> Lift<'tcx> for ty::InstanceDef<'a> {
646 type Lifted = ty::InstanceDef<'tcx>;
647 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
649 ty::InstanceDef::Item(def_id) => Some(ty::InstanceDef::Item(def_id)),
650 ty::InstanceDef::VtableShim(def_id) => Some(ty::InstanceDef::VtableShim(def_id)),
651 ty::InstanceDef::ReifyShim(def_id) => Some(ty::InstanceDef::ReifyShim(def_id)),
652 ty::InstanceDef::Intrinsic(def_id) => Some(ty::InstanceDef::Intrinsic(def_id)),
653 ty::InstanceDef::FnPtrShim(def_id, ty) => {
654 Some(ty::InstanceDef::FnPtrShim(def_id, tcx.lift(ty)?))
656 ty::InstanceDef::Virtual(def_id, n) => Some(ty::InstanceDef::Virtual(def_id, n)),
657 ty::InstanceDef::ClosureOnceShim { call_once, track_caller } => {
658 Some(ty::InstanceDef::ClosureOnceShim { call_once, track_caller })
660 ty::InstanceDef::DropGlue(def_id, ty) => {
661 Some(ty::InstanceDef::DropGlue(def_id, tcx.lift(ty)?))
663 ty::InstanceDef::CloneShim(def_id, ty) => {
664 Some(ty::InstanceDef::CloneShim(def_id, tcx.lift(ty)?))
670 ///////////////////////////////////////////////////////////////////////////
671 // TypeFoldable implementations.
673 /// AdtDefs are basically the same as a DefId.
674 impl<'tcx> TypeFoldable<'tcx> for ty::AdtDef<'tcx> {
675 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
678 ) -> Result<Self, F::Error> {
682 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
683 ControlFlow::CONTINUE
687 impl<'tcx, T: TypeFoldable<'tcx>, U: TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
688 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
691 ) -> Result<(T, U), F::Error> {
692 Ok((self.0.try_fold_with(folder)?, self.1.try_fold_with(folder)?))
695 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
696 self.0.visit_with(visitor)?;
697 self.1.visit_with(visitor)
701 impl<'tcx, A: TypeFoldable<'tcx>, B: TypeFoldable<'tcx>, C: TypeFoldable<'tcx>> TypeFoldable<'tcx>
704 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
707 ) -> Result<(A, B, C), F::Error> {
709 self.0.try_fold_with(folder)?,
710 self.1.try_fold_with(folder)?,
711 self.2.try_fold_with(folder)?,
715 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
716 self.0.visit_with(visitor)?;
717 self.1.visit_with(visitor)?;
718 self.2.visit_with(visitor)
722 EnumTypeFoldableImpl! {
723 impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
726 } where T: TypeFoldable<'tcx>
729 EnumTypeFoldableImpl! {
730 impl<'tcx, T, E> TypeFoldable<'tcx> for Result<T, E> {
733 } where T: TypeFoldable<'tcx>, E: TypeFoldable<'tcx>,
736 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
737 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
740 ) -> Result<Self, F::Error> {
741 // We merely want to replace the contained `T`, if at all possible,
742 // so that we don't needlessly allocate a new `Rc` or indeed clone
743 // the contained type.
745 // First step is to ensure that we have a unique reference to
746 // the contained type, which `Rc::make_mut` will accomplish (by
747 // allocating a new `Rc` and cloning the `T` only if required).
748 // This is done *before* casting to `Rc<ManuallyDrop<T>>` so that
749 // panicking during `make_mut` does not leak the `T`.
750 Rc::make_mut(&mut self);
752 // Casting to `Rc<ManuallyDrop<T>>` is safe because `ManuallyDrop`
753 // is `repr(transparent)`.
754 let ptr = Rc::into_raw(self).cast::<ManuallyDrop<T>>();
755 let mut unique = Rc::from_raw(ptr);
757 // Call to `Rc::make_mut` above guarantees that `unique` is the
758 // sole reference to the contained value, so we can avoid doing
759 // a checked `get_mut` here.
760 let slot = Rc::get_mut_unchecked(&mut unique);
762 // Semantically move the contained type out from `unique`, fold
763 // it, then move the folded value back into `unique`. Should
764 // folding fail, `ManuallyDrop` ensures that the "moved-out"
765 // value is not re-dropped.
766 let owned = ManuallyDrop::take(slot);
767 let folded = owned.try_fold_with(folder)?;
768 *slot = ManuallyDrop::new(folded);
770 // Cast back to `Rc<T>`.
771 Ok(Rc::from_raw(Rc::into_raw(unique).cast()))
775 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
776 (**self).visit_with(visitor)
780 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Arc<T> {
781 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
784 ) -> Result<Self, F::Error> {
785 // We merely want to replace the contained `T`, if at all possible,
786 // so that we don't needlessly allocate a new `Arc` or indeed clone
787 // the contained type.
789 // First step is to ensure that we have a unique reference to
790 // the contained type, which `Arc::make_mut` will accomplish (by
791 // allocating a new `Arc` and cloning the `T` only if required).
792 // This is done *before* casting to `Arc<ManuallyDrop<T>>` so that
793 // panicking during `make_mut` does not leak the `T`.
794 Arc::make_mut(&mut self);
796 // Casting to `Arc<ManuallyDrop<T>>` is safe because `ManuallyDrop`
797 // is `repr(transparent)`.
798 let ptr = Arc::into_raw(self).cast::<ManuallyDrop<T>>();
799 let mut unique = Arc::from_raw(ptr);
801 // Call to `Arc::make_mut` above guarantees that `unique` is the
802 // sole reference to the contained value, so we can avoid doing
803 // a checked `get_mut` here.
804 let slot = Arc::get_mut_unchecked(&mut unique);
806 // Semantically move the contained type out from `unique`, fold
807 // it, then move the folded value back into `unique`. Should
808 // folding fail, `ManuallyDrop` ensures that the "moved-out"
809 // value is not re-dropped.
810 let owned = ManuallyDrop::take(slot);
811 let folded = owned.try_fold_with(folder)?;
812 *slot = ManuallyDrop::new(folded);
814 // Cast back to `Arc<T>`.
815 Ok(Arc::from_raw(Arc::into_raw(unique).cast()))
819 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
820 (**self).visit_with(visitor)
824 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
825 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
828 ) -> Result<Self, F::Error> {
829 self.try_map_id(|value| value.try_fold_with(folder))
832 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
833 (**self).visit_with(visitor)
837 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
838 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
841 ) -> Result<Self, F::Error> {
842 self.try_map_id(|t| t.try_fold_with(folder))
845 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
846 self.iter().try_for_each(|t| t.visit_with(visitor))
850 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<[T]> {
851 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
854 ) -> Result<Self, F::Error> {
855 self.try_map_id(|t| t.try_fold_with(folder))
858 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
859 self.iter().try_for_each(|t| t.visit_with(visitor))
863 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::EarlyBinder<T> {
864 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
867 ) -> Result<Self, F::Error> {
868 self.try_map_bound(|ty| ty.try_fold_with(folder))
871 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
872 self.as_ref().0.visit_with(visitor)
876 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<'tcx, T> {
877 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
880 ) -> Result<Self, F::Error> {
881 self.try_map_bound(|ty| ty.try_fold_with(folder))
884 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
885 folder.try_fold_binder(self)
888 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
889 self.as_ref().skip_binder().visit_with(visitor)
892 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
893 visitor.visit_binder(self)
897 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>> {
898 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
901 ) -> Result<Self, F::Error> {
902 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_poly_existential_predicates(v))
905 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
906 self.iter().try_for_each(|p| p.visit_with(visitor))
910 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind> {
911 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
914 ) -> Result<Self, F::Error> {
915 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_projs(v))
918 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
919 self.iter().try_for_each(|t| t.visit_with(visitor))
923 impl<'tcx> TypeFoldable<'tcx> for ty::instance::Instance<'tcx> {
924 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
927 ) -> Result<Self, F::Error> {
928 use crate::ty::InstanceDef::*;
930 substs: self.substs.try_fold_with(folder)?,
931 def: match self.def {
932 Item(def) => Item(def.try_fold_with(folder)?),
933 VtableShim(did) => VtableShim(did.try_fold_with(folder)?),
934 ReifyShim(did) => ReifyShim(did.try_fold_with(folder)?),
935 Intrinsic(did) => Intrinsic(did.try_fold_with(folder)?),
936 FnPtrShim(did, ty) => {
937 FnPtrShim(did.try_fold_with(folder)?, ty.try_fold_with(folder)?)
939 Virtual(did, i) => Virtual(did.try_fold_with(folder)?, i),
940 ClosureOnceShim { call_once, track_caller } => {
941 ClosureOnceShim { call_once: call_once.try_fold_with(folder)?, track_caller }
943 DropGlue(did, ty) => {
944 DropGlue(did.try_fold_with(folder)?, ty.try_fold_with(folder)?)
946 CloneShim(did, ty) => {
947 CloneShim(did.try_fold_with(folder)?, ty.try_fold_with(folder)?)
953 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
954 use crate::ty::InstanceDef::*;
955 self.substs.visit_with(visitor)?;
957 Item(def) => def.visit_with(visitor),
958 VtableShim(did) | ReifyShim(did) | Intrinsic(did) | Virtual(did, _) => {
959 did.visit_with(visitor)
961 FnPtrShim(did, ty) | CloneShim(did, ty) => {
962 did.visit_with(visitor)?;
963 ty.visit_with(visitor)
965 DropGlue(did, ty) => {
966 did.visit_with(visitor)?;
967 ty.visit_with(visitor)
969 ClosureOnceShim { call_once, track_caller: _ } => call_once.visit_with(visitor),
974 impl<'tcx> TypeFoldable<'tcx> for interpret::GlobalId<'tcx> {
975 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
978 ) -> Result<Self, F::Error> {
979 Ok(Self { instance: self.instance.try_fold_with(folder)?, promoted: self.promoted })
982 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
983 self.instance.visit_with(visitor)
987 impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
988 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
991 ) -> Result<Self, F::Error> {
992 let kind = match *self.kind() {
993 ty::RawPtr(tm) => ty::RawPtr(tm.try_fold_with(folder)?),
994 ty::Array(typ, sz) => ty::Array(typ.try_fold_with(folder)?, sz.try_fold_with(folder)?),
995 ty::Slice(typ) => ty::Slice(typ.try_fold_with(folder)?),
996 ty::Adt(tid, substs) => ty::Adt(tid, substs.try_fold_with(folder)?),
997 ty::Dynamic(trait_ty, region) => {
998 ty::Dynamic(trait_ty.try_fold_with(folder)?, region.try_fold_with(folder)?)
1000 ty::Tuple(ts) => ty::Tuple(ts.try_fold_with(folder)?),
1001 ty::FnDef(def_id, substs) => ty::FnDef(def_id, substs.try_fold_with(folder)?),
1002 ty::FnPtr(f) => ty::FnPtr(f.try_fold_with(folder)?),
1003 ty::Ref(r, ty, mutbl) => {
1004 ty::Ref(r.try_fold_with(folder)?, ty.try_fold_with(folder)?, mutbl)
1006 ty::Generator(did, substs, movability) => {
1007 ty::Generator(did, substs.try_fold_with(folder)?, movability)
1009 ty::GeneratorWitness(types) => ty::GeneratorWitness(types.try_fold_with(folder)?),
1010 ty::Closure(did, substs) => ty::Closure(did, substs.try_fold_with(folder)?),
1011 ty::Projection(data) => ty::Projection(data.try_fold_with(folder)?),
1012 ty::Opaque(did, substs) => ty::Opaque(did, substs.try_fold_with(folder)?),
1024 | ty::Placeholder(..)
1026 | ty::Foreign(..) => return Ok(self),
1029 Ok(if *self.kind() == kind { self } else { folder.tcx().mk_ty(kind) })
1032 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
1033 folder.try_fold_ty(self)
1036 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1038 ty::RawPtr(ref tm) => tm.visit_with(visitor),
1039 ty::Array(typ, sz) => {
1040 typ.visit_with(visitor)?;
1041 sz.visit_with(visitor)
1043 ty::Slice(typ) => typ.visit_with(visitor),
1044 ty::Adt(_, substs) => substs.visit_with(visitor),
1045 ty::Dynamic(ref trait_ty, ref reg) => {
1046 trait_ty.visit_with(visitor)?;
1047 reg.visit_with(visitor)
1049 ty::Tuple(ts) => ts.visit_with(visitor),
1050 ty::FnDef(_, substs) => substs.visit_with(visitor),
1051 ty::FnPtr(ref f) => f.visit_with(visitor),
1052 ty::Ref(r, ty, _) => {
1053 r.visit_with(visitor)?;
1054 ty.visit_with(visitor)
1056 ty::Generator(_did, ref substs, _) => substs.visit_with(visitor),
1057 ty::GeneratorWitness(ref types) => types.visit_with(visitor),
1058 ty::Closure(_did, ref substs) => substs.visit_with(visitor),
1059 ty::Projection(ref data) => data.visit_with(visitor),
1060 ty::Opaque(_, ref substs) => substs.visit_with(visitor),
1071 | ty::Placeholder(..)
1074 | ty::Foreign(..) => ControlFlow::CONTINUE,
1078 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1079 visitor.visit_ty(*self)
1083 impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> {
1084 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1087 ) -> Result<Self, F::Error> {
1091 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
1092 folder.try_fold_region(self)
1095 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
1096 ControlFlow::CONTINUE
1099 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1100 visitor.visit_region(*self)
1104 impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
1105 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
1106 folder.try_fold_predicate(self)
1109 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1112 ) -> Result<Self, F::Error> {
1113 let new = self.kind().try_fold_with(folder)?;
1114 Ok(folder.tcx().reuse_or_mk_predicate(self, new))
1117 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1118 self.kind().visit_with(visitor)
1121 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1122 visitor.visit_predicate(*self)
1125 fn has_vars_bound_at_or_above(&self, binder: ty::DebruijnIndex) -> bool {
1126 self.outer_exclusive_binder() > binder
1129 fn has_type_flags(&self, flags: ty::TypeFlags) -> bool {
1130 self.flags().intersects(flags)
1134 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
1135 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1138 ) -> Result<Self, F::Error> {
1139 ty::util::fold_list(self, folder, |tcx, v| tcx.intern_predicates(v))
1142 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1143 self.iter().try_for_each(|p| p.visit_with(visitor))
1147 impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
1148 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1151 ) -> Result<Self, F::Error> {
1152 self.try_map_id(|x| x.try_fold_with(folder))
1155 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1156 self.iter().try_for_each(|t| t.visit_with(visitor))
1160 impl<'tcx> TypeFoldable<'tcx> for ty::Const<'tcx> {
1161 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1164 ) -> Result<Self, F::Error> {
1165 let ty = self.ty().try_fold_with(folder)?;
1166 let val = self.val().try_fold_with(folder)?;
1167 if ty != self.ty() || val != self.val() {
1168 Ok(folder.tcx().mk_const(ty::ConstS { ty, val }))
1174 fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
1175 folder.try_fold_const(self)
1178 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1179 self.ty().visit_with(visitor)?;
1180 self.val().visit_with(visitor)
1183 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1184 visitor.visit_const(*self)
1188 impl<'tcx> TypeFoldable<'tcx> for ty::ConstKind<'tcx> {
1189 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1192 ) -> Result<Self, F::Error> {
1194 ty::ConstKind::Infer(ic) => ty::ConstKind::Infer(ic.try_fold_with(folder)?),
1195 ty::ConstKind::Param(p) => ty::ConstKind::Param(p.try_fold_with(folder)?),
1196 ty::ConstKind::Unevaluated(uv) => ty::ConstKind::Unevaluated(uv.try_fold_with(folder)?),
1197 ty::ConstKind::Value(_)
1198 | ty::ConstKind::Bound(..)
1199 | ty::ConstKind::Placeholder(..)
1200 | ty::ConstKind::Error(_) => self,
1204 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1206 ty::ConstKind::Infer(ic) => ic.visit_with(visitor),
1207 ty::ConstKind::Param(p) => p.visit_with(visitor),
1208 ty::ConstKind::Unevaluated(uv) => uv.visit_with(visitor),
1209 ty::ConstKind::Value(_)
1210 | ty::ConstKind::Bound(..)
1211 | ty::ConstKind::Placeholder(_)
1212 | ty::ConstKind::Error(_) => ControlFlow::CONTINUE,
1217 impl<'tcx> TypeFoldable<'tcx> for InferConst<'tcx> {
1218 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1221 ) -> Result<Self, F::Error> {
1225 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
1226 ControlFlow::CONTINUE
1230 impl<'tcx> TypeFoldable<'tcx> for ty::Unevaluated<'tcx> {
1231 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1234 ) -> Result<Self, F::Error> {
1235 Ok(ty::Unevaluated {
1237 substs: self.substs.try_fold_with(folder)?,
1238 promoted: self.promoted,
1242 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1243 visitor.visit_unevaluated(*self)
1246 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1247 self.substs.visit_with(visitor)
1251 impl<'tcx> TypeFoldable<'tcx> for ty::Unevaluated<'tcx, ()> {
1252 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1255 ) -> Result<Self, F::Error> {
1256 Ok(ty::Unevaluated {
1258 substs: self.substs.try_fold_with(folder)?,
1259 promoted: self.promoted,
1263 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1264 visitor.visit_unevaluated(self.expand())
1267 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1268 self.substs.visit_with(visitor)
1272 impl<'tcx> TypeFoldable<'tcx> for hir::Constness {
1273 fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(self, _: &mut F) -> Result<Self, F::Error> {
1277 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<V::BreakTy> {
1278 ControlFlow::CONTINUE