rustc_infer/infer/outlives/for_liveness.rs
1use rustc_middle::ty::{
2 self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor,
3};
4
5use crate::infer::outlives::test_type_match;
6use crate::infer::region_constraints::VerifyIfEq;
7
8/// Visits free regions in the type that are relevant for liveness computation.
9/// These regions are passed to `OP`.
10///
11/// Specifically, we visit all of the regions of types recursively, except if
12/// the type is an alias, we look at the outlives bounds in the param-env
13/// and alias's item bounds. If there is a unique outlives bound, then visit
14/// that instead. If there is not a unique but there is a `'static` outlives
15/// bound, then don't visit anything. Otherwise, walk through the opaque's
16/// regions structurally.
17pub struct FreeRegionsVisitor<'tcx, OP: FnMut(ty::Region<'tcx>)> {
18 pub tcx: TyCtxt<'tcx>,
19 pub param_env: ty::ParamEnv<'tcx>,
20 pub op: OP,
21}
22
23impl<'tcx, OP> TypeVisitor<TyCtxt<'tcx>> for FreeRegionsVisitor<'tcx, OP>
24where
25 OP: FnMut(ty::Region<'tcx>),
26{
27 fn visit_binder<T: TypeVisitable<TyCtxt<'tcx>>>(&mut self, t: &ty::Binder<'tcx, T>) {
28 t.super_visit_with(self);
29 }
30
31 fn visit_region(&mut self, r: ty::Region<'tcx>) {
32 match *r {
33 // ignore bound regions, keep visiting
34 ty::ReBound(_, _) => {}
35 _ => (self.op)(r),
36 }
37 }
38
39 fn visit_ty(&mut self, ty: Ty<'tcx>) {
40 // We're only interested in types involving regions
41 if !ty.flags().intersects(ty::TypeFlags::HAS_FREE_REGIONS) {
42 return;
43 }
44
45 // FIXME: Don't consider alias bounds on types that have escaping bound
46 // vars. See #117455.
47 if ty.has_escaping_bound_vars() {
48 return ty.super_visit_with(self);
49 }
50
51 match *ty.kind() {
52 // We can prove that an alias is live two ways:
53 // 1. All the components are live.
54 //
55 // 2. There is a known outlives bound or where-clause, and that
56 // region is live.
57 //
58 // We search through the item bounds and where clauses for
59 // either `'static` or a unique outlives region, and if one is
60 // found, we just need to prove that that region is still live.
61 // If one is not found, then we continue to walk through the alias.
62 ty::Alias(kind, ty::AliasTy { def_id, args, .. }) => {
63 let tcx = self.tcx;
64 let param_env = self.param_env;
65 let outlives_bounds: Vec<_> = tcx
66 .item_bounds(def_id)
67 .iter_instantiated(tcx, args)
68 .chain(param_env.caller_bounds())
69 .filter_map(|clause| {
70 let outlives = clause.as_type_outlives_clause()?;
71 if let Some(outlives) = outlives.no_bound_vars()
72 && outlives.0 == ty
73 {
74 Some(outlives.1)
75 } else {
76 test_type_match::extract_verify_if_eq(
77 tcx,
78 &outlives.map_bound(|ty::OutlivesPredicate(ty, bound)| {
79 VerifyIfEq { ty, bound }
80 }),
81 ty,
82 )
83 }
84 })
85 .collect();
86 // If we find `'static`, then we know the alias doesn't capture *any* regions.
87 // Otherwise, all of the outlives regions should be equal -- if they're not,
88 // we don't really know how to proceed, so we continue recursing through the
89 // alias.
90 if outlives_bounds.contains(&tcx.lifetimes.re_static) {
91 // no
92 } else if let Some(r) = outlives_bounds.first()
93 && outlives_bounds[1..].iter().all(|other_r| other_r == r)
94 {
95 assert!(r.type_flags().intersects(ty::TypeFlags::HAS_FREE_REGIONS));
96 r.visit_with(self);
97 } else {
98 // Skip lifetime parameters that are not captured, since they do
99 // not need to be live.
100 let variances = tcx.opt_alias_variances(kind, def_id);
101
102 for (idx, s) in args.iter().enumerate() {
103 if variances.map(|variances| variances[idx]) != Some(ty::Bivariant) {
104 s.visit_with(self);
105 }
106 }
107 }
108 }
109
110 _ => ty.super_visit_with(self),
111 }
112 }
113}