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
;
20 impl fmt
::Debug
for ty
::TraitDef
{
21 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
23 with_no_trimmed_paths(|| {
24 FmtPrinter
::new(tcx
, f
, Namespace
::TypeNS
).print_def_path(self.def_id
, &[])
31 impl fmt
::Debug
for ty
::AdtDef
{
32 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
34 with_no_trimmed_paths(|| {
35 FmtPrinter
::new(tcx
, f
, Namespace
::TypeNS
).print_def_path(self.did
, &[])
42 impl fmt
::Debug
for ty
::UpvarId
{
43 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
44 let name
= ty
::tls
::with(|tcx
| tcx
.hir().name(self.var_path
.hir_id
));
45 write
!(f
, "UpvarId({:?};`{}`;{:?})", self.var_path
.hir_id
, name
, self.closure_expr_id
)
49 impl fmt
::Debug
for ty
::UpvarBorrow
<'tcx
> {
50 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
51 write
!(f
, "UpvarBorrow({:?}, {:?})", self.kind
, self.region
)
55 impl fmt
::Debug
for ty
::ExistentialTraitRef
<'tcx
> {
56 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
57 with_no_trimmed_paths(|| fmt
::Display
::fmt(self, f
))
61 impl fmt
::Debug
for ty
::adjustment
::Adjustment
<'tcx
> {
62 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
63 write
!(f
, "{:?} -> {}", self.kind
, self.target
)
67 impl fmt
::Debug
for ty
::BoundRegion
{
68 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
70 ty
::BrAnon(n
) => write
!(f
, "BrAnon({:?})", n
),
71 ty
::BrNamed(did
, name
) => {
72 if did
.index
== CRATE_DEF_INDEX
{
73 write
!(f
, "BrNamed({})", name
)
75 write
!(f
, "BrNamed({:?}, {})", did
, name
)
78 ty
::BrEnv
=> write
!(f
, "BrEnv"),
83 impl fmt
::Debug
for ty
::RegionKind
{
84 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
86 ty
::ReEarlyBound(ref data
) => write
!(f
, "ReEarlyBound({}, {})", data
.index
, data
.name
),
88 ty
::ReLateBound(binder_id
, ref bound_region
) => {
89 write
!(f
, "ReLateBound({:?}, {:?})", binder_id
, bound_region
)
92 ty
::ReFree(ref fr
) => fr
.fmt(f
),
94 ty
::ReStatic
=> write
!(f
, "ReStatic"),
96 ty
::ReVar(ref vid
) => vid
.fmt(f
),
98 ty
::RePlaceholder(placeholder
) => write
!(f
, "RePlaceholder({:?})", placeholder
),
100 ty
::ReEmpty(ui
) => write
!(f
, "ReEmpty({:?})", ui
),
102 ty
::ReErased
=> write
!(f
, "ReErased"),
107 impl fmt
::Debug
for ty
::FreeRegion
{
108 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
109 write
!(f
, "ReFree({:?}, {:?})", self.scope
, self.bound_region
)
113 impl fmt
::Debug
for ty
::Variance
{
114 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
115 f
.write_str(match *self {
116 ty
::Covariant
=> "+",
117 ty
::Contravariant
=> "-",
118 ty
::Invariant
=> "o",
119 ty
::Bivariant
=> "*",
124 impl fmt
::Debug
for ty
::FnSig
<'tcx
> {
125 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
126 write
!(f
, "({:?}; c_variadic: {})->{:?}", self.inputs(), self.c_variadic
, self.output())
130 impl fmt
::Debug
for ty
::TyVid
{
131 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
132 write
!(f
, "_#{}t", self.index
)
136 impl<'tcx
> fmt
::Debug
for ty
::ConstVid
<'tcx
> {
137 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
138 write
!(f
, "_#{}c", self.index
)
142 impl fmt
::Debug
for ty
::IntVid
{
143 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
144 write
!(f
, "_#{}i", self.index
)
148 impl fmt
::Debug
for ty
::FloatVid
{
149 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
150 write
!(f
, "_#{}f", self.index
)
154 impl fmt
::Debug
for ty
::RegionVid
{
155 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
156 write
!(f
, "'_#{}r", self.index())
160 impl fmt
::Debug
for ty
::InferTy
{
161 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
163 ty
::TyVar(ref v
) => v
.fmt(f
),
164 ty
::IntVar(ref v
) => v
.fmt(f
),
165 ty
::FloatVar(ref v
) => v
.fmt(f
),
166 ty
::FreshTy(v
) => write
!(f
, "FreshTy({:?})", v
),
167 ty
::FreshIntTy(v
) => write
!(f
, "FreshIntTy({:?})", v
),
168 ty
::FreshFloatTy(v
) => write
!(f
, "FreshFloatTy({:?})", v
),
173 impl fmt
::Debug
for ty
::IntVarValue
{
174 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
176 ty
::IntType(ref v
) => v
.fmt(f
),
177 ty
::UintType(ref v
) => v
.fmt(f
),
182 impl fmt
::Debug
for ty
::FloatVarValue
{
183 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
188 impl fmt
::Debug
for ty
::TraitRef
<'tcx
> {
189 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
190 with_no_trimmed_paths(|| fmt
::Display
::fmt(self, f
))
194 impl fmt
::Debug
for Ty
<'tcx
> {
195 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
196 with_no_trimmed_paths(|| fmt
::Display
::fmt(self, f
))
200 impl fmt
::Debug
for ty
::ParamTy
{
201 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
202 write
!(f
, "{}/#{}", self.name
, self.index
)
206 impl fmt
::Debug
for ty
::ParamConst
{
207 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
208 write
!(f
, "{}/#{}", self.name
, self.index
)
212 impl fmt
::Debug
for ty
::TraitPredicate
<'tcx
> {
213 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
214 write
!(f
, "TraitPredicate({:?})", self.trait_ref
)
218 impl fmt
::Debug
for ty
::ProjectionPredicate
<'tcx
> {
219 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
220 write
!(f
, "ProjectionPredicate({:?}, {:?})", self.projection_ty
, self.ty
)
224 impl fmt
::Debug
for ty
::Predicate
<'tcx
> {
225 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
226 write
!(f
, "{:?}", self.kind())
230 impl fmt
::Debug
for ty
::PredicateKind
<'tcx
> {
231 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
233 ty
::PredicateKind
::ForAll(binder
) => write
!(f
, "ForAll({:?})", binder
),
234 ty
::PredicateKind
::Atom(atom
) => write
!(f
, "{:?}", atom
),
239 impl fmt
::Debug
for ty
::PredicateAtom
<'tcx
> {
240 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
242 ty
::PredicateAtom
::Trait(ref a
, constness
) => {
243 if let hir
::Constness
::Const
= constness
{
244 write
!(f
, "const ")?
;
248 ty
::PredicateAtom
::Subtype(ref pair
) => pair
.fmt(f
),
249 ty
::PredicateAtom
::RegionOutlives(ref pair
) => pair
.fmt(f
),
250 ty
::PredicateAtom
::TypeOutlives(ref pair
) => pair
.fmt(f
),
251 ty
::PredicateAtom
::Projection(ref pair
) => pair
.fmt(f
),
252 ty
::PredicateAtom
::WellFormed(data
) => write
!(f
, "WellFormed({:?})", data
),
253 ty
::PredicateAtom
::ObjectSafe(trait_def_id
) => {
254 write
!(f
, "ObjectSafe({:?})", trait_def_id
)
256 ty
::PredicateAtom
::ClosureKind(closure_def_id
, closure_substs
, kind
) => {
257 write
!(f
, "ClosureKind({:?}, {:?}, {:?})", closure_def_id
, closure_substs
, kind
)
259 ty
::PredicateAtom
::ConstEvaluatable(def_id
, substs
) => {
260 write
!(f
, "ConstEvaluatable({:?}, {:?})", def_id
, substs
)
262 ty
::PredicateAtom
::ConstEquate(c1
, c2
) => write
!(f
, "ConstEquate({:?}, {:?})", c1
, c2
),
263 ty
::PredicateAtom
::TypeWellFormedFromEnv(ty
) => {
264 write
!(f
, "TypeWellFormedFromEnv({:?})", ty
)
270 ///////////////////////////////////////////////////////////////////////////
273 // For things that don't carry any arena-allocated data (and are
274 // copy...), just add them to this list.
276 CloneTypeFoldableAndLiftImpls
! {
280 ::rustc_target
::abi
::VariantIdx
,
284 crate::middle
::region
::Scope
,
285 ::rustc_ast
::FloatTy
,
286 ::rustc_ast
::InlineAsmOptions
,
287 ::rustc_ast
::InlineAsmTemplatePiece
,
289 ::rustc_span
::symbol
::Symbol
,
290 ::rustc_hir
::def
::Res
,
291 ::rustc_hir
::def_id
::DefId
,
292 ::rustc_hir
::def_id
::LocalDefId
,
294 ::rustc_hir
::LlvmInlineAsmInner
,
295 ::rustc_hir
::MatchSource
,
296 ::rustc_hir
::Mutability
,
297 ::rustc_hir
::Unsafety
,
298 ::rustc_target
::asm
::InlineAsmRegOrRegClass
,
299 ::rustc_target
::spec
::abi
::Abi
,
300 crate::mir
::coverage
::ExpressionOperandId
,
301 crate::mir
::coverage
::CounterValueReference
,
302 crate::mir
::coverage
::InjectedExpressionIndex
,
303 crate::mir
::coverage
::MappedExpressionIndex
,
305 crate::mir
::Promoted
,
306 crate::traits
::Reveal
,
307 crate::ty
::adjustment
::AutoBorrowMutability
,
309 // Including `BoundRegion` is a *bit* dubious, but direct
310 // references to bound region appear in `ty::Error`, and aren't
311 // really meant to be folded. In general, we can only fold a fully
313 crate::ty
::BoundRegion
,
314 crate::ty
::AssocItem
,
315 crate::ty
::Placeholder
<crate::ty
::BoundRegion
>,
316 crate::ty
::ClosureKind
,
317 crate::ty
::FreeRegion
,
319 crate::ty
::IntVarValue
,
320 crate::ty
::ParamConst
,
322 crate::ty
::adjustment
::PointerCast
,
323 crate::ty
::RegionVid
,
324 crate::ty
::UniverseIndex
,
329 ///////////////////////////////////////////////////////////////////////////
330 // Lift implementations
332 // FIXME(eddyb) replace all the uses of `Option::map` with `?`.
333 impl<'tcx
, A
: Lift
<'tcx
>, B
: Lift
<'tcx
>> Lift
<'tcx
> for (A
, B
) {
334 type Lifted
= (A
::Lifted
, B
::Lifted
);
335 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
336 tcx
.lift(&self.0).and_then(|a
| tcx
.lift(&self.1).map(|b
| (a
, b
)))
340 impl<'tcx
, A
: Lift
<'tcx
>, B
: Lift
<'tcx
>, C
: Lift
<'tcx
>> Lift
<'tcx
> for (A
, B
, C
) {
341 type Lifted
= (A
::Lifted
, B
::Lifted
, C
::Lifted
);
342 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
344 .and_then(|a
| tcx
.lift(&self.1).and_then(|b
| tcx
.lift(&self.2).map(|c
| (a
, b
, c
))))
348 impl<'tcx
, T
: Lift
<'tcx
>> Lift
<'tcx
> for Option
<T
> {
349 type Lifted
= Option
<T
::Lifted
>;
350 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
352 Some(ref x
) => tcx
.lift(x
).map(Some
),
358 impl<'tcx
, T
: Lift
<'tcx
>, E
: Lift
<'tcx
>> Lift
<'tcx
> for Result
<T
, E
> {
359 type Lifted
= Result
<T
::Lifted
, E
::Lifted
>;
360 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
362 Ok(ref x
) => tcx
.lift(x
).map(Ok
),
363 Err(ref e
) => tcx
.lift(e
).map(Err
),
368 impl<'tcx
, T
: Lift
<'tcx
>> Lift
<'tcx
> for Box
<T
> {
369 type Lifted
= Box
<T
::Lifted
>;
370 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
371 tcx
.lift(&**self).map(Box
::new
)
375 impl<'tcx
, T
: Lift
<'tcx
>> Lift
<'tcx
> for Rc
<T
> {
376 type Lifted
= Rc
<T
::Lifted
>;
377 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
378 tcx
.lift(&**self).map(Rc
::new
)
382 impl<'tcx
, T
: Lift
<'tcx
>> Lift
<'tcx
> for Arc
<T
> {
383 type Lifted
= Arc
<T
::Lifted
>;
384 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
385 tcx
.lift(&**self).map(Arc
::new
)
389 impl<'tcx
, T
: Lift
<'tcx
>> Lift
<'tcx
> for [T
] {
390 type Lifted
= Vec
<T
::Lifted
>;
391 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
392 // type annotation needed to inform `projection_must_outlive`
393 let mut result
: Vec
<<T
as Lift
<'tcx
>>::Lifted
> = Vec
::with_capacity(self.len());
395 if let Some(value
) = tcx
.lift(x
) {
405 impl<'tcx
, T
: Lift
<'tcx
>> Lift
<'tcx
> for Vec
<T
> {
406 type Lifted
= Vec
<T
::Lifted
>;
407 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
412 impl<'tcx
, I
: Idx
, T
: Lift
<'tcx
>> Lift
<'tcx
> for IndexVec
<I
, T
> {
413 type Lifted
= IndexVec
<I
, T
::Lifted
>;
414 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
415 self.iter().map(|e
| tcx
.lift(e
)).collect()
419 impl<'a
, 'tcx
> Lift
<'tcx
> for ty
::TraitRef
<'a
> {
420 type Lifted
= ty
::TraitRef
<'tcx
>;
421 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
422 tcx
.lift(&self.substs
).map(|substs
| ty
::TraitRef { def_id: self.def_id, substs }
)
426 impl<'a
, 'tcx
> Lift
<'tcx
> for ty
::ExistentialTraitRef
<'a
> {
427 type Lifted
= ty
::ExistentialTraitRef
<'tcx
>;
428 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
429 tcx
.lift(&self.substs
).map(|substs
| ty
::ExistentialTraitRef { def_id: self.def_id, substs }
)
433 impl<'a
, 'tcx
> Lift
<'tcx
> for ty
::ExistentialPredicate
<'a
> {
434 type Lifted
= ty
::ExistentialPredicate
<'tcx
>;
435 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
437 ty
::ExistentialPredicate
::Trait(x
) => tcx
.lift(x
).map(ty
::ExistentialPredicate
::Trait
),
438 ty
::ExistentialPredicate
::Projection(x
) => {
439 tcx
.lift(x
).map(ty
::ExistentialPredicate
::Projection
)
441 ty
::ExistentialPredicate
::AutoTrait(def_id
) => {
442 Some(ty
::ExistentialPredicate
::AutoTrait(*def_id
))
448 impl<'a
, 'tcx
> Lift
<'tcx
> for ty
::TraitPredicate
<'a
> {
449 type Lifted
= ty
::TraitPredicate
<'tcx
>;
450 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<ty
::TraitPredicate
<'tcx
>> {
451 tcx
.lift(&self.trait_ref
).map(|trait_ref
| ty
::TraitPredicate { trait_ref }
)
455 impl<'a
, 'tcx
> Lift
<'tcx
> for ty
::SubtypePredicate
<'a
> {
456 type Lifted
= ty
::SubtypePredicate
<'tcx
>;
457 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<ty
::SubtypePredicate
<'tcx
>> {
458 tcx
.lift(&(self.a
, self.b
)).map(|(a
, b
)| ty
::SubtypePredicate
{
459 a_is_expected
: self.a_is_expected
,
466 impl<'tcx
, A
: Copy
+ Lift
<'tcx
>, B
: Copy
+ Lift
<'tcx
>> Lift
<'tcx
> for ty
::OutlivesPredicate
<A
, B
> {
467 type Lifted
= ty
::OutlivesPredicate
<A
::Lifted
, B
::Lifted
>;
468 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
469 tcx
.lift(&(self.0, self.1)).map(|(a
, b
)| ty
::OutlivesPredicate(a
, b
))
473 impl<'a
, 'tcx
> Lift
<'tcx
> for ty
::ProjectionTy
<'a
> {
474 type Lifted
= ty
::ProjectionTy
<'tcx
>;
475 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<ty
::ProjectionTy
<'tcx
>> {
476 tcx
.lift(&self.substs
)
477 .map(|substs
| ty
::ProjectionTy { item_def_id: self.item_def_id, substs }
)
481 impl<'a
, 'tcx
> Lift
<'tcx
> for ty
::ProjectionPredicate
<'a
> {
482 type Lifted
= ty
::ProjectionPredicate
<'tcx
>;
483 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<ty
::ProjectionPredicate
<'tcx
>> {
484 tcx
.lift(&(self.projection_ty
, self.ty
))
485 .map(|(projection_ty
, ty
)| ty
::ProjectionPredicate { projection_ty, ty }
)
489 impl<'a
, 'tcx
> Lift
<'tcx
> for ty
::ExistentialProjection
<'a
> {
490 type Lifted
= ty
::ExistentialProjection
<'tcx
>;
491 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
492 tcx
.lift(&self.substs
).map(|substs
| ty
::ExistentialProjection
{
494 ty
: tcx
.lift(&self.ty
).expect("type must lift when substs do"),
495 item_def_id
: self.item_def_id
,
500 impl<'a
, 'tcx
> Lift
<'tcx
> for ty
::PredicateKind
<'a
> {
501 type Lifted
= ty
::PredicateKind
<'tcx
>;
502 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
504 ty
::PredicateKind
::ForAll(binder
) => tcx
.lift(binder
).map(ty
::PredicateKind
::ForAll
),
505 ty
::PredicateKind
::Atom(atom
) => tcx
.lift(atom
).map(ty
::PredicateKind
::Atom
),
510 impl<'a
, 'tcx
> Lift
<'tcx
> for ty
::PredicateAtom
<'a
> {
511 type Lifted
= ty
::PredicateAtom
<'tcx
>;
512 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
514 ty
::PredicateAtom
::Trait(ref data
, constness
) => {
515 tcx
.lift(data
).map(|data
| ty
::PredicateAtom
::Trait(data
, constness
))
517 ty
::PredicateAtom
::Subtype(ref data
) => tcx
.lift(data
).map(ty
::PredicateAtom
::Subtype
),
518 ty
::PredicateAtom
::RegionOutlives(ref data
) => {
519 tcx
.lift(data
).map(ty
::PredicateAtom
::RegionOutlives
)
521 ty
::PredicateAtom
::TypeOutlives(ref data
) => {
522 tcx
.lift(data
).map(ty
::PredicateAtom
::TypeOutlives
)
524 ty
::PredicateAtom
::Projection(ref data
) => {
525 tcx
.lift(data
).map(ty
::PredicateAtom
::Projection
)
527 ty
::PredicateAtom
::WellFormed(ty
) => tcx
.lift(&ty
).map(ty
::PredicateAtom
::WellFormed
),
528 ty
::PredicateAtom
::ClosureKind(closure_def_id
, closure_substs
, kind
) => {
529 tcx
.lift(&closure_substs
).map(|closure_substs
| {
530 ty
::PredicateAtom
::ClosureKind(closure_def_id
, closure_substs
, kind
)
533 ty
::PredicateAtom
::ObjectSafe(trait_def_id
) => {
534 Some(ty
::PredicateAtom
::ObjectSafe(trait_def_id
))
536 ty
::PredicateAtom
::ConstEvaluatable(def_id
, substs
) => {
537 tcx
.lift(&substs
).map(|substs
| ty
::PredicateAtom
::ConstEvaluatable(def_id
, substs
))
539 ty
::PredicateAtom
::ConstEquate(c1
, c2
) => {
540 tcx
.lift(&(c1
, c2
)).map(|(c1
, c2
)| ty
::PredicateAtom
::ConstEquate(c1
, c2
))
542 ty
::PredicateAtom
::TypeWellFormedFromEnv(ty
) => {
543 tcx
.lift(&ty
).map(ty
::PredicateAtom
::TypeWellFormedFromEnv
)
549 impl<'tcx
, T
: Lift
<'tcx
>> Lift
<'tcx
> for ty
::Binder
<T
> {
550 type Lifted
= ty
::Binder
<T
::Lifted
>;
551 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
552 tcx
.lift(self.as_ref().skip_binder()).map(ty
::Binder
::bind
)
556 impl<'a
, 'tcx
> Lift
<'tcx
> for ty
::ParamEnv
<'a
> {
557 type Lifted
= ty
::ParamEnv
<'tcx
>;
558 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
559 tcx
.lift(&self.caller_bounds())
560 .map(|caller_bounds
| ty
::ParamEnv
::new(caller_bounds
, self.reveal()))
564 impl<'a
, 'tcx
, T
: Lift
<'tcx
>> Lift
<'tcx
> for ty
::ParamEnvAnd
<'a
, T
> {
565 type Lifted
= ty
::ParamEnvAnd
<'tcx
, T
::Lifted
>;
566 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
567 tcx
.lift(&self.param_env
).and_then(|param_env
| {
568 tcx
.lift(&self.value
).map(|value
| ty
::ParamEnvAnd { param_env, value }
)
573 impl<'a
, 'tcx
> Lift
<'tcx
> for ty
::ClosureSubsts
<'a
> {
574 type Lifted
= ty
::ClosureSubsts
<'tcx
>;
575 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
576 tcx
.lift(&self.substs
).map(|substs
| ty
::ClosureSubsts { substs }
)
580 impl<'a
, 'tcx
> Lift
<'tcx
> for ty
::GeneratorSubsts
<'a
> {
581 type Lifted
= ty
::GeneratorSubsts
<'tcx
>;
582 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
583 tcx
.lift(&self.substs
).map(|substs
| ty
::GeneratorSubsts { substs }
)
587 impl<'a
, 'tcx
> Lift
<'tcx
> for ty
::adjustment
::Adjustment
<'a
> {
588 type Lifted
= ty
::adjustment
::Adjustment
<'tcx
>;
589 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
590 tcx
.lift(&self.kind
).and_then(|kind
| {
591 tcx
.lift(&self.target
).map(|target
| ty
::adjustment
::Adjustment { kind, target }
)
596 impl<'a
, 'tcx
> Lift
<'tcx
> for ty
::adjustment
::Adjust
<'a
> {
597 type Lifted
= ty
::adjustment
::Adjust
<'tcx
>;
598 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
600 ty
::adjustment
::Adjust
::NeverToAny
=> Some(ty
::adjustment
::Adjust
::NeverToAny
),
601 ty
::adjustment
::Adjust
::Pointer(ptr
) => Some(ty
::adjustment
::Adjust
::Pointer(ptr
)),
602 ty
::adjustment
::Adjust
::Deref(ref overloaded
) => {
603 tcx
.lift(overloaded
).map(ty
::adjustment
::Adjust
::Deref
)
605 ty
::adjustment
::Adjust
::Borrow(ref autoref
) => {
606 tcx
.lift(autoref
).map(ty
::adjustment
::Adjust
::Borrow
)
612 impl<'a
, 'tcx
> Lift
<'tcx
> for ty
::adjustment
::OverloadedDeref
<'a
> {
613 type Lifted
= ty
::adjustment
::OverloadedDeref
<'tcx
>;
614 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
615 tcx
.lift(&self.region
).map(|region
| ty
::adjustment
::OverloadedDeref
{
623 impl<'a
, 'tcx
> Lift
<'tcx
> for ty
::adjustment
::AutoBorrow
<'a
> {
624 type Lifted
= ty
::adjustment
::AutoBorrow
<'tcx
>;
625 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
627 ty
::adjustment
::AutoBorrow
::Ref(r
, m
) => {
628 tcx
.lift(&r
).map(|r
| ty
::adjustment
::AutoBorrow
::Ref(r
, m
))
630 ty
::adjustment
::AutoBorrow
::RawPtr(m
) => Some(ty
::adjustment
::AutoBorrow
::RawPtr(m
)),
635 impl<'a
, 'tcx
> Lift
<'tcx
> for ty
::GenSig
<'a
> {
636 type Lifted
= ty
::GenSig
<'tcx
>;
637 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
638 tcx
.lift(&(self.resume_ty
, self.yield_ty
, self.return_ty
))
639 .map(|(resume_ty
, yield_ty
, return_ty
)| ty
::GenSig { resume_ty, yield_ty, return_ty }
)
643 impl<'a
, 'tcx
> Lift
<'tcx
> for ty
::FnSig
<'a
> {
644 type Lifted
= ty
::FnSig
<'tcx
>;
645 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
646 tcx
.lift(&self.inputs_and_output
).map(|x
| ty
::FnSig
{
647 inputs_and_output
: x
,
648 c_variadic
: self.c_variadic
,
649 unsafety
: self.unsafety
,
655 impl<'tcx
, T
: Lift
<'tcx
>> Lift
<'tcx
> for ty
::error
::ExpectedFound
<T
> {
656 type Lifted
= ty
::error
::ExpectedFound
<T
::Lifted
>;
657 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
658 tcx
.lift(&self.expected
).and_then(|expected
| {
659 tcx
.lift(&self.found
).map(|found
| ty
::error
::ExpectedFound { expected, found }
)
664 impl<'a
, 'tcx
> Lift
<'tcx
> for ty
::error
::TypeError
<'a
> {
665 type Lifted
= ty
::error
::TypeError
<'tcx
>;
666 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
667 use crate::ty
::error
::TypeError
::*;
670 Mismatch
=> Mismatch
,
671 UnsafetyMismatch(x
) => UnsafetyMismatch(x
),
672 AbiMismatch(x
) => AbiMismatch(x
),
673 Mutability
=> Mutability
,
674 TupleSize(x
) => TupleSize(x
),
675 FixedArraySize(x
) => FixedArraySize(x
),
676 ArgCount
=> ArgCount
,
677 RegionsDoesNotOutlive(a
, b
) => {
678 return tcx
.lift(&(a
, b
)).map(|(a
, b
)| RegionsDoesNotOutlive(a
, b
));
680 RegionsInsufficientlyPolymorphic(a
, b
) => {
681 return tcx
.lift(&b
).map(|b
| RegionsInsufficientlyPolymorphic(a
, b
));
683 RegionsOverlyPolymorphic(a
, b
) => {
684 return tcx
.lift(&b
).map(|b
| RegionsOverlyPolymorphic(a
, b
));
686 RegionsPlaceholderMismatch
=> RegionsPlaceholderMismatch
,
687 IntMismatch(x
) => IntMismatch(x
),
688 FloatMismatch(x
) => FloatMismatch(x
),
689 Traits(x
) => Traits(x
),
690 VariadicMismatch(x
) => VariadicMismatch(x
),
691 CyclicTy(t
) => return tcx
.lift(&t
).map(|t
| CyclicTy(t
)),
692 CyclicConst(ct
) => return tcx
.lift(&ct
).map(|ct
| CyclicConst(ct
)),
693 ProjectionMismatched(x
) => ProjectionMismatched(x
),
694 Sorts(ref x
) => return tcx
.lift(x
).map(Sorts
),
695 ExistentialMismatch(ref x
) => return tcx
.lift(x
).map(ExistentialMismatch
),
696 ConstMismatch(ref x
) => return tcx
.lift(x
).map(ConstMismatch
),
697 IntrinsicCast
=> IntrinsicCast
,
698 TargetFeatureCast(ref x
) => TargetFeatureCast(*x
),
699 ObjectUnsafeCoercion(ref x
) => return tcx
.lift(x
).map(ObjectUnsafeCoercion
),
704 impl<'a
, 'tcx
> Lift
<'tcx
> for ty
::InstanceDef
<'a
> {
705 type Lifted
= ty
::InstanceDef
<'tcx
>;
706 fn lift_to_tcx(&self, tcx
: TyCtxt
<'tcx
>) -> Option
<Self::Lifted
> {
708 ty
::InstanceDef
::Item(def_id
) => Some(ty
::InstanceDef
::Item(def_id
)),
709 ty
::InstanceDef
::VtableShim(def_id
) => Some(ty
::InstanceDef
::VtableShim(def_id
)),
710 ty
::InstanceDef
::ReifyShim(def_id
) => Some(ty
::InstanceDef
::ReifyShim(def_id
)),
711 ty
::InstanceDef
::Intrinsic(def_id
) => Some(ty
::InstanceDef
::Intrinsic(def_id
)),
712 ty
::InstanceDef
::FnPtrShim(def_id
, ref ty
) => {
713 Some(ty
::InstanceDef
::FnPtrShim(def_id
, tcx
.lift(ty
)?
))
715 ty
::InstanceDef
::Virtual(def_id
, n
) => Some(ty
::InstanceDef
::Virtual(def_id
, n
)),
716 ty
::InstanceDef
::ClosureOnceShim { call_once }
=> {
717 Some(ty
::InstanceDef
::ClosureOnceShim { call_once }
)
719 ty
::InstanceDef
::DropGlue(def_id
, ref ty
) => {
720 Some(ty
::InstanceDef
::DropGlue(def_id
, tcx
.lift(ty
)?
))
722 ty
::InstanceDef
::CloneShim(def_id
, ref ty
) => {
723 Some(ty
::InstanceDef
::CloneShim(def_id
, tcx
.lift(ty
)?
))
729 ///////////////////////////////////////////////////////////////////////////
730 // TypeFoldable implementations.
732 // Ideally, each type should invoke `folder.fold_foo(self)` and
733 // nothing else. In some cases, though, we haven't gotten around to
734 // adding methods on the `folder` yet, and thus the folding is
735 // hard-coded here. This is less-flexible, because folders cannot
736 // override the behavior, but there are a lot of random types and one
737 // can easily refactor the folding into the TypeFolder trait as
740 /// AdtDefs are basically the same as a DefId.
741 impl<'tcx
> TypeFoldable
<'tcx
> for &'tcx ty
::AdtDef
{
742 fn super_fold_with
<F
: TypeFolder
<'tcx
>>(&self, _folder
: &mut F
) -> Self {
746 fn super_visit_with
<V
: TypeVisitor
<'tcx
>>(&self, _visitor
: &mut V
) -> bool
{
751 impl<'tcx
, T
: TypeFoldable
<'tcx
>, U
: TypeFoldable
<'tcx
>> TypeFoldable
<'tcx
> for (T
, U
) {
752 fn super_fold_with
<F
: TypeFolder
<'tcx
>>(&self, folder
: &mut F
) -> (T
, U
) {
753 (self.0.fold_with
(folder
), self.1.fold_with
(folder
))
756 fn super_visit_with
<V
: TypeVisitor
<'tcx
>>(&self, visitor
: &mut V
) -> bool
{
757 self.0.visit_with(visitor
) || self.1.visit_with(visitor
)
761 impl<'tcx
, A
: TypeFoldable
<'tcx
>, B
: TypeFoldable
<'tcx
>, C
: TypeFoldable
<'tcx
>> TypeFoldable
<'tcx
>
764 fn super_fold_with
<F
: TypeFolder
<'tcx
>>(&self, folder
: &mut F
) -> (A
, B
, C
) {
765 (self.0.fold_with
(folder
), self.1.fold_with
(folder
), self.2.fold_with
(folder
))
768 fn super_visit_with
<V
: TypeVisitor
<'tcx
>>(&self, visitor
: &mut V
) -> bool
{
769 self.0.visit_with(visitor
) || self.1.visit_with(visitor
) || self.2.visit_with(visitor
)
773 EnumTypeFoldableImpl
! {
774 impl<'tcx
, T
> TypeFoldable
<'tcx
> for Option
<T
> {
777 } where T
: TypeFoldable
<'tcx
>
780 EnumTypeFoldableImpl
! {
781 impl<'tcx
, T
, E
> TypeFoldable
<'tcx
> for Result
<T
, E
> {
784 } where T
: TypeFoldable
<'tcx
>, E
: TypeFoldable
<'tcx
>,
787 impl<'tcx
, T
: TypeFoldable
<'tcx
>> TypeFoldable
<'tcx
> for Rc
<T
> {
788 fn super_fold_with
<F
: TypeFolder
<'tcx
>>(&self, folder
: &mut F
) -> Self {
789 Rc
::new((**self).fold_with(folder
))
792 fn super_visit_with
<V
: TypeVisitor
<'tcx
>>(&self, visitor
: &mut V
) -> bool
{
793 (**self).visit_with(visitor
)
797 impl<'tcx
, T
: TypeFoldable
<'tcx
>> TypeFoldable
<'tcx
> for Arc
<T
> {
798 fn super_fold_with
<F
: TypeFolder
<'tcx
>>(&self, folder
: &mut F
) -> Self {
799 Arc
::new((**self).fold_with(folder
))
802 fn super_visit_with
<V
: TypeVisitor
<'tcx
>>(&self, visitor
: &mut V
) -> bool
{
803 (**self).visit_with(visitor
)
807 impl<'tcx
, T
: TypeFoldable
<'tcx
>> TypeFoldable
<'tcx
> for Box
<T
> {
808 fn super_fold_with
<F
: TypeFolder
<'tcx
>>(&self, folder
: &mut F
) -> Self {
809 let content
: T
= (**self).fold_with(folder
);
813 fn super_visit_with
<V
: TypeVisitor
<'tcx
>>(&self, visitor
: &mut V
) -> bool
{
814 (**self).visit_with(visitor
)
818 impl<'tcx
, T
: TypeFoldable
<'tcx
>> TypeFoldable
<'tcx
> for Vec
<T
> {
819 fn super_fold_with
<F
: TypeFolder
<'tcx
>>(&self, folder
: &mut F
) -> Self {
820 self.iter().map(|t
| t
.fold_with(folder
)).collect()
823 fn super_visit_with
<V
: TypeVisitor
<'tcx
>>(&self, visitor
: &mut V
) -> bool
{
824 self.iter().any(|t
| t
.visit_with(visitor
))
828 impl<'tcx
, T
: TypeFoldable
<'tcx
>> TypeFoldable
<'tcx
> for Box
<[T
]> {
829 fn super_fold_with
<F
: TypeFolder
<'tcx
>>(&self, folder
: &mut F
) -> Self {
830 self.iter().map(|t
| t
.fold_with(folder
)).collect
::<Vec
<_
>>().into_boxed_slice()
833 fn super_visit_with
<V
: TypeVisitor
<'tcx
>>(&self, visitor
: &mut V
) -> bool
{
834 self.iter().any(|t
| t
.visit_with(visitor
))
838 impl<'tcx
, T
: TypeFoldable
<'tcx
>> TypeFoldable
<'tcx
> for ty
::Binder
<T
> {
839 fn super_fold_with
<F
: TypeFolder
<'tcx
>>(&self, folder
: &mut F
) -> Self {
840 self.map_bound_ref(|ty
| ty
.fold_with(folder
))
843 fn fold_with
<F
: TypeFolder
<'tcx
>>(&self, folder
: &mut F
) -> Self {
844 folder
.fold_binder(self)
847 fn super_visit_with
<V
: TypeVisitor
<'tcx
>>(&self, visitor
: &mut V
) -> bool
{
848 self.as_ref().skip_binder().visit_with(visitor
)
851 fn visit_with
<V
: TypeVisitor
<'tcx
>>(&self, visitor
: &mut V
) -> bool
{
852 visitor
.visit_binder(self)
856 impl<'tcx
> TypeFoldable
<'tcx
> for &'tcx ty
::List
<ty
::ExistentialPredicate
<'tcx
>> {
857 fn super_fold_with
<F
: TypeFolder
<'tcx
>>(&self, folder
: &mut F
) -> Self {
858 fold_list(*self, folder
, |tcx
, v
| tcx
.intern_existential_predicates(v
))
861 fn super_visit_with
<V
: TypeVisitor
<'tcx
>>(&self, visitor
: &mut V
) -> bool
{
862 self.iter().any(|p
| p
.visit_with(visitor
))
866 impl<'tcx
> TypeFoldable
<'tcx
> for &'tcx ty
::List
<Ty
<'tcx
>> {
867 fn super_fold_with
<F
: TypeFolder
<'tcx
>>(&self, folder
: &mut F
) -> Self {
868 fold_list(*self, folder
, |tcx
, v
| tcx
.intern_type_list(v
))
871 fn super_visit_with
<V
: TypeVisitor
<'tcx
>>(&self, visitor
: &mut V
) -> bool
{
872 self.iter().any(|t
| t
.visit_with(visitor
))
876 impl<'tcx
> TypeFoldable
<'tcx
> for &'tcx ty
::List
<ProjectionKind
> {
877 fn super_fold_with
<F
: TypeFolder
<'tcx
>>(&self, folder
: &mut F
) -> Self {
878 fold_list(*self, folder
, |tcx
, v
| tcx
.intern_projs(v
))
881 fn super_visit_with
<V
: TypeVisitor
<'tcx
>>(&self, visitor
: &mut V
) -> bool
{
882 self.iter().any(|t
| t
.visit_with(visitor
))
886 impl<'tcx
> TypeFoldable
<'tcx
> for ty
::instance
::Instance
<'tcx
> {
887 fn super_fold_with
<F
: TypeFolder
<'tcx
>>(&self, folder
: &mut F
) -> Self {
888 use crate::ty
::InstanceDef
::*;
890 substs
: self.substs
.fold_with(folder
),
891 def
: match self.def
{
892 Item(def
) => Item(def
.fold_with(folder
)),
893 VtableShim(did
) => VtableShim(did
.fold_with(folder
)),
894 ReifyShim(did
) => ReifyShim(did
.fold_with(folder
)),
895 Intrinsic(did
) => Intrinsic(did
.fold_with(folder
)),
896 FnPtrShim(did
, ty
) => FnPtrShim(did
.fold_with(folder
), ty
.fold_with(folder
)),
897 Virtual(did
, i
) => Virtual(did
.fold_with(folder
), i
),
898 ClosureOnceShim { call_once }
=> {
899 ClosureOnceShim { call_once: call_once.fold_with(folder) }
901 DropGlue(did
, ty
) => DropGlue(did
.fold_with(folder
), ty
.fold_with(folder
)),
902 CloneShim(did
, ty
) => CloneShim(did
.fold_with(folder
), ty
.fold_with(folder
)),
907 fn super_visit_with
<V
: TypeVisitor
<'tcx
>>(&self, visitor
: &mut V
) -> bool
{
908 use crate::ty
::InstanceDef
::*;
909 self.substs
.visit_with(visitor
)
911 Item(def
) => def
.visit_with(visitor
),
912 VtableShim(did
) | ReifyShim(did
) | Intrinsic(did
) | Virtual(did
, _
) => {
913 did
.visit_with(visitor
)
915 FnPtrShim(did
, ty
) | CloneShim(did
, ty
) => {
916 did
.visit_with(visitor
) || ty
.visit_with(visitor
)
918 DropGlue(did
, ty
) => did
.visit_with(visitor
) || ty
.visit_with(visitor
),
919 ClosureOnceShim { call_once }
=> call_once
.visit_with(visitor
),
924 impl<'tcx
> TypeFoldable
<'tcx
> for interpret
::GlobalId
<'tcx
> {
925 fn super_fold_with
<F
: TypeFolder
<'tcx
>>(&self, folder
: &mut F
) -> Self {
926 Self { instance: self.instance.fold_with(folder), promoted: self.promoted }
929 fn super_visit_with
<V
: TypeVisitor
<'tcx
>>(&self, visitor
: &mut V
) -> bool
{
930 self.instance
.visit_with(visitor
)
934 impl<'tcx
> TypeFoldable
<'tcx
> for Ty
<'tcx
> {
935 fn super_fold_with
<F
: TypeFolder
<'tcx
>>(&self, folder
: &mut F
) -> Self {
936 let kind
= match self.kind() {
937 ty
::RawPtr(tm
) => ty
::RawPtr(tm
.fold_with(folder
)),
938 ty
::Array(typ
, sz
) => ty
::Array(typ
.fold_with(folder
), sz
.fold_with(folder
)),
939 ty
::Slice(typ
) => ty
::Slice(typ
.fold_with(folder
)),
940 ty
::Adt(tid
, substs
) => ty
::Adt(*tid
, substs
.fold_with(folder
)),
941 ty
::Dynamic(ref trait_ty
, ref region
) => {
942 ty
::Dynamic(trait_ty
.fold_with(folder
), region
.fold_with(folder
))
944 ty
::Tuple(ts
) => ty
::Tuple(ts
.fold_with(folder
)),
945 ty
::FnDef(def_id
, substs
) => ty
::FnDef(*def_id
, substs
.fold_with(folder
)),
946 ty
::FnPtr(f
) => ty
::FnPtr(f
.fold_with(folder
)),
947 ty
::Ref(ref r
, ty
, mutbl
) => ty
::Ref(r
.fold_with(folder
), ty
.fold_with(folder
), *mutbl
),
948 ty
::Generator(did
, substs
, movability
) => {
949 ty
::Generator(*did
, substs
.fold_with(folder
), *movability
)
951 ty
::GeneratorWitness(types
) => ty
::GeneratorWitness(types
.fold_with(folder
)),
952 ty
::Closure(did
, substs
) => ty
::Closure(*did
, substs
.fold_with(folder
)),
953 ty
::Projection(ref data
) => ty
::Projection(data
.fold_with(folder
)),
954 ty
::Opaque(did
, substs
) => ty
::Opaque(*did
, substs
.fold_with(folder
)),
966 | ty
::Placeholder(..)
968 | ty
::Foreign(..) => return self,
971 if *self.kind() == kind { self }
else { folder.tcx().mk_ty(kind) }
974 fn fold_with
<F
: TypeFolder
<'tcx
>>(&self, folder
: &mut F
) -> Self {
975 folder
.fold_ty(*self)
978 fn super_visit_with
<V
: TypeVisitor
<'tcx
>>(&self, visitor
: &mut V
) -> bool
{
980 ty
::RawPtr(ref tm
) => tm
.visit_with(visitor
),
981 ty
::Array(typ
, sz
) => typ
.visit_with(visitor
) || sz
.visit_with(visitor
),
982 ty
::Slice(typ
) => typ
.visit_with(visitor
),
983 ty
::Adt(_
, substs
) => substs
.visit_with(visitor
),
984 ty
::Dynamic(ref trait_ty
, ref reg
) => {
985 trait_ty
.visit_with(visitor
) || reg
.visit_with(visitor
)
987 ty
::Tuple(ts
) => ts
.visit_with(visitor
),
988 ty
::FnDef(_
, substs
) => substs
.visit_with(visitor
),
989 ty
::FnPtr(ref f
) => f
.visit_with(visitor
),
990 ty
::Ref(r
, ty
, _
) => r
.visit_with(visitor
) || 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(..) => false,
1013 fn visit_with
<V
: TypeVisitor
<'tcx
>>(&self, visitor
: &mut V
) -> bool
{
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
) -> bool
{
1031 fn visit_with
<V
: TypeVisitor
<'tcx
>>(&self, visitor
: &mut V
) -> bool
{
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
) -> bool
{
1043 ty
::PredicateKind
::super_visit_with(&self.inner
.kind
, visitor
)
1046 fn visit_with
<V
: TypeVisitor
<'tcx
>>(&self, visitor
: &mut V
) -> bool
{
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 pub(super) trait PredicateVisitor
<'tcx
>: TypeVisitor
<'tcx
> {
1060 fn visit_predicate(&mut self, predicate
: ty
::Predicate
<'tcx
>) -> bool
;
1063 impl<T
: TypeVisitor
<'tcx
>> PredicateVisitor
<'tcx
> for T
{
1064 default fn visit_predicate(&mut self, predicate
: ty
::Predicate
<'tcx
>) -> bool
{
1065 predicate
.super_visit_with(self)
1069 impl<'tcx
> TypeFoldable
<'tcx
> for &'tcx ty
::List
<ty
::Predicate
<'tcx
>> {
1070 fn super_fold_with
<F
: TypeFolder
<'tcx
>>(&self, folder
: &mut F
) -> Self {
1071 fold_list(*self, folder
, |tcx
, v
| tcx
.intern_predicates(v
))
1074 fn super_visit_with
<V
: TypeVisitor
<'tcx
>>(&self, visitor
: &mut V
) -> bool
{
1075 self.iter().any(|p
| p
.visit_with(visitor
))
1079 impl<'tcx
, T
: TypeFoldable
<'tcx
>, I
: Idx
> TypeFoldable
<'tcx
> for IndexVec
<I
, T
> {
1080 fn super_fold_with
<F
: TypeFolder
<'tcx
>>(&self, folder
: &mut F
) -> Self {
1081 self.iter().map(|x
| x
.fold_with(folder
)).collect()
1084 fn super_visit_with
<V
: TypeVisitor
<'tcx
>>(&self, visitor
: &mut V
) -> bool
{
1085 self.iter().any(|t
| t
.visit_with(visitor
))
1089 impl<'tcx
> TypeFoldable
<'tcx
> for &'tcx ty
::Const
<'tcx
> {
1090 fn super_fold_with
<F
: TypeFolder
<'tcx
>>(&self, folder
: &mut F
) -> Self {
1091 let ty
= self.ty
.fold_with(folder
);
1092 let val
= self.val
.fold_with(folder
);
1093 if ty
!= self.ty
|| val
!= self.val
{
1094 folder
.tcx().mk_const(ty
::Const { ty, val }
)
1100 fn fold_with
<F
: TypeFolder
<'tcx
>>(&self, folder
: &mut F
) -> Self {
1101 folder
.fold_const(*self)
1104 fn super_visit_with
<V
: TypeVisitor
<'tcx
>>(&self, visitor
: &mut V
) -> bool
{
1105 self.ty
.visit_with(visitor
) || self.val
.visit_with(visitor
)
1108 fn visit_with
<V
: TypeVisitor
<'tcx
>>(&self, visitor
: &mut V
) -> bool
{
1109 visitor
.visit_const(self)
1113 impl<'tcx
> TypeFoldable
<'tcx
> for ty
::ConstKind
<'tcx
> {
1114 fn super_fold_with
<F
: TypeFolder
<'tcx
>>(&self, folder
: &mut F
) -> Self {
1116 ty
::ConstKind
::Infer(ic
) => ty
::ConstKind
::Infer(ic
.fold_with(folder
)),
1117 ty
::ConstKind
::Param(p
) => ty
::ConstKind
::Param(p
.fold_with(folder
)),
1118 ty
::ConstKind
::Unevaluated(did
, substs
, promoted
) => {
1119 ty
::ConstKind
::Unevaluated(did
, substs
.fold_with(folder
), promoted
)
1121 ty
::ConstKind
::Value(_
)
1122 | ty
::ConstKind
::Bound(..)
1123 | ty
::ConstKind
::Placeholder(..)
1124 | ty
::ConstKind
::Error(_
) => *self,
1128 fn super_visit_with
<V
: TypeVisitor
<'tcx
>>(&self, visitor
: &mut V
) -> bool
{
1130 ty
::ConstKind
::Infer(ic
) => ic
.visit_with(visitor
),
1131 ty
::ConstKind
::Param(p
) => p
.visit_with(visitor
),
1132 ty
::ConstKind
::Unevaluated(_
, substs
, _
) => substs
.visit_with(visitor
),
1133 ty
::ConstKind
::Value(_
)
1134 | ty
::ConstKind
::Bound(..)
1135 | ty
::ConstKind
::Placeholder(_
)
1136 | ty
::ConstKind
::Error(_
) => false,
1141 impl<'tcx
> TypeFoldable
<'tcx
> for InferConst
<'tcx
> {
1142 fn super_fold_with
<F
: TypeFolder
<'tcx
>>(&self, _folder
: &mut F
) -> Self {
1146 fn super_visit_with
<V
: TypeVisitor
<'tcx
>>(&self, _visitor
: &mut V
) -> bool
{
1151 // Does the equivalent of
1153 // let v = self.iter().map(|p| p.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
1154 // folder.tcx().intern_*(&v)
1156 fn fold_list
<'tcx
, F
, T
>(
1157 list
: &'tcx ty
::List
<T
>,
1159 intern
: impl FnOnce(TyCtxt
<'tcx
>, &[T
]) -> &'tcx ty
::List
<T
>,
1160 ) -> &'tcx ty
::List
<T
>
1162 F
: TypeFolder
<'tcx
>,
1163 T
: TypeFoldable
<'tcx
> + PartialEq
+ Copy
,
1165 let mut iter
= list
.iter();
1166 // Look for the first element that changed
1167 if let Some((i
, new_t
)) = iter
.by_ref().enumerate().find_map(|(i
, t
)| {
1168 let new_t
= t
.fold_with(folder
);
1169 if new_t
== t { None }
else { Some((i, new_t)) }
1171 // An element changed, prepare to intern the resulting list
1172 let mut new_list
= SmallVec
::<[_
; 8]>::with_capacity(list
.len());
1173 new_list
.extend_from_slice(&list
[..i
]);
1174 new_list
.push(new_t
);
1175 new_list
.extend(iter
.map(|t
| t
.fold_with(folder
)));
1176 intern(folder
.tcx(), &new_list
)