1pub mod map;
6pub mod nested_filter;
7pub mod place;
8
9use rustc_data_structures::fingerprint::Fingerprint;
10use rustc_data_structures::sorted_map::SortedMap;
11use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
12use rustc_data_structures::sync::{DynSend, DynSync, try_par_for_each_in};
13use rustc_hir::def::DefKind;
14use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId};
15use rustc_hir::lints::DelayedLint;
16use rustc_hir::*;
17use rustc_macros::{Decodable, Encodable, HashStable};
18use rustc_span::{ErrorGuaranteed, ExpnId, Span};
19
20use crate::query::Providers;
21use crate::ty::{EarlyBinder, ImplSubject, TyCtxt};
22
23#[derive(Debug, HashStable, Encodable, Decodable)]
26pub struct ModuleItems {
27 add_root: bool,
30 submodules: Box<[OwnerId]>,
31 free_items: Box<[ItemId]>,
32 trait_items: Box<[TraitItemId]>,
33 impl_items: Box<[ImplItemId]>,
34 foreign_items: Box<[ForeignItemId]>,
35 opaques: Box<[LocalDefId]>,
36 body_owners: Box<[LocalDefId]>,
37 nested_bodies: Box<[LocalDefId]>,
38 delayed_lint_items: Box<[OwnerId]>,
40}
41
42impl ModuleItems {
43 pub fn free_items(&self) -> impl Iterator<Item = ItemId> {
50 self.free_items.iter().copied()
51 }
52
53 pub fn trait_items(&self) -> impl Iterator<Item = TraitItemId> {
54 self.trait_items.iter().copied()
55 }
56
57 pub fn delayed_lint_items(&self) -> impl Iterator<Item = OwnerId> {
58 self.delayed_lint_items.iter().copied()
59 }
60
61 pub fn impl_items(&self) -> impl Iterator<Item = ImplItemId> {
64 self.impl_items.iter().copied()
65 }
66
67 pub fn foreign_items(&self) -> impl Iterator<Item = ForeignItemId> {
68 self.foreign_items.iter().copied()
69 }
70
71 pub fn owners(&self) -> impl Iterator<Item = OwnerId> {
72 self.add_root
73 .then_some(CRATE_OWNER_ID)
74 .into_iter()
75 .chain(self.free_items.iter().map(|id| id.owner_id))
76 .chain(self.trait_items.iter().map(|id| id.owner_id))
77 .chain(self.impl_items.iter().map(|id| id.owner_id))
78 .chain(self.foreign_items.iter().map(|id| id.owner_id))
79 }
80
81 pub fn opaques(&self) -> impl Iterator<Item = LocalDefId> {
82 self.opaques.iter().copied()
83 }
84
85 pub fn nested_bodies(&self) -> impl Iterator<Item = LocalDefId> {
87 self.nested_bodies.iter().copied()
88 }
89
90 pub fn definitions(&self) -> impl Iterator<Item = LocalDefId> {
91 self.owners().map(|id| id.def_id)
92 }
93
94 pub fn par_nested_bodies(
96 &self,
97 f: impl Fn(LocalDefId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
98 ) -> Result<(), ErrorGuaranteed> {
99 try_par_for_each_in(&self.nested_bodies[..], |&&id| f(id))
100 }
101
102 pub fn par_items(
103 &self,
104 f: impl Fn(ItemId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
105 ) -> Result<(), ErrorGuaranteed> {
106 try_par_for_each_in(&self.free_items[..], |&&id| f(id))
107 }
108
109 pub fn par_trait_items(
110 &self,
111 f: impl Fn(TraitItemId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
112 ) -> Result<(), ErrorGuaranteed> {
113 try_par_for_each_in(&self.trait_items[..], |&&id| f(id))
114 }
115
116 pub fn par_impl_items(
117 &self,
118 f: impl Fn(ImplItemId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
119 ) -> Result<(), ErrorGuaranteed> {
120 try_par_for_each_in(&self.impl_items[..], |&&id| f(id))
121 }
122
123 pub fn par_foreign_items(
124 &self,
125 f: impl Fn(ForeignItemId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
126 ) -> Result<(), ErrorGuaranteed> {
127 try_par_for_each_in(&self.foreign_items[..], |&&id| f(id))
128 }
129
130 pub fn par_opaques(
131 &self,
132 f: impl Fn(LocalDefId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
133 ) -> Result<(), ErrorGuaranteed> {
134 try_par_for_each_in(&self.opaques[..], |&&id| f(id))
135 }
136}
137
138impl<'tcx> TyCtxt<'tcx> {
139 pub fn parent_module(self, id: HirId) -> LocalModDefId {
140 if !id.is_owner() && self.def_kind(id.owner) == DefKind::Mod {
141 LocalModDefId::new_unchecked(id.owner.def_id)
142 } else {
143 self.parent_module_from_def_id(id.owner.def_id)
144 }
145 }
146
147 pub fn parent_module_from_def_id(self, mut id: LocalDefId) -> LocalModDefId {
148 while let Some(parent) = self.opt_local_parent(id) {
149 id = parent;
150 if self.def_kind(id) == DefKind::Mod {
151 break;
152 }
153 }
154 LocalModDefId::new_unchecked(id)
155 }
156
157 pub fn impl_subject(self, def_id: DefId) -> EarlyBinder<'tcx, ImplSubject<'tcx>> {
158 match self.impl_trait_ref(def_id) {
159 Some(t) => t.map_bound(ImplSubject::Trait),
160 None => self.type_of(def_id).map_bound(ImplSubject::Inherent),
161 }
162 }
163
164 pub fn is_foreign_item(self, def_id: impl Into<DefId>) -> bool {
166 self.opt_parent(def_id.into())
167 .is_some_and(|parent| matches!(self.def_kind(parent), DefKind::ForeignMod))
168 }
169
170 pub fn hash_owner_nodes(
171 self,
172 node: OwnerNode<'_>,
173 bodies: &SortedMap<ItemLocalId, &Body<'_>>,
174 attrs: &SortedMap<ItemLocalId, &[Attribute]>,
175 delayed_lints: &[DelayedLint],
176 define_opaque: Option<&[(Span, LocalDefId)]>,
177 ) -> (Option<Fingerprint>, Option<Fingerprint>, Option<Fingerprint>) {
178 if self.needs_crate_hash() {
179 self.with_stable_hashing_context(|mut hcx| {
180 let mut stable_hasher = StableHasher::new();
181 node.hash_stable(&mut hcx, &mut stable_hasher);
182 bodies.hash_stable(&mut hcx, &mut stable_hasher);
184 let h1 = stable_hasher.finish();
185
186 let mut stable_hasher = StableHasher::new();
187 attrs.hash_stable(&mut hcx, &mut stable_hasher);
188
189 define_opaque.hash_stable(&mut hcx, &mut stable_hasher);
191
192 let h2 = stable_hasher.finish();
193
194 let mut stable_hasher = StableHasher::new();
196 delayed_lints.hash_stable(&mut hcx, &mut stable_hasher);
197 let h3 = stable_hasher.finish();
198
199 (Some(h1), Some(h2), Some(h3))
200 })
201 } else {
202 (None, None, None)
203 }
204 }
205}
206
207pub fn provide(providers: &mut Providers) {
208 providers.hir_crate_items = map::hir_crate_items;
209 providers.crate_hash = map::crate_hash;
210 providers.hir_module_items = map::hir_module_items;
211 providers.local_def_id_to_hir_id = |tcx, def_id| match tcx.hir_crate(()).owners[def_id] {
212 MaybeOwner::Owner(_) => HirId::make_owner(def_id),
213 MaybeOwner::NonOwner(hir_id) => hir_id,
214 MaybeOwner::Phantom => bug!("No HirId for {:?}", def_id),
215 };
216 providers.opt_hir_owner_nodes =
217 |tcx, id| tcx.hir_crate(()).owners.get(id)?.as_owner().map(|i| &i.nodes);
218 providers.hir_owner_parent = |tcx, owner_id| {
219 tcx.opt_local_parent(owner_id.def_id).map_or(CRATE_HIR_ID, |parent_def_id| {
220 let parent_owner_id = tcx.local_def_id_to_hir_id(parent_def_id).owner;
221 HirId {
222 owner: parent_owner_id,
223 local_id: tcx.hir_crate(()).owners[parent_owner_id.def_id]
224 .unwrap()
225 .parenting
226 .get(&owner_id.def_id)
227 .copied()
228 .unwrap_or(ItemLocalId::ZERO),
229 }
230 })
231 };
232 providers.hir_attr_map = |tcx, id| {
233 tcx.hir_crate(()).owners[id.def_id].as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs)
234 };
235 providers.opt_ast_lowering_delayed_lints =
236 |tcx, id| tcx.hir_crate(()).owners[id.def_id].as_owner().map(|o| &o.delayed_lints);
237 providers.def_span = |tcx, def_id| tcx.hir_span(tcx.local_def_id_to_hir_id(def_id));
238 providers.def_ident_span = |tcx, def_id| {
239 let hir_id = tcx.local_def_id_to_hir_id(def_id);
240 tcx.hir_opt_ident_span(hir_id)
241 };
242 providers.fn_arg_idents = |tcx, def_id| {
243 if let Some(body_id) = tcx.hir_node_by_def_id(def_id).body_id() {
244 tcx.arena.alloc_from_iter(tcx.hir_body_param_idents(body_id))
245 } else if let Node::TraitItem(&TraitItem {
246 kind: TraitItemKind::Fn(_, TraitFn::Required(idents)),
247 ..
248 })
249 | Node::ForeignItem(&ForeignItem {
250 kind: ForeignItemKind::Fn(_, idents, _),
251 ..
252 }) = tcx.hir_node(tcx.local_def_id_to_hir_id(def_id))
253 {
254 idents
255 } else {
256 span_bug!(
257 tcx.hir_span(tcx.local_def_id_to_hir_id(def_id)),
258 "fn_arg_idents: unexpected item {:?}",
259 def_id
260 );
261 }
262 };
263 providers.all_local_trait_impls = |tcx, ()| &tcx.resolutions(()).trait_impls;
264 providers.local_trait_impls =
265 |tcx, trait_id| tcx.resolutions(()).trait_impls.get(&trait_id).map_or(&[], |xs| &xs[..]);
266 providers.expn_that_defined =
267 |tcx, id| tcx.resolutions(()).expn_that_defined.get(&id).copied().unwrap_or(ExpnId::root());
268 providers.in_scope_traits_map = |tcx, id| {
269 tcx.hir_crate(()).owners[id.def_id].as_owner().map(|owner_info| &owner_info.trait_map)
270 };
271}