|
| 1 | +use rustc_data_structures::fx::FxHashSet; |
| 2 | +use rustc_data_structures::unord::UnordSet; |
| 3 | +use rustc_hir::def_id::DefId; |
| 4 | +use rustc_span::Span; |
| 5 | +use smallvec::{SmallVec, smallvec}; |
| 6 | +use tracing::{debug, instrument}; |
| 7 | + |
| 8 | +use crate::ty::{self, Ty, TyCtxt}; |
| 9 | + |
| 10 | +/// An additional filter to exclude well-known types from the ecosystem |
| 11 | +/// because their drops are trivial. |
| 12 | +/// This returns additional types to check if the drops are delegated to those. |
| 13 | +/// A typical example is `hashbrown::HashMap<K, V>`, whose drop is delegated to `K` and `V`. |
| 14 | +fn true_significant_drop_ty<'tcx>( |
| 15 | + tcx: TyCtxt<'tcx>, |
| 16 | + ty: Ty<'tcx>, |
| 17 | +) -> Option<SmallVec<[Ty<'tcx>; 2]>> { |
| 18 | + if let ty::Adt(def, args) = ty.kind() { |
| 19 | + let mut did = def.did(); |
| 20 | + let mut name_rev = vec![]; |
| 21 | + loop { |
| 22 | + let key = tcx.def_key(did); |
| 23 | + |
| 24 | + match key.disambiguated_data.data { |
| 25 | + rustc_hir::definitions::DefPathData::CrateRoot => { |
| 26 | + name_rev.push(tcx.crate_name(did.krate)) |
| 27 | + } |
| 28 | + rustc_hir::definitions::DefPathData::TypeNs(symbol) => name_rev.push(symbol), |
| 29 | + _ => return None, |
| 30 | + } |
| 31 | + if let Some(parent) = key.parent { |
| 32 | + did = DefId { krate: did.krate, index: parent }; |
| 33 | + } else { |
| 34 | + break; |
| 35 | + } |
| 36 | + } |
| 37 | + let name_str: Vec<_> = name_rev.iter().rev().map(|x| x.as_str()).collect(); |
| 38 | + debug!(?name_str); |
| 39 | + match name_str[..] { |
| 40 | + // These are the types from Rust core ecosystem |
| 41 | + ["syn" | "proc_macro2", ..] |
| 42 | + | ["core" | "std", "task", "LocalWaker" | "Waker"] |
| 43 | + | ["core" | "std", "task", "wake", "LocalWaker" | "Waker"] => Some(smallvec![]), |
| 44 | + // These are important types from Rust ecosystem |
| 45 | + ["tracing", "instrument", "Instrumented"] | ["bytes", "Bytes"] => Some(smallvec![]), |
| 46 | + ["hashbrown", "raw", "RawTable" | "RawIntoIter"] => { |
| 47 | + if let [ty, ..] = &***args |
| 48 | + && let Some(ty) = ty.as_type() |
| 49 | + { |
| 50 | + Some(smallvec![ty]) |
| 51 | + } else { |
| 52 | + None |
| 53 | + } |
| 54 | + } |
| 55 | + ["hashbrown", "raw", "RawDrain"] => { |
| 56 | + if let [_, ty, ..] = &***args |
| 57 | + && let Some(ty) = ty.as_type() |
| 58 | + { |
| 59 | + Some(smallvec![ty]) |
| 60 | + } else { |
| 61 | + None |
| 62 | + } |
| 63 | + } |
| 64 | + _ => None, |
| 65 | + } |
| 66 | + } else { |
| 67 | + None |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/// Returns the list of types with a "potentially sigificant" that may be dropped |
| 72 | +/// by dropping a value of type `ty`. |
| 73 | +#[instrument(level = "trace", skip(tcx, typing_env))] |
| 74 | +pub fn extract_component_raw<'tcx>( |
| 75 | + tcx: TyCtxt<'tcx>, |
| 76 | + typing_env: ty::TypingEnv<'tcx>, |
| 77 | + ty: Ty<'tcx>, |
| 78 | + ty_seen: &mut UnordSet<Ty<'tcx>>, |
| 79 | +) -> SmallVec<[Ty<'tcx>; 4]> { |
| 80 | + // Droppiness does not depend on regions, so let us erase them. |
| 81 | + let ty = tcx.try_normalize_erasing_regions(typing_env, ty).unwrap_or(ty); |
| 82 | + |
| 83 | + let tys = tcx.list_significant_drop_tys(typing_env.as_query_input(ty)); |
| 84 | + debug!(?ty, "components"); |
| 85 | + let mut out_tys = smallvec![]; |
| 86 | + for ty in tys { |
| 87 | + if let Some(tys) = true_significant_drop_ty(tcx, ty) { |
| 88 | + // Some types can be further opened up because the drop is simply delegated |
| 89 | + for ty in tys { |
| 90 | + if ty_seen.insert(ty) { |
| 91 | + out_tys.extend(extract_component_raw(tcx, typing_env, ty, ty_seen)); |
| 92 | + } |
| 93 | + } |
| 94 | + } else { |
| 95 | + if ty_seen.insert(ty) { |
| 96 | + out_tys.push(ty); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + out_tys |
| 101 | +} |
| 102 | + |
| 103 | +#[instrument(level = "trace", skip(tcx, typing_env))] |
| 104 | +pub fn extract_component_with_significant_dtor<'tcx>( |
| 105 | + tcx: TyCtxt<'tcx>, |
| 106 | + typing_env: ty::TypingEnv<'tcx>, |
| 107 | + ty: Ty<'tcx>, |
| 108 | +) -> SmallVec<[Ty<'tcx>; 4]> { |
| 109 | + let mut tys = extract_component_raw(tcx, typing_env, ty, &mut Default::default()); |
| 110 | + let mut deduplicate = FxHashSet::default(); |
| 111 | + tys.retain(|oty| deduplicate.insert(*oty)); |
| 112 | + tys.into_iter().collect() |
| 113 | +} |
| 114 | + |
| 115 | +/// Extract the span of the custom destructor of a type |
| 116 | +/// especially the span of the `impl Drop` header or its entire block |
| 117 | +/// when we are working with current local crate. |
| 118 | +#[instrument(level = "trace", skip(tcx))] |
| 119 | +pub fn ty_dtor_span<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<Span> { |
| 120 | + match ty.kind() { |
| 121 | + ty::Bool |
| 122 | + | ty::Char |
| 123 | + | ty::Int(_) |
| 124 | + | ty::Uint(_) |
| 125 | + | ty::Float(_) |
| 126 | + | ty::Error(_) |
| 127 | + | ty::Str |
| 128 | + | ty::Never |
| 129 | + | ty::RawPtr(_, _) |
| 130 | + | ty::Ref(_, _, _) |
| 131 | + | ty::FnPtr(_, _) |
| 132 | + | ty::Tuple(_) |
| 133 | + | ty::Dynamic(_, _, _) |
| 134 | + | ty::Alias(_, _) |
| 135 | + | ty::Bound(_, _) |
| 136 | + | ty::Pat(_, _) |
| 137 | + | ty::Placeholder(_) |
| 138 | + | ty::Infer(_) |
| 139 | + | ty::Slice(_) |
| 140 | + | ty::Array(_, _) |
| 141 | + | ty::UnsafeBinder(_) => None, |
| 142 | + |
| 143 | + ty::Adt(adt_def, _) => { |
| 144 | + let did = adt_def.did(); |
| 145 | + let try_local_did_span = |did: DefId| { |
| 146 | + if let Some(local) = did.as_local() { |
| 147 | + tcx.source_span(local) |
| 148 | + } else { |
| 149 | + tcx.def_span(did) |
| 150 | + } |
| 151 | + }; |
| 152 | + let dtor = if let Some(dtor) = tcx.adt_destructor(did) { |
| 153 | + dtor.did |
| 154 | + } else if let Some(dtor) = tcx.adt_async_destructor(did) { |
| 155 | + dtor.future |
| 156 | + } else { |
| 157 | + return Some(try_local_did_span(did)); |
| 158 | + }; |
| 159 | + let def_key = tcx.def_key(dtor); |
| 160 | + let Some(parent_index) = def_key.parent else { return Some(try_local_did_span(dtor)) }; |
| 161 | + let parent_did = DefId { index: parent_index, krate: dtor.krate }; |
| 162 | + Some(try_local_did_span(parent_did)) |
| 163 | + } |
| 164 | + ty::Coroutine(did, _) |
| 165 | + | ty::CoroutineWitness(did, _) |
| 166 | + | ty::CoroutineClosure(did, _) |
| 167 | + | ty::Closure(did, _) |
| 168 | + | ty::FnDef(did, _) |
| 169 | + | ty::Foreign(did) => Some(tcx.def_span(did)), |
| 170 | + ty::Param(_) => None, |
| 171 | + } |
| 172 | +} |
0 commit comments