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