1 use crate::ty::{self, Binder, BoundTy, Ty, TyCtxt, TypeVisitableExt};
2 use rustc_data_structures::fx::FxIndexMap;
3 use rustc_hir::def_id::DefId;
4
5 use std::collections::BTreeMap;
6
7 pub use rustc_type_ir::fold::{FallibleTypeFolder, TypeFoldable, TypeFolder, TypeSuperFoldable};
8
9 ///////////////////////////////////////////////////////////////////////////
10 // Some sample folders
11
12 pub struct BottomUpFolder<'tcx, F, G, H>
13 where
14 F: FnMut(Ty<'tcx>) -> Ty<'tcx>,
15 G: FnMut(ty::Region<'tcx>) -> ty::Region<'tcx>,
16 H: FnMut(ty::Const<'tcx>) -> ty::Const<'tcx>,
17 {
18 pub tcx: TyCtxt<'tcx>,
19 pub ty_op: F,
20 pub lt_op: G,
21 pub ct_op: H,
22 }
23
24 impl<'tcx, F, G, H> TypeFolder<TyCtxt<'tcx>> for BottomUpFolder<'tcx, F, G, H>
25 where
26 F: FnMut(Ty<'tcx>) -> Ty<'tcx>,
27 G: FnMut(ty::Region<'tcx>) -> ty::Region<'tcx>,
28 H: FnMut(ty::Const<'tcx>) -> ty::Const<'tcx>,
29 {
interner(&self) -> TyCtxt<'tcx>30 fn interner(&self) -> TyCtxt<'tcx> {
31 self.tcx
32 }
33
fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx>34 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
35 let t = ty.super_fold_with(self);
36 (self.ty_op)(t)
37 }
38
fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx>39 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
40 // This one is a little different, because `super_fold_with` is not
41 // implemented on non-recursive `Region`.
42 (self.lt_op)(r)
43 }
44
fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx>45 fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
46 let ct = ct.super_fold_with(self);
47 (self.ct_op)(ct)
48 }
49 }
50
51 ///////////////////////////////////////////////////////////////////////////
52 // Region folder
53
54 impl<'tcx> TyCtxt<'tcx> {
55 /// Folds the escaping and free regions in `value` using `f`.
fold_regions<T>( self, value: T, mut f: impl FnMut(ty::Region<'tcx>, ty::DebruijnIndex) -> ty::Region<'tcx>, ) -> T where T: TypeFoldable<TyCtxt<'tcx>>,56 pub fn fold_regions<T>(
57 self,
58 value: T,
59 mut f: impl FnMut(ty::Region<'tcx>, ty::DebruijnIndex) -> ty::Region<'tcx>,
60 ) -> T
61 where
62 T: TypeFoldable<TyCtxt<'tcx>>,
63 {
64 value.fold_with(&mut RegionFolder::new(self, &mut f))
65 }
66 }
67
68 /// Folds over the substructure of a type, visiting its component
69 /// types and all regions that occur *free* within it.
70 ///
71 /// That is, `Ty` can contain function or method types that bind
72 /// regions at the call site (`ReLateBound`), and occurrences of
73 /// regions (aka "lifetimes") that are bound within a type are not
74 /// visited by this folder; only regions that occur free will be
75 /// visited by `fld_r`.
76
77 pub struct RegionFolder<'a, 'tcx> {
78 tcx: TyCtxt<'tcx>,
79
80 /// Stores the index of a binder *just outside* the stuff we have
81 /// visited. So this begins as INNERMOST; when we pass through a
82 /// binder, it is incremented (via `shift_in`).
83 current_index: ty::DebruijnIndex,
84
85 /// Callback invokes for each free region. The `DebruijnIndex`
86 /// points to the binder *just outside* the ones we have passed
87 /// through.
88 fold_region_fn:
89 &'a mut (dyn FnMut(ty::Region<'tcx>, ty::DebruijnIndex) -> ty::Region<'tcx> + 'a),
90 }
91
92 impl<'a, 'tcx> RegionFolder<'a, 'tcx> {
93 #[inline]
new( tcx: TyCtxt<'tcx>, fold_region_fn: &'a mut dyn FnMut(ty::Region<'tcx>, ty::DebruijnIndex) -> ty::Region<'tcx>, ) -> RegionFolder<'a, 'tcx>94 pub fn new(
95 tcx: TyCtxt<'tcx>,
96 fold_region_fn: &'a mut dyn FnMut(ty::Region<'tcx>, ty::DebruijnIndex) -> ty::Region<'tcx>,
97 ) -> RegionFolder<'a, 'tcx> {
98 RegionFolder { tcx, current_index: ty::INNERMOST, fold_region_fn }
99 }
100 }
101
102 impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for RegionFolder<'a, 'tcx> {
interner(&self) -> TyCtxt<'tcx>103 fn interner(&self) -> TyCtxt<'tcx> {
104 self.tcx
105 }
106
fold_binder<T: TypeFoldable<TyCtxt<'tcx>>>( &mut self, t: ty::Binder<'tcx, T>, ) -> ty::Binder<'tcx, T>107 fn fold_binder<T: TypeFoldable<TyCtxt<'tcx>>>(
108 &mut self,
109 t: ty::Binder<'tcx, T>,
110 ) -> ty::Binder<'tcx, T> {
111 self.current_index.shift_in(1);
112 let t = t.super_fold_with(self);
113 self.current_index.shift_out(1);
114 t
115 }
116
117 #[instrument(skip(self), level = "debug", ret)]
fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx>118 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
119 match *r {
120 ty::ReLateBound(debruijn, _) if debruijn < self.current_index => {
121 debug!(?self.current_index, "skipped bound region");
122 r
123 }
124 _ => {
125 debug!(?self.current_index, "folding free region");
126 (self.fold_region_fn)(r, self.current_index)
127 }
128 }
129 }
130 }
131
132 ///////////////////////////////////////////////////////////////////////////
133 // Bound vars replacer
134
135 pub trait BoundVarReplacerDelegate<'tcx> {
replace_region(&mut self, br: ty::BoundRegion) -> ty::Region<'tcx>136 fn replace_region(&mut self, br: ty::BoundRegion) -> ty::Region<'tcx>;
replace_ty(&mut self, bt: ty::BoundTy) -> Ty<'tcx>137 fn replace_ty(&mut self, bt: ty::BoundTy) -> Ty<'tcx>;
replace_const(&mut self, bv: ty::BoundVar, ty: Ty<'tcx>) -> ty::Const<'tcx>138 fn replace_const(&mut self, bv: ty::BoundVar, ty: Ty<'tcx>) -> ty::Const<'tcx>;
139 }
140
141 pub struct FnMutDelegate<'a, 'tcx> {
142 pub regions: &'a mut (dyn FnMut(ty::BoundRegion) -> ty::Region<'tcx> + 'a),
143 pub types: &'a mut (dyn FnMut(ty::BoundTy) -> Ty<'tcx> + 'a),
144 pub consts: &'a mut (dyn FnMut(ty::BoundVar, Ty<'tcx>) -> ty::Const<'tcx> + 'a),
145 }
146
147 impl<'a, 'tcx> BoundVarReplacerDelegate<'tcx> for FnMutDelegate<'a, 'tcx> {
replace_region(&mut self, br: ty::BoundRegion) -> ty::Region<'tcx>148 fn replace_region(&mut self, br: ty::BoundRegion) -> ty::Region<'tcx> {
149 (self.regions)(br)
150 }
replace_ty(&mut self, bt: ty::BoundTy) -> Ty<'tcx>151 fn replace_ty(&mut self, bt: ty::BoundTy) -> Ty<'tcx> {
152 (self.types)(bt)
153 }
replace_const(&mut self, bv: ty::BoundVar, ty: Ty<'tcx>) -> ty::Const<'tcx>154 fn replace_const(&mut self, bv: ty::BoundVar, ty: Ty<'tcx>) -> ty::Const<'tcx> {
155 (self.consts)(bv, ty)
156 }
157 }
158
159 /// Replaces the escaping bound vars (late bound regions or bound types) in a type.
160 struct BoundVarReplacer<'tcx, D> {
161 tcx: TyCtxt<'tcx>,
162
163 /// As with `RegionFolder`, represents the index of a binder *just outside*
164 /// the ones we have visited.
165 current_index: ty::DebruijnIndex,
166
167 delegate: D,
168 }
169
170 impl<'tcx, D: BoundVarReplacerDelegate<'tcx>> BoundVarReplacer<'tcx, D> {
new(tcx: TyCtxt<'tcx>, delegate: D) -> Self171 fn new(tcx: TyCtxt<'tcx>, delegate: D) -> Self {
172 BoundVarReplacer { tcx, current_index: ty::INNERMOST, delegate }
173 }
174 }
175
176 impl<'tcx, D> TypeFolder<TyCtxt<'tcx>> for BoundVarReplacer<'tcx, D>
177 where
178 D: BoundVarReplacerDelegate<'tcx>,
179 {
interner(&self) -> TyCtxt<'tcx>180 fn interner(&self) -> TyCtxt<'tcx> {
181 self.tcx
182 }
183
fold_binder<T: TypeFoldable<TyCtxt<'tcx>>>( &mut self, t: ty::Binder<'tcx, T>, ) -> ty::Binder<'tcx, T>184 fn fold_binder<T: TypeFoldable<TyCtxt<'tcx>>>(
185 &mut self,
186 t: ty::Binder<'tcx, T>,
187 ) -> ty::Binder<'tcx, T> {
188 self.current_index.shift_in(1);
189 let t = t.super_fold_with(self);
190 self.current_index.shift_out(1);
191 t
192 }
193
fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx>194 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
195 match *t.kind() {
196 ty::Bound(debruijn, bound_ty) if debruijn == self.current_index => {
197 let ty = self.delegate.replace_ty(bound_ty);
198 debug_assert!(!ty.has_vars_bound_above(ty::INNERMOST));
199 ty::fold::shift_vars(self.tcx, ty, self.current_index.as_u32())
200 }
201 _ if t.has_vars_bound_at_or_above(self.current_index) => t.super_fold_with(self),
202 _ => t,
203 }
204 }
205
fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx>206 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
207 match *r {
208 ty::ReLateBound(debruijn, br) if debruijn == self.current_index => {
209 let region = self.delegate.replace_region(br);
210 if let ty::ReLateBound(debruijn1, br) = *region {
211 // If the callback returns a late-bound region,
212 // that region should always use the INNERMOST
213 // debruijn index. Then we adjust it to the
214 // correct depth.
215 assert_eq!(debruijn1, ty::INNERMOST);
216 ty::Region::new_late_bound(self.tcx, debruijn, br)
217 } else {
218 region
219 }
220 }
221 _ => r,
222 }
223 }
224
fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx>225 fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
226 match ct.kind() {
227 ty::ConstKind::Bound(debruijn, bound_const) if debruijn == self.current_index => {
228 let ct = self.delegate.replace_const(bound_const, ct.ty());
229 debug_assert!(!ct.has_vars_bound_above(ty::INNERMOST));
230 ty::fold::shift_vars(self.tcx, ct, self.current_index.as_u32())
231 }
232 _ => ct.super_fold_with(self),
233 }
234 }
235
fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx>236 fn fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> {
237 if p.has_vars_bound_at_or_above(self.current_index) { p.super_fold_with(self) } else { p }
238 }
239 }
240
241 impl<'tcx> TyCtxt<'tcx> {
242 /// Replaces all regions bound by the given `Binder` with the
243 /// results returned by the closure; the closure is expected to
244 /// return a free region (relative to this binder), and hence the
245 /// binder is removed in the return type. The closure is invoked
246 /// once for each unique `BoundRegionKind`; multiple references to the
247 /// same `BoundRegionKind` will reuse the previous result. A map is
248 /// returned at the end with each bound region and the free region
249 /// that replaced it.
250 ///
251 /// # Panics
252 ///
253 /// This method only replaces late bound regions. Any types or
254 /// constants bound by `value` will cause an ICE.
replace_late_bound_regions<T, F>( self, value: Binder<'tcx, T>, mut fld_r: F, ) -> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>) where F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>, T: TypeFoldable<TyCtxt<'tcx>>,255 pub fn replace_late_bound_regions<T, F>(
256 self,
257 value: Binder<'tcx, T>,
258 mut fld_r: F,
259 ) -> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
260 where
261 F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
262 T: TypeFoldable<TyCtxt<'tcx>>,
263 {
264 let mut region_map = BTreeMap::new();
265 let real_fld_r = |br: ty::BoundRegion| *region_map.entry(br).or_insert_with(|| fld_r(br));
266 let value = self.replace_late_bound_regions_uncached(value, real_fld_r);
267 (value, region_map)
268 }
269
replace_late_bound_regions_uncached<T, F>( self, value: Binder<'tcx, T>, mut replace_regions: F, ) -> T where F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>, T: TypeFoldable<TyCtxt<'tcx>>,270 pub fn replace_late_bound_regions_uncached<T, F>(
271 self,
272 value: Binder<'tcx, T>,
273 mut replace_regions: F,
274 ) -> T
275 where
276 F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
277 T: TypeFoldable<TyCtxt<'tcx>>,
278 {
279 let value = value.skip_binder();
280 if !value.has_escaping_bound_vars() {
281 value
282 } else {
283 let delegate = FnMutDelegate {
284 regions: &mut replace_regions,
285 types: &mut |b| bug!("unexpected bound ty in binder: {b:?}"),
286 consts: &mut |b, ty| bug!("unexpected bound ct in binder: {b:?} {ty}"),
287 };
288 let mut replacer = BoundVarReplacer::new(self, delegate);
289 value.fold_with(&mut replacer)
290 }
291 }
292
293 /// Replaces all escaping bound vars. The `fld_r` closure replaces escaping
294 /// bound regions; the `fld_t` closure replaces escaping bound types and the `fld_c`
295 /// closure replaces escaping bound consts.
replace_escaping_bound_vars_uncached<T: TypeFoldable<TyCtxt<'tcx>>>( self, value: T, delegate: impl BoundVarReplacerDelegate<'tcx>, ) -> T296 pub fn replace_escaping_bound_vars_uncached<T: TypeFoldable<TyCtxt<'tcx>>>(
297 self,
298 value: T,
299 delegate: impl BoundVarReplacerDelegate<'tcx>,
300 ) -> T {
301 if !value.has_escaping_bound_vars() {
302 value
303 } else {
304 let mut replacer = BoundVarReplacer::new(self, delegate);
305 value.fold_with(&mut replacer)
306 }
307 }
308
309 /// Replaces all types or regions bound by the given `Binder`. The `fld_r`
310 /// closure replaces bound regions, the `fld_t` closure replaces bound
311 /// types, and `fld_c` replaces bound constants.
replace_bound_vars_uncached<T: TypeFoldable<TyCtxt<'tcx>>>( self, value: Binder<'tcx, T>, delegate: impl BoundVarReplacerDelegate<'tcx>, ) -> T312 pub fn replace_bound_vars_uncached<T: TypeFoldable<TyCtxt<'tcx>>>(
313 self,
314 value: Binder<'tcx, T>,
315 delegate: impl BoundVarReplacerDelegate<'tcx>,
316 ) -> T {
317 self.replace_escaping_bound_vars_uncached(value.skip_binder(), delegate)
318 }
319
320 /// Replaces any late-bound regions bound in `value` with
321 /// free variants attached to `all_outlive_scope`.
liberate_late_bound_regions<T>( self, all_outlive_scope: DefId, value: ty::Binder<'tcx, T>, ) -> T where T: TypeFoldable<TyCtxt<'tcx>>,322 pub fn liberate_late_bound_regions<T>(
323 self,
324 all_outlive_scope: DefId,
325 value: ty::Binder<'tcx, T>,
326 ) -> T
327 where
328 T: TypeFoldable<TyCtxt<'tcx>>,
329 {
330 self.replace_late_bound_regions_uncached(value, |br| {
331 ty::Region::new_free(self, all_outlive_scope, br.kind)
332 })
333 }
334
shift_bound_var_indices<T>(self, bound_vars: usize, value: T) -> T where T: TypeFoldable<TyCtxt<'tcx>>,335 pub fn shift_bound_var_indices<T>(self, bound_vars: usize, value: T) -> T
336 where
337 T: TypeFoldable<TyCtxt<'tcx>>,
338 {
339 let shift_bv = |bv: ty::BoundVar| ty::BoundVar::from_usize(bv.as_usize() + bound_vars);
340 self.replace_escaping_bound_vars_uncached(
341 value,
342 FnMutDelegate {
343 regions: &mut |r: ty::BoundRegion| {
344 ty::Region::new_late_bound(
345 self,
346 ty::INNERMOST,
347 ty::BoundRegion { var: shift_bv(r.var), kind: r.kind },
348 )
349 },
350 types: &mut |t: ty::BoundTy| {
351 Ty::new_bound(
352 self,
353 ty::INNERMOST,
354 ty::BoundTy { var: shift_bv(t.var), kind: t.kind },
355 )
356 },
357 consts: &mut |c, ty: Ty<'tcx>| {
358 ty::Const::new_bound(self, ty::INNERMOST, shift_bv(c), ty)
359 },
360 },
361 )
362 }
363
364 /// Replaces any late-bound regions bound in `value` with `'erased`. Useful in codegen but also
365 /// method lookup and a few other places where precise region relationships are not required.
erase_late_bound_regions<T>(self, value: Binder<'tcx, T>) -> T where T: TypeFoldable<TyCtxt<'tcx>>,366 pub fn erase_late_bound_regions<T>(self, value: Binder<'tcx, T>) -> T
367 where
368 T: TypeFoldable<TyCtxt<'tcx>>,
369 {
370 self.replace_late_bound_regions(value, |_| self.lifetimes.re_erased).0
371 }
372
373 /// Anonymize all bound variables in `value`, this is mostly used to improve caching.
anonymize_bound_vars<T>(self, value: Binder<'tcx, T>) -> Binder<'tcx, T> where T: TypeFoldable<TyCtxt<'tcx>>,374 pub fn anonymize_bound_vars<T>(self, value: Binder<'tcx, T>) -> Binder<'tcx, T>
375 where
376 T: TypeFoldable<TyCtxt<'tcx>>,
377 {
378 struct Anonymize<'a, 'tcx> {
379 tcx: TyCtxt<'tcx>,
380 map: &'a mut FxIndexMap<ty::BoundVar, ty::BoundVariableKind>,
381 }
382 impl<'tcx> BoundVarReplacerDelegate<'tcx> for Anonymize<'_, 'tcx> {
383 fn replace_region(&mut self, br: ty::BoundRegion) -> ty::Region<'tcx> {
384 let entry = self.map.entry(br.var);
385 let index = entry.index();
386 let var = ty::BoundVar::from_usize(index);
387 let kind = entry
388 .or_insert_with(|| ty::BoundVariableKind::Region(ty::BrAnon(None)))
389 .expect_region();
390 let br = ty::BoundRegion { var, kind };
391 ty::Region::new_late_bound(self.tcx, ty::INNERMOST, br)
392 }
393 fn replace_ty(&mut self, bt: ty::BoundTy) -> Ty<'tcx> {
394 let entry = self.map.entry(bt.var);
395 let index = entry.index();
396 let var = ty::BoundVar::from_usize(index);
397 let kind = entry
398 .or_insert_with(|| ty::BoundVariableKind::Ty(ty::BoundTyKind::Anon))
399 .expect_ty();
400 Ty::new_bound(self.tcx, ty::INNERMOST, BoundTy { var, kind })
401 }
402 fn replace_const(&mut self, bv: ty::BoundVar, ty: Ty<'tcx>) -> ty::Const<'tcx> {
403 let entry = self.map.entry(bv);
404 let index = entry.index();
405 let var = ty::BoundVar::from_usize(index);
406 let () = entry.or_insert_with(|| ty::BoundVariableKind::Const).expect_const();
407 ty::Const::new_bound(self.tcx, ty::INNERMOST, var, ty)
408 }
409 }
410
411 let mut map = Default::default();
412 let delegate = Anonymize { tcx: self, map: &mut map };
413 let inner = self.replace_escaping_bound_vars_uncached(value.skip_binder(), delegate);
414 let bound_vars = self.mk_bound_variable_kinds_from_iter(map.into_values());
415 Binder::bind_with_vars(inner, bound_vars)
416 }
417 }
418
419 ///////////////////////////////////////////////////////////////////////////
420 // Shifter
421 //
422 // Shifts the De Bruijn indices on all escaping bound vars by a
423 // fixed amount. Useful in substitution or when otherwise introducing
424 // a binding level that is not intended to capture the existing bound
425 // vars. See comment on `shift_vars_through_binders` method in
426 // `subst.rs` for more details.
427
428 struct Shifter<'tcx> {
429 tcx: TyCtxt<'tcx>,
430 current_index: ty::DebruijnIndex,
431 amount: u32,
432 }
433
434 impl<'tcx> Shifter<'tcx> {
new(tcx: TyCtxt<'tcx>, amount: u32) -> Self435 pub fn new(tcx: TyCtxt<'tcx>, amount: u32) -> Self {
436 Shifter { tcx, current_index: ty::INNERMOST, amount }
437 }
438 }
439
440 impl<'tcx> TypeFolder<TyCtxt<'tcx>> for Shifter<'tcx> {
interner(&self) -> TyCtxt<'tcx>441 fn interner(&self) -> TyCtxt<'tcx> {
442 self.tcx
443 }
444
fold_binder<T: TypeFoldable<TyCtxt<'tcx>>>( &mut self, t: ty::Binder<'tcx, T>, ) -> ty::Binder<'tcx, T>445 fn fold_binder<T: TypeFoldable<TyCtxt<'tcx>>>(
446 &mut self,
447 t: ty::Binder<'tcx, T>,
448 ) -> ty::Binder<'tcx, T> {
449 self.current_index.shift_in(1);
450 let t = t.super_fold_with(self);
451 self.current_index.shift_out(1);
452 t
453 }
454
fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx>455 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
456 match *r {
457 ty::ReLateBound(debruijn, br) if debruijn >= self.current_index => {
458 let debruijn = debruijn.shifted_in(self.amount);
459 ty::Region::new_late_bound(self.tcx, debruijn, br)
460 }
461 _ => r,
462 }
463 }
464
fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx>465 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
466 match *ty.kind() {
467 ty::Bound(debruijn, bound_ty) if debruijn >= self.current_index => {
468 let debruijn = debruijn.shifted_in(self.amount);
469 Ty::new_bound(self.tcx, debruijn, bound_ty)
470 }
471
472 _ if ty.has_vars_bound_at_or_above(self.current_index) => ty.super_fold_with(self),
473 _ => ty,
474 }
475 }
476
fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx>477 fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
478 if let ty::ConstKind::Bound(debruijn, bound_ct) = ct.kind()
479 && debruijn >= self.current_index
480 {
481 let debruijn = debruijn.shifted_in(self.amount);
482 ty::Const::new_bound(self.tcx, debruijn, bound_ct, ct.ty())
483 } else {
484 ct.super_fold_with(self)
485 }
486 }
487
fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx>488 fn fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> {
489 if p.has_vars_bound_at_or_above(self.current_index) { p.super_fold_with(self) } else { p }
490 }
491 }
492
shift_region<'tcx>( tcx: TyCtxt<'tcx>, region: ty::Region<'tcx>, amount: u32, ) -> ty::Region<'tcx>493 pub fn shift_region<'tcx>(
494 tcx: TyCtxt<'tcx>,
495 region: ty::Region<'tcx>,
496 amount: u32,
497 ) -> ty::Region<'tcx> {
498 match *region {
499 ty::ReLateBound(debruijn, br) if amount > 0 => {
500 ty::Region::new_late_bound(tcx, debruijn.shifted_in(amount), br)
501 }
502 _ => region,
503 }
504 }
505
shift_vars<'tcx, T>(tcx: TyCtxt<'tcx>, value: T, amount: u32) -> T where T: TypeFoldable<TyCtxt<'tcx>>,506 pub fn shift_vars<'tcx, T>(tcx: TyCtxt<'tcx>, value: T, amount: u32) -> T
507 where
508 T: TypeFoldable<TyCtxt<'tcx>>,
509 {
510 debug!("shift_vars(value={:?}, amount={})", value, amount);
511
512 if amount == 0 || !value.has_escaping_bound_vars() {
513 return value;
514 }
515
516 value.fold_with(&mut Shifter::new(tcx, amount))
517 }
518