|
| 1 | +use super::VariableLengths; |
| 2 | +use crate::infer::InferCtxt; |
| 3 | +use rustc_middle::ty::{self, Ty, TyCtxt}; |
| 4 | +use rustc_middle::ty::{TypeSuperVisitable, TypeVisitor}; |
| 5 | +use std::ops::ControlFlow; |
| 6 | + |
| 7 | +pub struct HasSnapshotLeaksVisitor { |
| 8 | + universe: ty::UniverseIndex, |
| 9 | + variable_lengths: VariableLengths, |
| 10 | +} |
| 11 | +impl HasSnapshotLeaksVisitor { |
| 12 | + pub fn new<'tcx>(infcx: &InferCtxt<'tcx>) -> Self { |
| 13 | + HasSnapshotLeaksVisitor { |
| 14 | + universe: infcx.universe(), |
| 15 | + variable_lengths: infcx.variable_lengths(), |
| 16 | + } |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +fn continue_if(b: bool) -> ControlFlow<()> { |
| 21 | + if b { ControlFlow::Continue(()) } else { ControlFlow::Break(()) } |
| 22 | +} |
| 23 | + |
| 24 | +impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for HasSnapshotLeaksVisitor { |
| 25 | + type Result = ControlFlow<()>; |
| 26 | + |
| 27 | + fn visit_region(&mut self, r: ty::Region<'tcx>) -> Self::Result { |
| 28 | + match r.kind() { |
| 29 | + ty::ReVar(var) => continue_if(var.as_usize() < self.variable_lengths.region_vars), |
| 30 | + ty::RePlaceholder(p) => continue_if(self.universe.can_name(p.universe)), |
| 31 | + ty::ReEarlyParam(_) |
| 32 | + | ty::ReBound(_, _) |
| 33 | + | ty::ReLateParam(_) |
| 34 | + | ty::ReStatic |
| 35 | + | ty::ReErased |
| 36 | + | ty::ReError(_) => ControlFlow::Continue(()), |
| 37 | + } |
| 38 | + } |
| 39 | + fn visit_ty(&mut self, t: Ty<'tcx>) -> Self::Result { |
| 40 | + match t.kind() { |
| 41 | + ty::Infer(ty::TyVar(var)) => { |
| 42 | + continue_if(var.as_usize() < self.variable_lengths.type_vars) |
| 43 | + } |
| 44 | + ty::Infer(ty::IntVar(var)) => { |
| 45 | + continue_if(var.as_usize() < self.variable_lengths.int_vars) |
| 46 | + } |
| 47 | + ty::Infer(ty::FloatVar(var)) => { |
| 48 | + continue_if(var.as_usize() < self.variable_lengths.float_vars) |
| 49 | + } |
| 50 | + ty::Placeholder(p) => continue_if(self.universe.can_name(p.universe)), |
| 51 | + ty::Infer(ty::FreshTy(..) | ty::FreshIntTy(..) | ty::FreshFloatTy(..)) |
| 52 | + | ty::Bool |
| 53 | + | ty::Char |
| 54 | + | ty::Int(_) |
| 55 | + | ty::Uint(_) |
| 56 | + | ty::Float(_) |
| 57 | + | ty::Adt(_, _) |
| 58 | + | ty::Foreign(_) |
| 59 | + | ty::Str |
| 60 | + | ty::Array(_, _) |
| 61 | + | ty::Slice(_) |
| 62 | + | ty::RawPtr(_) |
| 63 | + | ty::Ref(_, _, _) |
| 64 | + | ty::FnDef(_, _) |
| 65 | + | ty::FnPtr(_) |
| 66 | + | ty::Dynamic(_, _, _) |
| 67 | + | ty::Closure(_, _) |
| 68 | + | ty::CoroutineClosure(_, _) |
| 69 | + | ty::Coroutine(_, _) |
| 70 | + | ty::CoroutineWitness(_, _) |
| 71 | + | ty::Never |
| 72 | + | ty::Tuple(_) |
| 73 | + | ty::Alias(_, _) |
| 74 | + | ty::Param(_) |
| 75 | + | ty::Bound(_, _) |
| 76 | + | ty::Error(_) => t.super_visit_with(self), |
| 77 | + } |
| 78 | + } |
| 79 | + fn visit_const(&mut self, c: ty::Const<'tcx>) -> Self::Result { |
| 80 | + match c.kind() { |
| 81 | + ty::ConstKind::Infer(ty::InferConst::Var(var)) => { |
| 82 | + continue_if(var.as_usize() < self.variable_lengths.const_vars) |
| 83 | + } |
| 84 | + // FIXME(const_trait_impl): need to handle effect vars here and in `fudge_inference_if_ok`. |
| 85 | + ty::ConstKind::Infer(ty::InferConst::EffectVar(_)) => ControlFlow::Continue(()), |
| 86 | + ty::ConstKind::Placeholder(p) => continue_if(self.universe.can_name(p.universe)), |
| 87 | + ty::ConstKind::Infer(ty::InferConst::Fresh(_)) |
| 88 | + | ty::ConstKind::Param(_) |
| 89 | + | ty::ConstKind::Bound(_, _) |
| 90 | + | ty::ConstKind::Unevaluated(_) |
| 91 | + | ty::ConstKind::Value(_) |
| 92 | + | ty::ConstKind::Expr(_) |
| 93 | + | ty::ConstKind::Error(_) => c.super_visit_with(self), |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +#[macro_export] |
| 99 | +macro_rules! type_foldable_verify_no_snapshot_leaks { |
| 100 | + ($tcx:lifetime, $t:ty) => { |
| 101 | + const _: () = { |
| 102 | + use rustc_middle::ty::TypeVisitable; |
| 103 | + use $crate::infer::snapshot::check_leaks::HasSnapshotLeaksVisitor; |
| 104 | + use $crate::infer::InferCtxt; |
| 105 | + impl<$tcx> $crate::infer::snapshot::NoSnapshotLeaks<$tcx> for $t { |
| 106 | + type DataStart = HasSnapshotLeaksVisitor; |
| 107 | + type DataEnd = HasSnapshotLeaksVisitor; |
| 108 | + fn mk_data_snapshot_start( |
| 109 | + infcx: &$crate::infer::InferCtxt<$tcx>, |
| 110 | + ) -> Self::DataStart { |
| 111 | + HasSnapshotLeaksVisitor::new(infcx) |
| 112 | + } |
| 113 | + fn mk_data_snapshot_end( |
| 114 | + _: &InferCtxt<'tcx>, |
| 115 | + visitor: Self::DataStart, |
| 116 | + ) -> Self::DataEnd { |
| 117 | + visitor |
| 118 | + } |
| 119 | + fn avoid_leaks( |
| 120 | + self, |
| 121 | + _: &InferCtxt<$tcx>, |
| 122 | + mut visitor: HasSnapshotLeaksVisitor, |
| 123 | + ) -> Self { |
| 124 | + if cfg!(debug_assertions) && self.visit_with(&mut visitor).is_break() { |
| 125 | + bug!("leaking vars from snapshot: {self:?}"); |
| 126 | + } |
| 127 | + |
| 128 | + self |
| 129 | + } |
| 130 | + } |
| 131 | + }; |
| 132 | + }; |
| 133 | +} |
0 commit comments