rustc_infer/infer/outlives/for_liveness.rs
1use rustc_middle::ty::{
2 self, Flags, 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(_, alias_ty @ ty::AliasTy { kind, args, .. }) => {
60 let tcx = self.tcx;
61 let param_env = self.param_env;
62 let def_id = match kind {
63 ty::AliasTyKind::Projection { def_id }
64 | ty::AliasTyKind::Inherent { def_id }
65 | ty::AliasTyKind::Opaque { def_id }
66 | ty::AliasTyKind::Free { def_id } => def_id,
67 };
68 let outlives_bounds: Vec<_> = tcx
69 .item_bounds(def_id)
70 .iter_instantiated(tcx, args)
71 .map(Unnormalized::skip_norm_wip)
72 .chain(param_env.caller_bounds())
73 .filter_map(|clause| {
74 let outlives = clause.as_type_outlives_clause()?;
75 if let Some(outlives) = outlives.no_bound_vars()
76 && outlives.0 == ty
77 {
78 Some(outlives.1)
79 } else {
80 test_type_match::extract_verify_if_eq(
81 tcx,
82 &outlives.map_bound(|ty::OutlivesPredicate(ty, bound)| {
83 VerifyIfEq { ty, bound }
84 }),
85 // FIXME(#155345): Region handling should generally only
86 // deal with rigid aliases, making sure we do so correctly
87 // everywhere is effort, so we're just using `No` everywhere
88 // for now. This should change soon.
89 alias_ty.to_ty(tcx, ty::IsRigid::No),
90 )
91 }
92 })
93 .collect();
94 // If we find `'static`, then we know the alias doesn't capture *any* regions.
95 // Otherwise, all of the outlives regions should be equal -- if they're not,
96 // we don't really know how to proceed, so we continue recursing through the
97 // alias.
98 if outlives_bounds.contains(&tcx.lifetimes.re_static) {
99 // no
100 } else if let Some(r) = outlives_bounds.first()
101 && outlives_bounds[1..].iter().all(|other_r| other_r == r)
102 {
103 assert!(r.type_flags().intersects(ty::TypeFlags::HAS_FREE_REGIONS));
104 r.visit_with(self);
105 } else {
106 // Skip lifetime parameters that are not captured, since they do
107 // not need to be live.
108 let variances = tcx.opt_alias_variances(kind);
109
110 for (idx, s) in args.iter().enumerate() {
111 if variances.map(|variances| variances[idx]) != Some(ty::Bivariant) {
112 s.visit_with(self);
113 }
114 }
115 }
116 }
117
118 _ => ty.super_visit_with(self),
119 }
120 }
121}