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