1 //! This module contains implements of the `Lift` and `TypeFoldable`
2 //! traits for various types in the Rust compiler. Most are written by
3 //! hand, though we've recently added some macros and proc-macros to help with the tedium.
5 use crate::mir::interpret;
6 use crate::mir::ProjectionKind;
7 use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
8 use crate::ty::print::{with_no_trimmed_paths, FmtPrinter, Printer};
9 use crate::ty::{self, InferConst, Lift, Ty, TyCtxt};
11 use rustc_hir::def::Namespace;
12 use rustc_hir::def_id::CRATE_DEF_INDEX;
13 use rustc_index::vec::{Idx, IndexVec};
15 use smallvec::SmallVec;
17 use std::ops::ControlFlow;
21 impl fmt::Debug for ty::TraitDef {
22 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24 with_no_trimmed_paths(|| {
25 FmtPrinter::new(tcx, f, Namespace::TypeNS).print_def_path(self.def_id, &[])
32 impl fmt::Debug for ty::AdtDef {
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35 with_no_trimmed_paths(|| {
36 FmtPrinter::new(tcx, f, Namespace::TypeNS).print_def_path(self.did, &[])
43 impl fmt::Debug for ty::UpvarId {
44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45 let name = ty::tls::with(|tcx| tcx.hir().name(self.var_path.hir_id));
46 write!(f, "UpvarId({:?};`{}`;{:?})", self.var_path.hir_id, name, self.closure_expr_id)
50 impl fmt::Debug for ty::UpvarBorrow<'tcx> {
51 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52 write!(f, "UpvarBorrow({:?}, {:?})", self.kind, self.region)
56 impl fmt::Debug for ty::ExistentialTraitRef<'tcx> {
57 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58 with_no_trimmed_paths(|| fmt::Display::fmt(self, f))
62 impl fmt::Debug for ty::adjustment::Adjustment<'tcx> {
63 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64 write!(f, "{:?} -> {}", self.kind, self.target)
68 impl fmt::Debug for ty::BoundRegion {
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71 ty::BrAnon(n) => write!(f, "BrAnon({:?})", n),
72 ty::BrNamed(did, name) => {
73 if did.index == CRATE_DEF_INDEX {
74 write!(f, "BrNamed({})", name)
76 write!(f, "BrNamed({:?}, {})", did, name)
79 ty::BrEnv => write!(f, "BrEnv"),
84 impl fmt::Debug for ty::RegionKind {
85 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87 ty::ReEarlyBound(ref data) => write!(f, "ReEarlyBound({}, {})", data.index, data.name),
89 ty::ReLateBound(binder_id, ref bound_region) => {
90 write!(f, "ReLateBound({:?}, {:?})", binder_id, bound_region)
93 ty::ReFree(ref fr) => fr.fmt(f),
95 ty::ReStatic => write!(f, "ReStatic"),
97 ty::ReVar(ref vid) => vid.fmt(f),
99 ty::RePlaceholder(placeholder) => write!(f, "RePlaceholder({:?})", placeholder),
101 ty::ReEmpty(ui) => write!(f, "ReEmpty({:?})", ui),
103 ty::ReErased => write!(f, "ReErased"),
108 impl fmt::Debug for ty::FreeRegion {
109 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110 write!(f, "ReFree({:?}, {:?})", self.scope, self.bound_region)
114 impl fmt::Debug for ty::Variance {
115 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
116 f.write_str(match *self {
117 ty::Covariant => "+",
118 ty::Contravariant => "-",
119 ty::Invariant => "o",
120 ty::Bivariant => "*",
125 impl fmt::Debug for ty::FnSig<'tcx> {
126 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127 write!(f, "({:?}; c_variadic: {})->{:?}", self.inputs(), self.c_variadic, self.output())
131 impl fmt::Debug for ty::TyVid {
132 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
133 write!(f, "_#{}t", self.index)
137 impl<'tcx> fmt::Debug for ty::ConstVid<'tcx> {
138 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
139 write!(f, "_#{}c", self.index)
143 impl fmt::Debug for ty::IntVid {
144 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
145 write!(f, "_#{}i", self.index)
149 impl fmt::Debug for ty::FloatVid {
150 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
151 write!(f, "_#{}f", self.index)
155 impl fmt::Debug for ty::RegionVid {
156 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
157 write!(f, "'_#{}r", self.index())
161 impl fmt::Debug for ty::InferTy {
162 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
164 ty::TyVar(ref v) => v.fmt(f),
165 ty::IntVar(ref v) => v.fmt(f),
166 ty::FloatVar(ref v) => v.fmt(f),
167 ty::FreshTy(v) => write!(f, "FreshTy({:?})", v),
168 ty::FreshIntTy(v) => write!(f, "FreshIntTy({:?})", v),
169 ty::FreshFloatTy(v) => write!(f, "FreshFloatTy({:?})", v),
174 impl fmt::Debug for ty::IntVarValue {
175 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
177 ty::IntType(ref v) => v.fmt(f),
178 ty::UintType(ref v) => v.fmt(f),
183 impl fmt::Debug for ty::FloatVarValue {
184 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
189 impl fmt::Debug for ty::TraitRef<'tcx> {
190 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
191 with_no_trimmed_paths(|| fmt::Display::fmt(self, f))
195 impl fmt::Debug for Ty<'tcx> {
196 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
197 with_no_trimmed_paths(|| fmt::Display::fmt(self, f))
201 impl fmt::Debug for ty::ParamTy {
202 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
203 write!(f, "{}/#{}", self.name, self.index)
207 impl fmt::Debug for ty::ParamConst {
208 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
209 write!(f, "{}/#{}", self.name, self.index)
213 impl fmt::Debug for ty::TraitPredicate<'tcx> {
214 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
215 write!(f, "TraitPredicate({:?})", self.trait_ref)
219 impl fmt::Debug for ty::ProjectionPredicate<'tcx> {
220 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
221 write!(f, "ProjectionPredicate({:?}, {:?})", self.projection_ty, self.ty)
225 impl fmt::Debug for ty::Predicate<'tcx> {
226 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
227 write!(f, "{:?}", self.kind())
231 impl fmt::Debug for ty::PredicateKind<'tcx> {
232 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
234 ty::PredicateKind::ForAll(binder) => write!(f, "ForAll({:?})", binder),
235 ty::PredicateKind::Atom(atom) => write!(f, "{:?}", atom),
240 impl fmt::Debug for ty::PredicateAtom<'tcx> {
241 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
243 ty::PredicateAtom::Trait(ref a, constness) => {
244 if let hir::Constness::Const = constness {
245 write!(f, "const ")?;
249 ty::PredicateAtom::Subtype(ref pair) => pair.fmt(f),
250 ty::PredicateAtom::RegionOutlives(ref pair) => pair.fmt(f),
251 ty::PredicateAtom::TypeOutlives(ref pair) => pair.fmt(f),
252 ty::PredicateAtom::Projection(ref pair) => pair.fmt(f),
253 ty::PredicateAtom::WellFormed(data) => write!(f, "WellFormed({:?})", data),
254 ty::PredicateAtom::ObjectSafe(trait_def_id) => {
255 write!(f, "ObjectSafe({:?})", trait_def_id)
257 ty::PredicateAtom::ClosureKind(closure_def_id, closure_substs, kind) => {
258 write!(f, "ClosureKind({:?}, {:?}, {:?})", closure_def_id, closure_substs, kind)
260 ty::PredicateAtom::ConstEvaluatable(def_id, substs) => {
261 write!(f, "ConstEvaluatable({:?}, {:?})", def_id, substs)
263 ty::PredicateAtom::ConstEquate(c1, c2) => write!(f, "ConstEquate({:?}, {:?})", c1, c2),
264 ty::PredicateAtom::TypeWellFormedFromEnv(ty) => {
265 write!(f, "TypeWellFormedFromEnv({:?})", ty)
271 ///////////////////////////////////////////////////////////////////////////
274 // For things that don't carry any arena-allocated data (and are
275 // copy...), just add them to this list.
277 CloneTypeFoldableAndLiftImpls! {
281 ::rustc_target::abi::VariantIdx,
285 crate::middle::region::Scope,
286 ::rustc_ast::FloatTy,
287 ::rustc_ast::InlineAsmOptions,
288 ::rustc_ast::InlineAsmTemplatePiece,
290 ::rustc_span::symbol::Symbol,
291 ::rustc_hir::def::Res,
292 ::rustc_hir::def_id::DefId,
293 ::rustc_hir::def_id::LocalDefId,
295 ::rustc_hir::LlvmInlineAsmInner,
296 ::rustc_hir::MatchSource,
297 ::rustc_hir::Mutability,
298 ::rustc_hir::Unsafety,
299 ::rustc_target::asm::InlineAsmRegOrRegClass,
300 ::rustc_target::spec::abi::Abi,
301 crate::mir::coverage::ExpressionOperandId,
302 crate::mir::coverage::CounterValueReference,
303 crate::mir::coverage::InjectedExpressionId,
304 crate::mir::coverage::InjectedExpressionIndex,
305 crate::mir::coverage::MappedExpressionIndex,
307 crate::mir::Promoted,
308 crate::traits::Reveal,
309 crate::ty::adjustment::AutoBorrowMutability,
311 // Including `BoundRegion` is a *bit* dubious, but direct
312 // references to bound region appear in `ty::Error`, and aren't
313 // really meant to be folded. In general, we can only fold a fully
315 crate::ty::BoundRegion,
316 crate::ty::AssocItem,
317 crate::ty::Placeholder<crate::ty::BoundRegion>,
318 crate::ty::ClosureKind,
319 crate::ty::FreeRegion,
321 crate::ty::IntVarValue,
322 crate::ty::ParamConst,
324 crate::ty::adjustment::PointerCast,
325 crate::ty::RegionVid,
326 crate::ty::UniverseIndex,
331 ///////////////////////////////////////////////////////////////////////////
332 // Lift implementations
334 // FIXME(eddyb) replace all the uses of `Option::map` with `?`.
335 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>> Lift<'tcx> for (A, B) {
336 type Lifted = (A::Lifted, B::Lifted);
337 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
338 Some((tcx.lift(self.0)?, tcx.lift(self.1)?))
342 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>, C: Lift<'tcx>> Lift<'tcx> for (A, B, C) {
343 type Lifted = (A::Lifted, B::Lifted, C::Lifted);
344 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
345 Some((tcx.lift(self.0)?, tcx.lift(self.1)?, tcx.lift(self.2)?))
349 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Option<T> {
350 type Lifted = Option<T::Lifted>;
351 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
353 Some(x) => tcx.lift(x).map(Some),
359 impl<'tcx, T: Lift<'tcx>, E: Lift<'tcx>> Lift<'tcx> for Result<T, E> {
360 type Lifted = Result<T::Lifted, E::Lifted>;
361 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
363 Ok(x) => tcx.lift(x).map(Ok),
364 Err(e) => tcx.lift(e).map(Err),
369 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Box<T> {
370 type Lifted = Box<T::Lifted>;
371 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
372 tcx.lift(*self).map(Box::new)
376 impl<'tcx, T: Lift<'tcx> + Clone> Lift<'tcx> for Rc<T> {
377 type Lifted = Rc<T::Lifted>;
378 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
379 tcx.lift(self.as_ref().clone()).map(Rc::new)
383 impl<'tcx, T: Lift<'tcx> + Clone> Lift<'tcx> for Arc<T> {
384 type Lifted = Arc<T::Lifted>;
385 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
386 tcx.lift(self.as_ref().clone()).map(Arc::new)
389 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Vec<T> {
390 type Lifted = Vec<T::Lifted>;
391 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
392 self.into_iter().map(|v| tcx.lift(v)).collect()
396 impl<'tcx, I: Idx, T: Lift<'tcx>> Lift<'tcx> for IndexVec<I, T> {
397 type Lifted = IndexVec<I, T::Lifted>;
398 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
399 self.into_iter().map(|e| tcx.lift(e)).collect()
403 impl<'a, 'tcx> Lift<'tcx> for ty::TraitRef<'a> {
404 type Lifted = ty::TraitRef<'tcx>;
405 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
406 tcx.lift(self.substs).map(|substs| ty::TraitRef { def_id: self.def_id, substs })
410 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialTraitRef<'a> {
411 type Lifted = ty::ExistentialTraitRef<'tcx>;
412 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
413 tcx.lift(self.substs).map(|substs| ty::ExistentialTraitRef { def_id: self.def_id, substs })
417 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialPredicate<'a> {
418 type Lifted = ty::ExistentialPredicate<'tcx>;
419 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
421 ty::ExistentialPredicate::Trait(x) => tcx.lift(x).map(ty::ExistentialPredicate::Trait),
422 ty::ExistentialPredicate::Projection(x) => {
423 tcx.lift(x).map(ty::ExistentialPredicate::Projection)
425 ty::ExistentialPredicate::AutoTrait(def_id) => {
426 Some(ty::ExistentialPredicate::AutoTrait(def_id))
432 impl<'a, 'tcx> Lift<'tcx> for ty::TraitPredicate<'a> {
433 type Lifted = ty::TraitPredicate<'tcx>;
434 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::TraitPredicate<'tcx>> {
435 tcx.lift(self.trait_ref).map(|trait_ref| ty::TraitPredicate { trait_ref })
439 impl<'a, 'tcx> Lift<'tcx> for ty::SubtypePredicate<'a> {
440 type Lifted = ty::SubtypePredicate<'tcx>;
441 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::SubtypePredicate<'tcx>> {
442 tcx.lift((self.a, self.b)).map(|(a, b)| ty::SubtypePredicate {
443 a_is_expected: self.a_is_expected,
450 impl<'tcx, A: Copy + Lift<'tcx>, B: Copy + Lift<'tcx>> Lift<'tcx> for ty::OutlivesPredicate<A, B> {
451 type Lifted = ty::OutlivesPredicate<A::Lifted, B::Lifted>;
452 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
453 tcx.lift((self.0, self.1)).map(|(a, b)| ty::OutlivesPredicate(a, b))
457 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionTy<'a> {
458 type Lifted = ty::ProjectionTy<'tcx>;
459 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::ProjectionTy<'tcx>> {
460 tcx.lift(self.substs)
461 .map(|substs| ty::ProjectionTy { item_def_id: self.item_def_id, substs })
465 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionPredicate<'a> {
466 type Lifted = ty::ProjectionPredicate<'tcx>;
467 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::ProjectionPredicate<'tcx>> {
468 tcx.lift((self.projection_ty, self.ty))
469 .map(|(projection_ty, ty)| ty::ProjectionPredicate { projection_ty, ty })
473 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialProjection<'a> {
474 type Lifted = ty::ExistentialProjection<'tcx>;
475 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
476 tcx.lift(self.substs).map(|substs| ty::ExistentialProjection {
478 ty: tcx.lift(self.ty).expect("type must lift when substs do"),
479 item_def_id: self.item_def_id,
484 impl<'a, 'tcx> Lift<'tcx> for ty::PredicateKind<'a> {
485 type Lifted = ty::PredicateKind<'tcx>;
486 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
488 ty::PredicateKind::ForAll(binder) => tcx.lift(binder).map(ty::PredicateKind::ForAll),
489 ty::PredicateKind::Atom(atom) => tcx.lift(atom).map(ty::PredicateKind::Atom),
494 impl<'a, 'tcx> Lift<'tcx> for ty::PredicateAtom<'a> {
495 type Lifted = ty::PredicateAtom<'tcx>;
496 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
498 ty::PredicateAtom::Trait(data, constness) => {
499 tcx.lift(data).map(|data| ty::PredicateAtom::Trait(data, constness))
501 ty::PredicateAtom::Subtype(data) => tcx.lift(data).map(ty::PredicateAtom::Subtype),
502 ty::PredicateAtom::RegionOutlives(data) => {
503 tcx.lift(data).map(ty::PredicateAtom::RegionOutlives)
505 ty::PredicateAtom::TypeOutlives(data) => {
506 tcx.lift(data).map(ty::PredicateAtom::TypeOutlives)
508 ty::PredicateAtom::Projection(data) => {
509 tcx.lift(data).map(ty::PredicateAtom::Projection)
511 ty::PredicateAtom::WellFormed(ty) => tcx.lift(ty).map(ty::PredicateAtom::WellFormed),
512 ty::PredicateAtom::ClosureKind(closure_def_id, closure_substs, kind) => {
513 tcx.lift(closure_substs).map(|closure_substs| {
514 ty::PredicateAtom::ClosureKind(closure_def_id, closure_substs, kind)
517 ty::PredicateAtom::ObjectSafe(trait_def_id) => {
518 Some(ty::PredicateAtom::ObjectSafe(trait_def_id))
520 ty::PredicateAtom::ConstEvaluatable(def_id, substs) => {
521 tcx.lift(substs).map(|substs| ty::PredicateAtom::ConstEvaluatable(def_id, substs))
523 ty::PredicateAtom::ConstEquate(c1, c2) => {
524 tcx.lift((c1, c2)).map(|(c1, c2)| ty::PredicateAtom::ConstEquate(c1, c2))
526 ty::PredicateAtom::TypeWellFormedFromEnv(ty) => {
527 tcx.lift(ty).map(ty::PredicateAtom::TypeWellFormedFromEnv)
533 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::Binder<T> {
534 type Lifted = ty::Binder<T::Lifted>;
535 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
536 self.map_bound(|v| tcx.lift(v)).transpose()
540 impl<'a, 'tcx> Lift<'tcx> for ty::ParamEnv<'a> {
541 type Lifted = ty::ParamEnv<'tcx>;
542 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
543 tcx.lift(self.caller_bounds())
544 .map(|caller_bounds| ty::ParamEnv::new(caller_bounds, self.reveal()))
548 impl<'a, 'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::ParamEnvAnd<'a, T> {
549 type Lifted = ty::ParamEnvAnd<'tcx, T::Lifted>;
550 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
551 tcx.lift(self.param_env).and_then(|param_env| {
552 tcx.lift(self.value).map(|value| ty::ParamEnvAnd { param_env, value })
557 impl<'a, 'tcx> Lift<'tcx> for ty::ClosureSubsts<'a> {
558 type Lifted = ty::ClosureSubsts<'tcx>;
559 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
560 tcx.lift(self.substs).map(|substs| ty::ClosureSubsts { substs })
564 impl<'a, 'tcx> Lift<'tcx> for ty::GeneratorSubsts<'a> {
565 type Lifted = ty::GeneratorSubsts<'tcx>;
566 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
567 tcx.lift(self.substs).map(|substs| ty::GeneratorSubsts { substs })
571 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjustment<'a> {
572 type Lifted = ty::adjustment::Adjustment<'tcx>;
573 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
574 let ty::adjustment::Adjustment { kind, target } = self;
575 tcx.lift(kind).and_then(|kind| {
576 tcx.lift(target).map(|target| ty::adjustment::Adjustment { kind, target })
581 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjust<'a> {
582 type Lifted = ty::adjustment::Adjust<'tcx>;
583 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
585 ty::adjustment::Adjust::NeverToAny => Some(ty::adjustment::Adjust::NeverToAny),
586 ty::adjustment::Adjust::Pointer(ptr) => Some(ty::adjustment::Adjust::Pointer(ptr)),
587 ty::adjustment::Adjust::Deref(overloaded) => {
588 tcx.lift(overloaded).map(ty::adjustment::Adjust::Deref)
590 ty::adjustment::Adjust::Borrow(autoref) => {
591 tcx.lift(autoref).map(ty::adjustment::Adjust::Borrow)
597 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::OverloadedDeref<'a> {
598 type Lifted = ty::adjustment::OverloadedDeref<'tcx>;
599 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
600 tcx.lift(self.region).map(|region| ty::adjustment::OverloadedDeref {
608 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::AutoBorrow<'a> {
609 type Lifted = ty::adjustment::AutoBorrow<'tcx>;
610 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
612 ty::adjustment::AutoBorrow::Ref(r, m) => {
613 tcx.lift(r).map(|r| ty::adjustment::AutoBorrow::Ref(r, m))
615 ty::adjustment::AutoBorrow::RawPtr(m) => Some(ty::adjustment::AutoBorrow::RawPtr(m)),
620 impl<'a, 'tcx> Lift<'tcx> for ty::GenSig<'a> {
621 type Lifted = ty::GenSig<'tcx>;
622 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
623 tcx.lift((self.resume_ty, self.yield_ty, self.return_ty))
624 .map(|(resume_ty, yield_ty, return_ty)| ty::GenSig { resume_ty, yield_ty, return_ty })
628 impl<'a, 'tcx> Lift<'tcx> for ty::FnSig<'a> {
629 type Lifted = ty::FnSig<'tcx>;
630 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
631 tcx.lift(self.inputs_and_output).map(|x| ty::FnSig {
632 inputs_and_output: x,
633 c_variadic: self.c_variadic,
634 unsafety: self.unsafety,
640 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::error::ExpectedFound<T> {
641 type Lifted = ty::error::ExpectedFound<T::Lifted>;
642 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
643 let ty::error::ExpectedFound { expected, found } = self;
644 tcx.lift(expected).and_then(|expected| {
645 tcx.lift(found).map(|found| ty::error::ExpectedFound { expected, found })
650 impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> {
651 type Lifted = ty::error::TypeError<'tcx>;
652 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
653 use crate::ty::error::TypeError::*;
656 Mismatch => Mismatch,
657 UnsafetyMismatch(x) => UnsafetyMismatch(x),
658 AbiMismatch(x) => AbiMismatch(x),
659 Mutability => Mutability,
660 TupleSize(x) => TupleSize(x),
661 FixedArraySize(x) => FixedArraySize(x),
662 ArgCount => ArgCount,
663 RegionsDoesNotOutlive(a, b) => {
664 return tcx.lift((a, b)).map(|(a, b)| RegionsDoesNotOutlive(a, b));
666 RegionsInsufficientlyPolymorphic(a, b) => {
667 return tcx.lift(b).map(|b| RegionsInsufficientlyPolymorphic(a, b));
669 RegionsOverlyPolymorphic(a, b) => {
670 return tcx.lift(b).map(|b| RegionsOverlyPolymorphic(a, b));
672 RegionsPlaceholderMismatch => RegionsPlaceholderMismatch,
673 IntMismatch(x) => IntMismatch(x),
674 FloatMismatch(x) => FloatMismatch(x),
675 Traits(x) => Traits(x),
676 VariadicMismatch(x) => VariadicMismatch(x),
677 CyclicTy(t) => return tcx.lift(t).map(|t| CyclicTy(t)),
678 CyclicConst(ct) => return tcx.lift(ct).map(|ct| CyclicConst(ct)),
679 ProjectionMismatched(x) => ProjectionMismatched(x),
680 Sorts(x) => return tcx.lift(x).map(Sorts),
681 ExistentialMismatch(x) => return tcx.lift(x).map(ExistentialMismatch),
682 ConstMismatch(x) => return tcx.lift(x).map(ConstMismatch),
683 IntrinsicCast => IntrinsicCast,
684 TargetFeatureCast(x) => TargetFeatureCast(x),
685 ObjectUnsafeCoercion(x) => return tcx.lift(x).map(ObjectUnsafeCoercion),
690 impl<'a, 'tcx> Lift<'tcx> for ty::InstanceDef<'a> {
691 type Lifted = ty::InstanceDef<'tcx>;
692 fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
694 ty::InstanceDef::Item(def_id) => Some(ty::InstanceDef::Item(def_id)),
695 ty::InstanceDef::VtableShim(def_id) => Some(ty::InstanceDef::VtableShim(def_id)),
696 ty::InstanceDef::ReifyShim(def_id) => Some(ty::InstanceDef::ReifyShim(def_id)),
697 ty::InstanceDef::Intrinsic(def_id) => Some(ty::InstanceDef::Intrinsic(def_id)),
698 ty::InstanceDef::FnPtrShim(def_id, ty) => {
699 Some(ty::InstanceDef::FnPtrShim(def_id, tcx.lift(ty)?))
701 ty::InstanceDef::Virtual(def_id, n) => Some(ty::InstanceDef::Virtual(def_id, n)),
702 ty::InstanceDef::ClosureOnceShim { call_once } => {
703 Some(ty::InstanceDef::ClosureOnceShim { call_once })
705 ty::InstanceDef::DropGlue(def_id, ty) => {
706 Some(ty::InstanceDef::DropGlue(def_id, tcx.lift(ty)?))
708 ty::InstanceDef::CloneShim(def_id, ty) => {
709 Some(ty::InstanceDef::CloneShim(def_id, tcx.lift(ty)?))
715 ///////////////////////////////////////////////////////////////////////////
716 // TypeFoldable implementations.
718 // Ideally, each type should invoke `folder.fold_foo(self)` and
719 // nothing else. In some cases, though, we haven't gotten around to
720 // adding methods on the `folder` yet, and thus the folding is
721 // hard-coded here. This is less-flexible, because folders cannot
722 // override the behavior, but there are a lot of random types and one
723 // can easily refactor the folding into the TypeFolder trait as
726 /// AdtDefs are basically the same as a DefId.
727 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::AdtDef {
728 fn super_fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> Self {
732 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<()> {
733 ControlFlow::CONTINUE
737 impl<'tcx, T: TypeFoldable<'tcx>, U: TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
738 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> (T, U) {
739 (self.0.fold_with(folder), self.1.fold_with(folder))
742 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
743 self.0.visit_with(visitor)?;
744 self.1.visit_with(visitor)
748 impl<'tcx, A: TypeFoldable<'tcx>, B: TypeFoldable<'tcx>, C: TypeFoldable<'tcx>> TypeFoldable<'tcx>
751 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> (A, B, C) {
752 (self.0.fold_with(folder), self.1.fold_with(folder), self.2.fold_with(folder))
755 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
756 self.0.visit_with(visitor)?;
757 self.1.visit_with(visitor)?;
758 self.2.visit_with(visitor)
762 EnumTypeFoldableImpl! {
763 impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
766 } where T: TypeFoldable<'tcx>
769 EnumTypeFoldableImpl! {
770 impl<'tcx, T, E> TypeFoldable<'tcx> for Result<T, E> {
773 } where T: TypeFoldable<'tcx>, E: TypeFoldable<'tcx>,
776 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
777 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
778 Rc::new((**self).fold_with(folder))
781 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
782 (**self).visit_with(visitor)
786 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Arc<T> {
787 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
788 Arc::new((**self).fold_with(folder))
791 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
792 (**self).visit_with(visitor)
796 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
797 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
798 let content: T = (**self).fold_with(folder);
802 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
803 (**self).visit_with(visitor)
807 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
808 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
809 self.iter().map(|t| t.fold_with(folder)).collect()
812 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
813 self.iter().try_for_each(|t| t.visit_with(visitor))
817 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<[T]> {
818 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
819 self.iter().map(|t| t.fold_with(folder)).collect::<Vec<_>>().into_boxed_slice()
822 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
823 self.iter().try_for_each(|t| t.visit_with(visitor))
827 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<T> {
828 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
829 self.map_bound_ref(|ty| ty.fold_with(folder))
832 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
833 folder.fold_binder(self)
836 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
837 self.as_ref().skip_binder().visit_with(visitor)
840 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
841 visitor.visit_binder(self)
845 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::ExistentialPredicate<'tcx>> {
846 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
847 fold_list(*self, folder, |tcx, v| tcx.intern_existential_predicates(v))
850 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
851 self.iter().try_for_each(|p| p.visit_with(visitor))
855 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<Ty<'tcx>> {
856 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
857 fold_list(*self, folder, |tcx, v| tcx.intern_type_list(v))
860 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
861 self.iter().try_for_each(|t| t.visit_with(visitor))
865 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind> {
866 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
867 fold_list(*self, folder, |tcx, v| tcx.intern_projs(v))
870 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
871 self.iter().try_for_each(|t| t.visit_with(visitor))
875 impl<'tcx> TypeFoldable<'tcx> for ty::instance::Instance<'tcx> {
876 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
877 use crate::ty::InstanceDef::*;
879 substs: self.substs.fold_with(folder),
880 def: match self.def {
881 Item(def) => Item(def.fold_with(folder)),
882 VtableShim(did) => VtableShim(did.fold_with(folder)),
883 ReifyShim(did) => ReifyShim(did.fold_with(folder)),
884 Intrinsic(did) => Intrinsic(did.fold_with(folder)),
885 FnPtrShim(did, ty) => FnPtrShim(did.fold_with(folder), ty.fold_with(folder)),
886 Virtual(did, i) => Virtual(did.fold_with(folder), i),
887 ClosureOnceShim { call_once } => {
888 ClosureOnceShim { call_once: call_once.fold_with(folder) }
890 DropGlue(did, ty) => DropGlue(did.fold_with(folder), ty.fold_with(folder)),
891 CloneShim(did, ty) => CloneShim(did.fold_with(folder), ty.fold_with(folder)),
896 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
897 use crate::ty::InstanceDef::*;
898 self.substs.visit_with(visitor)?;
900 Item(def) => def.visit_with(visitor),
901 VtableShim(did) | ReifyShim(did) | Intrinsic(did) | Virtual(did, _) => {
902 did.visit_with(visitor)
904 FnPtrShim(did, ty) | CloneShim(did, ty) => {
905 did.visit_with(visitor)?;
906 ty.visit_with(visitor)
908 DropGlue(did, ty) => {
909 did.visit_with(visitor)?;
910 ty.visit_with(visitor)
912 ClosureOnceShim { call_once } => call_once.visit_with(visitor),
917 impl<'tcx> TypeFoldable<'tcx> for interpret::GlobalId<'tcx> {
918 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
919 Self { instance: self.instance.fold_with(folder), promoted: self.promoted }
922 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
923 self.instance.visit_with(visitor)
927 impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
928 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
929 let kind = match self.kind() {
930 ty::RawPtr(tm) => ty::RawPtr(tm.fold_with(folder)),
931 ty::Array(typ, sz) => ty::Array(typ.fold_with(folder), sz.fold_with(folder)),
932 ty::Slice(typ) => ty::Slice(typ.fold_with(folder)),
933 ty::Adt(tid, substs) => ty::Adt(*tid, substs.fold_with(folder)),
934 ty::Dynamic(ref trait_ty, ref region) => {
935 ty::Dynamic(trait_ty.fold_with(folder), region.fold_with(folder))
937 ty::Tuple(ts) => ty::Tuple(ts.fold_with(folder)),
938 ty::FnDef(def_id, substs) => ty::FnDef(*def_id, substs.fold_with(folder)),
939 ty::FnPtr(f) => ty::FnPtr(f.fold_with(folder)),
940 ty::Ref(ref r, ty, mutbl) => ty::Ref(r.fold_with(folder), ty.fold_with(folder), *mutbl),
941 ty::Generator(did, substs, movability) => {
942 ty::Generator(*did, substs.fold_with(folder), *movability)
944 ty::GeneratorWitness(types) => ty::GeneratorWitness(types.fold_with(folder)),
945 ty::Closure(did, substs) => ty::Closure(*did, substs.fold_with(folder)),
946 ty::Projection(ref data) => ty::Projection(data.fold_with(folder)),
947 ty::Opaque(did, substs) => ty::Opaque(*did, substs.fold_with(folder)),
959 | ty::Placeholder(..)
961 | ty::Foreign(..) => return self,
964 if *self.kind() == kind { self } else { folder.tcx().mk_ty(kind) }
967 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
968 folder.fold_ty(*self)
971 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
973 ty::RawPtr(ref tm) => tm.visit_with(visitor),
974 ty::Array(typ, sz) => {
975 typ.visit_with(visitor)?;
976 sz.visit_with(visitor)
978 ty::Slice(typ) => typ.visit_with(visitor),
979 ty::Adt(_, substs) => substs.visit_with(visitor),
980 ty::Dynamic(ref trait_ty, ref reg) => {
981 trait_ty.visit_with(visitor)?;
982 reg.visit_with(visitor)
984 ty::Tuple(ts) => ts.visit_with(visitor),
985 ty::FnDef(_, substs) => substs.visit_with(visitor),
986 ty::FnPtr(ref f) => f.visit_with(visitor),
987 ty::Ref(r, ty, _) => {
988 r.visit_with(visitor)?;
989 ty.visit_with(visitor)
991 ty::Generator(_did, ref substs, _) => substs.visit_with(visitor),
992 ty::GeneratorWitness(ref types) => types.visit_with(visitor),
993 ty::Closure(_did, ref substs) => substs.visit_with(visitor),
994 ty::Projection(ref data) => data.visit_with(visitor),
995 ty::Opaque(_, ref substs) => substs.visit_with(visitor),
1006 | ty::Placeholder(..)
1009 | ty::Foreign(..) => ControlFlow::CONTINUE,
1013 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
1014 visitor.visit_ty(self)
1018 impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> {
1019 fn super_fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> Self {
1023 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1024 folder.fold_region(*self)
1027 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<()> {
1028 ControlFlow::CONTINUE
1031 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
1032 visitor.visit_region(*self)
1036 impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
1037 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1038 let new = ty::PredicateKind::super_fold_with(&self.inner.kind, folder);
1039 folder.tcx().reuse_or_mk_predicate(*self, new)
1042 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
1043 ty::PredicateKind::super_visit_with(&self.inner.kind, visitor)
1046 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
1047 visitor.visit_predicate(*self)
1050 fn has_vars_bound_at_or_above(&self, binder: ty::DebruijnIndex) -> bool {
1051 self.inner.outer_exclusive_binder > binder
1054 fn has_type_flags(&self, flags: ty::TypeFlags) -> bool {
1055 self.inner.flags.intersects(flags)
1059 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
1060 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1061 fold_list(*self, folder, |tcx, v| tcx.intern_predicates(v))
1064 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
1065 self.iter().try_for_each(|p| p.visit_with(visitor))
1069 impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
1070 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1071 self.iter().map(|x| x.fold_with(folder)).collect()
1074 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
1075 self.iter().try_for_each(|t| t.visit_with(visitor))
1079 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Const<'tcx> {
1080 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1081 let ty = self.ty.fold_with(folder);
1082 let val = self.val.fold_with(folder);
1083 if ty != self.ty || val != self.val {
1084 folder.tcx().mk_const(ty::Const { ty, val })
1090 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1091 folder.fold_const(*self)
1094 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
1095 self.ty.visit_with(visitor)?;
1096 self.val.visit_with(visitor)
1099 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
1100 visitor.visit_const(self)
1104 impl<'tcx> TypeFoldable<'tcx> for ty::ConstKind<'tcx> {
1105 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1107 ty::ConstKind::Infer(ic) => ty::ConstKind::Infer(ic.fold_with(folder)),
1108 ty::ConstKind::Param(p) => ty::ConstKind::Param(p.fold_with(folder)),
1109 ty::ConstKind::Unevaluated(did, substs, promoted) => {
1110 ty::ConstKind::Unevaluated(did, substs.fold_with(folder), promoted)
1112 ty::ConstKind::Value(_)
1113 | ty::ConstKind::Bound(..)
1114 | ty::ConstKind::Placeholder(..)
1115 | ty::ConstKind::Error(_) => *self,
1119 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
1121 ty::ConstKind::Infer(ic) => ic.visit_with(visitor),
1122 ty::ConstKind::Param(p) => p.visit_with(visitor),
1123 ty::ConstKind::Unevaluated(_, substs, _) => substs.visit_with(visitor),
1124 ty::ConstKind::Value(_)
1125 | ty::ConstKind::Bound(..)
1126 | ty::ConstKind::Placeholder(_)
1127 | ty::ConstKind::Error(_) => ControlFlow::CONTINUE,
1132 impl<'tcx> TypeFoldable<'tcx> for InferConst<'tcx> {
1133 fn super_fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> Self {
1137 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<()> {
1138 ControlFlow::CONTINUE
1142 // Does the equivalent of
1144 // let v = self.iter().map(|p| p.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
1145 // folder.tcx().intern_*(&v)
1147 fn fold_list<'tcx, F, T>(
1148 list: &'tcx ty::List<T>,
1150 intern: impl FnOnce(TyCtxt<'tcx>, &[T]) -> &'tcx ty::List<T>,
1151 ) -> &'tcx ty::List<T>
1153 F: TypeFolder<'tcx>,
1154 T: TypeFoldable<'tcx> + PartialEq + Copy,
1156 let mut iter = list.iter();
1157 // Look for the first element that changed
1158 if let Some((i, new_t)) = iter.by_ref().enumerate().find_map(|(i, t)| {
1159 let new_t = t.fold_with(folder);
1160 if new_t == t { None } else { Some((i, new_t)) }
1162 // An element changed, prepare to intern the resulting list
1163 let mut new_list = SmallVec::<[_; 8]>::with_capacity(list.len());
1164 new_list.extend_from_slice(&list[..i]);
1165 new_list.push(new_t);
1166 new_list.extend(iter.map(|t| t.fold_with(folder)));
1167 intern(folder.tcx(), &new_list)