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::{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;
20 impl fmt::Debug for ty::TraitDef {
21 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23 FmtPrinter::new(tcx, f, Namespace::TypeNS).print_def_path(self.def_id, &[])?;
29 impl fmt::Debug for ty::AdtDef {
30 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 FmtPrinter::new(tcx, f, Namespace::TypeNS).print_def_path(self.did, &[])?;
38 impl fmt::Debug for ty::UpvarId {
39 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40 let name = ty::tls::with(|tcx| tcx.hir().name(self.var_path.hir_id));
41 write!(f, "UpvarId({:?};`{}`;{:?})", self.var_path.hir_id, name, self.closure_expr_id)
45 impl fmt::Debug for ty::UpvarBorrow<'tcx> {
46 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47 write!(f, "UpvarBorrow({:?}, {:?})", self.kind, self.region)
51 impl fmt::Debug for ty::ExistentialTraitRef<'tcx> {
52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53 fmt::Display::fmt(self, f)
57 impl fmt::Debug for ty::adjustment::Adjustment<'tcx> {
58 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59 write!(f, "{:?} -> {}", self.kind, self.target)
63 impl fmt::Debug for ty::BoundRegion {
64 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66 ty::BrAnon(n) => write!(f, "BrAnon({:?})", n),
67 ty::BrNamed(did, name) => {
68 if did.index == CRATE_DEF_INDEX {
69 write!(f, "BrNamed({})", name)
71 write!(f, "BrNamed({:?}, {})", did, name)
74 ty::BrEnv => write!(f, "BrEnv"),
79 impl fmt::Debug for ty::RegionKind {
80 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82 ty::ReEarlyBound(ref data) => write!(f, "ReEarlyBound({}, {})", data.index, data.name),
84 ty::ReLateBound(binder_id, ref bound_region) => {
85 write!(f, "ReLateBound({:?}, {:?})", binder_id, bound_region)
88 ty::ReFree(ref fr) => fr.fmt(f),
90 ty::ReStatic => write!(f, "ReStatic"),
92 ty::ReVar(ref vid) => vid.fmt(f),
94 ty::RePlaceholder(placeholder) => write!(f, "RePlaceholder({:?})", placeholder),
96 ty::ReEmpty(ui) => write!(f, "ReEmpty({:?})", ui),
98 ty::ReErased => write!(f, "ReErased"),
103 impl fmt::Debug for ty::FreeRegion {
104 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
105 write!(f, "ReFree({:?}, {:?})", self.scope, self.bound_region)
109 impl fmt::Debug for ty::Variance {
110 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
111 f.write_str(match *self {
112 ty::Covariant => "+",
113 ty::Contravariant => "-",
114 ty::Invariant => "o",
115 ty::Bivariant => "*",
120 impl fmt::Debug for ty::FnSig<'tcx> {
121 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
122 write!(f, "({:?}; c_variadic: {})->{:?}", self.inputs(), self.c_variadic, self.output())
126 impl fmt::Debug for ty::TyVid {
127 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
128 write!(f, "_#{}t", self.index)
132 impl<'tcx> fmt::Debug for ty::ConstVid<'tcx> {
133 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
134 write!(f, "_#{}c", self.index)
138 impl fmt::Debug for ty::IntVid {
139 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
140 write!(f, "_#{}i", self.index)
144 impl fmt::Debug for ty::FloatVid {
145 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
146 write!(f, "_#{}f", self.index)
150 impl fmt::Debug for ty::RegionVid {
151 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152 write!(f, "'_#{}r", self.index())
156 impl fmt::Debug for ty::InferTy {
157 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
159 ty::TyVar(ref v) => v.fmt(f),
160 ty::IntVar(ref v) => v.fmt(f),
161 ty::FloatVar(ref v) => v.fmt(f),
162 ty::FreshTy(v) => write!(f, "FreshTy({:?})", v),
163 ty::FreshIntTy(v) => write!(f, "FreshIntTy({:?})", v),
164 ty::FreshFloatTy(v) => write!(f, "FreshFloatTy({:?})", v),
169 impl fmt::Debug for ty::IntVarValue {
170 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
172 ty::IntType(ref v) => v.fmt(f),
173 ty::UintType(ref v) => v.fmt(f),
178 impl fmt::Debug for ty::FloatVarValue {
179 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
184 impl fmt::Debug for ty::TraitRef<'tcx> {
185 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
186 fmt::Display::fmt(self, f)
190 impl fmt::Debug for Ty<'tcx> {
191 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
192 fmt::Display::fmt(self, f)
196 impl fmt::Debug for ty::ParamTy {
197 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
198 write!(f, "{}/#{}", self.name, self.index)
202 impl fmt::Debug for ty::ParamConst {
203 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
204 write!(f, "{}/#{}", self.name, self.index)
208 impl fmt::Debug for ty::TraitPredicate<'tcx> {
209 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
210 write!(f, "TraitPredicate({:?})", self.trait_ref)
214 impl fmt::Debug for ty::ProjectionPredicate<'tcx> {
215 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
216 write!(f, "ProjectionPredicate({:?}, {:?})", self.projection_ty, self.ty)
220 impl fmt::Debug for ty::Predicate<'tcx> {
221 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
222 write!(f, "{:?}", self.kind())
226 impl fmt::Debug for ty::PredicateKind<'tcx> {
227 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
229 ty::PredicateKind::ForAll(binder) => write!(f, "ForAll({:?})", binder),
230 ty::PredicateKind::Atom(atom) => write!(f, "{:?}", atom),
235 impl fmt::Debug for ty::PredicateAtom<'tcx> {
236 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
238 ty::PredicateAtom::Trait(ref a, constness) => {
239 if let hir::Constness::Const = constness {
240 write!(f, "const ")?;
244 ty::PredicateAtom::Subtype(ref pair) => pair.fmt(f),
245 ty::PredicateAtom::RegionOutlives(ref pair) => pair.fmt(f),
246 ty::PredicateAtom::TypeOutlives(ref pair) => pair.fmt(f),
247 ty::PredicateAtom::Projection(ref pair) => pair.fmt(f),
248 ty::PredicateAtom::WellFormed(data) => write!(f, "WellFormed({:?})", data),
249 ty::PredicateAtom::ObjectSafe(trait_def_id) => {
250 write!(f, "ObjectSafe({:?})", trait_def_id)
252 ty::PredicateAtom::ClosureKind(closure_def_id, closure_substs, kind) => {
253 write!(f, "ClosureKind({:?}, {:?}, {:?})", closure_def_id, closure_substs, kind)
255 ty::PredicateAtom::ConstEvaluatable(def_id, substs) => {
256 write!(f, "ConstEvaluatable({:?}, {:?})", def_id, substs)
258 ty::PredicateAtom::ConstEquate(c1, c2) => write!(f, "ConstEquate({:?}, {:?})", c1, c2),
263 ///////////////////////////////////////////////////////////////////////////
266 // For things that don't carry any arena-allocated data (and are
267 // copy...), just add them to this list.
269 CloneTypeFoldableAndLiftImpls! {
273 ::rustc_target::abi::VariantIdx,
276 crate::middle::region::Scope,
277 ::rustc_ast::ast::FloatTy,
278 ::rustc_ast::ast::InlineAsmOptions,
279 ::rustc_ast::ast::InlineAsmTemplatePiece,
280 ::rustc_ast::ast::NodeId,
281 ::rustc_span::symbol::Symbol,
282 ::rustc_hir::def::Res,
283 ::rustc_hir::def_id::DefId,
284 ::rustc_hir::def_id::LocalDefId,
285 ::rustc_hir::LlvmInlineAsmInner,
286 ::rustc_hir::MatchSource,
287 ::rustc_hir::Mutability,
288 ::rustc_hir::Unsafety,
289 ::rustc_target::asm::InlineAsmRegOrRegClass,
290 ::rustc_target::spec::abi::Abi,
292 crate::mir::Promoted,
293 crate::traits::Reveal,
294 crate::ty::adjustment::AutoBorrowMutability,
296 // Including `BoundRegion` is a *bit* dubious, but direct
297 // references to bound region appear in `ty::Error`, and aren't
298 // really meant to be folded. In general, we can only fold a fully
300 crate::ty::BoundRegion,
301 crate::ty::Placeholder<crate::ty::BoundRegion>,
302 crate::ty::ClosureKind,
303 crate::ty::FreeRegion,
305 crate::ty::IntVarValue,
306 crate::ty::ParamConst,
308 crate::ty::adjustment::PointerCast,
309 crate::ty::RegionVid,
310 crate::ty::UniverseIndex,
315 ///////////////////////////////////////////////////////////////////////////
316 // Lift implementations
318 // FIXME(eddyb) replace all the uses of `Option::map` with `?`.
319 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>> Lift<'tcx> for (A, B) {
320 type Lifted = (A::Lifted, B::Lifted);
321 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
322 tcx.lift(&self.0).and_then(|a| tcx.lift(&self.1).map(|b| (a, b)))
326 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>, C: Lift<'tcx>> Lift<'tcx> for (A, B, C) {
327 type Lifted = (A::Lifted, B::Lifted, C::Lifted);
328 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
330 .and_then(|a| tcx.lift(&self.1).and_then(|b| tcx.lift(&self.2).map(|c| (a, b, c))))
334 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Option<T> {
335 type Lifted = Option<T::Lifted>;
336 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
338 Some(ref x) => tcx.lift(x).map(Some),
344 impl<'tcx, T: Lift<'tcx>, E: Lift<'tcx>> Lift<'tcx> for Result<T, E> {
345 type Lifted = Result<T::Lifted, E::Lifted>;
346 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
348 Ok(ref x) => tcx.lift(x).map(Ok),
349 Err(ref e) => tcx.lift(e).map(Err),
354 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Box<T> {
355 type Lifted = Box<T::Lifted>;
356 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
357 tcx.lift(&**self).map(Box::new)
361 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Rc<T> {
362 type Lifted = Rc<T::Lifted>;
363 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
364 tcx.lift(&**self).map(Rc::new)
368 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Arc<T> {
369 type Lifted = Arc<T::Lifted>;
370 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
371 tcx.lift(&**self).map(Arc::new)
375 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for [T] {
376 type Lifted = Vec<T::Lifted>;
377 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
378 // type annotation needed to inform `projection_must_outlive`
379 let mut result: Vec<<T as Lift<'tcx>>::Lifted> = Vec::with_capacity(self.len());
381 if let Some(value) = tcx.lift(x) {
391 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Vec<T> {
392 type Lifted = Vec<T::Lifted>;
393 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
398 impl<'tcx, I: Idx, T: Lift<'tcx>> Lift<'tcx> for IndexVec<I, T> {
399 type Lifted = IndexVec<I, T::Lifted>;
400 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
401 self.iter().map(|e| tcx.lift(e)).collect()
405 impl<'a, 'tcx> Lift<'tcx> for ty::TraitRef<'a> {
406 type Lifted = ty::TraitRef<'tcx>;
407 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
408 tcx.lift(&self.substs).map(|substs| ty::TraitRef { def_id: self.def_id, substs })
412 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialTraitRef<'a> {
413 type Lifted = ty::ExistentialTraitRef<'tcx>;
414 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
415 tcx.lift(&self.substs).map(|substs| ty::ExistentialTraitRef { def_id: self.def_id, substs })
419 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialPredicate<'a> {
420 type Lifted = ty::ExistentialPredicate<'tcx>;
421 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
423 ty::ExistentialPredicate::Trait(x) => tcx.lift(x).map(ty::ExistentialPredicate::Trait),
424 ty::ExistentialPredicate::Projection(x) => {
425 tcx.lift(x).map(ty::ExistentialPredicate::Projection)
427 ty::ExistentialPredicate::AutoTrait(def_id) => {
428 Some(ty::ExistentialPredicate::AutoTrait(*def_id))
434 impl<'a, 'tcx> Lift<'tcx> for ty::TraitPredicate<'a> {
435 type Lifted = ty::TraitPredicate<'tcx>;
436 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<ty::TraitPredicate<'tcx>> {
437 tcx.lift(&self.trait_ref).map(|trait_ref| ty::TraitPredicate { trait_ref })
441 impl<'a, 'tcx> Lift<'tcx> for ty::SubtypePredicate<'a> {
442 type Lifted = ty::SubtypePredicate<'tcx>;
443 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<ty::SubtypePredicate<'tcx>> {
444 tcx.lift(&(self.a, self.b)).map(|(a, b)| ty::SubtypePredicate {
445 a_is_expected: self.a_is_expected,
452 impl<'tcx, A: Copy + Lift<'tcx>, B: Copy + Lift<'tcx>> Lift<'tcx> for ty::OutlivesPredicate<A, B> {
453 type Lifted = ty::OutlivesPredicate<A::Lifted, B::Lifted>;
454 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
455 tcx.lift(&(self.0, self.1)).map(|(a, b)| ty::OutlivesPredicate(a, b))
459 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionTy<'a> {
460 type Lifted = ty::ProjectionTy<'tcx>;
461 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<ty::ProjectionTy<'tcx>> {
462 tcx.lift(&self.substs)
463 .map(|substs| ty::ProjectionTy { item_def_id: self.item_def_id, substs })
467 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionPredicate<'a> {
468 type Lifted = ty::ProjectionPredicate<'tcx>;
469 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<ty::ProjectionPredicate<'tcx>> {
470 tcx.lift(&(self.projection_ty, self.ty))
471 .map(|(projection_ty, ty)| ty::ProjectionPredicate { projection_ty, ty })
475 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialProjection<'a> {
476 type Lifted = ty::ExistentialProjection<'tcx>;
477 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
478 tcx.lift(&self.substs).map(|substs| ty::ExistentialProjection {
480 ty: tcx.lift(&self.ty).expect("type must lift when substs do"),
481 item_def_id: self.item_def_id,
486 impl<'a, 'tcx> Lift<'tcx> for ty::PredicateKind<'a> {
487 type Lifted = ty::PredicateKind<'tcx>;
488 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
490 ty::PredicateKind::ForAll(ref binder) => {
491 tcx.lift(binder).map(ty::PredicateKind::ForAll)
493 ty::PredicateKind::Atom(ref atom) => tcx.lift(atom).map(ty::PredicateKind::Atom),
498 impl<'a, 'tcx> Lift<'tcx> for ty::PredicateAtom<'a> {
499 type Lifted = ty::PredicateAtom<'tcx>;
500 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
502 ty::PredicateAtom::Trait(ref data, constness) => {
503 tcx.lift(data).map(|data| ty::PredicateAtom::Trait(data, constness))
505 ty::PredicateAtom::Subtype(ref data) => tcx.lift(data).map(ty::PredicateAtom::Subtype),
506 ty::PredicateAtom::RegionOutlives(ref data) => {
507 tcx.lift(data).map(ty::PredicateAtom::RegionOutlives)
509 ty::PredicateAtom::TypeOutlives(ref data) => {
510 tcx.lift(data).map(ty::PredicateAtom::TypeOutlives)
512 ty::PredicateAtom::Projection(ref data) => {
513 tcx.lift(data).map(ty::PredicateAtom::Projection)
515 ty::PredicateAtom::WellFormed(ty) => tcx.lift(&ty).map(ty::PredicateAtom::WellFormed),
516 ty::PredicateAtom::ClosureKind(closure_def_id, closure_substs, kind) => {
517 tcx.lift(&closure_substs).map(|closure_substs| {
518 ty::PredicateAtom::ClosureKind(closure_def_id, closure_substs, kind)
521 ty::PredicateAtom::ObjectSafe(trait_def_id) => {
522 Some(ty::PredicateAtom::ObjectSafe(trait_def_id))
524 ty::PredicateAtom::ConstEvaluatable(def_id, substs) => {
525 tcx.lift(&substs).map(|substs| ty::PredicateAtom::ConstEvaluatable(def_id, substs))
527 ty::PredicateAtom::ConstEquate(c1, c2) => {
528 tcx.lift(&(c1, c2)).map(|(c1, c2)| ty::PredicateAtom::ConstEquate(c1, c2))
534 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::Binder<T> {
535 type Lifted = ty::Binder<T::Lifted>;
536 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
537 tcx.lift(self.as_ref().skip_binder()).map(ty::Binder::bind)
541 impl<'a, 'tcx> Lift<'tcx> for ty::ParamEnv<'a> {
542 type Lifted = ty::ParamEnv<'tcx>;
543 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
544 tcx.lift(&self.caller_bounds())
545 .map(|caller_bounds| ty::ParamEnv::new(caller_bounds, self.reveal(), self.def_id))
549 impl<'a, 'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::ParamEnvAnd<'a, T> {
550 type Lifted = ty::ParamEnvAnd<'tcx, T::Lifted>;
551 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
552 tcx.lift(&self.param_env).and_then(|param_env| {
553 tcx.lift(&self.value).map(|value| ty::ParamEnvAnd { param_env, value })
558 impl<'a, 'tcx> Lift<'tcx> for ty::ClosureSubsts<'a> {
559 type Lifted = ty::ClosureSubsts<'tcx>;
560 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
561 tcx.lift(&self.substs).map(|substs| ty::ClosureSubsts { substs })
565 impl<'a, 'tcx> Lift<'tcx> for ty::GeneratorSubsts<'a> {
566 type Lifted = ty::GeneratorSubsts<'tcx>;
567 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
568 tcx.lift(&self.substs).map(|substs| ty::GeneratorSubsts { substs })
572 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjustment<'a> {
573 type Lifted = ty::adjustment::Adjustment<'tcx>;
574 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
575 tcx.lift(&self.kind).and_then(|kind| {
576 tcx.lift(&self.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(ref overloaded) => {
588 tcx.lift(overloaded).map(ty::adjustment::Adjust::Deref)
590 ty::adjustment::Adjust::Borrow(ref 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)
601 .map(|region| ty::adjustment::OverloadedDeref { region, mutbl: self.mutbl })
605 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::AutoBorrow<'a> {
606 type Lifted = ty::adjustment::AutoBorrow<'tcx>;
607 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
609 ty::adjustment::AutoBorrow::Ref(r, m) => {
610 tcx.lift(&r).map(|r| ty::adjustment::AutoBorrow::Ref(r, m))
612 ty::adjustment::AutoBorrow::RawPtr(m) => Some(ty::adjustment::AutoBorrow::RawPtr(m)),
617 impl<'a, 'tcx> Lift<'tcx> for ty::GenSig<'a> {
618 type Lifted = ty::GenSig<'tcx>;
619 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
620 tcx.lift(&(self.resume_ty, self.yield_ty, self.return_ty))
621 .map(|(resume_ty, yield_ty, return_ty)| ty::GenSig { resume_ty, yield_ty, return_ty })
625 impl<'a, 'tcx> Lift<'tcx> for ty::FnSig<'a> {
626 type Lifted = ty::FnSig<'tcx>;
627 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
628 tcx.lift(&self.inputs_and_output).map(|x| ty::FnSig {
629 inputs_and_output: x,
630 c_variadic: self.c_variadic,
631 unsafety: self.unsafety,
637 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::error::ExpectedFound<T> {
638 type Lifted = ty::error::ExpectedFound<T::Lifted>;
639 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
640 tcx.lift(&self.expected).and_then(|expected| {
641 tcx.lift(&self.found).map(|found| ty::error::ExpectedFound { expected, found })
646 impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> {
647 type Lifted = ty::error::TypeError<'tcx>;
648 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
649 use crate::ty::error::TypeError::*;
652 Mismatch => Mismatch,
653 UnsafetyMismatch(x) => UnsafetyMismatch(x),
654 AbiMismatch(x) => AbiMismatch(x),
655 Mutability => Mutability,
656 TupleSize(x) => TupleSize(x),
657 FixedArraySize(x) => FixedArraySize(x),
658 ArgCount => ArgCount,
659 RegionsDoesNotOutlive(a, b) => {
660 return tcx.lift(&(a, b)).map(|(a, b)| RegionsDoesNotOutlive(a, b));
662 RegionsInsufficientlyPolymorphic(a, b) => {
663 return tcx.lift(&b).map(|b| RegionsInsufficientlyPolymorphic(a, b));
665 RegionsOverlyPolymorphic(a, b) => {
666 return tcx.lift(&b).map(|b| RegionsOverlyPolymorphic(a, b));
668 RegionsPlaceholderMismatch => RegionsPlaceholderMismatch,
669 IntMismatch(x) => IntMismatch(x),
670 FloatMismatch(x) => FloatMismatch(x),
671 Traits(x) => Traits(x),
672 VariadicMismatch(x) => VariadicMismatch(x),
673 CyclicTy(t) => return tcx.lift(&t).map(|t| CyclicTy(t)),
674 ProjectionMismatched(x) => ProjectionMismatched(x),
675 Sorts(ref x) => return tcx.lift(x).map(Sorts),
676 ExistentialMismatch(ref x) => return tcx.lift(x).map(ExistentialMismatch),
677 ConstMismatch(ref x) => return tcx.lift(x).map(ConstMismatch),
678 IntrinsicCast => IntrinsicCast,
679 TargetFeatureCast(ref x) => TargetFeatureCast(*x),
680 ObjectUnsafeCoercion(ref x) => return tcx.lift(x).map(ObjectUnsafeCoercion),
685 impl<'a, 'tcx> Lift<'tcx> for ty::InstanceDef<'a> {
686 type Lifted = ty::InstanceDef<'tcx>;
687 fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
689 ty::InstanceDef::Item(def_id) => Some(ty::InstanceDef::Item(def_id)),
690 ty::InstanceDef::VtableShim(def_id) => Some(ty::InstanceDef::VtableShim(def_id)),
691 ty::InstanceDef::ReifyShim(def_id) => Some(ty::InstanceDef::ReifyShim(def_id)),
692 ty::InstanceDef::Intrinsic(def_id) => Some(ty::InstanceDef::Intrinsic(def_id)),
693 ty::InstanceDef::FnPtrShim(def_id, ref ty) => {
694 Some(ty::InstanceDef::FnPtrShim(def_id, tcx.lift(ty)?))
696 ty::InstanceDef::Virtual(def_id, n) => Some(ty::InstanceDef::Virtual(def_id, n)),
697 ty::InstanceDef::ClosureOnceShim { call_once } => {
698 Some(ty::InstanceDef::ClosureOnceShim { call_once })
700 ty::InstanceDef::DropGlue(def_id, ref ty) => {
701 Some(ty::InstanceDef::DropGlue(def_id, tcx.lift(ty)?))
703 ty::InstanceDef::CloneShim(def_id, ref ty) => {
704 Some(ty::InstanceDef::CloneShim(def_id, tcx.lift(ty)?))
710 ///////////////////////////////////////////////////////////////////////////
711 // TypeFoldable implementations.
713 // Ideally, each type should invoke `folder.fold_foo(self)` and
714 // nothing else. In some cases, though, we haven't gotten around to
715 // adding methods on the `folder` yet, and thus the folding is
716 // hard-coded here. This is less-flexible, because folders cannot
717 // override the behavior, but there are a lot of random types and one
718 // can easily refactor the folding into the TypeFolder trait as
721 /// AdtDefs are basically the same as a DefId.
722 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::AdtDef {
723 fn super_fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> Self {
727 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
732 impl<'tcx, T: TypeFoldable<'tcx>, U: TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
733 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> (T, U) {
734 (self.0.fold_with(folder), self.1.fold_with(folder))
737 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
738 self.0.visit_with(visitor) || self.1.visit_with(visitor)
742 impl<'tcx, A: TypeFoldable<'tcx>, B: TypeFoldable<'tcx>, C: TypeFoldable<'tcx>> TypeFoldable<'tcx>
745 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> (A, B, C) {
746 (self.0.fold_with(folder), self.1.fold_with(folder), self.2.fold_with(folder))
749 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
750 self.0.visit_with(visitor) || self.1.visit_with(visitor) || self.2.visit_with(visitor)
754 EnumTypeFoldableImpl! {
755 impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
758 } where T: TypeFoldable<'tcx>
761 EnumTypeFoldableImpl! {
762 impl<'tcx, T, E> TypeFoldable<'tcx> for Result<T, E> {
765 } where T: TypeFoldable<'tcx>, E: TypeFoldable<'tcx>,
768 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
769 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
770 Rc::new((**self).fold_with(folder))
773 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
774 (**self).visit_with(visitor)
778 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Arc<T> {
779 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
780 Arc::new((**self).fold_with(folder))
783 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
784 (**self).visit_with(visitor)
788 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
789 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
790 let content: T = (**self).fold_with(folder);
794 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
795 (**self).visit_with(visitor)
799 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
800 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
801 self.iter().map(|t| t.fold_with(folder)).collect()
804 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
805 self.iter().any(|t| t.visit_with(visitor))
809 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<[T]> {
810 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
811 self.iter().map(|t| t.fold_with(folder)).collect::<Vec<_>>().into_boxed_slice()
814 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
815 self.iter().any(|t| t.visit_with(visitor))
819 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<T> {
820 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
821 self.map_bound_ref(|ty| ty.fold_with(folder))
824 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
825 folder.fold_binder(self)
828 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
829 self.as_ref().skip_binder().visit_with(visitor)
832 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
833 visitor.visit_binder(self)
837 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::ExistentialPredicate<'tcx>> {
838 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
839 fold_list(*self, folder, |tcx, v| tcx.intern_existential_predicates(v))
842 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
843 self.iter().any(|p| p.visit_with(visitor))
847 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<Ty<'tcx>> {
848 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
849 fold_list(*self, folder, |tcx, v| tcx.intern_type_list(v))
852 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
853 self.iter().any(|t| t.visit_with(visitor))
857 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind> {
858 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
859 fold_list(*self, folder, |tcx, v| tcx.intern_projs(v))
862 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
863 self.iter().any(|t| t.visit_with(visitor))
867 impl<'tcx> TypeFoldable<'tcx> for ty::instance::Instance<'tcx> {
868 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
869 use crate::ty::InstanceDef::*;
871 substs: self.substs.fold_with(folder),
872 def: match self.def {
873 Item(def) => Item(def.fold_with(folder)),
874 VtableShim(did) => VtableShim(did.fold_with(folder)),
875 ReifyShim(did) => ReifyShim(did.fold_with(folder)),
876 Intrinsic(did) => Intrinsic(did.fold_with(folder)),
877 FnPtrShim(did, ty) => FnPtrShim(did.fold_with(folder), ty.fold_with(folder)),
878 Virtual(did, i) => Virtual(did.fold_with(folder), i),
879 ClosureOnceShim { call_once } => {
880 ClosureOnceShim { call_once: call_once.fold_with(folder) }
882 DropGlue(did, ty) => DropGlue(did.fold_with(folder), ty.fold_with(folder)),
883 CloneShim(did, ty) => CloneShim(did.fold_with(folder), ty.fold_with(folder)),
888 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
889 use crate::ty::InstanceDef::*;
890 self.substs.visit_with(visitor)
892 Item(def) => def.visit_with(visitor),
893 VtableShim(did) | ReifyShim(did) | Intrinsic(did) | Virtual(did, _) => {
894 did.visit_with(visitor)
896 FnPtrShim(did, ty) | CloneShim(did, ty) => {
897 did.visit_with(visitor) || ty.visit_with(visitor)
899 DropGlue(did, ty) => did.visit_with(visitor) || ty.visit_with(visitor),
900 ClosureOnceShim { call_once } => call_once.visit_with(visitor),
905 impl<'tcx> TypeFoldable<'tcx> for interpret::GlobalId<'tcx> {
906 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
907 Self { instance: self.instance.fold_with(folder), promoted: self.promoted }
910 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
911 self.instance.visit_with(visitor)
915 impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
916 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
917 let kind = match self.kind {
918 ty::RawPtr(tm) => ty::RawPtr(tm.fold_with(folder)),
919 ty::Array(typ, sz) => ty::Array(typ.fold_with(folder), sz.fold_with(folder)),
920 ty::Slice(typ) => ty::Slice(typ.fold_with(folder)),
921 ty::Adt(tid, substs) => ty::Adt(tid, substs.fold_with(folder)),
922 ty::Dynamic(ref trait_ty, ref region) => {
923 ty::Dynamic(trait_ty.fold_with(folder), region.fold_with(folder))
925 ty::Tuple(ts) => ty::Tuple(ts.fold_with(folder)),
926 ty::FnDef(def_id, substs) => ty::FnDef(def_id, substs.fold_with(folder)),
927 ty::FnPtr(f) => ty::FnPtr(f.fold_with(folder)),
928 ty::Ref(ref r, ty, mutbl) => ty::Ref(r.fold_with(folder), ty.fold_with(folder), mutbl),
929 ty::Generator(did, substs, movability) => {
930 ty::Generator(did, substs.fold_with(folder), movability)
932 ty::GeneratorWitness(types) => ty::GeneratorWitness(types.fold_with(folder)),
933 ty::Closure(did, substs) => ty::Closure(did, substs.fold_with(folder)),
934 ty::Projection(ref data) => ty::Projection(data.fold_with(folder)),
935 ty::Opaque(did, substs) => ty::Opaque(did, substs.fold_with(folder)),
947 | ty::Placeholder(..)
949 | ty::Foreign(..) => return self,
952 if self.kind == kind { self } else { folder.tcx().mk_ty(kind) }
955 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
956 folder.fold_ty(*self)
959 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
961 ty::RawPtr(ref tm) => tm.visit_with(visitor),
962 ty::Array(typ, sz) => typ.visit_with(visitor) || sz.visit_with(visitor),
963 ty::Slice(typ) => typ.visit_with(visitor),
964 ty::Adt(_, substs) => substs.visit_with(visitor),
965 ty::Dynamic(ref trait_ty, ref reg) => {
966 trait_ty.visit_with(visitor) || reg.visit_with(visitor)
968 ty::Tuple(ts) => ts.visit_with(visitor),
969 ty::FnDef(_, substs) => substs.visit_with(visitor),
970 ty::FnPtr(ref f) => f.visit_with(visitor),
971 ty::Ref(r, ty, _) => r.visit_with(visitor) || ty.visit_with(visitor),
972 ty::Generator(_did, ref substs, _) => substs.visit_with(visitor),
973 ty::GeneratorWitness(ref types) => types.visit_with(visitor),
974 ty::Closure(_did, ref substs) => substs.visit_with(visitor),
975 ty::Projection(ref data) => data.visit_with(visitor),
976 ty::Opaque(_, ref substs) => substs.visit_with(visitor),
987 | ty::Placeholder(..)
990 | ty::Foreign(..) => false,
994 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
995 visitor.visit_ty(self)
999 impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> {
1000 fn super_fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> Self {
1004 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1005 folder.fold_region(*self)
1008 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
1012 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1013 visitor.visit_region(*self)
1017 impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
1018 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1019 let new = ty::PredicateKind::super_fold_with(&self.inner.kind, folder);
1020 folder.tcx().reuse_or_mk_predicate(*self, new)
1023 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1024 ty::PredicateKind::super_visit_with(&self.inner.kind, visitor)
1027 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1028 visitor.visit_predicate(*self)
1031 fn has_vars_bound_at_or_above(&self, binder: ty::DebruijnIndex) -> bool {
1032 self.inner.outer_exclusive_binder > binder
1035 fn has_type_flags(&self, flags: ty::TypeFlags) -> bool {
1036 self.inner.flags.intersects(flags)
1040 pub(super) trait PredicateVisitor<'tcx>: TypeVisitor<'tcx> {
1041 fn visit_predicate(&mut self, predicate: ty::Predicate<'tcx>) -> bool;
1044 impl<T: TypeVisitor<'tcx>> PredicateVisitor<'tcx> for T {
1045 default fn visit_predicate(&mut self, predicate: ty::Predicate<'tcx>) -> bool {
1046 predicate.super_visit_with(self)
1050 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
1051 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1052 fold_list(*self, folder, |tcx, v| tcx.intern_predicates(v))
1055 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1056 self.iter().any(|p| p.visit_with(visitor))
1060 impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
1061 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1062 self.iter().map(|x| x.fold_with(folder)).collect()
1065 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1066 self.iter().any(|t| t.visit_with(visitor))
1070 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Const<'tcx> {
1071 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1072 let ty = self.ty.fold_with(folder);
1073 let val = self.val.fold_with(folder);
1074 if ty != self.ty || val != self.val {
1075 folder.tcx().mk_const(ty::Const { ty, val })
1081 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1082 folder.fold_const(*self)
1085 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1086 self.ty.visit_with(visitor) || self.val.visit_with(visitor)
1089 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1090 visitor.visit_const(self)
1094 impl<'tcx> TypeFoldable<'tcx> for ty::ConstKind<'tcx> {
1095 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1097 ty::ConstKind::Infer(ic) => ty::ConstKind::Infer(ic.fold_with(folder)),
1098 ty::ConstKind::Param(p) => ty::ConstKind::Param(p.fold_with(folder)),
1099 ty::ConstKind::Unevaluated(did, substs, promoted) => {
1100 ty::ConstKind::Unevaluated(did, substs.fold_with(folder), promoted)
1102 ty::ConstKind::Value(_)
1103 | ty::ConstKind::Bound(..)
1104 | ty::ConstKind::Placeholder(..)
1105 | ty::ConstKind::Error(_) => *self,
1109 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1111 ty::ConstKind::Infer(ic) => ic.visit_with(visitor),
1112 ty::ConstKind::Param(p) => p.visit_with(visitor),
1113 ty::ConstKind::Unevaluated(_, substs, _) => substs.visit_with(visitor),
1114 ty::ConstKind::Value(_)
1115 | ty::ConstKind::Bound(..)
1116 | ty::ConstKind::Placeholder(_)
1117 | ty::ConstKind::Error(_) => false,
1122 impl<'tcx> TypeFoldable<'tcx> for InferConst<'tcx> {
1123 fn super_fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> Self {
1127 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
1132 // Does the equivalent of
1134 // let v = self.iter().map(|p| p.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
1135 // folder.tcx().intern_*(&v)
1137 fn fold_list<'tcx, F, T>(
1138 list: &'tcx ty::List<T>,
1140 intern: impl FnOnce(TyCtxt<'tcx>, &[T]) -> &'tcx ty::List<T>,
1141 ) -> &'tcx ty::List<T>
1143 F: TypeFolder<'tcx>,
1144 T: TypeFoldable<'tcx> + PartialEq + Copy,
1146 let mut iter = list.iter();
1147 // Look for the first element that changed
1148 if let Some((i, new_t)) = iter.by_ref().enumerate().find_map(|(i, t)| {
1149 let new_t = t.fold_with(folder);
1150 if new_t == t { None } else { Some((i, new_t)) }
1152 // An element changed, prepare to intern the resulting list
1153 let mut new_list = SmallVec::<[_; 8]>::with_capacity(list.len());
1154 new_list.extend_from_slice(&list[..i]);
1155 new_list.push(new_t);
1156 new_list.extend(iter.map(|t| t.fold_with(folder)));
1157 intern(folder.tcx(), &new_list)