1use rustc_abi::ExternAbi;
6use rustc_ast::visit::{VisitorResult, walk_list};
7use rustc_data_structures::fingerprint::Fingerprint;
8use rustc_data_structures::stable_hash::{StableHash, StableHasher};
9use rustc_data_structures::steal::Steal;
10use rustc_data_structures::svh::Svh;
11use rustc_data_structures::sync::{DynSend, DynSync, par_for_each_in, try_par_for_each_in};
12use rustc_hir::def::{DefKind, Res};
13use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId, LocalModDefId};
14use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
15use rustc_hir::intravisit::Visitor;
16use rustc_hir::lints::DelayedLints;
17use rustc_hir::*;
18use rustc_hir_pretty as pprust_hir;
19use rustc_span::def_id::StableCrateId;
20use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol, kw, with_metavar_spans};
21
22use crate::hir::{ModuleItems, ProjectedMaybeOwner, nested_filter};
23use crate::middle::debugger_visualizer::DebuggerVisualizerFile;
24use crate::query::{IntoQueryKey, LocalCrate};
25use crate::ty::TyCtxt;
26
27struct ParentHirIterator<'tcx> {
30 current_id: HirId,
31 tcx: TyCtxt<'tcx>,
32 current_owner_nodes: Option<&'tcx OwnerNodes<'tcx>>,
35}
36
37impl<'tcx> ParentHirIterator<'tcx> {
38 fn new(tcx: TyCtxt<'tcx>, current_id: HirId) -> ParentHirIterator<'tcx> {
39 ParentHirIterator { current_id, tcx, current_owner_nodes: None }
40 }
41}
42
43impl<'tcx> Iterator for ParentHirIterator<'tcx> {
44 type Item = HirId;
45
46 fn next(&mut self) -> Option<Self::Item> {
47 if self.current_id == CRATE_HIR_ID {
48 return None;
49 }
50
51 let HirId { owner, local_id } = self.current_id;
52
53 let parent_id = if local_id == ItemLocalId::ZERO {
54 self.current_owner_nodes = None;
56 self.tcx.hir_owner_parent(owner)
57 } else {
58 let owner_nodes =
59 self.current_owner_nodes.get_or_insert_with(|| self.tcx.hir_owner_nodes(owner));
60 let parent_local_id = owner_nodes.nodes[local_id].parent;
61 if true {
{
match (&parent_local_id, &local_id) {
(left_val, right_val) => {
if *left_val == *right_val {
let kind = ::core::panicking::AssertKind::Ne;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};
};debug_assert_ne!(parent_local_id, local_id);
63 HirId { owner, local_id: parent_local_id }
64 };
65
66 if true {
{
match (&parent_id, &self.current_id) {
(left_val, right_val) => {
if *left_val == *right_val {
let kind = ::core::panicking::AssertKind::Ne;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};
};debug_assert_ne!(parent_id, self.current_id);
67
68 self.current_id = parent_id;
69 Some(parent_id)
70 }
71}
72
73pub struct ParentOwnerIterator<'tcx> {
76 current_id: HirId,
77 tcx: TyCtxt<'tcx>,
78}
79
80impl<'tcx> Iterator for ParentOwnerIterator<'tcx> {
81 type Item = (OwnerId, OwnerNode<'tcx>);
82
83 fn next(&mut self) -> Option<Self::Item> {
84 if self.current_id.local_id.index() != 0 {
85 self.current_id.local_id = ItemLocalId::ZERO;
86 let node = self.tcx.hir_owner_node(self.current_id.owner);
87 return Some((self.current_id.owner, node));
88 }
89 if self.current_id == CRATE_HIR_ID {
90 return None;
91 }
92
93 let parent_id = self.tcx.hir_def_key(self.current_id.owner.def_id).parent;
94 let parent_id = parent_id.map_or(CRATE_OWNER_ID, |local_def_index| {
95 let def_id = LocalDefId { local_def_index };
96 self.tcx.local_def_id_to_hir_id(def_id).owner
97 });
98 self.current_id = HirId::make_owner(parent_id.def_id);
99
100 let node = self.tcx.hir_owner_node(self.current_id.owner);
101 Some((self.current_id.owner, node))
102 }
103}
104
105impl<'tcx> TyCtxt<'tcx> {
106 #[inline]
107 pub fn local_def_id_to_hir_id(self, def_id: impl IntoQueryKey<LocalDefId>) -> HirId {
108 let def_id = def_id.into_query_key();
109 match self.hir_owner(def_id) {
110 ProjectedMaybeOwner::Owner(_) => HirId::make_owner(def_id),
111 ProjectedMaybeOwner::NonOwner(hir_id) => hir_id,
112 }
113 }
114
115 #[inline]
119 pub fn opt_ast_lowering_delayed_lints(self, id: OwnerId) -> Option<&'tcx Steal<DelayedLints>> {
120 self.dep_graph.assert_eval_always();
121 self.hir_owner(id.def_id).as_owner().map(|o| o.delayed_lints)
122 }
123
124 #[inline]
125 pub fn in_scope_traits_map(
126 self,
127 id: OwnerId,
128 ) -> Option<&'tcx ItemLocalMap<&'tcx [TraitCandidate<'tcx>]>> {
129 self.hir_owner(id.def_id).as_owner().map(|o| o.trait_map)
130 }
131
132 #[inline]
133 pub fn opt_hir_owner_nodes(self, def_id: LocalDefId) -> Option<&'tcx OwnerNodes<'tcx>> {
134 self.hir_owner(def_id).as_owner().map(|o| o.nodes)
135 }
136
137 #[inline]
138 fn expect_hir_owner_nodes(self, def_id: LocalDefId) -> &'tcx OwnerNodes<'tcx> {
139 self.opt_hir_owner_nodes(def_id)
140 .unwrap_or_else(|| crate::util::bug::span_bug_fmt(self.def_span(def_id),
format_args!("{0:?} is not an owner", def_id))span_bug!(self.def_span(def_id), "{def_id:?} is not an owner"))
141 }
142
143 #[inline]
144 pub fn hir_owner_nodes(self, owner_id: OwnerId) -> &'tcx OwnerNodes<'tcx> {
145 self.expect_hir_owner_nodes(owner_id.def_id)
146 }
147
148 #[inline]
149 fn opt_hir_owner_node(self, def_id: LocalDefId) -> Option<OwnerNode<'tcx>> {
150 self.opt_hir_owner_nodes(def_id).map(|nodes| nodes.node())
151 }
152
153 #[inline]
154 pub fn expect_hir_owner_node(self, def_id: LocalDefId) -> OwnerNode<'tcx> {
155 self.expect_hir_owner_nodes(def_id).node()
156 }
157
158 #[inline]
159 pub fn hir_owner_node(self, owner_id: OwnerId) -> OwnerNode<'tcx> {
160 self.hir_owner_nodes(owner_id).node()
161 }
162
163 pub fn hir_node(self, id: HirId) -> Node<'tcx> {
165 self.hir_owner_nodes(id.owner).nodes[id.local_id].node
166 }
167
168 #[inline]
170 pub fn hir_node_by_def_id(self, id: LocalDefId) -> Node<'tcx> {
171 self.hir_node(self.local_def_id_to_hir_id(id))
172 }
173
174 pub fn parent_hir_id(self, hir_id: HirId) -> HirId {
179 let HirId { owner, local_id } = hir_id;
180 if local_id == ItemLocalId::ZERO {
181 self.hir_owner_parent(owner)
182 } else {
183 let parent_local_id = self.hir_owner_nodes(owner).nodes[local_id].parent;
184 if true {
{
match (&parent_local_id, &local_id) {
(left_val, right_val) => {
if *left_val == *right_val {
let kind = ::core::panicking::AssertKind::Ne;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};
};debug_assert_ne!(parent_local_id, local_id);
186 HirId { owner, local_id: parent_local_id }
187 }
188 }
189
190 pub fn parent_hir_node(self, hir_id: HirId) -> Node<'tcx> {
193 self.hir_node(self.parent_hir_id(hir_id))
194 }
195
196 #[inline]
197 pub fn hir_root_module(self) -> &'tcx Mod<'tcx> {
198 match self.hir_owner_node(CRATE_OWNER_ID) {
199 OwnerNode::Crate(item) => item,
200 _ => crate::util::bug::bug_fmt(format_args!("impossible case reached"))bug!(),
201 }
202 }
203
204 #[inline]
205 pub fn hir_free_items(self) -> impl Iterator<Item = ItemId> {
206 self.hir_crate_items(()).free_items.iter().copied()
207 }
208
209 #[inline]
210 pub fn hir_module_free_items(self, module: LocalModDefId) -> impl Iterator<Item = ItemId> {
211 self.hir_module_items(module).free_items()
212 }
213
214 pub fn hir_def_key(self, def_id: LocalDefId) -> DefKey {
215 self.definitions_untracked().def_key(def_id)
217 }
218
219 pub fn hir_def_path(self, def_id: LocalDefId) -> DefPath {
220 self.definitions_untracked().def_path(def_id)
222 }
223
224 #[inline]
225 pub fn hir_def_path_hash(self, def_id: LocalDefId) -> DefPathHash {
226 self.definitions_untracked().def_path_hash(def_id)
228 }
229
230 pub fn hir_get_if_local(self, id: DefId) -> Option<Node<'tcx>> {
231 id.as_local().map(|id| self.hir_node_by_def_id(id))
232 }
233
234 pub fn hir_get_generics(self, id: LocalDefId) -> Option<&'tcx Generics<'tcx>> {
235 self.opt_hir_owner_node(id)?.generics()
236 }
237
238 pub fn hir_item(self, id: ItemId) -> &'tcx Item<'tcx> {
239 self.hir_owner_node(id.owner_id).expect_item()
240 }
241
242 pub fn hir_trait_item(self, id: TraitItemId) -> &'tcx TraitItem<'tcx> {
243 self.hir_owner_node(id.owner_id).expect_trait_item()
244 }
245
246 pub fn hir_impl_item(self, id: ImplItemId) -> &'tcx ImplItem<'tcx> {
247 self.hir_owner_node(id.owner_id).expect_impl_item()
248 }
249
250 pub fn hir_foreign_item(self, id: ForeignItemId) -> &'tcx ForeignItem<'tcx> {
251 self.hir_owner_node(id.owner_id).expect_foreign_item()
252 }
253
254 pub fn hir_body(self, id: BodyId) -> &'tcx Body<'tcx> {
255 self.hir_owner_nodes(id.hir_id.owner).bodies[&id.hir_id.local_id]
256 }
257
258 #[track_caller]
259 pub fn hir_fn_decl_by_hir_id(self, hir_id: HirId) -> Option<&'tcx FnDecl<'tcx>> {
260 self.hir_node(hir_id).fn_decl()
261 }
262
263 #[track_caller]
264 pub fn hir_fn_sig_by_hir_id(self, hir_id: HirId) -> Option<&'tcx FnSig<'tcx>> {
265 self.hir_node(hir_id).fn_sig()
266 }
267
268 #[track_caller]
269 pub fn hir_enclosing_body_owner(self, hir_id: HirId) -> LocalDefId {
270 for (_, node) in self.hir_parent_iter(hir_id) {
271 if let Some((def_id, _)) = node.associated_body() {
272 return def_id;
273 }
274 }
275
276 crate::util::bug::bug_fmt(format_args!("no `hir_enclosing_body_owner` for hir_id `{0}`",
hir_id));bug!("no `hir_enclosing_body_owner` for hir_id `{}`", hir_id);
277 }
278
279 pub fn hir_body_owner(self, BodyId { hir_id }: BodyId) -> HirId {
283 let parent = self.parent_hir_id(hir_id);
284 {
match (&self.hir_node(parent).body_id().unwrap().hir_id, &hir_id) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val,
::core::option::Option::Some(format_args!("{0:?}",
hir_id)));
}
}
}
};assert_eq!(self.hir_node(parent).body_id().unwrap().hir_id, hir_id, "{hir_id:?}");
285 parent
286 }
287
288 pub fn hir_body_owner_def_id(self, BodyId { hir_id }: BodyId) -> LocalDefId {
289 self.parent_hir_node(hir_id).associated_body().unwrap().0
290 }
291
292 pub fn hir_maybe_body_owned_by(self, id: LocalDefId) -> Option<&'tcx Body<'tcx>> {
295 Some(self.hir_body(self.hir_node_by_def_id(id).body_id()?))
296 }
297
298 #[track_caller]
300 pub fn hir_body_owned_by(self, id: LocalDefId) -> &'tcx Body<'tcx> {
301 self.hir_maybe_body_owned_by(id).unwrap_or_else(|| {
302 let hir_id = self.local_def_id_to_hir_id(id);
303 crate::util::bug::span_bug_fmt(self.hir_span(hir_id),
format_args!("body_owned_by: {0} has no associated body",
self.hir_id_to_string(hir_id)));span_bug!(
304 self.hir_span(hir_id),
305 "body_owned_by: {} has no associated body",
306 self.hir_id_to_string(hir_id)
307 );
308 })
309 }
310
311 pub fn hir_body_param_idents(self, id: BodyId) -> impl Iterator<Item = Option<Ident>> {
312 self.hir_body(id).params.iter().map(|param| match param.pat.kind {
313 PatKind::Binding(_, _, ident, _) => Some(ident),
314 PatKind::Wild => Some(Ident::new(kw::Underscore, param.pat.span)),
315 _ => None,
316 })
317 }
318
319 pub fn hir_body_owner_kind(self, def_id: impl Into<DefId>) -> BodyOwnerKind {
323 let def_id = def_id.into();
324 match self.def_kind(def_id) {
325 DefKind::Const { .. } | DefKind::AssocConst { .. } | DefKind::AnonConst => {
326 BodyOwnerKind::Const { inline: false }
327 }
328 DefKind::InlineConst => BodyOwnerKind::Const { inline: true },
329 DefKind::Ctor(..) | DefKind::Fn | DefKind::AssocFn => BodyOwnerKind::Fn,
330 DefKind::Closure | DefKind::SyntheticCoroutineBody => BodyOwnerKind::Closure,
331 DefKind::Static { safety: _, mutability, nested: false } => {
332 BodyOwnerKind::Static(mutability)
333 }
334 DefKind::GlobalAsm => BodyOwnerKind::GlobalAsm,
335 dk => crate::util::bug::bug_fmt(format_args!("{0:?} is not a body node: {1:?}",
def_id, dk))bug!("{:?} is not a body node: {:?}", def_id, dk),
336 }
337 }
338
339 pub fn hir_body_const_context(self, local_def_id: LocalDefId) -> Option<ConstContext> {
347 let def_id = local_def_id.into();
348 let ccx = match self.hir_body_owner_kind(def_id) {
349 BodyOwnerKind::Const { inline } => {
350 ConstContext::Const { allow_const_fn_promotion: !inline }
351 }
352 BodyOwnerKind::Static(mutability) => ConstContext::Static(mutability),
353
354 BodyOwnerKind::Fn if self.is_constructor(def_id) => return None,
355 BodyOwnerKind::Fn | BodyOwnerKind::Closure if self.is_const_fn(def_id) => {
356 if #[allow(non_exhaustive_omitted_patterns)] match self.constness(def_id) {
rustc_hir::Constness::Const { always: true } => true,
_ => false,
}matches!(self.constness(def_id), rustc_hir::Constness::Const { always: true }) {
357 ConstContext::Const { allow_const_fn_promotion: false }
358 } else {
359 ConstContext::ConstFn
360 }
361 }
362 BodyOwnerKind::Fn | BodyOwnerKind::Closure | BodyOwnerKind::GlobalAsm => return None,
363 };
364
365 Some(ccx)
366 }
367
368 #[inline]
371 pub fn hir_body_owners(self) -> impl Iterator<Item = LocalDefId> {
372 self.hir_crate_items(()).body_owners.iter().copied()
373 }
374
375 #[inline]
376 pub fn par_hir_body_owners(self, f: impl Fn(LocalDefId) + DynSend + DynSync) {
377 par_for_each_in(&self.hir_crate_items(()).body_owners[..], |&&def_id| f(def_id));
378 }
379
380 pub fn hir_ty_param_owner(self, def_id: LocalDefId) -> LocalDefId {
381 let def_kind = self.def_kind(def_id);
382 match def_kind {
383 DefKind::Trait | DefKind::TraitAlias => def_id,
384 DefKind::LifetimeParam | DefKind::TyParam | DefKind::ConstParam => {
385 self.local_parent(def_id)
386 }
387 _ => crate::util::bug::bug_fmt(format_args!("ty_param_owner: {0:?} is a {1:?} not a type parameter",
def_id, def_kind))bug!("ty_param_owner: {:?} is a {:?} not a type parameter", def_id, def_kind),
388 }
389 }
390
391 pub fn hir_ty_param_name(self, def_id: LocalDefId) -> Symbol {
392 let def_kind = self.def_kind(def_id);
393 match def_kind {
394 DefKind::Trait | DefKind::TraitAlias => kw::SelfUpper,
395 DefKind::LifetimeParam | DefKind::TyParam | DefKind::ConstParam => {
396 self.item_name(def_id.to_def_id())
397 }
398 _ => crate::util::bug::bug_fmt(format_args!("ty_param_name: {0:?} is a {1:?} not a type parameter",
def_id, def_kind))bug!("ty_param_name: {:?} is a {:?} not a type parameter", def_id, def_kind),
399 }
400 }
401
402 pub fn hir_krate_attrs(self) -> &'tcx [Attribute] {
406 self.hir_attrs(CRATE_HIR_ID)
407 }
408
409 pub fn hir_rustc_coherence_is_core(self) -> bool {
410 {
{
'done:
{
for i in self.hir_krate_attrs() {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(RustcCoherenceIsCore) => {
break 'done Some(());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}.is_some()
}find_attr!(self.hir_krate_attrs(), RustcCoherenceIsCore)
411 }
412
413 pub fn hir_get_module(self, module: LocalModDefId) -> (&'tcx Mod<'tcx>, Span, HirId) {
414 let hir_id = HirId::make_owner(module.to_local_def_id());
415 match self.hir_owner_node(hir_id.owner) {
416 OwnerNode::Item(&Item { span, kind: ItemKind::Mod(_, m), .. }) => (m, span, hir_id),
417 OwnerNode::Crate(item) => (item, item.spans.inner_span, hir_id),
418 node => { ::core::panicking::panic_fmt(format_args!("not a module: {0:?}", node)); }panic!("not a module: {node:?}"),
419 }
420 }
421
422 pub fn hir_walk_toplevel_module<V>(self, visitor: &mut V) -> V::Result
424 where
425 V: Visitor<'tcx>,
426 {
427 let (top_mod, span, hir_id) = self.hir_get_module(LocalModDefId::CRATE_DEF_ID);
428 visitor.visit_mod(top_mod, span, hir_id)
429 }
430
431 pub fn hir_walk_attributes<V>(self, visitor: &mut V) -> V::Result
433 where
434 V: Visitor<'tcx>,
435 {
436 let krate = self.hir_crate_items(());
437 for owner in krate.owners() {
438 let attrs = self.hir_attr_map(owner);
439 for attrs in attrs.map.values() {
440 for elem in *attrs {
match ::rustc_ast_ir::visit::VisitorResult::branch(visitor.visit_attribute(elem))
{
core::ops::ControlFlow::Continue(()) =>
(),
#[allow(unreachable_code)]
core::ops::ControlFlow::Break(r) => {
return ::rustc_ast_ir::visit::VisitorResult::from_residual(r);
}
};
};walk_list!(visitor, visit_attribute, *attrs);
441 }
442 }
443 V::Result::output()
444 }
445
446 pub fn hir_visit_all_item_likes_in_crate<V>(self, visitor: &mut V) -> V::Result
457 where
458 V: Visitor<'tcx>,
459 {
460 let krate = self.hir_crate_items(());
461 for elem in krate.free_items().map(|id| self.hir_item(id)) {
match ::rustc_ast_ir::visit::VisitorResult::branch(visitor.visit_item(elem))
{
core::ops::ControlFlow::Continue(()) =>
(),
#[allow(unreachable_code)]
core::ops::ControlFlow::Break(r) => {
return ::rustc_ast_ir::visit::VisitorResult::from_residual(r);
}
};
};walk_list!(visitor, visit_item, krate.free_items().map(|id| self.hir_item(id)));
462 for elem in krate.trait_items().map(|id| self.hir_trait_item(id)) {
match ::rustc_ast_ir::visit::VisitorResult::branch(visitor.visit_trait_item(elem))
{
core::ops::ControlFlow::Continue(()) =>
(),
#[allow(unreachable_code)]
core::ops::ControlFlow::Break(r) => {
return ::rustc_ast_ir::visit::VisitorResult::from_residual(r);
}
};
};walk_list!(
463 visitor,
464 visit_trait_item,
465 krate.trait_items().map(|id| self.hir_trait_item(id))
466 );
467 for elem in krate.impl_items().map(|id| self.hir_impl_item(id)) {
match ::rustc_ast_ir::visit::VisitorResult::branch(visitor.visit_impl_item(elem))
{
core::ops::ControlFlow::Continue(()) =>
(),
#[allow(unreachable_code)]
core::ops::ControlFlow::Break(r) => {
return ::rustc_ast_ir::visit::VisitorResult::from_residual(r);
}
};
};walk_list!(visitor, visit_impl_item, krate.impl_items().map(|id| self.hir_impl_item(id)));
468 for elem in krate.foreign_items().map(|id| self.hir_foreign_item(id)) {
match ::rustc_ast_ir::visit::VisitorResult::branch(visitor.visit_foreign_item(elem))
{
core::ops::ControlFlow::Continue(()) =>
(),
#[allow(unreachable_code)]
core::ops::ControlFlow::Break(r) => {
return ::rustc_ast_ir::visit::VisitorResult::from_residual(r);
}
};
};walk_list!(
469 visitor,
470 visit_foreign_item,
471 krate.foreign_items().map(|id| self.hir_foreign_item(id))
472 );
473 V::Result::output()
474 }
475
476 pub fn hir_visit_item_likes_in_module<V>(
479 self,
480 module: LocalModDefId,
481 visitor: &mut V,
482 ) -> V::Result
483 where
484 V: Visitor<'tcx>,
485 {
486 let module = self.hir_module_items(module);
487 for elem in module.free_items().map(|id| self.hir_item(id)) {
match ::rustc_ast_ir::visit::VisitorResult::branch(visitor.visit_item(elem))
{
core::ops::ControlFlow::Continue(()) =>
(),
#[allow(unreachable_code)]
core::ops::ControlFlow::Break(r) => {
return ::rustc_ast_ir::visit::VisitorResult::from_residual(r);
}
};
};walk_list!(visitor, visit_item, module.free_items().map(|id| self.hir_item(id)));
488 for elem in module.trait_items().map(|id| self.hir_trait_item(id)) {
match ::rustc_ast_ir::visit::VisitorResult::branch(visitor.visit_trait_item(elem))
{
core::ops::ControlFlow::Continue(()) =>
(),
#[allow(unreachable_code)]
core::ops::ControlFlow::Break(r) => {
return ::rustc_ast_ir::visit::VisitorResult::from_residual(r);
}
};
};walk_list!(
489 visitor,
490 visit_trait_item,
491 module.trait_items().map(|id| self.hir_trait_item(id))
492 );
493 for elem in module.impl_items().map(|id| self.hir_impl_item(id)) {
match ::rustc_ast_ir::visit::VisitorResult::branch(visitor.visit_impl_item(elem))
{
core::ops::ControlFlow::Continue(()) =>
(),
#[allow(unreachable_code)]
core::ops::ControlFlow::Break(r) => {
return ::rustc_ast_ir::visit::VisitorResult::from_residual(r);
}
};
};walk_list!(visitor, visit_impl_item, module.impl_items().map(|id| self.hir_impl_item(id)));
494 for elem in module.foreign_items().map(|id| self.hir_foreign_item(id)) {
match ::rustc_ast_ir::visit::VisitorResult::branch(visitor.visit_foreign_item(elem))
{
core::ops::ControlFlow::Continue(()) =>
(),
#[allow(unreachable_code)]
core::ops::ControlFlow::Break(r) => {
return ::rustc_ast_ir::visit::VisitorResult::from_residual(r);
}
};
};walk_list!(
495 visitor,
496 visit_foreign_item,
497 module.foreign_items().map(|id| self.hir_foreign_item(id))
498 );
499 V::Result::output()
500 }
501
502 pub fn hir_for_each_module(self, mut f: impl FnMut(LocalModDefId)) {
503 let crate_items = self.hir_crate_items(());
504 for module in crate_items.submodules.iter() {
505 f(LocalModDefId::new_unchecked(module.def_id))
506 }
507 }
508
509 #[inline]
510 pub fn par_hir_for_each_module(self, f: impl Fn(LocalModDefId) + DynSend + DynSync) {
511 let crate_items = self.hir_crate_items(());
512 par_for_each_in(&crate_items.submodules[..], |module| {
513 f(LocalModDefId::new_unchecked(module.def_id))
514 })
515 }
516
517 #[inline]
518 pub fn try_par_hir_for_each_module(
519 self,
520 f: impl Fn(LocalModDefId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
521 ) -> Result<(), ErrorGuaranteed> {
522 let crate_items = self.hir_crate_items(());
523 try_par_for_each_in(&crate_items.submodules[..], |module| {
524 f(LocalModDefId::new_unchecked(module.def_id))
525 })
526 }
527
528 #[inline]
531 pub fn hir_parent_id_iter(self, current_id: HirId) -> impl Iterator<Item = HirId> {
532 ParentHirIterator::new(self, current_id)
533 }
534
535 #[inline]
538 pub fn hir_parent_iter(self, current_id: HirId) -> impl Iterator<Item = (HirId, Node<'tcx>)> {
539 self.hir_parent_id_iter(current_id).map(move |id| (id, self.hir_node(id)))
540 }
541
542 #[inline]
545 pub fn hir_parent_owner_iter(self, current_id: HirId) -> ParentOwnerIterator<'tcx> {
546 ParentOwnerIterator { current_id, tcx: self }
547 }
548
549 pub fn hir_is_lhs(self, id: HirId) -> bool {
551 match self.parent_hir_node(id) {
552 Node::Expr(expr) => match expr.kind {
553 ExprKind::Assign(lhs, _rhs, _span) => lhs.hir_id == id,
554 _ => false,
555 },
556 _ => false,
557 }
558 }
559
560 pub fn hir_is_inside_const_context(self, hir_id: HirId) -> bool {
563 self.hir_body_const_context(self.hir_enclosing_body_owner(hir_id)).is_some()
564 }
565
566 pub fn hir_get_fn_id_for_return_block(self, id: HirId) -> Option<HirId> {
593 let enclosing_body_owner = self.local_def_id_to_hir_id(self.hir_enclosing_body_owner(id));
594
595 let mut iter = [id].into_iter().chain(self.hir_parent_id_iter(id)).peekable();
597 while let Some(cur_id) = iter.next() {
598 if enclosing_body_owner == cur_id {
599 break;
600 }
601
602 if let Node::Expr(Expr { kind: ExprKind::Ret(_), .. }) = self.hir_node(cur_id) {
605 break;
606 }
607
608 if let Some(&parent_id) = iter.peek() {
611 match self.hir_node(parent_id) {
612 Node::Block(Block { expr: Some(e), .. }) if cur_id != e.hir_id => return None,
615 Node::Block(Block { expr: Some(e), .. })
616 if #[allow(non_exhaustive_omitted_patterns)] match e.kind {
ExprKind::If(_, _, None) => true,
_ => false,
}matches!(e.kind, ExprKind::If(_, _, None)) =>
617 {
618 return None;
619 }
620
621 Node::Block(Block { expr: None, .. })
624 | Node::Expr(Expr { kind: ExprKind::Loop(..), .. })
625 | Node::LetStmt(..) => return None,
626
627 _ => {}
628 }
629 }
630 }
631
632 Some(enclosing_body_owner)
633 }
634
635 pub fn hir_get_parent_item(self, hir_id: HirId) -> OwnerId {
640 if hir_id.local_id != ItemLocalId::ZERO {
641 hir_id.owner
643 } else if let Some((def_id, _node)) = self.hir_parent_owner_iter(hir_id).next() {
644 def_id
645 } else {
646 CRATE_OWNER_ID
647 }
648 }
649
650 pub fn hir_get_if_cause(self, hir_id: HirId) -> Option<&'tcx Expr<'tcx>> {
656 for (_, node) in self.hir_parent_iter(hir_id) {
657 match node {
658 Node::Item(_)
659 | Node::ForeignItem(_)
660 | Node::TraitItem(_)
661 | Node::ImplItem(_)
662 | Node::Stmt(Stmt { kind: StmtKind::Let(_), .. }) => break,
663 Node::Expr(expr @ Expr { kind: ExprKind::If(..) | ExprKind::Match(..), .. }) => {
664 return Some(expr);
665 }
666 _ => {}
667 }
668 }
669 None
670 }
671
672 pub fn hir_get_enclosing_scope(self, hir_id: HirId) -> Option<HirId> {
674 for (hir_id, node) in self.hir_parent_iter(hir_id) {
675 if let Node::Item(Item {
676 kind:
677 ItemKind::Fn { .. }
678 | ItemKind::Const(..)
679 | ItemKind::Static(..)
680 | ItemKind::Mod(..)
681 | ItemKind::Enum(..)
682 | ItemKind::Struct(..)
683 | ItemKind::Union(..)
684 | ItemKind::Trait { .. }
685 | ItemKind::Impl { .. },
686 ..
687 })
688 | Node::ForeignItem(ForeignItem { kind: ForeignItemKind::Fn(..), .. })
689 | Node::TraitItem(TraitItem { kind: TraitItemKind::Fn(..), .. })
690 | Node::ImplItem(ImplItem { kind: ImplItemKind::Fn(..), .. })
691 | Node::Block(_) = node
692 {
693 return Some(hir_id);
694 }
695 }
696 None
697 }
698
699 pub fn hir_get_defining_scope(self, id: HirId) -> HirId {
701 let mut scope = id;
702 loop {
703 scope = self.hir_get_enclosing_scope(scope).unwrap_or(CRATE_HIR_ID);
704 if scope == CRATE_HIR_ID || !#[allow(non_exhaustive_omitted_patterns)] match self.hir_node(scope) {
Node::Block(_) => true,
_ => false,
}matches!(self.hir_node(scope), Node::Block(_)) {
705 return scope;
706 }
707 }
708 }
709
710 pub fn hir_id_to_string(self, id: HirId) -> String {
713 let path_str = |def_id: LocalDefId| self.def_path_str(def_id);
714
715 let span_str =
716 || self.sess.source_map().span_to_snippet(self.hir_span(id)).unwrap_or_default();
717 let node_str = |prefix| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{1} ({2} `{0}`)", span_str(), id,
prefix))
})format!("{id} ({prefix} `{}`)", span_str());
718
719 match self.hir_node(id) {
720 Node::Item(item) => {
721 let item_str = match item.kind {
722 ItemKind::ExternCrate(..) => "extern crate",
723 ItemKind::Use(..) => "use",
724 ItemKind::Static(..) => "static",
725 ItemKind::Const(..) => "const",
726 ItemKind::Fn { .. } => "fn",
727 ItemKind::Macro(..) => "macro",
728 ItemKind::Mod(..) => "mod",
729 ItemKind::ForeignMod { .. } => "foreign mod",
730 ItemKind::GlobalAsm { .. } => "global asm",
731 ItemKind::TyAlias(..) => "ty",
732 ItemKind::Enum(..) => "enum",
733 ItemKind::Struct(..) => "struct",
734 ItemKind::Union(..) => "union",
735 ItemKind::Trait { .. } => "trait",
736 ItemKind::TraitAlias(..) => "trait alias",
737 ItemKind::Impl { .. } => "impl",
738 };
739 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{1} ({2} {0})",
path_str(item.owner_id.def_id), id, item_str))
})format!("{id} ({item_str} {})", path_str(item.owner_id.def_id))
740 }
741 Node::ForeignItem(item) => {
742 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{1} (foreign item {0})",
path_str(item.owner_id.def_id), id))
})format!("{id} (foreign item {})", path_str(item.owner_id.def_id))
743 }
744 Node::ImplItem(ii) => {
745 let kind = match ii.kind {
746 ImplItemKind::Const(..) => "associated constant",
747 ImplItemKind::Fn(fn_sig, _) => match fn_sig.decl.implicit_self() {
748 ImplicitSelfKind::None => "associated function",
749 _ => "method",
750 },
751 ImplItemKind::Type(_) => "associated type",
752 };
753 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{2} ({3} `{0}` in {1})", ii.ident,
path_str(ii.owner_id.def_id), id, kind))
})format!("{id} ({kind} `{}` in {})", ii.ident, path_str(ii.owner_id.def_id))
754 }
755 Node::TraitItem(ti) => {
756 let kind = match ti.kind {
757 TraitItemKind::Const(..) => "associated constant",
758 TraitItemKind::Fn(fn_sig, _) => match fn_sig.decl.implicit_self() {
759 ImplicitSelfKind::None => "associated function",
760 _ => "trait method",
761 },
762 TraitItemKind::Type(..) => "associated type",
763 };
764
765 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{2} ({3} `{0}` in {1})", ti.ident,
path_str(ti.owner_id.def_id), id, kind))
})format!("{id} ({kind} `{}` in {})", ti.ident, path_str(ti.owner_id.def_id))
766 }
767 Node::Variant(variant) => {
768 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{2} (variant `{0}` in {1})",
variant.ident, path_str(variant.def_id), id))
})format!("{id} (variant `{}` in {})", variant.ident, path_str(variant.def_id))
769 }
770 Node::Field(field) => {
771 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{2} (field `{0}` in {1})",
field.ident, path_str(field.def_id), id))
})format!("{id} (field `{}` in {})", field.ident, path_str(field.def_id))
772 }
773 Node::AnonConst(_) => node_str("const"),
774 Node::ConstBlock(_) => node_str("const"),
775 Node::ConstArg(_) => node_str("const"),
776 Node::Expr(_) => node_str("expr"),
777 Node::ExprField(_) => node_str("expr field"),
778 Node::ConstArgExprField(_) => node_str("const arg expr field"),
779 Node::Stmt(_) => node_str("stmt"),
780 Node::PathSegment(_) => node_str("path segment"),
781 Node::Ty(_) => node_str("type"),
782 Node::AssocItemConstraint(_) => node_str("assoc item constraint"),
783 Node::TraitRef(_) => node_str("trait ref"),
784 Node::OpaqueTy(_) => node_str("opaque type"),
785 Node::Pat(_) => node_str("pat"),
786 Node::TyPat(_) => node_str("pat ty"),
787 Node::PatField(_) => node_str("pattern field"),
788 Node::PatExpr(_) => node_str("pattern literal"),
789 Node::Param(_) => node_str("param"),
790 Node::Arm(_) => node_str("arm"),
791 Node::Block(_) => node_str("block"),
792 Node::Infer(_) => node_str("infer"),
793 Node::LetStmt(_) => node_str("local"),
794 Node::Ctor(ctor) => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{1} (ctor {0})",
ctor.ctor_def_id().map_or("<missing path>".into(),
|def_id| path_str(def_id)), id))
})format!(
795 "{id} (ctor {})",
796 ctor.ctor_def_id().map_or("<missing path>".into(), |def_id| path_str(def_id)),
797 ),
798 Node::Lifetime(_) => node_str("lifetime"),
799 Node::GenericParam(param) => {
800 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{1} (generic_param {0})",
path_str(param.def_id), id))
})format!("{id} (generic_param {})", path_str(param.def_id))
801 }
802 Node::Crate(..) => String::from("(root_crate)"),
803 Node::WherePredicate(_) => node_str("where predicate"),
804 Node::Synthetic => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
805 Node::Err(_) => node_str("error"),
806 Node::PreciseCapturingNonLifetimeArg(_param) => node_str("parameter"),
807 }
808 }
809
810 pub fn hir_get_foreign_abi(self, hir_id: HirId) -> ExternAbi {
811 let parent = self.hir_get_parent_item(hir_id);
812 if let OwnerNode::Item(Item { kind: ItemKind::ForeignMod { abi, .. }, .. }) =
813 self.hir_owner_node(parent)
814 {
815 return *abi;
816 }
817 crate::util::bug::bug_fmt(format_args!("expected foreign mod or inlined parent, found {0}",
self.hir_id_to_string(HirId::make_owner(parent.def_id))))bug!(
818 "expected foreign mod or inlined parent, found {}",
819 self.hir_id_to_string(HirId::make_owner(parent.def_id))
820 )
821 }
822
823 pub fn hir_expect_item(self, id: LocalDefId) -> &'tcx Item<'tcx> {
824 match self.expect_hir_owner_node(id) {
825 OwnerNode::Item(item) => item,
826 _ => crate::util::bug::bug_fmt(format_args!("expected item, found {0}",
self.hir_id_to_string(HirId::make_owner(id))))bug!("expected item, found {}", self.hir_id_to_string(HirId::make_owner(id))),
827 }
828 }
829
830 pub fn hir_expect_impl_item(self, id: LocalDefId) -> &'tcx ImplItem<'tcx> {
831 match self.expect_hir_owner_node(id) {
832 OwnerNode::ImplItem(item) => item,
833 _ => crate::util::bug::bug_fmt(format_args!("expected impl item, found {0}",
self.hir_id_to_string(HirId::make_owner(id))))bug!("expected impl item, found {}", self.hir_id_to_string(HirId::make_owner(id))),
834 }
835 }
836
837 pub fn hir_expect_trait_item(self, id: LocalDefId) -> &'tcx TraitItem<'tcx> {
838 match self.expect_hir_owner_node(id) {
839 OwnerNode::TraitItem(item) => item,
840 _ => {
841 crate::util::bug::bug_fmt(format_args!("expected trait item, found {0}",
self.hir_id_to_string(HirId::make_owner(id))))bug!("expected trait item, found {}", self.hir_id_to_string(HirId::make_owner(id)))
842 }
843 }
844 }
845
846 pub fn hir_get_fn_output(self, def_id: LocalDefId) -> Option<&'tcx FnRetTy<'tcx>> {
847 Some(&self.opt_hir_owner_node(def_id)?.fn_decl()?.output)
848 }
849
850 #[track_caller]
851 pub fn hir_expect_opaque_ty(self, id: LocalDefId) -> &'tcx OpaqueTy<'tcx> {
852 match self.hir_node_by_def_id(id) {
853 Node::OpaqueTy(opaq) => opaq,
854 _ => {
855 crate::util::bug::bug_fmt(format_args!("expected opaque type definition, found {0}",
self.hir_id_to_string(self.local_def_id_to_hir_id(id))))bug!(
856 "expected opaque type definition, found {}",
857 self.hir_id_to_string(self.local_def_id_to_hir_id(id))
858 )
859 }
860 }
861 }
862
863 pub fn hir_expect_expr(self, id: HirId) -> &'tcx Expr<'tcx> {
864 match self.hir_node(id) {
865 Node::Expr(expr) => expr,
866 _ => crate::util::bug::bug_fmt(format_args!("expected expr, found {0}",
self.hir_id_to_string(id)))bug!("expected expr, found {}", self.hir_id_to_string(id)),
867 }
868 }
869
870 pub fn hir_opt_delegation_sig_id(self, def_id: LocalDefId) -> Option<DefId> {
871 self.opt_hir_owner_node(def_id)?.fn_decl()?.opt_delegation_sig_id()
872 }
873
874 pub fn hir_opt_delegation_info(self, def_id: LocalDefId) -> Option<&'tcx DelegationInfo> {
875 self.opt_hir_owner_node(def_id)?.fn_decl()?.opt_delegation_info()
876 }
877
878 pub fn hir_delegation_info(self, delegation_id: LocalDefId) -> &'tcx DelegationInfo {
879 self.hir_opt_delegation_info(delegation_id).expect("processing delegation")
880 }
881
882 pub fn hir_is_delegation_child_segment(self, segment: &PathSegment<'_>) -> bool {
883 let parent_def = self.hir_get_parent_item(segment.hir_id).def_id;
884
885 self.hir_opt_delegation_info(parent_def)
886 .is_some_and(|info| info.child_seg_id == segment.hir_id)
887 }
888
889 #[inline]
890 fn hir_opt_ident(self, id: HirId) -> Option<Ident> {
891 match self.hir_node(id) {
892 Node::Pat(&Pat { kind: PatKind::Binding(_, _, ident, _), .. }) => Some(ident),
893 Node::Ctor(..) => match self.parent_hir_node(id) {
896 Node::Item(item) => Some(item.kind.ident().unwrap()),
897 Node::Variant(variant) => Some(variant.ident),
898 _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
899 },
900 node => node.ident(),
901 }
902 }
903
904 #[inline]
905 pub(super) fn hir_opt_ident_span(self, id: HirId) -> Option<Span> {
906 self.hir_opt_ident(id).map(|ident| ident.span)
907 }
908
909 #[inline]
910 pub fn hir_ident(self, id: HirId) -> Ident {
911 self.hir_opt_ident(id).unwrap()
912 }
913
914 #[inline]
915 pub fn hir_opt_name(self, id: HirId) -> Option<Symbol> {
916 self.hir_opt_ident(id).map(|ident| ident.name)
917 }
918
919 pub fn hir_name(self, id: HirId) -> Symbol {
920 self.hir_opt_name(id).unwrap_or_else(|| crate::util::bug::bug_fmt(format_args!("no name for {0}",
self.hir_id_to_string(id)))bug!("no name for {}", self.hir_id_to_string(id)))
921 }
922
923 pub fn hir_attrs(self, id: HirId) -> &'tcx [Attribute] {
926 self.hir_attr_map(id.owner).get(id.local_id)
927 }
928
929 pub fn hir_span(self, hir_id: HirId) -> Span {
932 fn until_within(outer: Span, end: Span) -> Span {
933 if let Some(end) = end.find_ancestor_inside(outer) {
934 outer.with_hi(end.hi())
935 } else {
936 outer
937 }
938 }
939
940 fn named_span(item_span: Span, ident: Ident, generics: Option<&Generics<'_>>) -> Span {
941 let mut span = until_within(item_span, ident.span);
942 if let Some(g) = generics
943 && !g.span.is_dummy()
944 && let Some(g_span) = g.span.find_ancestor_inside(item_span)
945 {
946 span = span.to(g_span);
947 }
948 span
949 }
950
951 let span = match self.hir_node(hir_id) {
952 Node::Item(Item { kind: ItemKind::Fn { sig, .. }, span: outer_span, .. })
954 | Node::TraitItem(TraitItem {
955 kind: TraitItemKind::Fn(sig, ..),
956 span: outer_span,
957 ..
958 })
959 | Node::ImplItem(ImplItem {
960 kind: ImplItemKind::Fn(sig, ..), span: outer_span, ..
961 })
962 | Node::ForeignItem(ForeignItem {
963 kind: ForeignItemKind::Fn(sig, ..),
964 span: outer_span,
965 ..
966 }) => {
967 sig.span.find_ancestor_in_same_ctxt(*outer_span).unwrap_or(*outer_span)
970 }
971 Node::Item(Item {
973 kind: ItemKind::Impl(Impl { generics, .. }),
974 span: outer_span,
975 ..
976 }) => until_within(*outer_span, generics.where_clause_span),
977 Node::Item(Item {
979 kind: ItemKind::Const(_, _, ty, _) | ItemKind::Static(_, _, ty, _),
980 span: outer_span,
981 ..
982 })
983 | Node::TraitItem(TraitItem {
984 kind: TraitItemKind::Const(ty, ..),
985 span: outer_span,
986 ..
987 })
988 | Node::ImplItem(ImplItem {
989 kind: ImplItemKind::Const(ty, ..),
990 span: outer_span,
991 ..
992 })
993 | Node::ForeignItem(ForeignItem {
994 kind: ForeignItemKind::Static(ty, ..),
995 span: outer_span,
996 ..
997 }) => until_within(*outer_span, ty.span),
998 Node::Item(Item {
1000 kind: ItemKind::Trait { generics, bounds, .. },
1001 span: outer_span,
1002 ..
1003 })
1004 | Node::TraitItem(TraitItem {
1005 kind: TraitItemKind::Type(bounds, _),
1006 generics,
1007 span: outer_span,
1008 ..
1009 }) => {
1010 let end = if let Some(b) = bounds.last() { b.span() } else { generics.span };
1011 until_within(*outer_span, end)
1012 }
1013 Node::Item(item) => match &item.kind {
1015 ItemKind::Use(path, _) => {
1016 path.span.find_ancestor_in_same_ctxt(item.span).unwrap_or(item.span)
1019 }
1020 _ => {
1021 if let Some(ident) = item.kind.ident() {
1022 named_span(item.span, ident, item.kind.generics())
1023 } else {
1024 item.span
1025 }
1026 }
1027 },
1028 Node::Variant(variant) => named_span(variant.span, variant.ident, None),
1029 Node::ImplItem(item) => named_span(item.span, item.ident, Some(item.generics)),
1030 Node::ForeignItem(item) => named_span(item.span, item.ident, None),
1031 Node::Ctor(_) => return self.hir_span(self.parent_hir_id(hir_id)),
1032 Node::Expr(Expr {
1033 kind: ExprKind::Closure(Closure { fn_decl_span, .. }),
1034 span,
1035 ..
1036 }) => {
1037 fn_decl_span.find_ancestor_inside_same_ctxt(*span).unwrap_or(*span)
1039 }
1040 _ => self.hir_span_with_body(hir_id),
1041 };
1042 if true {
{
match (&span.ctxt(), &self.hir_span_with_body(hir_id).ctxt()) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};
};debug_assert_eq!(span.ctxt(), self.hir_span_with_body(hir_id).ctxt());
1043 span
1044 }
1045
1046 pub fn hir_span_with_body(self, hir_id: HirId) -> Span {
1049 match self.hir_node(hir_id) {
1050 Node::Param(param) => param.span,
1051 Node::Item(item) => item.span,
1052 Node::ForeignItem(foreign_item) => foreign_item.span,
1053 Node::TraitItem(trait_item) => trait_item.span,
1054 Node::ImplItem(impl_item) => impl_item.span,
1055 Node::Variant(variant) => variant.span,
1056 Node::Field(field) => field.span,
1057 Node::AnonConst(constant) => constant.span,
1058 Node::ConstBlock(constant) => self.hir_body(constant.body).value.span,
1059 Node::ConstArg(const_arg) => const_arg.span,
1060 Node::Expr(expr) => expr.span,
1061 Node::ExprField(field) => field.span,
1062 Node::ConstArgExprField(field) => field.span,
1063 Node::Stmt(stmt) => stmt.span,
1064 Node::PathSegment(seg) => {
1065 let ident_span = seg.ident.span;
1066 ident_span
1067 .with_hi(seg.args.map_or_else(|| ident_span.hi(), |args| args.span_ext.hi()))
1068 }
1069 Node::Ty(ty) => ty.span,
1070 Node::AssocItemConstraint(constraint) => constraint.span,
1071 Node::TraitRef(tr) => tr.path.span,
1072 Node::OpaqueTy(op) => op.span,
1073 Node::Pat(pat) => pat.span,
1074 Node::TyPat(pat) => pat.span,
1075 Node::PatField(field) => field.span,
1076 Node::PatExpr(lit) => lit.span,
1077 Node::Arm(arm) => arm.span,
1078 Node::Block(block) => block.span,
1079 Node::Ctor(..) => self.hir_span_with_body(self.parent_hir_id(hir_id)),
1080 Node::Lifetime(lifetime) => lifetime.ident.span,
1081 Node::GenericParam(param) => param.span,
1082 Node::Infer(i) => i.span,
1083 Node::LetStmt(local) => local.span,
1084 Node::Crate(item) => item.spans.inner_span,
1085 Node::WherePredicate(pred) => pred.span,
1086 Node::PreciseCapturingNonLifetimeArg(param) => param.ident.span,
1087 Node::Synthetic => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
1088 Node::Err(span) => span,
1089 }
1090 }
1091
1092 pub fn hir_span_if_local(self, id: DefId) -> Option<Span> {
1093 id.is_local().then(|| self.def_span(id))
1094 }
1095
1096 pub fn hir_res_span(self, res: Res) -> Option<Span> {
1097 match res {
1098 Res::Err => None,
1099 Res::Local(id) => Some(self.hir_span(id)),
1100 res => self.hir_span_if_local(res.opt_def_id()?),
1101 }
1102 }
1103
1104 pub fn hir_opt_const_param_default_param_def_id(self, anon_const: HirId) -> Option<LocalDefId> {
1107 let const_arg = self.parent_hir_id(anon_const);
1108 match self.parent_hir_node(const_arg) {
1109 Node::GenericParam(GenericParam {
1110 def_id: param_id,
1111 kind: GenericParamKind::Const { .. },
1112 ..
1113 }) => Some(*param_id),
1114 _ => None,
1115 }
1116 }
1117
1118 pub fn hir_maybe_get_struct_pattern_shorthand_field(self, expr: &Expr<'_>) -> Option<Symbol> {
1119 let local = match expr {
1120 Expr {
1121 kind:
1122 ExprKind::Path(QPath::Resolved(
1123 None,
1124 Path {
1125 res: def::Res::Local(_), segments: [PathSegment { ident, .. }], ..
1126 },
1127 )),
1128 ..
1129 } => Some(ident),
1130 _ => None,
1131 }?;
1132
1133 match self.parent_hir_node(expr.hir_id) {
1134 Node::ExprField(field) => {
1135 if field.ident.name == local.name && field.is_shorthand {
1136 return Some(local.name);
1137 }
1138 }
1139 _ => {}
1140 }
1141
1142 None
1143 }
1144}
1145
1146impl<'tcx> intravisit::HirTyCtxt<'tcx> for TyCtxt<'tcx> {
1147 fn hir_node(&self, hir_id: HirId) -> Node<'tcx> {
1148 (*self).hir_node(hir_id)
1149 }
1150
1151 fn hir_body(&self, id: BodyId) -> &'tcx Body<'tcx> {
1152 (*self).hir_body(id)
1153 }
1154
1155 fn hir_item(&self, id: ItemId) -> &'tcx Item<'tcx> {
1156 (*self).hir_item(id)
1157 }
1158
1159 fn hir_trait_item(&self, id: TraitItemId) -> &'tcx TraitItem<'tcx> {
1160 (*self).hir_trait_item(id)
1161 }
1162
1163 fn hir_impl_item(&self, id: ImplItemId) -> &'tcx ImplItem<'tcx> {
1164 (*self).hir_impl_item(id)
1165 }
1166
1167 fn hir_foreign_item(&self, id: ForeignItemId) -> &'tcx ForeignItem<'tcx> {
1168 (*self).hir_foreign_item(id)
1169 }
1170}
1171
1172impl<'tcx> pprust_hir::PpAnn for TyCtxt<'tcx> {
1173 fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
1174 pprust_hir::PpAnn::nested(&(self as &dyn intravisit::HirTyCtxt<'_>), state, nested)
1175 }
1176}
1177
1178pub(super) fn crate_hash(tcx: TyCtxt<'_>, _: LocalCrate) -> Svh {
1179 let krate = tcx.hir_crate_items(());
1180 let upstream_crates = upstream_crates(tcx);
1181 let resolutions = tcx.resolutions(());
1182
1183 let mut source_file_names: Vec<_> = tcx
1189 .sess
1190 .source_map()
1191 .files()
1192 .iter()
1193 .filter(|source_file| source_file.cnum == LOCAL_CRATE)
1194 .map(|source_file| source_file.stable_id)
1195 .collect();
1196
1197 source_file_names.sort_unstable();
1198
1199 let debugger_visualizers: Vec<_> = tcx
1205 .debugger_visualizers(LOCAL_CRATE)
1206 .iter()
1207 .map(DebuggerVisualizerFile::path_erased)
1211 .collect();
1212
1213 let crate_hash: Fingerprint = tcx.with_stable_hashing_context(|mut hcx| {
1214 let mut stable_hasher = StableHasher::new();
1215 for owner in krate.owners() {
1217 if let Some(info) = tcx.lower_to_hir(owner.def_id).as_owner() {
1218 info.stable_hash(&mut hcx, &mut stable_hasher);
1219 }
1220 }
1221 upstream_crates.stable_hash(&mut hcx, &mut stable_hasher);
1222 source_file_names.stable_hash(&mut hcx, &mut stable_hasher);
1223 debugger_visualizers.stable_hash(&mut hcx, &mut stable_hasher);
1224 if tcx.sess.opts.incremental.is_some() {
1225 let definitions = tcx.untracked().definitions.freeze();
1226 let mut owner_spans: Vec<_> = tcx
1227 .hir_crate_items(())
1228 .definitions()
1229 .map(|def_id| {
1230 let def_path_hash = definitions.def_path_hash(def_id);
1231 let span = tcx.source_span(def_id);
1232 if true {
{
match (&span.parent(), &None) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};
};debug_assert_eq!(span.parent(), None);
1233 (def_path_hash, span)
1234 })
1235 .collect();
1236 owner_spans.sort_unstable_by_key(|bn| bn.0);
1237 owner_spans.stable_hash(&mut hcx, &mut stable_hasher);
1238 }
1239 tcx.sess.opts.dep_tracking_hash(true).stable_hash(&mut hcx, &mut stable_hasher);
1240 tcx.stable_crate_id(LOCAL_CRATE).stable_hash(&mut hcx, &mut stable_hasher);
1241 resolutions.visibilities_for_hashing.stable_hash(&mut hcx, &mut stable_hasher);
1246 with_metavar_spans(|mspans| {
1247 mspans.freeze_and_get_read_spans().stable_hash(&mut hcx, &mut stable_hasher);
1248 });
1249 stable_hasher.finish()
1250 });
1251
1252 Svh::new(crate_hash)
1253}
1254
1255fn upstream_crates(tcx: TyCtxt<'_>) -> Vec<(StableCrateId, Svh)> {
1256 let mut upstream_crates: Vec<_> = tcx
1257 .crates(())
1258 .iter()
1259 .map(|&cnum| {
1260 let stable_crate_id = tcx.stable_crate_id(cnum);
1261 let hash = tcx.crate_hash(cnum);
1262 (stable_crate_id, hash)
1263 })
1264 .collect();
1265 upstream_crates.sort_unstable_by_key(|&(stable_crate_id, _)| stable_crate_id);
1266 upstream_crates
1267}
1268
1269pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalModDefId) -> ModuleItems {
1270 let mut collector = ItemCollector::new(tcx, false);
1271
1272 let (hir_mod, span, hir_id) = tcx.hir_get_module(module_id);
1273 collector.visit_mod(hir_mod, span, hir_id);
1274
1275 let ItemCollector {
1276 submodules,
1277 items,
1278 trait_items,
1279 impl_items,
1280 foreign_items,
1281 body_owners,
1282 opaques,
1283 nested_bodies,
1284 eiis,
1285 proc_macro_decls,
1286 ..
1287 } = collector;
1288
1289 ModuleItems {
1290 add_root: false,
1291 submodules: submodules.into_boxed_slice(),
1292 free_items: items.into_boxed_slice(),
1293 trait_items: trait_items.into_boxed_slice(),
1294 impl_items: impl_items.into_boxed_slice(),
1295 foreign_items: foreign_items.into_boxed_slice(),
1296 body_owners: body_owners.into_boxed_slice(),
1297 opaques: opaques.into_boxed_slice(),
1298 nested_bodies: nested_bodies.into_boxed_slice(),
1299 eiis: eiis.into_boxed_slice(),
1300 proc_macro_decls,
1301 }
1302}
1303
1304pub(crate) fn hir_crate_items(tcx: TyCtxt<'_>, _: ()) -> ModuleItems {
1305 let mut collector = ItemCollector::new(tcx, true);
1306
1307 collector.submodules.push(CRATE_OWNER_ID);
1311 tcx.hir_walk_toplevel_module(&mut collector);
1312
1313 let ItemCollector {
1314 submodules,
1315 items,
1316 trait_items,
1317 impl_items,
1318 foreign_items,
1319 body_owners,
1320 opaques,
1321 nested_bodies,
1322 eiis,
1323 proc_macro_decls,
1324 ..
1325 } = collector;
1326
1327 ModuleItems {
1328 add_root: true,
1329 submodules: submodules.into_boxed_slice(),
1330 free_items: items.into_boxed_slice(),
1331 trait_items: trait_items.into_boxed_slice(),
1332 impl_items: impl_items.into_boxed_slice(),
1333 foreign_items: foreign_items.into_boxed_slice(),
1334 body_owners: body_owners.into_boxed_slice(),
1335 opaques: opaques.into_boxed_slice(),
1336 nested_bodies: nested_bodies.into_boxed_slice(),
1337 eiis: eiis.into_boxed_slice(),
1338 proc_macro_decls,
1339 }
1340}
1341
1342struct ItemCollector<'tcx> {
1343 crate_collector: bool,
1348 tcx: TyCtxt<'tcx>,
1349 submodules: Vec<OwnerId> = ::alloc::vec::Vec::new()vec![],
1350 items: Vec<ItemId> = ::alloc::vec::Vec::new()vec![],
1351 trait_items: Vec<TraitItemId> = ::alloc::vec::Vec::new()vec![],
1352 impl_items: Vec<ImplItemId> = ::alloc::vec::Vec::new()vec![],
1353 foreign_items: Vec<ForeignItemId> = ::alloc::vec::Vec::new()vec![],
1354 body_owners: Vec<LocalDefId> = ::alloc::vec::Vec::new()vec![],
1355 opaques: Vec<LocalDefId> = ::alloc::vec::Vec::new()vec![],
1356 nested_bodies: Vec<LocalDefId> = ::alloc::vec::Vec::new()vec![],
1357 eiis: Vec<LocalDefId> = ::alloc::vec::Vec::new()vec![],
1358 proc_macro_decls: Option<LocalDefId> = None,
1359}
1360
1361impl<'tcx> ItemCollector<'tcx> {
1362 fn new(tcx: TyCtxt<'tcx>, crate_collector: bool) -> ItemCollector<'tcx> {
1363 ItemCollector { crate_collector, tcx, .. }
1364 }
1365}
1366
1367impl<'hir> Visitor<'hir> for ItemCollector<'hir> {
1368 type NestedFilter = nested_filter::All;
1369
1370 fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
1371 self.tcx
1372 }
1373
1374 fn visit_item(&mut self, item: &'hir Item<'hir>) {
1375 if Node::Item(item).associated_body().is_some() {
1376 self.body_owners.push(item.owner_id.def_id);
1377 }
1378
1379 let item_id = item.item_id();
1380
1381 if self.crate_collector
1382 && self.proc_macro_decls.is_none()
1383 && {
{
'done:
{
for i in
::rustc_hir::attrs::HasAttrs::get_attrs(item_id.hir_id(),
&self.tcx) {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(RustcProcMacroDecls) => {
break 'done Some(());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}.is_some()find_attr!(self.tcx, item_id.hir_id(), RustcProcMacroDecls)
1384 {
1385 self.proc_macro_decls = Some(item_id.owner_id.def_id);
1386 }
1387
1388 self.items.push(item_id);
1389
1390 if let ItemKind::Static(..) | ItemKind::Fn { .. } | ItemKind::Macro(..) = &item.kind
1391 && item.eii
1392 {
1393 self.eiis.push(item.owner_id.def_id)
1394 }
1395
1396 if let ItemKind::Mod(_, module) = &item.kind {
1398 self.submodules.push(item.owner_id);
1399 if self.crate_collector {
1401 intravisit::walk_mod(self, module);
1402 }
1403 } else {
1404 intravisit::walk_item(self, item)
1405 }
1406 }
1407
1408 fn visit_foreign_item(&mut self, item: &'hir ForeignItem<'hir>) {
1409 self.foreign_items.push(item.foreign_item_id());
1410 intravisit::walk_foreign_item(self, item)
1411 }
1412
1413 fn visit_anon_const(&mut self, c: &'hir AnonConst) {
1414 self.body_owners.push(c.def_id);
1415 intravisit::walk_anon_const(self, c)
1416 }
1417
1418 fn visit_inline_const(&mut self, c: &'hir ConstBlock) {
1419 self.body_owners.push(c.def_id);
1420 self.nested_bodies.push(c.def_id);
1421 intravisit::walk_inline_const(self, c)
1422 }
1423
1424 fn visit_opaque_ty(&mut self, o: &'hir OpaqueTy<'hir>) {
1425 self.opaques.push(o.def_id);
1426 intravisit::walk_opaque_ty(self, o)
1427 }
1428
1429 fn visit_expr(&mut self, ex: &'hir Expr<'hir>) {
1430 if let ExprKind::Closure(closure) = ex.kind {
1431 self.body_owners.push(closure.def_id);
1432 self.nested_bodies.push(closure.def_id);
1433 }
1434 intravisit::walk_expr(self, ex)
1435 }
1436
1437 fn visit_trait_item(&mut self, item: &'hir TraitItem<'hir>) {
1438 if Node::TraitItem(item).associated_body().is_some() {
1439 self.body_owners.push(item.owner_id.def_id);
1440 }
1441
1442 self.trait_items.push(item.trait_item_id());
1443
1444 intravisit::walk_trait_item(self, item)
1445 }
1446
1447 fn visit_impl_item(&mut self, item: &'hir ImplItem<'hir>) {
1448 if Node::ImplItem(item).associated_body().is_some() {
1449 self.body_owners.push(item.owner_id.def_id);
1450 }
1451
1452 self.impl_items.push(item.impl_item_id());
1453
1454 intravisit::walk_impl_item(self, item)
1455 }
1456}