1use rustc_abi::ExternAbi;
6use rustc_ast::visit::{VisitorResult, walk_list};
7use rustc_data_structures::fingerprint::Fingerprint;
8use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
9use rustc_data_structures::svh::Svh;
10use rustc_data_structures::sync::{DynSend, DynSync, par_for_each_in, try_par_for_each_in};
11use rustc_hir::def::{DefKind, Res};
12use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId, LocalModDefId};
13use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
14use rustc_hir::intravisit::Visitor;
15use rustc_hir::*;
16use rustc_hir_pretty as pprust_hir;
17use rustc_span::def_id::StableCrateId;
18use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol, kw, sym, with_metavar_spans};
19
20use crate::hir::{ModuleItems, nested_filter};
21use crate::middle::debugger_visualizer::DebuggerVisualizerFile;
22use crate::query::LocalCrate;
23use crate::ty::TyCtxt;
24
25struct ParentHirIterator<'tcx> {
28 current_id: HirId,
29 tcx: TyCtxt<'tcx>,
30 current_owner_nodes: Option<&'tcx OwnerNodes<'tcx>>,
33}
34
35impl<'tcx> ParentHirIterator<'tcx> {
36 fn new(tcx: TyCtxt<'tcx>, current_id: HirId) -> ParentHirIterator<'tcx> {
37 ParentHirIterator { current_id, tcx, current_owner_nodes: None }
38 }
39}
40
41impl<'tcx> Iterator for ParentHirIterator<'tcx> {
42 type Item = HirId;
43
44 fn next(&mut self) -> Option<Self::Item> {
45 if self.current_id == CRATE_HIR_ID {
46 return None;
47 }
48
49 let HirId { owner, local_id } = self.current_id;
50
51 let parent_id = if local_id == ItemLocalId::ZERO {
52 self.current_owner_nodes = None;
54 self.tcx.hir_owner_parent(owner)
55 } else {
56 let owner_nodes =
57 self.current_owner_nodes.get_or_insert_with(|| self.tcx.hir_owner_nodes(owner));
58 let parent_local_id = owner_nodes.nodes[local_id].parent;
59 debug_assert_ne!(parent_local_id, local_id);
61 HirId { owner, local_id: parent_local_id }
62 };
63
64 debug_assert_ne!(parent_id, self.current_id);
65
66 self.current_id = parent_id;
67 Some(parent_id)
68 }
69}
70
71pub struct ParentOwnerIterator<'tcx> {
74 current_id: HirId,
75 tcx: TyCtxt<'tcx>,
76}
77
78impl<'tcx> Iterator for ParentOwnerIterator<'tcx> {
79 type Item = (OwnerId, OwnerNode<'tcx>);
80
81 fn next(&mut self) -> Option<Self::Item> {
82 if self.current_id.local_id.index() != 0 {
83 self.current_id.local_id = ItemLocalId::ZERO;
84 let node = self.tcx.hir_owner_node(self.current_id.owner);
85 return Some((self.current_id.owner, node));
86 }
87 if self.current_id == CRATE_HIR_ID {
88 return None;
89 }
90
91 let parent_id = self.tcx.hir_def_key(self.current_id.owner.def_id).parent;
92 let parent_id = parent_id.map_or(CRATE_OWNER_ID, |local_def_index| {
93 let def_id = LocalDefId { local_def_index };
94 self.tcx.local_def_id_to_hir_id(def_id).owner
95 });
96 self.current_id = HirId::make_owner(parent_id.def_id);
97
98 let node = self.tcx.hir_owner_node(self.current_id.owner);
99 Some((self.current_id.owner, node))
100 }
101}
102
103impl<'tcx> TyCtxt<'tcx> {
104 #[inline]
105 fn expect_hir_owner_nodes(self, def_id: LocalDefId) -> &'tcx OwnerNodes<'tcx> {
106 self.opt_hir_owner_nodes(def_id)
107 .unwrap_or_else(|| span_bug!(self.def_span(def_id), "{def_id:?} is not an owner"))
108 }
109
110 #[inline]
111 pub fn hir_owner_nodes(self, owner_id: OwnerId) -> &'tcx OwnerNodes<'tcx> {
112 self.expect_hir_owner_nodes(owner_id.def_id)
113 }
114
115 #[inline]
116 fn opt_hir_owner_node(self, def_id: LocalDefId) -> Option<OwnerNode<'tcx>> {
117 self.opt_hir_owner_nodes(def_id).map(|nodes| nodes.node())
118 }
119
120 #[inline]
121 pub fn expect_hir_owner_node(self, def_id: LocalDefId) -> OwnerNode<'tcx> {
122 self.expect_hir_owner_nodes(def_id).node()
123 }
124
125 #[inline]
126 pub fn hir_owner_node(self, owner_id: OwnerId) -> OwnerNode<'tcx> {
127 self.hir_owner_nodes(owner_id).node()
128 }
129
130 pub fn hir_node(self, id: HirId) -> Node<'tcx> {
132 self.hir_owner_nodes(id.owner).nodes[id.local_id].node
133 }
134
135 #[inline]
137 pub fn hir_node_by_def_id(self, id: LocalDefId) -> Node<'tcx> {
138 self.hir_node(self.local_def_id_to_hir_id(id))
139 }
140
141 pub fn parent_hir_id(self, hir_id: HirId) -> HirId {
146 let HirId { owner, local_id } = hir_id;
147 if local_id == ItemLocalId::ZERO {
148 self.hir_owner_parent(owner)
149 } else {
150 let parent_local_id = self.hir_owner_nodes(owner).nodes[local_id].parent;
151 debug_assert_ne!(parent_local_id, local_id);
153 HirId { owner, local_id: parent_local_id }
154 }
155 }
156
157 pub fn parent_hir_node(self, hir_id: HirId) -> Node<'tcx> {
160 self.hir_node(self.parent_hir_id(hir_id))
161 }
162
163 #[inline]
164 pub fn hir_root_module(self) -> &'tcx Mod<'tcx> {
165 match self.hir_owner_node(CRATE_OWNER_ID) {
166 OwnerNode::Crate(item) => item,
167 _ => bug!(),
168 }
169 }
170
171 #[inline]
172 pub fn hir_free_items(self) -> impl Iterator<Item = ItemId> {
173 self.hir_crate_items(()).free_items.iter().copied()
174 }
175
176 #[inline]
177 pub fn hir_module_free_items(self, module: LocalModDefId) -> impl Iterator<Item = ItemId> {
178 self.hir_module_items(module).free_items()
179 }
180
181 pub fn hir_def_key(self, def_id: LocalDefId) -> DefKey {
182 self.definitions_untracked().def_key(def_id)
184 }
185
186 pub fn hir_def_path(self, def_id: LocalDefId) -> DefPath {
187 self.definitions_untracked().def_path(def_id)
189 }
190
191 #[inline]
192 pub fn hir_def_path_hash(self, def_id: LocalDefId) -> DefPathHash {
193 self.definitions_untracked().def_path_hash(def_id)
195 }
196
197 pub fn hir_get_if_local(self, id: DefId) -> Option<Node<'tcx>> {
198 id.as_local().map(|id| self.hir_node_by_def_id(id))
199 }
200
201 pub fn hir_get_generics(self, id: LocalDefId) -> Option<&'tcx Generics<'tcx>> {
202 self.opt_hir_owner_node(id)?.generics()
203 }
204
205 pub fn hir_item(self, id: ItemId) -> &'tcx Item<'tcx> {
206 self.hir_owner_node(id.owner_id).expect_item()
207 }
208
209 pub fn hir_trait_item(self, id: TraitItemId) -> &'tcx TraitItem<'tcx> {
210 self.hir_owner_node(id.owner_id).expect_trait_item()
211 }
212
213 pub fn hir_impl_item(self, id: ImplItemId) -> &'tcx ImplItem<'tcx> {
214 self.hir_owner_node(id.owner_id).expect_impl_item()
215 }
216
217 pub fn hir_foreign_item(self, id: ForeignItemId) -> &'tcx ForeignItem<'tcx> {
218 self.hir_owner_node(id.owner_id).expect_foreign_item()
219 }
220
221 pub fn hir_body(self, id: BodyId) -> &'tcx Body<'tcx> {
222 self.hir_owner_nodes(id.hir_id.owner).bodies[&id.hir_id.local_id]
223 }
224
225 #[track_caller]
226 pub fn hir_fn_decl_by_hir_id(self, hir_id: HirId) -> Option<&'tcx FnDecl<'tcx>> {
227 self.hir_node(hir_id).fn_decl()
228 }
229
230 #[track_caller]
231 pub fn hir_fn_sig_by_hir_id(self, hir_id: HirId) -> Option<&'tcx FnSig<'tcx>> {
232 self.hir_node(hir_id).fn_sig()
233 }
234
235 #[track_caller]
236 pub fn hir_enclosing_body_owner(self, hir_id: HirId) -> LocalDefId {
237 for (_, node) in self.hir_parent_iter(hir_id) {
238 if let Some((def_id, _)) = node.associated_body() {
239 return def_id;
240 }
241 }
242
243 bug!("no `hir_enclosing_body_owner` for hir_id `{}`", hir_id);
244 }
245
246 pub fn hir_body_owner(self, BodyId { hir_id }: BodyId) -> HirId {
250 let parent = self.parent_hir_id(hir_id);
251 assert_eq!(self.hir_node(parent).body_id().unwrap().hir_id, hir_id, "{hir_id:?}");
252 parent
253 }
254
255 pub fn hir_body_owner_def_id(self, BodyId { hir_id }: BodyId) -> LocalDefId {
256 self.parent_hir_node(hir_id).associated_body().unwrap().0
257 }
258
259 pub fn hir_maybe_body_owned_by(self, id: LocalDefId) -> Option<&'tcx Body<'tcx>> {
262 Some(self.hir_body(self.hir_node_by_def_id(id).body_id()?))
263 }
264
265 #[track_caller]
267 pub fn hir_body_owned_by(self, id: LocalDefId) -> &'tcx Body<'tcx> {
268 self.hir_maybe_body_owned_by(id).unwrap_or_else(|| {
269 let hir_id = self.local_def_id_to_hir_id(id);
270 span_bug!(
271 self.hir_span(hir_id),
272 "body_owned_by: {} has no associated body",
273 self.hir_id_to_string(hir_id)
274 );
275 })
276 }
277
278 pub fn hir_body_param_idents(self, id: BodyId) -> impl Iterator<Item = Option<Ident>> {
279 self.hir_body(id).params.iter().map(|param| match param.pat.kind {
280 PatKind::Binding(_, _, ident, _) => Some(ident),
281 PatKind::Wild => Some(Ident::new(kw::Underscore, param.pat.span)),
282 _ => None,
283 })
284 }
285
286 pub fn hir_body_owner_kind(self, def_id: impl Into<DefId>) -> BodyOwnerKind {
290 let def_id = def_id.into();
291 match self.def_kind(def_id) {
292 DefKind::Const | DefKind::AssocConst | DefKind::AnonConst => {
293 BodyOwnerKind::Const { inline: false }
294 }
295 DefKind::InlineConst => BodyOwnerKind::Const { inline: true },
296 DefKind::Ctor(..) | DefKind::Fn | DefKind::AssocFn => BodyOwnerKind::Fn,
297 DefKind::Closure | DefKind::SyntheticCoroutineBody => BodyOwnerKind::Closure,
298 DefKind::Static { safety: _, mutability, nested: false } => {
299 BodyOwnerKind::Static(mutability)
300 }
301 DefKind::GlobalAsm => BodyOwnerKind::GlobalAsm,
302 dk => bug!("{:?} is not a body node: {:?}", def_id, dk),
303 }
304 }
305
306 pub fn hir_body_const_context(self, def_id: LocalDefId) -> Option<ConstContext> {
314 let def_id = def_id.into();
315 let ccx = match self.hir_body_owner_kind(def_id) {
316 BodyOwnerKind::Const { inline } => ConstContext::Const { inline },
317 BodyOwnerKind::Static(mutability) => ConstContext::Static(mutability),
318
319 BodyOwnerKind::Fn if self.is_constructor(def_id) => return None,
320 BodyOwnerKind::Fn | BodyOwnerKind::Closure if self.is_const_fn(def_id) => {
321 ConstContext::ConstFn
322 }
323 BodyOwnerKind::Fn if self.is_const_default_method(def_id) => ConstContext::ConstFn,
324 BodyOwnerKind::Fn | BodyOwnerKind::Closure | BodyOwnerKind::GlobalAsm => return None,
325 };
326
327 Some(ccx)
328 }
329
330 #[inline]
334 pub fn hir_body_owners(self) -> impl Iterator<Item = LocalDefId> {
335 self.hir_crate_items(()).body_owners.iter().copied()
336 }
337
338 #[inline]
339 pub fn par_hir_body_owners(self, f: impl Fn(LocalDefId) + DynSend + DynSync) {
340 par_for_each_in(&self.hir_crate_items(()).body_owners[..], |&&def_id| f(def_id));
341 }
342
343 pub fn hir_ty_param_owner(self, def_id: LocalDefId) -> LocalDefId {
344 let def_kind = self.def_kind(def_id);
345 match def_kind {
346 DefKind::Trait | DefKind::TraitAlias => def_id,
347 DefKind::LifetimeParam | DefKind::TyParam | DefKind::ConstParam => {
348 self.local_parent(def_id)
349 }
350 _ => bug!("ty_param_owner: {:?} is a {:?} not a type parameter", def_id, def_kind),
351 }
352 }
353
354 pub fn hir_ty_param_name(self, def_id: LocalDefId) -> Symbol {
355 let def_kind = self.def_kind(def_id);
356 match def_kind {
357 DefKind::Trait | DefKind::TraitAlias => kw::SelfUpper,
358 DefKind::LifetimeParam | DefKind::TyParam | DefKind::ConstParam => {
359 self.item_name(def_id.to_def_id())
360 }
361 _ => bug!("ty_param_name: {:?} is a {:?} not a type parameter", def_id, def_kind),
362 }
363 }
364
365 pub fn hir_krate_attrs(self) -> &'tcx [Attribute] {
369 self.hir_attrs(CRATE_HIR_ID)
370 }
371
372 pub fn hir_rustc_coherence_is_core(self) -> bool {
373 self.hir_krate_attrs().iter().any(|attr| attr.has_name(sym::rustc_coherence_is_core))
374 }
375
376 pub fn hir_get_module(self, module: LocalModDefId) -> (&'tcx Mod<'tcx>, Span, HirId) {
377 let hir_id = HirId::make_owner(module.to_local_def_id());
378 match self.hir_owner_node(hir_id.owner) {
379 OwnerNode::Item(&Item { span, kind: ItemKind::Mod(_, m), .. }) => (m, span, hir_id),
380 OwnerNode::Crate(item) => (item, item.spans.inner_span, hir_id),
381 node => panic!("not a module: {node:?}"),
382 }
383 }
384
385 pub fn hir_walk_toplevel_module<V>(self, visitor: &mut V) -> V::Result
387 where
388 V: Visitor<'tcx>,
389 {
390 let (top_mod, span, hir_id) = self.hir_get_module(LocalModDefId::CRATE_DEF_ID);
391 visitor.visit_mod(top_mod, span, hir_id)
392 }
393
394 pub fn hir_walk_attributes<V>(self, visitor: &mut V) -> V::Result
396 where
397 V: Visitor<'tcx>,
398 {
399 let krate = self.hir_crate(());
400 for info in krate.owners.iter() {
401 if let MaybeOwner::Owner(info) = info {
402 for attrs in info.attrs.map.values() {
403 walk_list!(visitor, visit_attribute, *attrs);
404 }
405 }
406 }
407 V::Result::output()
408 }
409
410 pub fn hir_visit_all_item_likes_in_crate<V>(self, visitor: &mut V) -> V::Result
421 where
422 V: Visitor<'tcx>,
423 {
424 let krate = self.hir_crate_items(());
425 walk_list!(visitor, visit_item, krate.free_items().map(|id| self.hir_item(id)));
426 walk_list!(
427 visitor,
428 visit_trait_item,
429 krate.trait_items().map(|id| self.hir_trait_item(id))
430 );
431 walk_list!(visitor, visit_impl_item, krate.impl_items().map(|id| self.hir_impl_item(id)));
432 walk_list!(
433 visitor,
434 visit_foreign_item,
435 krate.foreign_items().map(|id| self.hir_foreign_item(id))
436 );
437 V::Result::output()
438 }
439
440 pub fn hir_visit_item_likes_in_module<V>(
443 self,
444 module: LocalModDefId,
445 visitor: &mut V,
446 ) -> V::Result
447 where
448 V: Visitor<'tcx>,
449 {
450 let module = self.hir_module_items(module);
451 walk_list!(visitor, visit_item, module.free_items().map(|id| self.hir_item(id)));
452 walk_list!(
453 visitor,
454 visit_trait_item,
455 module.trait_items().map(|id| self.hir_trait_item(id))
456 );
457 walk_list!(visitor, visit_impl_item, module.impl_items().map(|id| self.hir_impl_item(id)));
458 walk_list!(
459 visitor,
460 visit_foreign_item,
461 module.foreign_items().map(|id| self.hir_foreign_item(id))
462 );
463 V::Result::output()
464 }
465
466 pub fn hir_for_each_module(self, mut f: impl FnMut(LocalModDefId)) {
467 let crate_items = self.hir_crate_items(());
468 for module in crate_items.submodules.iter() {
469 f(LocalModDefId::new_unchecked(module.def_id))
470 }
471 }
472
473 #[inline]
474 pub fn par_hir_for_each_module(self, f: impl Fn(LocalModDefId) + DynSend + DynSync) {
475 let crate_items = self.hir_crate_items(());
476 par_for_each_in(&crate_items.submodules[..], |module| {
477 f(LocalModDefId::new_unchecked(module.def_id))
478 })
479 }
480
481 #[inline]
482 pub fn try_par_hir_for_each_module(
483 self,
484 f: impl Fn(LocalModDefId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
485 ) -> Result<(), ErrorGuaranteed> {
486 let crate_items = self.hir_crate_items(());
487 try_par_for_each_in(&crate_items.submodules[..], |module| {
488 f(LocalModDefId::new_unchecked(module.def_id))
489 })
490 }
491
492 #[inline]
495 pub fn hir_parent_id_iter(self, current_id: HirId) -> impl Iterator<Item = HirId> {
496 ParentHirIterator::new(self, current_id)
497 }
498
499 #[inline]
502 pub fn hir_parent_iter(self, current_id: HirId) -> impl Iterator<Item = (HirId, Node<'tcx>)> {
503 self.hir_parent_id_iter(current_id).map(move |id| (id, self.hir_node(id)))
504 }
505
506 #[inline]
509 pub fn hir_parent_owner_iter(self, current_id: HirId) -> ParentOwnerIterator<'tcx> {
510 ParentOwnerIterator { current_id, tcx: self }
511 }
512
513 pub fn hir_is_lhs(self, id: HirId) -> bool {
515 match self.parent_hir_node(id) {
516 Node::Expr(expr) => match expr.kind {
517 ExprKind::Assign(lhs, _rhs, _span) => lhs.hir_id == id,
518 _ => false,
519 },
520 _ => false,
521 }
522 }
523
524 pub fn hir_is_inside_const_context(self, hir_id: HirId) -> bool {
527 self.hir_body_const_context(self.hir_enclosing_body_owner(hir_id)).is_some()
528 }
529
530 pub fn hir_get_fn_id_for_return_block(self, id: HirId) -> Option<HirId> {
553 let enclosing_body_owner = self.local_def_id_to_hir_id(self.hir_enclosing_body_owner(id));
554
555 let mut iter = [id].into_iter().chain(self.hir_parent_id_iter(id)).peekable();
557 while let Some(cur_id) = iter.next() {
558 if enclosing_body_owner == cur_id {
559 break;
560 }
561
562 if let Node::Expr(Expr { kind: ExprKind::Ret(_), .. }) = self.hir_node(cur_id) {
565 break;
566 }
567
568 if let Some(&parent_id) = iter.peek() {
571 match self.hir_node(parent_id) {
572 Node::Block(Block { expr: Some(e), .. }) if cur_id != e.hir_id => return None,
575 Node::Block(Block { expr: Some(e), .. })
576 if matches!(e.kind, ExprKind::If(_, _, None)) =>
577 {
578 return None;
579 }
580
581 Node::Block(Block { expr: None, .. })
584 | Node::Expr(Expr { kind: ExprKind::Loop(..), .. })
585 | Node::LetStmt(..) => return None,
586
587 _ => {}
588 }
589 }
590 }
591
592 Some(enclosing_body_owner)
593 }
594
595 pub fn hir_get_parent_item(self, hir_id: HirId) -> OwnerId {
600 if hir_id.local_id != ItemLocalId::ZERO {
601 hir_id.owner
603 } else if let Some((def_id, _node)) = self.hir_parent_owner_iter(hir_id).next() {
604 def_id
605 } else {
606 CRATE_OWNER_ID
607 }
608 }
609
610 pub fn hir_get_if_cause(self, hir_id: HirId) -> Option<&'tcx Expr<'tcx>> {
616 for (_, node) in self.hir_parent_iter(hir_id) {
617 match node {
618 Node::Item(_)
619 | Node::ForeignItem(_)
620 | Node::TraitItem(_)
621 | Node::ImplItem(_)
622 | Node::Stmt(Stmt { kind: StmtKind::Let(_), .. }) => break,
623 Node::Expr(expr @ Expr { kind: ExprKind::If(..) | ExprKind::Match(..), .. }) => {
624 return Some(expr);
625 }
626 _ => {}
627 }
628 }
629 None
630 }
631
632 pub fn hir_get_enclosing_scope(self, hir_id: HirId) -> Option<HirId> {
634 for (hir_id, node) in self.hir_parent_iter(hir_id) {
635 if let Node::Item(Item {
636 kind:
637 ItemKind::Fn { .. }
638 | ItemKind::Const(..)
639 | ItemKind::Static(..)
640 | ItemKind::Mod(..)
641 | ItemKind::Enum(..)
642 | ItemKind::Struct(..)
643 | ItemKind::Union(..)
644 | ItemKind::Trait(..)
645 | ItemKind::Impl { .. },
646 ..
647 })
648 | Node::ForeignItem(ForeignItem { kind: ForeignItemKind::Fn(..), .. })
649 | Node::TraitItem(TraitItem { kind: TraitItemKind::Fn(..), .. })
650 | Node::ImplItem(ImplItem { kind: ImplItemKind::Fn(..), .. })
651 | Node::Block(_) = node
652 {
653 return Some(hir_id);
654 }
655 }
656 None
657 }
658
659 pub fn hir_get_defining_scope(self, id: HirId) -> HirId {
661 let mut scope = id;
662 loop {
663 scope = self.hir_get_enclosing_scope(scope).unwrap_or(CRATE_HIR_ID);
664 if scope == CRATE_HIR_ID || !matches!(self.hir_node(scope), Node::Block(_)) {
665 return scope;
666 }
667 }
668 }
669
670 pub fn hir_id_to_string(self, id: HirId) -> String {
673 let path_str = |def_id: LocalDefId| self.def_path_str(def_id);
674
675 let span_str =
676 || self.sess.source_map().span_to_snippet(self.hir_span(id)).unwrap_or_default();
677 let node_str = |prefix| format!("{id} ({prefix} `{}`)", span_str());
678
679 match self.hir_node(id) {
680 Node::Item(item) => {
681 let item_str = match item.kind {
682 ItemKind::ExternCrate(..) => "extern crate",
683 ItemKind::Use(..) => "use",
684 ItemKind::Static(..) => "static",
685 ItemKind::Const(..) => "const",
686 ItemKind::Fn { .. } => "fn",
687 ItemKind::Macro(..) => "macro",
688 ItemKind::Mod(..) => "mod",
689 ItemKind::ForeignMod { .. } => "foreign mod",
690 ItemKind::GlobalAsm { .. } => "global asm",
691 ItemKind::TyAlias(..) => "ty",
692 ItemKind::Enum(..) => "enum",
693 ItemKind::Struct(..) => "struct",
694 ItemKind::Union(..) => "union",
695 ItemKind::Trait(..) => "trait",
696 ItemKind::TraitAlias(..) => "trait alias",
697 ItemKind::Impl { .. } => "impl",
698 };
699 format!("{id} ({item_str} {})", path_str(item.owner_id.def_id))
700 }
701 Node::ForeignItem(item) => {
702 format!("{id} (foreign item {})", path_str(item.owner_id.def_id))
703 }
704 Node::ImplItem(ii) => {
705 let kind = match ii.kind {
706 ImplItemKind::Const(..) => "associated constant",
707 ImplItemKind::Fn(fn_sig, _) => match fn_sig.decl.implicit_self {
708 ImplicitSelfKind::None => "associated function",
709 _ => "method",
710 },
711 ImplItemKind::Type(_) => "associated type",
712 };
713 format!("{id} ({kind} `{}` in {})", ii.ident, path_str(ii.owner_id.def_id))
714 }
715 Node::TraitItem(ti) => {
716 let kind = match ti.kind {
717 TraitItemKind::Const(..) => "associated constant",
718 TraitItemKind::Fn(fn_sig, _) => match fn_sig.decl.implicit_self {
719 ImplicitSelfKind::None => "associated function",
720 _ => "trait method",
721 },
722 TraitItemKind::Type(..) => "associated type",
723 };
724
725 format!("{id} ({kind} `{}` in {})", ti.ident, path_str(ti.owner_id.def_id))
726 }
727 Node::Variant(variant) => {
728 format!("{id} (variant `{}` in {})", variant.ident, path_str(variant.def_id))
729 }
730 Node::Field(field) => {
731 format!("{id} (field `{}` in {})", field.ident, path_str(field.def_id))
732 }
733 Node::AnonConst(_) => node_str("const"),
734 Node::ConstBlock(_) => node_str("const"),
735 Node::ConstArg(_) => node_str("const"),
736 Node::Expr(_) => node_str("expr"),
737 Node::ExprField(_) => node_str("expr field"),
738 Node::Stmt(_) => node_str("stmt"),
739 Node::PathSegment(_) => node_str("path segment"),
740 Node::Ty(_) => node_str("type"),
741 Node::AssocItemConstraint(_) => node_str("assoc item constraint"),
742 Node::TraitRef(_) => node_str("trait ref"),
743 Node::OpaqueTy(_) => node_str("opaque type"),
744 Node::Pat(_) => node_str("pat"),
745 Node::TyPat(_) => node_str("pat ty"),
746 Node::PatField(_) => node_str("pattern field"),
747 Node::PatExpr(_) => node_str("pattern literal"),
748 Node::Param(_) => node_str("param"),
749 Node::Arm(_) => node_str("arm"),
750 Node::Block(_) => node_str("block"),
751 Node::Infer(_) => node_str("infer"),
752 Node::LetStmt(_) => node_str("local"),
753 Node::Ctor(ctor) => format!(
754 "{id} (ctor {})",
755 ctor.ctor_def_id().map_or("<missing path>".into(), |def_id| path_str(def_id)),
756 ),
757 Node::Lifetime(_) => node_str("lifetime"),
758 Node::GenericParam(param) => {
759 format!("{id} (generic_param {})", path_str(param.def_id))
760 }
761 Node::Crate(..) => String::from("(root_crate)"),
762 Node::WherePredicate(_) => node_str("where predicate"),
763 Node::Synthetic => unreachable!(),
764 Node::Err(_) => node_str("error"),
765 Node::PreciseCapturingNonLifetimeArg(_param) => node_str("parameter"),
766 }
767 }
768
769 pub fn hir_get_foreign_abi(self, hir_id: HirId) -> ExternAbi {
770 let parent = self.hir_get_parent_item(hir_id);
771 if let OwnerNode::Item(Item { kind: ItemKind::ForeignMod { abi, .. }, .. }) =
772 self.hir_owner_node(parent)
773 {
774 return *abi;
775 }
776 bug!(
777 "expected foreign mod or inlined parent, found {}",
778 self.hir_id_to_string(HirId::make_owner(parent.def_id))
779 )
780 }
781
782 pub fn hir_expect_item(self, id: LocalDefId) -> &'tcx Item<'tcx> {
783 match self.expect_hir_owner_node(id) {
784 OwnerNode::Item(item) => item,
785 _ => bug!("expected item, found {}", self.hir_id_to_string(HirId::make_owner(id))),
786 }
787 }
788
789 pub fn hir_expect_impl_item(self, id: LocalDefId) -> &'tcx ImplItem<'tcx> {
790 match self.expect_hir_owner_node(id) {
791 OwnerNode::ImplItem(item) => item,
792 _ => bug!("expected impl item, found {}", self.hir_id_to_string(HirId::make_owner(id))),
793 }
794 }
795
796 pub fn hir_expect_trait_item(self, id: LocalDefId) -> &'tcx TraitItem<'tcx> {
797 match self.expect_hir_owner_node(id) {
798 OwnerNode::TraitItem(item) => item,
799 _ => {
800 bug!("expected trait item, found {}", self.hir_id_to_string(HirId::make_owner(id)))
801 }
802 }
803 }
804
805 pub fn hir_get_fn_output(self, def_id: LocalDefId) -> Option<&'tcx FnRetTy<'tcx>> {
806 Some(&self.opt_hir_owner_node(def_id)?.fn_decl()?.output)
807 }
808
809 #[track_caller]
810 pub fn hir_expect_opaque_ty(self, id: LocalDefId) -> &'tcx OpaqueTy<'tcx> {
811 match self.hir_node_by_def_id(id) {
812 Node::OpaqueTy(opaq) => opaq,
813 _ => {
814 bug!(
815 "expected opaque type definition, found {}",
816 self.hir_id_to_string(self.local_def_id_to_hir_id(id))
817 )
818 }
819 }
820 }
821
822 pub fn hir_expect_expr(self, id: HirId) -> &'tcx Expr<'tcx> {
823 match self.hir_node(id) {
824 Node::Expr(expr) => expr,
825 _ => bug!("expected expr, found {}", self.hir_id_to_string(id)),
826 }
827 }
828
829 pub fn hir_opt_delegation_sig_id(self, def_id: LocalDefId) -> Option<DefId> {
830 self.opt_hir_owner_node(def_id)?.fn_decl()?.opt_delegation_sig_id()
831 }
832
833 #[inline]
834 fn hir_opt_ident(self, id: HirId) -> Option<Ident> {
835 match self.hir_node(id) {
836 Node::Pat(&Pat { kind: PatKind::Binding(_, _, ident, _), .. }) => Some(ident),
837 Node::Ctor(..) => match self.parent_hir_node(id) {
840 Node::Item(item) => Some(item.kind.ident().unwrap()),
841 Node::Variant(variant) => Some(variant.ident),
842 _ => unreachable!(),
843 },
844 node => node.ident(),
845 }
846 }
847
848 #[inline]
849 pub(super) fn hir_opt_ident_span(self, id: HirId) -> Option<Span> {
850 self.hir_opt_ident(id).map(|ident| ident.span)
851 }
852
853 #[inline]
854 pub fn hir_ident(self, id: HirId) -> Ident {
855 self.hir_opt_ident(id).unwrap()
856 }
857
858 #[inline]
859 pub fn hir_opt_name(self, id: HirId) -> Option<Symbol> {
860 self.hir_opt_ident(id).map(|ident| ident.name)
861 }
862
863 pub fn hir_name(self, id: HirId) -> Symbol {
864 self.hir_opt_name(id).unwrap_or_else(|| bug!("no name for {}", self.hir_id_to_string(id)))
865 }
866
867 pub fn hir_attrs(self, id: HirId) -> &'tcx [Attribute] {
870 self.hir_attr_map(id.owner).get(id.local_id)
871 }
872
873 pub fn hir_span(self, hir_id: HirId) -> Span {
876 fn until_within(outer: Span, end: Span) -> Span {
877 if let Some(end) = end.find_ancestor_inside(outer) {
878 outer.with_hi(end.hi())
879 } else {
880 outer
881 }
882 }
883
884 fn named_span(item_span: Span, ident: Ident, generics: Option<&Generics<'_>>) -> Span {
885 let mut span = until_within(item_span, ident.span);
886 if let Some(g) = generics
887 && !g.span.is_dummy()
888 && let Some(g_span) = g.span.find_ancestor_inside(item_span)
889 {
890 span = span.to(g_span);
891 }
892 span
893 }
894
895 let span = match self.hir_node(hir_id) {
896 Node::Item(Item { kind: ItemKind::Fn { sig, .. }, span: outer_span, .. })
898 | Node::TraitItem(TraitItem {
899 kind: TraitItemKind::Fn(sig, ..),
900 span: outer_span,
901 ..
902 })
903 | Node::ImplItem(ImplItem {
904 kind: ImplItemKind::Fn(sig, ..), span: outer_span, ..
905 })
906 | Node::ForeignItem(ForeignItem {
907 kind: ForeignItemKind::Fn(sig, ..),
908 span: outer_span,
909 ..
910 }) => {
911 sig.span.find_ancestor_in_same_ctxt(*outer_span).unwrap_or(*outer_span)
914 }
915 Node::Item(Item {
917 kind: ItemKind::Impl(Impl { generics, .. }),
918 span: outer_span,
919 ..
920 }) => until_within(*outer_span, generics.where_clause_span),
921 Node::Item(Item {
923 kind: ItemKind::Const(_, _, ty, _) | ItemKind::Static(_, _, ty, _),
924 span: outer_span,
925 ..
926 })
927 | Node::TraitItem(TraitItem {
928 kind: TraitItemKind::Const(ty, ..),
929 span: outer_span,
930 ..
931 })
932 | Node::ImplItem(ImplItem {
933 kind: ImplItemKind::Const(ty, ..),
934 span: outer_span,
935 ..
936 })
937 | Node::ForeignItem(ForeignItem {
938 kind: ForeignItemKind::Static(ty, ..),
939 span: outer_span,
940 ..
941 }) => until_within(*outer_span, ty.span),
942 Node::Item(Item {
944 kind: ItemKind::Trait(_, _, _, generics, bounds, _),
945 span: outer_span,
946 ..
947 })
948 | Node::TraitItem(TraitItem {
949 kind: TraitItemKind::Type(bounds, _),
950 generics,
951 span: outer_span,
952 ..
953 }) => {
954 let end = if let Some(b) = bounds.last() { b.span() } else { generics.span };
955 until_within(*outer_span, end)
956 }
957 Node::Item(item) => match &item.kind {
959 ItemKind::Use(path, _) => {
960 path.span.find_ancestor_in_same_ctxt(item.span).unwrap_or(item.span)
963 }
964 _ => {
965 if let Some(ident) = item.kind.ident() {
966 named_span(item.span, ident, item.kind.generics())
967 } else {
968 item.span
969 }
970 }
971 },
972 Node::Variant(variant) => named_span(variant.span, variant.ident, None),
973 Node::ImplItem(item) => named_span(item.span, item.ident, Some(item.generics)),
974 Node::ForeignItem(item) => named_span(item.span, item.ident, None),
975 Node::Ctor(_) => return self.hir_span(self.parent_hir_id(hir_id)),
976 Node::Expr(Expr {
977 kind: ExprKind::Closure(Closure { fn_decl_span, .. }),
978 span,
979 ..
980 }) => {
981 fn_decl_span.find_ancestor_inside(*span).unwrap_or(*span)
983 }
984 _ => self.hir_span_with_body(hir_id),
985 };
986 debug_assert_eq!(span.ctxt(), self.hir_span_with_body(hir_id).ctxt());
987 span
988 }
989
990 pub fn hir_span_with_body(self, hir_id: HirId) -> Span {
993 match self.hir_node(hir_id) {
994 Node::Param(param) => param.span,
995 Node::Item(item) => item.span,
996 Node::ForeignItem(foreign_item) => foreign_item.span,
997 Node::TraitItem(trait_item) => trait_item.span,
998 Node::ImplItem(impl_item) => impl_item.span,
999 Node::Variant(variant) => variant.span,
1000 Node::Field(field) => field.span,
1001 Node::AnonConst(constant) => constant.span,
1002 Node::ConstBlock(constant) => self.hir_body(constant.body).value.span,
1003 Node::ConstArg(const_arg) => const_arg.span(),
1004 Node::Expr(expr) => expr.span,
1005 Node::ExprField(field) => field.span,
1006 Node::Stmt(stmt) => stmt.span,
1007 Node::PathSegment(seg) => {
1008 let ident_span = seg.ident.span;
1009 ident_span
1010 .with_hi(seg.args.map_or_else(|| ident_span.hi(), |args| args.span_ext.hi()))
1011 }
1012 Node::Ty(ty) => ty.span,
1013 Node::AssocItemConstraint(constraint) => constraint.span,
1014 Node::TraitRef(tr) => tr.path.span,
1015 Node::OpaqueTy(op) => op.span,
1016 Node::Pat(pat) => pat.span,
1017 Node::TyPat(pat) => pat.span,
1018 Node::PatField(field) => field.span,
1019 Node::PatExpr(lit) => lit.span,
1020 Node::Arm(arm) => arm.span,
1021 Node::Block(block) => block.span,
1022 Node::Ctor(..) => self.hir_span_with_body(self.parent_hir_id(hir_id)),
1023 Node::Lifetime(lifetime) => lifetime.ident.span,
1024 Node::GenericParam(param) => param.span,
1025 Node::Infer(i) => i.span,
1026 Node::LetStmt(local) => local.span,
1027 Node::Crate(item) => item.spans.inner_span,
1028 Node::WherePredicate(pred) => pred.span,
1029 Node::PreciseCapturingNonLifetimeArg(param) => param.ident.span,
1030 Node::Synthetic => unreachable!(),
1031 Node::Err(span) => span,
1032 }
1033 }
1034
1035 pub fn hir_span_if_local(self, id: DefId) -> Option<Span> {
1036 id.is_local().then(|| self.def_span(id))
1037 }
1038
1039 pub fn hir_res_span(self, res: Res) -> Option<Span> {
1040 match res {
1041 Res::Err => None,
1042 Res::Local(id) => Some(self.hir_span(id)),
1043 res => self.hir_span_if_local(res.opt_def_id()?),
1044 }
1045 }
1046
1047 pub fn hir_opt_const_param_default_param_def_id(self, anon_const: HirId) -> Option<LocalDefId> {
1050 let const_arg = self.parent_hir_id(anon_const);
1051 match self.parent_hir_node(const_arg) {
1052 Node::GenericParam(GenericParam {
1053 def_id: param_id,
1054 kind: GenericParamKind::Const { .. },
1055 ..
1056 }) => Some(*param_id),
1057 _ => None,
1058 }
1059 }
1060
1061 pub fn hir_maybe_get_struct_pattern_shorthand_field(self, expr: &Expr<'_>) -> Option<Symbol> {
1062 let local = match expr {
1063 Expr {
1064 kind:
1065 ExprKind::Path(QPath::Resolved(
1066 None,
1067 Path {
1068 res: def::Res::Local(_), segments: [PathSegment { ident, .. }], ..
1069 },
1070 )),
1071 ..
1072 } => Some(ident),
1073 _ => None,
1074 }?;
1075
1076 match self.parent_hir_node(expr.hir_id) {
1077 Node::ExprField(field) => {
1078 if field.ident.name == local.name && field.is_shorthand {
1079 return Some(local.name);
1080 }
1081 }
1082 _ => {}
1083 }
1084
1085 None
1086 }
1087}
1088
1089impl<'tcx> intravisit::HirTyCtxt<'tcx> for TyCtxt<'tcx> {
1090 fn hir_node(&self, hir_id: HirId) -> Node<'tcx> {
1091 (*self).hir_node(hir_id)
1092 }
1093
1094 fn hir_body(&self, id: BodyId) -> &'tcx Body<'tcx> {
1095 (*self).hir_body(id)
1096 }
1097
1098 fn hir_item(&self, id: ItemId) -> &'tcx Item<'tcx> {
1099 (*self).hir_item(id)
1100 }
1101
1102 fn hir_trait_item(&self, id: TraitItemId) -> &'tcx TraitItem<'tcx> {
1103 (*self).hir_trait_item(id)
1104 }
1105
1106 fn hir_impl_item(&self, id: ImplItemId) -> &'tcx ImplItem<'tcx> {
1107 (*self).hir_impl_item(id)
1108 }
1109
1110 fn hir_foreign_item(&self, id: ForeignItemId) -> &'tcx ForeignItem<'tcx> {
1111 (*self).hir_foreign_item(id)
1112 }
1113}
1114
1115impl<'tcx> pprust_hir::PpAnn for TyCtxt<'tcx> {
1116 fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
1117 pprust_hir::PpAnn::nested(&(self as &dyn intravisit::HirTyCtxt<'_>), state, nested)
1118 }
1119}
1120
1121pub(super) fn crate_hash(tcx: TyCtxt<'_>, _: LocalCrate) -> Svh {
1122 let krate = tcx.hir_crate(());
1123 let hir_body_hash = krate.opt_hir_hash.expect("HIR hash missing while computing crate hash");
1124
1125 let upstream_crates = upstream_crates(tcx);
1126
1127 let resolutions = tcx.resolutions(());
1128
1129 let mut source_file_names: Vec<_> = tcx
1135 .sess
1136 .source_map()
1137 .files()
1138 .iter()
1139 .filter(|source_file| source_file.cnum == LOCAL_CRATE)
1140 .map(|source_file| source_file.stable_id)
1141 .collect();
1142
1143 source_file_names.sort_unstable();
1144
1145 let debugger_visualizers: Vec<_> = tcx
1151 .debugger_visualizers(LOCAL_CRATE)
1152 .iter()
1153 .map(DebuggerVisualizerFile::path_erased)
1157 .collect();
1158
1159 let crate_hash: Fingerprint = tcx.with_stable_hashing_context(|mut hcx| {
1160 let mut stable_hasher = StableHasher::new();
1161 hir_body_hash.hash_stable(&mut hcx, &mut stable_hasher);
1162 upstream_crates.hash_stable(&mut hcx, &mut stable_hasher);
1163 source_file_names.hash_stable(&mut hcx, &mut stable_hasher);
1164 debugger_visualizers.hash_stable(&mut hcx, &mut stable_hasher);
1165 if tcx.sess.opts.incremental.is_some() {
1166 let definitions = tcx.untracked().definitions.freeze();
1167 let mut owner_spans: Vec<_> = tcx
1168 .hir_crate_items(())
1169 .definitions()
1170 .map(|def_id| {
1171 let def_path_hash = definitions.def_path_hash(def_id);
1172 let span = tcx.source_span(def_id);
1173 debug_assert_eq!(span.parent(), None);
1174 (def_path_hash, span)
1175 })
1176 .collect();
1177 owner_spans.sort_unstable_by_key(|bn| bn.0);
1178 owner_spans.hash_stable(&mut hcx, &mut stable_hasher);
1179 }
1180 tcx.sess.opts.dep_tracking_hash(true).hash_stable(&mut hcx, &mut stable_hasher);
1181 tcx.stable_crate_id(LOCAL_CRATE).hash_stable(&mut hcx, &mut stable_hasher);
1182 resolutions.visibilities_for_hashing.hash_stable(&mut hcx, &mut stable_hasher);
1187 with_metavar_spans(|mspans| {
1188 mspans.freeze_and_get_read_spans().hash_stable(&mut hcx, &mut stable_hasher);
1189 });
1190 stable_hasher.finish()
1191 });
1192
1193 Svh::new(crate_hash)
1194}
1195
1196fn upstream_crates(tcx: TyCtxt<'_>) -> Vec<(StableCrateId, Svh)> {
1197 let mut upstream_crates: Vec<_> = tcx
1198 .crates(())
1199 .iter()
1200 .map(|&cnum| {
1201 let stable_crate_id = tcx.stable_crate_id(cnum);
1202 let hash = tcx.crate_hash(cnum);
1203 (stable_crate_id, hash)
1204 })
1205 .collect();
1206 upstream_crates.sort_unstable_by_key(|&(stable_crate_id, _)| stable_crate_id);
1207 upstream_crates
1208}
1209
1210pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalModDefId) -> ModuleItems {
1211 let mut collector = ItemCollector::new(tcx, false);
1212
1213 let (hir_mod, span, hir_id) = tcx.hir_get_module(module_id);
1214 collector.visit_mod(hir_mod, span, hir_id);
1215
1216 let ItemCollector {
1217 submodules,
1218 items,
1219 trait_items,
1220 impl_items,
1221 foreign_items,
1222 body_owners,
1223 opaques,
1224 nested_bodies,
1225 ..
1226 } = collector;
1227 ModuleItems {
1228 submodules: submodules.into_boxed_slice(),
1229 free_items: items.into_boxed_slice(),
1230 trait_items: trait_items.into_boxed_slice(),
1231 impl_items: impl_items.into_boxed_slice(),
1232 foreign_items: foreign_items.into_boxed_slice(),
1233 body_owners: body_owners.into_boxed_slice(),
1234 opaques: opaques.into_boxed_slice(),
1235 nested_bodies: nested_bodies.into_boxed_slice(),
1236 }
1237}
1238
1239pub(crate) fn hir_crate_items(tcx: TyCtxt<'_>, _: ()) -> ModuleItems {
1240 let mut collector = ItemCollector::new(tcx, true);
1241
1242 collector.submodules.push(CRATE_OWNER_ID);
1246 tcx.hir_walk_toplevel_module(&mut collector);
1247
1248 let ItemCollector {
1249 submodules,
1250 items,
1251 trait_items,
1252 impl_items,
1253 foreign_items,
1254 body_owners,
1255 opaques,
1256 nested_bodies,
1257 ..
1258 } = collector;
1259
1260 ModuleItems {
1261 submodules: submodules.into_boxed_slice(),
1262 free_items: items.into_boxed_slice(),
1263 trait_items: trait_items.into_boxed_slice(),
1264 impl_items: impl_items.into_boxed_slice(),
1265 foreign_items: foreign_items.into_boxed_slice(),
1266 body_owners: body_owners.into_boxed_slice(),
1267 opaques: opaques.into_boxed_slice(),
1268 nested_bodies: nested_bodies.into_boxed_slice(),
1269 }
1270}
1271
1272struct ItemCollector<'tcx> {
1273 crate_collector: bool,
1276 tcx: TyCtxt<'tcx>,
1277 submodules: Vec<OwnerId>,
1278 items: Vec<ItemId>,
1279 trait_items: Vec<TraitItemId>,
1280 impl_items: Vec<ImplItemId>,
1281 foreign_items: Vec<ForeignItemId>,
1282 body_owners: Vec<LocalDefId>,
1283 opaques: Vec<LocalDefId>,
1284 nested_bodies: Vec<LocalDefId>,
1285}
1286
1287impl<'tcx> ItemCollector<'tcx> {
1288 fn new(tcx: TyCtxt<'tcx>, crate_collector: bool) -> ItemCollector<'tcx> {
1289 ItemCollector {
1290 crate_collector,
1291 tcx,
1292 submodules: Vec::default(),
1293 items: Vec::default(),
1294 trait_items: Vec::default(),
1295 impl_items: Vec::default(),
1296 foreign_items: Vec::default(),
1297 body_owners: Vec::default(),
1298 opaques: Vec::default(),
1299 nested_bodies: Vec::default(),
1300 }
1301 }
1302}
1303
1304impl<'hir> Visitor<'hir> for ItemCollector<'hir> {
1305 type NestedFilter = nested_filter::All;
1306
1307 fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
1308 self.tcx
1309 }
1310
1311 fn visit_item(&mut self, item: &'hir Item<'hir>) {
1312 if Node::Item(item).associated_body().is_some() {
1313 self.body_owners.push(item.owner_id.def_id);
1314 }
1315
1316 self.items.push(item.item_id());
1317
1318 if let ItemKind::Mod(_, module) = &item.kind {
1320 self.submodules.push(item.owner_id);
1321 if self.crate_collector {
1323 intravisit::walk_mod(self, module);
1324 }
1325 } else {
1326 intravisit::walk_item(self, item)
1327 }
1328 }
1329
1330 fn visit_foreign_item(&mut self, item: &'hir ForeignItem<'hir>) {
1331 self.foreign_items.push(item.foreign_item_id());
1332 intravisit::walk_foreign_item(self, item)
1333 }
1334
1335 fn visit_anon_const(&mut self, c: &'hir AnonConst) {
1336 self.body_owners.push(c.def_id);
1337 intravisit::walk_anon_const(self, c)
1338 }
1339
1340 fn visit_inline_const(&mut self, c: &'hir ConstBlock) {
1341 self.body_owners.push(c.def_id);
1342 self.nested_bodies.push(c.def_id);
1343 intravisit::walk_inline_const(self, c)
1344 }
1345
1346 fn visit_opaque_ty(&mut self, o: &'hir OpaqueTy<'hir>) {
1347 self.opaques.push(o.def_id);
1348 intravisit::walk_opaque_ty(self, o)
1349 }
1350
1351 fn visit_expr(&mut self, ex: &'hir Expr<'hir>) {
1352 if let ExprKind::Closure(closure) = ex.kind {
1353 self.body_owners.push(closure.def_id);
1354 self.nested_bodies.push(closure.def_id);
1355 }
1356 intravisit::walk_expr(self, ex)
1357 }
1358
1359 fn visit_trait_item(&mut self, item: &'hir TraitItem<'hir>) {
1360 if Node::TraitItem(item).associated_body().is_some() {
1361 self.body_owners.push(item.owner_id.def_id);
1362 }
1363
1364 self.trait_items.push(item.trait_item_id());
1365 intravisit::walk_trait_item(self, item)
1366 }
1367
1368 fn visit_impl_item(&mut self, item: &'hir ImplItem<'hir>) {
1369 if Node::ImplItem(item).associated_body().is_some() {
1370 self.body_owners.push(item.owner_id.def_id);
1371 }
1372
1373 self.impl_items.push(item.impl_item_id());
1374 intravisit::walk_impl_item(self, item)
1375 }
1376}