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_region(&mut self, r: ty::Region<'tcx>) {
28 match r.kind() {
29 // ignore bound regions, keep visiting
30 ty::ReBound(_, _) => {}
31 _ => (self.op)(r),
32 }
33 }
34
35 fn visit_ty(&mut self, ty: Ty<'tcx>) {
36 // We're only interested in types involving regions
37 if !ty.flags().intersects(ty::TypeFlags::HAS_FREE_REGIONS) {
38 return;
39 }
40
41 // FIXME: Don't consider alias bounds on types that have escaping bound
42 // vars. See #117455.
43 if ty.has_escaping_bound_vars() {
44 return ty.super_visit_with(self);
45 }
46
47 match *ty.kind() {
48 // We can prove that an alias is live two ways:
49 // 1. All the components are live.
50 //
51 // 2. There is a known outlives bound or where-clause, and that
52 // region is live.
53 //
54 // We search through the item bounds and where clauses for
55 // either `'static` or a unique outlives region, and if one is
56 // found, we just need to prove that that region is still live.
57 // If one is not found, then we continue to walk through the alias.
58 ty::Alias(kind, ty::AliasTy { def_id, args, .. }) => {
59 let tcx = self.tcx;
60 let param_env = self.param_env;
61 let outlives_bounds: Vec<_> = tcx
62 .item_bounds(def_id)
63 .iter_instantiated(tcx, args)
64 .chain(param_env.caller_bounds())
65 .filter_map(|clause| {
66 let outlives = clause.as_type_outlives_clause()?;
67 if let Some(outlives) = outlives.no_bound_vars()
68 && outlives.0 == ty
69 {
70 Some(outlives.1)
71 } else {
72 test_type_match::extract_verify_if_eq(
73 tcx,
74 &outlives.map_bound(|ty::OutlivesPredicate(ty, bound)| {
75 VerifyIfEq { ty, bound }
76 }),
77 ty,
78 )
79 }
80 })
81 .collect();
82 // If we find `'static`, then we know the alias doesn't capture *any* regions.
83 // Otherwise, all of the outlives regions should be equal -- if they're not,
84 // we don't really know how to proceed, so we continue recursing through the
85 // alias.
86 if outlives_bounds.contains(&tcx.lifetimes.re_static) {
87 // no
88 } else if let Some(r) = outlives_bounds.first()
89 && outlives_bounds[1..].iter().all(|other_r| other_r == r)
90 {
91 assert!(r.type_flags().intersects(ty::TypeFlags::HAS_FREE_REGIONS));
92 r.visit_with(self);
93 } else {
94 // Skip lifetime parameters that are not captured, since they do
95 // not need to be live.
96 let variances = tcx.opt_alias_variances(kind, def_id);
97
98 for (idx, s) in args.iter().enumerate() {
99 if variances.map(|variances| variances[idx]) != Some(ty::Bivariant) {
100 s.visit_with(self);
101 }
102 }
103 }
104 }
105
106 _ => ty.super_visit_with(self),
107 }
108 }
109}