|
| 1 | +use rustc_middle::ty::{self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor}; |
| 2 | + |
| 3 | +use std::ops::ControlFlow; |
| 4 | + |
| 5 | +use crate::infer::outlives::test_type_match; |
| 6 | +use 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: |
| 12 | +/// |
| 13 | +/// 1. If 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. |
| 18 | +/// |
| 19 | +/// 2. If the type is a closure, only walk through the signature of the closure |
| 20 | +/// and the upvars. Similarly, if the type is a generator, walk through the |
| 21 | +/// three "signature" types (yield/resume/return) and its upvars. All generator |
| 22 | +/// interior regions are bound regions, so ther is no need to walk through |
| 23 | +/// that type. |
| 24 | +/// |
| 25 | +/// # How does this differ from other region visitors? |
| 26 | +/// |
| 27 | +/// We cannot use `push_outlives_components` because regions in closure |
| 28 | +/// signatures are not included in their outlives components. We need to |
| 29 | +/// ensure all regions outlive the given bound so that we don't end up with, |
| 30 | +/// say, `ReVar` appearing in a return type and causing ICEs when other |
| 31 | +/// functions end up with region constraints involving regions from other |
| 32 | +/// functions. |
| 33 | +/// |
| 34 | +/// We also cannot use `for_each_free_region` because for closures it includes |
| 35 | +/// the regions parameters from the enclosing item, which we know are always |
| 36 | +/// universal, so we don't particularly care about since they're not relevant |
| 37 | +/// for opaque type captures or computing liveness. |
| 38 | +pub struct FreeRegionsVisitor<'tcx, OP: FnMut(ty::Region<'tcx>)> { |
| 39 | + pub tcx: TyCtxt<'tcx>, |
| 40 | + pub param_env: ty::ParamEnv<'tcx>, |
| 41 | + pub op: OP, |
| 42 | +} |
| 43 | + |
| 44 | +impl<'tcx, OP> TypeVisitor<TyCtxt<'tcx>> for FreeRegionsVisitor<'tcx, OP> |
| 45 | +where |
| 46 | + OP: FnMut(ty::Region<'tcx>), |
| 47 | +{ |
| 48 | + fn visit_binder<T: TypeVisitable<TyCtxt<'tcx>>>( |
| 49 | + &mut self, |
| 50 | + t: &ty::Binder<'tcx, T>, |
| 51 | + ) -> ControlFlow<Self::BreakTy> { |
| 52 | + t.super_visit_with(self); |
| 53 | + ControlFlow::Continue(()) |
| 54 | + } |
| 55 | + |
| 56 | + fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> { |
| 57 | + match *r { |
| 58 | + // ignore bound regions, keep visiting |
| 59 | + ty::ReLateBound(_, _) => ControlFlow::Continue(()), |
| 60 | + _ => { |
| 61 | + (self.op)(r); |
| 62 | + ControlFlow::Continue(()) |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> { |
| 68 | + // We're only interested in types involving regions |
| 69 | + if !ty.flags().intersects(ty::TypeFlags::HAS_FREE_REGIONS) { |
| 70 | + return ControlFlow::Continue(()); |
| 71 | + } |
| 72 | + |
| 73 | + match ty.kind() { |
| 74 | + ty::Closure(_, ref args) => { |
| 75 | + // Skip lifetime parameters of the enclosing item(s) |
| 76 | + |
| 77 | + for upvar in args.as_closure().upvar_tys() { |
| 78 | + upvar.visit_with(self); |
| 79 | + } |
| 80 | + args.as_closure().sig_as_fn_ptr_ty().visit_with(self); |
| 81 | + } |
| 82 | + |
| 83 | + ty::Generator(_, ref args, _) => { |
| 84 | + // Skip lifetime parameters of the enclosing item(s) |
| 85 | + // Also skip the witness type, because that has no free regions. |
| 86 | + |
| 87 | + for upvar in args.as_generator().upvar_tys() { |
| 88 | + upvar.visit_with(self); |
| 89 | + } |
| 90 | + args.as_generator().return_ty().visit_with(self); |
| 91 | + args.as_generator().yield_ty().visit_with(self); |
| 92 | + args.as_generator().resume_ty().visit_with(self); |
| 93 | + } |
| 94 | + |
| 95 | + // We can prove that an alias is live two ways: |
| 96 | + // 1. All the components are live. |
| 97 | + // |
| 98 | + // 2. There is a known outlives bound or where-clause, and that |
| 99 | + // region is live. |
| 100 | + // |
| 101 | + // We search through the item bounds and where clauses for |
| 102 | + // either `'static` or a unique outlives region, and if one is |
| 103 | + // found, we just need to prove that that region is still live. |
| 104 | + // If one is not found, then we continue to walk through the alias. |
| 105 | + ty::Alias(kind, ty::AliasTy { def_id, args, .. }) => { |
| 106 | + let tcx = self.tcx; |
| 107 | + let param_env = self.param_env; |
| 108 | + let outlives_bounds: Vec<_> = tcx |
| 109 | + .item_bounds(def_id) |
| 110 | + .iter_instantiated(tcx, args) |
| 111 | + .chain(param_env.caller_bounds()) |
| 112 | + .filter_map(|clause| { |
| 113 | + let outlives = clause.as_type_outlives_clause()?; |
| 114 | + if let Some(outlives) = outlives.no_bound_vars() |
| 115 | + && outlives.0 == ty |
| 116 | + { |
| 117 | + Some(outlives.1) |
| 118 | + } else { |
| 119 | + test_type_match::extract_verify_if_eq( |
| 120 | + tcx, |
| 121 | + param_env, |
| 122 | + &outlives.map_bound(|ty::OutlivesPredicate(ty, bound)| { |
| 123 | + VerifyIfEq { ty, bound } |
| 124 | + }), |
| 125 | + ty, |
| 126 | + ) |
| 127 | + } |
| 128 | + }) |
| 129 | + .collect(); |
| 130 | + // If we find `'static`, then we know the alias doesn't capture *any* regions. |
| 131 | + // Otherwise, all of the outlives regions should be equal -- if they're not, |
| 132 | + // we don't really know how to proceed, so we continue recursing through the |
| 133 | + // alias. |
| 134 | + if outlives_bounds.contains(&tcx.lifetimes.re_static) { |
| 135 | + // no |
| 136 | + } else if let Some(r) = outlives_bounds.first() |
| 137 | + && outlives_bounds[1..].iter().all(|other_r| other_r == r) |
| 138 | + { |
| 139 | + assert!(r.type_flags().intersects(ty::TypeFlags::HAS_FREE_REGIONS)); |
| 140 | + r.visit_with(self)?; |
| 141 | + } else { |
| 142 | + // Skip lifetime parameters that are not captures. |
| 143 | + let variances = match kind { |
| 144 | + ty::Opaque => Some(self.tcx.variances_of(*def_id)), |
| 145 | + _ => None, |
| 146 | + }; |
| 147 | + |
| 148 | + for (idx, s) in args.iter().enumerate() { |
| 149 | + if variances.map(|variances| variances[idx]) != Some(ty::Variance::Bivariant) { |
| 150 | + s.visit_with(self)?; |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + _ => { |
| 157 | + ty.super_visit_with(self)?; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + ControlFlow::Continue(()) |
| 162 | + } |
| 163 | +} |
0 commit comments