]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_middle/src/ty/fold.rs
New upstream version 1.49.0~beta.4+dfsg1
[rustc.git] / compiler / rustc_middle / src / ty / fold.rs
1 //! Generalized type folding mechanism. The setup is a bit convoluted
2 //! but allows for convenient usage. Let T be an instance of some
3 //! "foldable type" (one which implements `TypeFoldable`) and F be an
4 //! instance of a "folder" (a type which implements `TypeFolder`). Then
5 //! the setup is intended to be:
6 //!
7 //! T.fold_with(F) --calls--> F.fold_T(T) --calls--> T.super_fold_with(F)
8 //!
9 //! This way, when you define a new folder F, you can override
10 //! `fold_T()` to customize the behavior, and invoke `T.super_fold_with()`
11 //! to get the original behavior. Meanwhile, to actually fold
12 //! something, you can just write `T.fold_with(F)`, which is
13 //! convenient. (Note that `fold_with` will also transparently handle
14 //! things like a `Vec<T>` where T is foldable and so on.)
15 //!
16 //! In this ideal setup, the only function that actually *does*
17 //! anything is `T.super_fold_with()`, which traverses the type `T`.
18 //! Moreover, `T.super_fold_with()` should only ever call `T.fold_with()`.
19 //!
20 //! In some cases, we follow a degenerate pattern where we do not have
21 //! a `fold_T` method. Instead, `T.fold_with` traverses the structure directly.
22 //! This is suboptimal because the behavior cannot be overridden, but it's
23 //! much less work to implement. If you ever *do* need an override that
24 //! doesn't exist, it's not hard to convert the degenerate pattern into the
25 //! proper thing.
26 //!
27 //! A `TypeFoldable` T can also be visited by a `TypeVisitor` V using similar setup:
28 //!
29 //! T.visit_with(V) --calls--> V.visit_T(T) --calls--> T.super_visit_with(V).
30 //!
31 //! These methods return true to indicate that the visitor has found what it is
32 //! looking for, and does not need to visit anything else.
33 use crate::ty::{self, flags::FlagComputation, Binder, Ty, TyCtxt, TypeFlags};
34 use rustc_hir as hir;
35 use rustc_hir::def_id::DefId;
36
37 use rustc_data_structures::fx::FxHashSet;
38 use std::collections::BTreeMap;
39 use std::fmt;
40 use std::ops::ControlFlow;
41
42 /// This trait is implemented for every type that can be folded.
43 /// Basically, every type that has a corresponding method in `TypeFolder`.
44 ///
45 /// To implement this conveniently, use the derive macro located in librustc_macros.
46 pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
47 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self;
48 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
49 self.super_fold_with(folder)
50 }
51
52 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()>;
53 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
54 self.super_visit_with(visitor)
55 }
56
57 /// Returns `true` if `self` has any late-bound regions that are either
58 /// bound by `binder` or bound by some binder outside of `binder`.
59 /// If `binder` is `ty::INNERMOST`, this indicates whether
60 /// there are any late-bound regions that appear free.
61 fn has_vars_bound_at_or_above(&self, binder: ty::DebruijnIndex) -> bool {
62 self.visit_with(&mut HasEscapingVarsVisitor { outer_index: binder }).is_break()
63 }
64
65 /// Returns `true` if this `self` has any regions that escape `binder` (and
66 /// hence are not bound by it).
67 fn has_vars_bound_above(&self, binder: ty::DebruijnIndex) -> bool {
68 self.has_vars_bound_at_or_above(binder.shifted_in(1))
69 }
70
71 fn has_escaping_bound_vars(&self) -> bool {
72 self.has_vars_bound_at_or_above(ty::INNERMOST)
73 }
74
75 fn has_type_flags(&self, flags: TypeFlags) -> bool {
76 self.visit_with(&mut HasTypeFlagsVisitor { flags }).is_break()
77 }
78 fn has_projections(&self) -> bool {
79 self.has_type_flags(TypeFlags::HAS_PROJECTION)
80 }
81 fn has_opaque_types(&self) -> bool {
82 self.has_type_flags(TypeFlags::HAS_TY_OPAQUE)
83 }
84 fn references_error(&self) -> bool {
85 self.has_type_flags(TypeFlags::HAS_ERROR)
86 }
87 fn has_param_types_or_consts(&self) -> bool {
88 self.has_type_flags(TypeFlags::HAS_TY_PARAM | TypeFlags::HAS_CT_PARAM)
89 }
90 fn has_infer_regions(&self) -> bool {
91 self.has_type_flags(TypeFlags::HAS_RE_INFER)
92 }
93 fn has_infer_types(&self) -> bool {
94 self.has_type_flags(TypeFlags::HAS_TY_INFER)
95 }
96 fn has_infer_types_or_consts(&self) -> bool {
97 self.has_type_flags(TypeFlags::HAS_TY_INFER | TypeFlags::HAS_CT_INFER)
98 }
99 fn needs_infer(&self) -> bool {
100 self.has_type_flags(TypeFlags::NEEDS_INFER)
101 }
102 fn has_placeholders(&self) -> bool {
103 self.has_type_flags(
104 TypeFlags::HAS_RE_PLACEHOLDER
105 | TypeFlags::HAS_TY_PLACEHOLDER
106 | TypeFlags::HAS_CT_PLACEHOLDER,
107 )
108 }
109 fn needs_subst(&self) -> bool {
110 self.has_type_flags(TypeFlags::NEEDS_SUBST)
111 }
112 /// "Free" regions in this context means that it has any region
113 /// that is not (a) erased or (b) late-bound.
114 fn has_free_regions(&self) -> bool {
115 self.has_type_flags(TypeFlags::HAS_FREE_REGIONS)
116 }
117
118 fn has_erased_regions(&self) -> bool {
119 self.has_type_flags(TypeFlags::HAS_RE_ERASED)
120 }
121
122 /// True if there are any un-erased free regions.
123 fn has_erasable_regions(&self) -> bool {
124 self.has_type_flags(TypeFlags::HAS_FREE_REGIONS)
125 }
126
127 /// Indicates whether this value references only 'global'
128 /// generic parameters that are the same regardless of what fn we are
129 /// in. This is used for caching.
130 fn is_global(&self) -> bool {
131 !self.has_type_flags(TypeFlags::HAS_FREE_LOCAL_NAMES)
132 }
133
134 /// True if there are any late-bound regions
135 fn has_late_bound_regions(&self) -> bool {
136 self.has_type_flags(TypeFlags::HAS_RE_LATE_BOUND)
137 }
138
139 /// Indicates whether this value still has parameters/placeholders/inference variables
140 /// which could be replaced later, in a way that would change the results of `impl`
141 /// specialization.
142 fn still_further_specializable(&self) -> bool {
143 self.has_type_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE)
144 }
145
146 /// A visitor that does not recurse into types, works like `fn walk_shallow` in `Ty`.
147 fn visit_tys_shallow(&self, visit: impl FnMut(Ty<'tcx>) -> ControlFlow<()>) -> ControlFlow<()> {
148 pub struct Visitor<F>(F);
149
150 impl<'tcx, F: FnMut(Ty<'tcx>) -> ControlFlow<()>> TypeVisitor<'tcx> for Visitor<F> {
151 fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<()> {
152 self.0(ty)
153 }
154 }
155
156 self.visit_with(&mut Visitor(visit))
157 }
158 }
159
160 impl TypeFoldable<'tcx> for hir::Constness {
161 fn super_fold_with<F: TypeFolder<'tcx>>(&self, _: &mut F) -> Self {
162 *self
163 }
164 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<()> {
165 ControlFlow::CONTINUE
166 }
167 }
168
169 /// The `TypeFolder` trait defines the actual *folding*. There is a
170 /// method defined for every foldable type. Each of these has a
171 /// default implementation that does an "identity" fold. Within each
172 /// identity fold, it should invoke `foo.fold_with(self)` to fold each
173 /// sub-item.
174 pub trait TypeFolder<'tcx>: Sized {
175 fn tcx<'a>(&'a self) -> TyCtxt<'tcx>;
176
177 fn fold_binder<T>(&mut self, t: &Binder<T>) -> Binder<T>
178 where
179 T: TypeFoldable<'tcx>,
180 {
181 t.super_fold_with(self)
182 }
183
184 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
185 t.super_fold_with(self)
186 }
187
188 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
189 r.super_fold_with(self)
190 }
191
192 fn fold_const(&mut self, c: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
193 c.super_fold_with(self)
194 }
195 }
196
197 pub trait TypeVisitor<'tcx>: Sized {
198 fn visit_binder<T: TypeFoldable<'tcx>>(&mut self, t: &Binder<T>) -> ControlFlow<()> {
199 t.super_visit_with(self)
200 }
201
202 fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<()> {
203 t.super_visit_with(self)
204 }
205
206 fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<()> {
207 r.super_visit_with(self)
208 }
209
210 fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> ControlFlow<()> {
211 c.super_visit_with(self)
212 }
213
214 fn visit_predicate(&mut self, p: ty::Predicate<'tcx>) -> ControlFlow<()> {
215 p.super_visit_with(self)
216 }
217 }
218
219 ///////////////////////////////////////////////////////////////////////////
220 // Some sample folders
221
222 pub struct BottomUpFolder<'tcx, F, G, H>
223 where
224 F: FnMut(Ty<'tcx>) -> Ty<'tcx>,
225 G: FnMut(ty::Region<'tcx>) -> ty::Region<'tcx>,
226 H: FnMut(&'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx>,
227 {
228 pub tcx: TyCtxt<'tcx>,
229 pub ty_op: F,
230 pub lt_op: G,
231 pub ct_op: H,
232 }
233
234 impl<'tcx, F, G, H> TypeFolder<'tcx> for BottomUpFolder<'tcx, F, G, H>
235 where
236 F: FnMut(Ty<'tcx>) -> Ty<'tcx>,
237 G: FnMut(ty::Region<'tcx>) -> ty::Region<'tcx>,
238 H: FnMut(&'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx>,
239 {
240 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
241 self.tcx
242 }
243
244 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
245 let t = ty.super_fold_with(self);
246 (self.ty_op)(t)
247 }
248
249 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
250 let r = r.super_fold_with(self);
251 (self.lt_op)(r)
252 }
253
254 fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
255 let ct = ct.super_fold_with(self);
256 (self.ct_op)(ct)
257 }
258 }
259
260 ///////////////////////////////////////////////////////////////////////////
261 // Region folder
262
263 impl<'tcx> TyCtxt<'tcx> {
264 /// Folds the escaping and free regions in `value` using `f`, and
265 /// sets `skipped_regions` to true if any late-bound region was found
266 /// and skipped.
267 pub fn fold_regions<T>(
268 self,
269 value: &T,
270 skipped_regions: &mut bool,
271 mut f: impl FnMut(ty::Region<'tcx>, ty::DebruijnIndex) -> ty::Region<'tcx>,
272 ) -> T
273 where
274 T: TypeFoldable<'tcx>,
275 {
276 value.fold_with(&mut RegionFolder::new(self, skipped_regions, &mut f))
277 }
278
279 /// Invoke `callback` on every region appearing free in `value`.
280 pub fn for_each_free_region(
281 self,
282 value: &impl TypeFoldable<'tcx>,
283 mut callback: impl FnMut(ty::Region<'tcx>),
284 ) {
285 self.any_free_region_meets(value, |r| {
286 callback(r);
287 false
288 });
289 }
290
291 /// Returns `true` if `callback` returns true for every region appearing free in `value`.
292 pub fn all_free_regions_meet(
293 self,
294 value: &impl TypeFoldable<'tcx>,
295 mut callback: impl FnMut(ty::Region<'tcx>) -> bool,
296 ) -> bool {
297 !self.any_free_region_meets(value, |r| !callback(r))
298 }
299
300 /// Returns `true` if `callback` returns true for some region appearing free in `value`.
301 pub fn any_free_region_meets(
302 self,
303 value: &impl TypeFoldable<'tcx>,
304 callback: impl FnMut(ty::Region<'tcx>) -> bool,
305 ) -> bool {
306 struct RegionVisitor<F> {
307 /// The index of a binder *just outside* the things we have
308 /// traversed. If we encounter a bound region bound by this
309 /// binder or one outer to it, it appears free. Example:
310 ///
311 /// ```
312 /// for<'a> fn(for<'b> fn(), T)
313 /// ^ ^ ^ ^
314 /// | | | | here, would be shifted in 1
315 /// | | | here, would be shifted in 2
316 /// | | here, would be `INNERMOST` shifted in by 1
317 /// | here, initially, binder would be `INNERMOST`
318 /// ```
319 ///
320 /// You see that, initially, *any* bound value is free,
321 /// because we've not traversed any binders. As we pass
322 /// through a binder, we shift the `outer_index` by 1 to
323 /// account for the new binder that encloses us.
324 outer_index: ty::DebruijnIndex,
325 callback: F,
326 }
327
328 impl<'tcx, F> TypeVisitor<'tcx> for RegionVisitor<F>
329 where
330 F: FnMut(ty::Region<'tcx>) -> bool,
331 {
332 fn visit_binder<T: TypeFoldable<'tcx>>(&mut self, t: &Binder<T>) -> ControlFlow<()> {
333 self.outer_index.shift_in(1);
334 let result = t.as_ref().skip_binder().visit_with(self);
335 self.outer_index.shift_out(1);
336 result
337 }
338
339 fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<()> {
340 match *r {
341 ty::ReLateBound(debruijn, _) if debruijn < self.outer_index => {
342 ControlFlow::CONTINUE
343 }
344 _ => {
345 if (self.callback)(r) {
346 ControlFlow::BREAK
347 } else {
348 ControlFlow::CONTINUE
349 }
350 }
351 }
352 }
353
354 fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<()> {
355 // We're only interested in types involving regions
356 if ty.flags().intersects(TypeFlags::HAS_FREE_REGIONS) {
357 ty.super_visit_with(self)
358 } else {
359 ControlFlow::CONTINUE
360 }
361 }
362 }
363
364 value.visit_with(&mut RegionVisitor { outer_index: ty::INNERMOST, callback }).is_break()
365 }
366 }
367
368 /// Folds over the substructure of a type, visiting its component
369 /// types and all regions that occur *free* within it.
370 ///
371 /// That is, `Ty` can contain function or method types that bind
372 /// regions at the call site (`ReLateBound`), and occurrences of
373 /// regions (aka "lifetimes") that are bound within a type are not
374 /// visited by this folder; only regions that occur free will be
375 /// visited by `fld_r`.
376
377 pub struct RegionFolder<'a, 'tcx> {
378 tcx: TyCtxt<'tcx>,
379 skipped_regions: &'a mut bool,
380
381 /// Stores the index of a binder *just outside* the stuff we have
382 /// visited. So this begins as INNERMOST; when we pass through a
383 /// binder, it is incremented (via `shift_in`).
384 current_index: ty::DebruijnIndex,
385
386 /// Callback invokes for each free region. The `DebruijnIndex`
387 /// points to the binder *just outside* the ones we have passed
388 /// through.
389 fold_region_fn:
390 &'a mut (dyn FnMut(ty::Region<'tcx>, ty::DebruijnIndex) -> ty::Region<'tcx> + 'a),
391 }
392
393 impl<'a, 'tcx> RegionFolder<'a, 'tcx> {
394 #[inline]
395 pub fn new(
396 tcx: TyCtxt<'tcx>,
397 skipped_regions: &'a mut bool,
398 fold_region_fn: &'a mut dyn FnMut(ty::Region<'tcx>, ty::DebruijnIndex) -> ty::Region<'tcx>,
399 ) -> RegionFolder<'a, 'tcx> {
400 RegionFolder { tcx, skipped_regions, current_index: ty::INNERMOST, fold_region_fn }
401 }
402 }
403
404 impl<'a, 'tcx> TypeFolder<'tcx> for RegionFolder<'a, 'tcx> {
405 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
406 self.tcx
407 }
408
409 fn fold_binder<T: TypeFoldable<'tcx>>(&mut self, t: &ty::Binder<T>) -> ty::Binder<T> {
410 self.current_index.shift_in(1);
411 let t = t.super_fold_with(self);
412 self.current_index.shift_out(1);
413 t
414 }
415
416 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
417 match *r {
418 ty::ReLateBound(debruijn, _) if debruijn < self.current_index => {
419 debug!(
420 "RegionFolder.fold_region({:?}) skipped bound region (current index={:?})",
421 r, self.current_index
422 );
423 *self.skipped_regions = true;
424 r
425 }
426 _ => {
427 debug!(
428 "RegionFolder.fold_region({:?}) folding free region (current_index={:?})",
429 r, self.current_index
430 );
431 (self.fold_region_fn)(r, self.current_index)
432 }
433 }
434 }
435 }
436
437 ///////////////////////////////////////////////////////////////////////////
438 // Bound vars replacer
439
440 /// Replaces the escaping bound vars (late bound regions or bound types) in a type.
441 struct BoundVarReplacer<'a, 'tcx> {
442 tcx: TyCtxt<'tcx>,
443
444 /// As with `RegionFolder`, represents the index of a binder *just outside*
445 /// the ones we have visited.
446 current_index: ty::DebruijnIndex,
447
448 fld_r: &'a mut (dyn FnMut(ty::BoundRegion) -> ty::Region<'tcx> + 'a),
449 fld_t: &'a mut (dyn FnMut(ty::BoundTy) -> Ty<'tcx> + 'a),
450 fld_c: &'a mut (dyn FnMut(ty::BoundVar, Ty<'tcx>) -> &'tcx ty::Const<'tcx> + 'a),
451 }
452
453 impl<'a, 'tcx> BoundVarReplacer<'a, 'tcx> {
454 fn new<F, G, H>(tcx: TyCtxt<'tcx>, fld_r: &'a mut F, fld_t: &'a mut G, fld_c: &'a mut H) -> Self
455 where
456 F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
457 G: FnMut(ty::BoundTy) -> Ty<'tcx>,
458 H: FnMut(ty::BoundVar, Ty<'tcx>) -> &'tcx ty::Const<'tcx>,
459 {
460 BoundVarReplacer { tcx, current_index: ty::INNERMOST, fld_r, fld_t, fld_c }
461 }
462 }
463
464 impl<'a, 'tcx> TypeFolder<'tcx> for BoundVarReplacer<'a, 'tcx> {
465 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
466 self.tcx
467 }
468
469 fn fold_binder<T: TypeFoldable<'tcx>>(&mut self, t: &ty::Binder<T>) -> ty::Binder<T> {
470 self.current_index.shift_in(1);
471 let t = t.super_fold_with(self);
472 self.current_index.shift_out(1);
473 t
474 }
475
476 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
477 match *t.kind() {
478 ty::Bound(debruijn, bound_ty) => {
479 if debruijn == self.current_index {
480 let fld_t = &mut self.fld_t;
481 let ty = fld_t(bound_ty);
482 ty::fold::shift_vars(self.tcx, &ty, self.current_index.as_u32())
483 } else {
484 t
485 }
486 }
487 _ => {
488 if !t.has_vars_bound_at_or_above(self.current_index) {
489 // Nothing more to substitute.
490 t
491 } else {
492 t.super_fold_with(self)
493 }
494 }
495 }
496 }
497
498 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
499 match *r {
500 ty::ReLateBound(debruijn, br) if debruijn == self.current_index => {
501 let fld_r = &mut self.fld_r;
502 let region = fld_r(br);
503 if let ty::ReLateBound(debruijn1, br) = *region {
504 // If the callback returns a late-bound region,
505 // that region should always use the INNERMOST
506 // debruijn index. Then we adjust it to the
507 // correct depth.
508 assert_eq!(debruijn1, ty::INNERMOST);
509 self.tcx.mk_region(ty::ReLateBound(debruijn, br))
510 } else {
511 region
512 }
513 }
514 _ => r,
515 }
516 }
517
518 fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
519 if let ty::Const { val: ty::ConstKind::Bound(debruijn, bound_const), ty } = *ct {
520 if debruijn == self.current_index {
521 let fld_c = &mut self.fld_c;
522 let ct = fld_c(bound_const, ty);
523 ty::fold::shift_vars(self.tcx, &ct, self.current_index.as_u32())
524 } else {
525 ct
526 }
527 } else {
528 if !ct.has_vars_bound_at_or_above(self.current_index) {
529 // Nothing more to substitute.
530 ct
531 } else {
532 ct.super_fold_with(self)
533 }
534 }
535 }
536 }
537
538 impl<'tcx> TyCtxt<'tcx> {
539 /// Replaces all regions bound by the given `Binder` with the
540 /// results returned by the closure; the closure is expected to
541 /// return a free region (relative to this binder), and hence the
542 /// binder is removed in the return type. The closure is invoked
543 /// once for each unique `BoundRegion`; multiple references to the
544 /// same `BoundRegion` will reuse the previous result. A map is
545 /// returned at the end with each bound region and the free region
546 /// that replaced it.
547 ///
548 /// This method only replaces late bound regions and the result may still
549 /// contain escaping bound types.
550 pub fn replace_late_bound_regions<T, F>(
551 self,
552 value: &Binder<T>,
553 fld_r: F,
554 ) -> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
555 where
556 F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
557 T: TypeFoldable<'tcx>,
558 {
559 // identity for bound types and consts
560 let fld_t = |bound_ty| self.mk_ty(ty::Bound(ty::INNERMOST, bound_ty));
561 let fld_c = |bound_ct, ty| {
562 self.mk_const(ty::Const { val: ty::ConstKind::Bound(ty::INNERMOST, bound_ct), ty })
563 };
564 self.replace_escaping_bound_vars(value.as_ref().skip_binder(), fld_r, fld_t, fld_c)
565 }
566
567 /// Replaces all escaping bound vars. The `fld_r` closure replaces escaping
568 /// bound regions; the `fld_t` closure replaces escaping bound types and the `fld_c`
569 /// closure replaces escaping bound consts.
570 pub fn replace_escaping_bound_vars<T, F, G, H>(
571 self,
572 value: &T,
573 mut fld_r: F,
574 mut fld_t: G,
575 mut fld_c: H,
576 ) -> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
577 where
578 F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
579 G: FnMut(ty::BoundTy) -> Ty<'tcx>,
580 H: FnMut(ty::BoundVar, Ty<'tcx>) -> &'tcx ty::Const<'tcx>,
581 T: TypeFoldable<'tcx>,
582 {
583 use rustc_data_structures::fx::FxHashMap;
584
585 let mut region_map = BTreeMap::new();
586 let mut type_map = FxHashMap::default();
587 let mut const_map = FxHashMap::default();
588
589 if !value.has_escaping_bound_vars() {
590 (value.clone(), region_map)
591 } else {
592 let mut real_fld_r = |br| *region_map.entry(br).or_insert_with(|| fld_r(br));
593
594 let mut real_fld_t =
595 |bound_ty| *type_map.entry(bound_ty).or_insert_with(|| fld_t(bound_ty));
596
597 let mut real_fld_c =
598 |bound_ct, ty| *const_map.entry(bound_ct).or_insert_with(|| fld_c(bound_ct, ty));
599
600 let mut replacer =
601 BoundVarReplacer::new(self, &mut real_fld_r, &mut real_fld_t, &mut real_fld_c);
602 let result = value.fold_with(&mut replacer);
603 (result, region_map)
604 }
605 }
606
607 /// Replaces all types or regions bound by the given `Binder`. The `fld_r`
608 /// closure replaces bound regions while the `fld_t` closure replaces bound
609 /// types.
610 pub fn replace_bound_vars<T, F, G, H>(
611 self,
612 value: &Binder<T>,
613 fld_r: F,
614 fld_t: G,
615 fld_c: H,
616 ) -> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
617 where
618 F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
619 G: FnMut(ty::BoundTy) -> Ty<'tcx>,
620 H: FnMut(ty::BoundVar, Ty<'tcx>) -> &'tcx ty::Const<'tcx>,
621 T: TypeFoldable<'tcx>,
622 {
623 self.replace_escaping_bound_vars(value.as_ref().skip_binder(), fld_r, fld_t, fld_c)
624 }
625
626 /// Replaces any late-bound regions bound in `value` with
627 /// free variants attached to `all_outlive_scope`.
628 pub fn liberate_late_bound_regions<T>(
629 self,
630 all_outlive_scope: DefId,
631 value: &ty::Binder<T>,
632 ) -> T
633 where
634 T: TypeFoldable<'tcx>,
635 {
636 self.replace_late_bound_regions(value, |br| {
637 self.mk_region(ty::ReFree(ty::FreeRegion {
638 scope: all_outlive_scope,
639 bound_region: br,
640 }))
641 })
642 .0
643 }
644
645 /// Returns a set of all late-bound regions that are constrained
646 /// by `value`, meaning that if we instantiate those LBR with
647 /// variables and equate `value` with something else, those
648 /// variables will also be equated.
649 pub fn collect_constrained_late_bound_regions<T>(
650 self,
651 value: &Binder<T>,
652 ) -> FxHashSet<ty::BoundRegion>
653 where
654 T: TypeFoldable<'tcx>,
655 {
656 self.collect_late_bound_regions(value, true)
657 }
658
659 /// Returns a set of all late-bound regions that appear in `value` anywhere.
660 pub fn collect_referenced_late_bound_regions<T>(
661 self,
662 value: &Binder<T>,
663 ) -> FxHashSet<ty::BoundRegion>
664 where
665 T: TypeFoldable<'tcx>,
666 {
667 self.collect_late_bound_regions(value, false)
668 }
669
670 fn collect_late_bound_regions<T>(
671 self,
672 value: &Binder<T>,
673 just_constraint: bool,
674 ) -> FxHashSet<ty::BoundRegion>
675 where
676 T: TypeFoldable<'tcx>,
677 {
678 let mut collector = LateBoundRegionsCollector::new(just_constraint);
679 let result = value.as_ref().skip_binder().visit_with(&mut collector);
680 assert!(result.is_continue()); // should never have stopped early
681 collector.regions
682 }
683
684 /// Replaces any late-bound regions bound in `value` with `'erased`. Useful in codegen but also
685 /// method lookup and a few other places where precise region relationships are not required.
686 pub fn erase_late_bound_regions<T>(self, value: &Binder<T>) -> T
687 where
688 T: TypeFoldable<'tcx>,
689 {
690 self.replace_late_bound_regions(value, |_| self.lifetimes.re_erased).0
691 }
692
693 /// Rewrite any late-bound regions so that they are anonymous. Region numbers are
694 /// assigned starting at 0 and increasing monotonically in the order traversed
695 /// by the fold operation.
696 ///
697 /// The chief purpose of this function is to canonicalize regions so that two
698 /// `FnSig`s or `TraitRef`s which are equivalent up to region naming will become
699 /// structurally identical. For example, `for<'a, 'b> fn(&'a isize, &'b isize)` and
700 /// `for<'a, 'b> fn(&'b isize, &'a isize)` will become identical after anonymization.
701 pub fn anonymize_late_bound_regions<T>(self, sig: &Binder<T>) -> Binder<T>
702 where
703 T: TypeFoldable<'tcx>,
704 {
705 let mut counter = 0;
706 Binder::bind(
707 self.replace_late_bound_regions(sig, |_| {
708 let r = self.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BrAnon(counter)));
709 counter += 1;
710 r
711 })
712 .0,
713 )
714 }
715 }
716
717 ///////////////////////////////////////////////////////////////////////////
718 // Shifter
719 //
720 // Shifts the De Bruijn indices on all escaping bound vars by a
721 // fixed amount. Useful in substitution or when otherwise introducing
722 // a binding level that is not intended to capture the existing bound
723 // vars. See comment on `shift_vars_through_binders` method in
724 // `subst.rs` for more details.
725
726 struct Shifter<'tcx> {
727 tcx: TyCtxt<'tcx>,
728 current_index: ty::DebruijnIndex,
729 amount: u32,
730 }
731
732 impl Shifter<'tcx> {
733 pub fn new(tcx: TyCtxt<'tcx>, amount: u32) -> Self {
734 Shifter { tcx, current_index: ty::INNERMOST, amount }
735 }
736 }
737
738 impl TypeFolder<'tcx> for Shifter<'tcx> {
739 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
740 self.tcx
741 }
742
743 fn fold_binder<T: TypeFoldable<'tcx>>(&mut self, t: &ty::Binder<T>) -> ty::Binder<T> {
744 self.current_index.shift_in(1);
745 let t = t.super_fold_with(self);
746 self.current_index.shift_out(1);
747 t
748 }
749
750 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
751 match *r {
752 ty::ReLateBound(debruijn, br) => {
753 if self.amount == 0 || debruijn < self.current_index {
754 r
755 } else {
756 let debruijn = debruijn.shifted_in(self.amount);
757 let shifted = ty::ReLateBound(debruijn, br);
758 self.tcx.mk_region(shifted)
759 }
760 }
761 _ => r,
762 }
763 }
764
765 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
766 match *ty.kind() {
767 ty::Bound(debruijn, bound_ty) => {
768 if self.amount == 0 || debruijn < self.current_index {
769 ty
770 } else {
771 let debruijn = debruijn.shifted_in(self.amount);
772 self.tcx.mk_ty(ty::Bound(debruijn, bound_ty))
773 }
774 }
775
776 _ => ty.super_fold_with(self),
777 }
778 }
779
780 fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
781 if let ty::Const { val: ty::ConstKind::Bound(debruijn, bound_ct), ty } = *ct {
782 if self.amount == 0 || debruijn < self.current_index {
783 ct
784 } else {
785 let debruijn = debruijn.shifted_in(self.amount);
786 self.tcx.mk_const(ty::Const { val: ty::ConstKind::Bound(debruijn, bound_ct), ty })
787 }
788 } else {
789 ct.super_fold_with(self)
790 }
791 }
792 }
793
794 pub fn shift_region<'tcx>(
795 tcx: TyCtxt<'tcx>,
796 region: ty::Region<'tcx>,
797 amount: u32,
798 ) -> ty::Region<'tcx> {
799 match region {
800 ty::ReLateBound(debruijn, br) if amount > 0 => {
801 tcx.mk_region(ty::ReLateBound(debruijn.shifted_in(amount), *br))
802 }
803 _ => region,
804 }
805 }
806
807 pub fn shift_vars<'tcx, T>(tcx: TyCtxt<'tcx>, value: &T, amount: u32) -> T
808 where
809 T: TypeFoldable<'tcx>,
810 {
811 debug!("shift_vars(value={:?}, amount={})", value, amount);
812
813 value.fold_with(&mut Shifter::new(tcx, amount))
814 }
815
816 /// An "escaping var" is a bound var whose binder is not part of `t`. A bound var can be a
817 /// bound region or a bound type.
818 ///
819 /// So, for example, consider a type like the following, which has two binders:
820 ///
821 /// for<'a> fn(x: for<'b> fn(&'a isize, &'b isize))
822 /// ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ outer scope
823 /// ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ inner scope
824 ///
825 /// This type has *bound regions* (`'a`, `'b`), but it does not have escaping regions, because the
826 /// binders of both `'a` and `'b` are part of the type itself. However, if we consider the *inner
827 /// fn type*, that type has an escaping region: `'a`.
828 ///
829 /// Note that what I'm calling an "escaping var" is often just called a "free var". However,
830 /// we already use the term "free var". It refers to the regions or types that we use to represent
831 /// bound regions or type params on a fn definition while we are type checking its body.
832 ///
833 /// To clarify, conceptually there is no particular difference between
834 /// an "escaping" var and a "free" var. However, there is a big
835 /// difference in practice. Basically, when "entering" a binding
836 /// level, one is generally required to do some sort of processing to
837 /// a bound var, such as replacing it with a fresh/placeholder
838 /// var, or making an entry in the environment to represent the
839 /// scope to which it is attached, etc. An escaping var represents
840 /// a bound var for which this processing has not yet been done.
841 struct HasEscapingVarsVisitor {
842 /// Anything bound by `outer_index` or "above" is escaping.
843 outer_index: ty::DebruijnIndex,
844 }
845
846 impl<'tcx> TypeVisitor<'tcx> for HasEscapingVarsVisitor {
847 fn visit_binder<T: TypeFoldable<'tcx>>(&mut self, t: &Binder<T>) -> ControlFlow<()> {
848 self.outer_index.shift_in(1);
849 let result = t.super_visit_with(self);
850 self.outer_index.shift_out(1);
851 result
852 }
853
854 fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<()> {
855 // If the outer-exclusive-binder is *strictly greater* than
856 // `outer_index`, that means that `t` contains some content
857 // bound at `outer_index` or above (because
858 // `outer_exclusive_binder` is always 1 higher than the
859 // content in `t`). Therefore, `t` has some escaping vars.
860 if t.outer_exclusive_binder > self.outer_index {
861 ControlFlow::BREAK
862 } else {
863 ControlFlow::CONTINUE
864 }
865 }
866
867 fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<()> {
868 // If the region is bound by `outer_index` or anything outside
869 // of outer index, then it escapes the binders we have
870 // visited.
871 if r.bound_at_or_above_binder(self.outer_index) {
872 ControlFlow::BREAK
873 } else {
874 ControlFlow::CONTINUE
875 }
876 }
877
878 fn visit_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> ControlFlow<()> {
879 // we don't have a `visit_infer_const` callback, so we have to
880 // hook in here to catch this case (annoying...), but
881 // otherwise we do want to remember to visit the rest of the
882 // const, as it has types/regions embedded in a lot of other
883 // places.
884 match ct.val {
885 ty::ConstKind::Bound(debruijn, _) if debruijn >= self.outer_index => ControlFlow::BREAK,
886 _ => ct.super_visit_with(self),
887 }
888 }
889
890 fn visit_predicate(&mut self, predicate: ty::Predicate<'tcx>) -> ControlFlow<()> {
891 if predicate.inner.outer_exclusive_binder > self.outer_index {
892 ControlFlow::BREAK
893 } else {
894 ControlFlow::CONTINUE
895 }
896 }
897 }
898
899 // FIXME: Optimize for checking for infer flags
900 struct HasTypeFlagsVisitor {
901 flags: ty::TypeFlags,
902 }
903
904 impl<'tcx> TypeVisitor<'tcx> for HasTypeFlagsVisitor {
905 fn visit_ty(&mut self, t: Ty<'_>) -> ControlFlow<()> {
906 debug!(
907 "HasTypeFlagsVisitor: t={:?} t.flags={:?} self.flags={:?}",
908 t,
909 t.flags(),
910 self.flags
911 );
912 if t.flags().intersects(self.flags) { ControlFlow::BREAK } else { ControlFlow::CONTINUE }
913 }
914
915 fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<()> {
916 let flags = r.type_flags();
917 debug!("HasTypeFlagsVisitor: r={:?} r.flags={:?} self.flags={:?}", r, flags, self.flags);
918 if flags.intersects(self.flags) { ControlFlow::BREAK } else { ControlFlow::CONTINUE }
919 }
920
921 fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> ControlFlow<()> {
922 let flags = FlagComputation::for_const(c);
923 debug!("HasTypeFlagsVisitor: c={:?} c.flags={:?} self.flags={:?}", c, flags, self.flags);
924 if flags.intersects(self.flags) { ControlFlow::BREAK } else { ControlFlow::CONTINUE }
925 }
926
927 fn visit_predicate(&mut self, predicate: ty::Predicate<'tcx>) -> ControlFlow<()> {
928 debug!(
929 "HasTypeFlagsVisitor: predicate={:?} predicate.flags={:?} self.flags={:?}",
930 predicate, predicate.inner.flags, self.flags
931 );
932 if predicate.inner.flags.intersects(self.flags) {
933 ControlFlow::BREAK
934 } else {
935 ControlFlow::CONTINUE
936 }
937 }
938 }
939
940 /// Collects all the late-bound regions at the innermost binding level
941 /// into a hash set.
942 struct LateBoundRegionsCollector {
943 current_index: ty::DebruijnIndex,
944 regions: FxHashSet<ty::BoundRegion>,
945
946 /// `true` if we only want regions that are known to be
947 /// "constrained" when you equate this type with another type. In
948 /// particular, if you have e.g., `&'a u32` and `&'b u32`, equating
949 /// them constraints `'a == 'b`. But if you have `<&'a u32 as
950 /// Trait>::Foo` and `<&'b u32 as Trait>::Foo`, normalizing those
951 /// types may mean that `'a` and `'b` don't appear in the results,
952 /// so they are not considered *constrained*.
953 just_constrained: bool,
954 }
955
956 impl LateBoundRegionsCollector {
957 fn new(just_constrained: bool) -> Self {
958 LateBoundRegionsCollector {
959 current_index: ty::INNERMOST,
960 regions: Default::default(),
961 just_constrained,
962 }
963 }
964 }
965
966 impl<'tcx> TypeVisitor<'tcx> for LateBoundRegionsCollector {
967 fn visit_binder<T: TypeFoldable<'tcx>>(&mut self, t: &Binder<T>) -> ControlFlow<()> {
968 self.current_index.shift_in(1);
969 let result = t.super_visit_with(self);
970 self.current_index.shift_out(1);
971 result
972 }
973
974 fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<()> {
975 // if we are only looking for "constrained" region, we have to
976 // ignore the inputs to a projection, as they may not appear
977 // in the normalized form
978 if self.just_constrained {
979 if let ty::Projection(..) | ty::Opaque(..) = t.kind() {
980 return ControlFlow::CONTINUE;
981 }
982 }
983
984 t.super_visit_with(self)
985 }
986
987 fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> ControlFlow<()> {
988 // if we are only looking for "constrained" region, we have to
989 // ignore the inputs of an unevaluated const, as they may not appear
990 // in the normalized form
991 if self.just_constrained {
992 if let ty::ConstKind::Unevaluated(..) = c.val {
993 return ControlFlow::CONTINUE;
994 }
995 }
996
997 c.super_visit_with(self)
998 }
999
1000 fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<()> {
1001 if let ty::ReLateBound(debruijn, br) = *r {
1002 if debruijn == self.current_index {
1003 self.regions.insert(br);
1004 }
1005 }
1006 ControlFlow::CONTINUE
1007 }
1008 }