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