1use rustc_hir::def_id::DefId;
10use rustc_middle::ty::TyCtxt;
11use rustc_public::{CrateDef, rustc_internal};
12use rustc_span::sym;
13use std::collections::HashMap;
14use std::sync::OnceLock;
15
16static TYPES: OnceLock<HashMap<DefId, StdAdtType>> = OnceLock::new();
17
18pub fn init(tcx: TyCtxt) {
19 TYPES.get_or_init(|| init_types(tcx));
20}
21
22fn init_types(tcx: TyCtxt) -> HashMap<DefId, StdAdtType> {
23 let mut types = HashMap::new();
24
25 if let Some(did) = tcx.lang_items().owned_box() {
27 types.insert(did, StdAdtType::Box);
28 }
29 if let Some(did) = tcx.lang_items().ordering_enum() {
30 types.insert(did, StdAdtType::Ordering);
31 }
32 if let Some(did) = tcx.lang_items().maybe_uninit() {
33 types.insert(did, StdAdtType::MaybeUninit);
34 }
35 if let Some(unsafe_cell) = tcx.lang_items().unsafe_cell_type() {
36 types.insert(unsafe_cell, StdAdtType::UnsafeCell);
37 }
38
39 if let Some(did) = tcx.get_diagnostic_item(sym::cstring_type) {
41 types.insert(did, StdAdtType::CString);
42 }
43 if let Some(did) = tcx.get_diagnostic_item(sym::Vec) {
44 types.insert(did, StdAdtType::Vec);
45 }
46 if let Some(did) = tcx.get_diagnostic_item(sym::Rc) {
47 types.insert(did, StdAdtType::Rc);
48 }
49
50 for krate in std::iter::once(rustc_public::local_crate())
52 .chain(rustc_public::external_crates())
53 .filter(|k| ["core", "std", "alloc"].iter().any(|n| *n == k.name))
54 {
55 for adt in krate.adts() {
56 let did = rustc_internal::internal(tcx, adt.def_id());
57 if let Some(group) = classify_type(tcx, did, &krate.name, krate.is_local) {
58 types.insert(did, group);
59 }
60 }
61 }
62
63 types
64}
65
66#[derive(Clone, Copy, Debug, PartialEq, Eq)]
67pub(crate) enum StdAdtType {
68 AsciiChar,
69 Box,
70 Cell,
71 CString,
72 IndexRange,
73 Iter,
74 MappedMutexGuard,
75 MaybeUninit,
76 MutexGuard,
77 NonNull,
78 Ordering,
79 Rc,
80 Ref,
81 RefCell,
82 RefMut,
83 RwLockWriteGuard,
84 String,
85 SyncPrimitive,
86 UnsafeCell,
87 Vec,
88}
89
90fn classify_type(
91 tcx: TyCtxt<'_>,
92 did: DefId,
93 crate_name: &str,
94 is_local: bool,
95) -> Option<StdAdtType> {
96 let path = tcx.def_path_str(did);
97 let path = if is_local && !path.starts_with(&format!("{crate_name}::")) {
98 format!("{crate_name}::{path}")
99 } else {
100 path
101 };
102 let name = path.as_str();
103 Some(match name {
104 "alloc::string::String" | "std::string::String" => StdAdtType::String,
105 "core::slice::iter::Iter"
106 | "core::slice::iter::IterMut"
107 | "std::slice::Iter"
108 | "std::slice::IterMut" => StdAdtType::Iter,
109 "core::ptr::non_null::NonNull" | "std::ptr::NonNull" => StdAdtType::NonNull,
110 "core::ops::index_range::IndexRange"
111 | "std::ops::index_range::IndexRange"
112 | "std::ops::IndexRange" => StdAdtType::IndexRange,
113 "core::ascii::ascii_char::AsciiChar" | "std::ascii::AsciiChar" => StdAdtType::AsciiChar,
114 "alloc::rc::Rc" | "alloc::rcs::rc::Rc" | "std::rc::Rc" => StdAdtType::Rc,
115 "std::sync::poison::mutex::Mutex"
116 | "std::sync::nonpoison::mutex::Mutex"
117 | "std::sync::nonpoison::Mutex"
118 | "std::sync::poison::rwlock::RwLock"
119 | "std::sync::nonpoison::rwlock::RwLock"
120 | "std::sync::once_lock::OnceLock"
121 | "std::sync::Mutex"
122 | "std::sync::RwLock"
123 | "std::sync::OnceLock"
124 | "core::sync::atomic::Atomic"
125 | "std::sync::atomic::Atomic" => StdAdtType::SyncPrimitive,
126 "core::cell::Cell" | "std::cell::Cell" => StdAdtType::Cell,
127 "core::cell::RefCell" | "std::cell::RefCell" => StdAdtType::RefCell,
128 "core::cell::Ref" | "std::cell::Ref" => StdAdtType::Ref,
129 "core::cell::RefMut" | "std::cell::RefMut" => StdAdtType::RefMut,
130 "std::sync::poison::mutex::MutexGuard"
131 | "std::sync::nonpoison::mutex::MutexGuard"
132 | "std::sync::MutexGuard" => StdAdtType::MutexGuard,
133 "std::sync::poison::mutex::MappedMutexGuard"
134 | "std::sync::nonpoison::mutex::MappedMutexGuard"
135 | "std::sync::MappedMutexGuard" => StdAdtType::MappedMutexGuard,
136 "std::sync::poison::rwlock::RwLockWriteGuard"
137 | "std::sync::nonpoison::rwlock::RwLockWriteGuard"
138 | "std::sync::RwLockWriteGuard" => StdAdtType::RwLockWriteGuard,
139 _ => return None,
140 })
141}
142
143fn types() -> &'static HashMap<DefId, StdAdtType> {
144 TYPES.get().expect("Type DefIds haven't been initialized.")
145}
146
147pub(crate) fn def_ids_for_type(group: StdAdtType) -> impl Iterator<Item = DefId> {
148 types()
149 .iter()
150 .filter_map(move |(&did, &kind)| (kind == group).then_some(did))
151}
152
153macro_rules! type_predicates {
154 ($( $(#[$attr:meta])* $name:ident => $group:ident ,)+) => {
155 $(
156 $(#[$attr])*
157 pub fn $name(did: DefId) -> bool {
158 types().get(&did) == Some(&StdAdtType::$group)
159 }
160 )+
161 };
162}
163
164type_predicates! {
165 is_std_box => Box,
167 is_std_cstring => CString,
169 is_std_vec => Vec,
171 is_std_string => String,
173 is_std_nonnull => NonNull,
175 is_maybe_uninit_type => MaybeUninit,
177 is_std_ordering => Ordering,
179 is_std_iter_or_itermut => Iter,
181 is_std_rc => Rc,
183 is_std_sync_primitive => SyncPrimitive,
185 is_index_range => IndexRange,
187}