1use rustc_abi::ExternAbi;
2use rustc_ast::visit::{VisitorResult, walk_list};
3use rustc_data_structures::fingerprint::Fingerprint;
4use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
5use rustc_data_structures::svh::Svh;
6use rustc_data_structures::sync::{DynSend, DynSync, par_for_each_in, try_par_for_each_in};
7use rustc_hir::def::{DefKind, Res};
8use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId, LocalModDefId};
9use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
10use rustc_hir::intravisit::Visitor;
11use rustc_hir::*;
12use rustc_hir_pretty as pprust_hir;
13use rustc_span::def_id::StableCrateId;
14use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol, kw, sym, with_metavar_spans};
15
16use crate::hir::{ModuleItems, nested_filter};
17use crate::middle::debugger_visualizer::DebuggerVisualizerFile;
18use crate::query::LocalCrate;
19use crate::ty::TyCtxt;
20
21#[derive(Copy, Clone)]
26pub struct Map<'hir> {
27 pub(super) tcx: TyCtxt<'hir>,
28}
29
30struct ParentHirIterator<'hir> {
33 current_id: HirId,
34 map: Map<'hir>,
35 current_owner_nodes: Option<&'hir OwnerNodes<'hir>>,
38}
39
40impl<'hir> ParentHirIterator<'hir> {
41 fn new(map: Map<'hir>, current_id: HirId) -> ParentHirIterator<'hir> {
42 ParentHirIterator { current_id, map, current_owner_nodes: None }
43 }
44}
45
46impl<'hir> Iterator for ParentHirIterator<'hir> {
47 type Item = HirId;
48
49 fn next(&mut self) -> Option<Self::Item> {
50 if self.current_id == CRATE_HIR_ID {
51 return None;
52 }
53
54 let HirId { owner, local_id } = self.current_id;
55
56 let parent_id = if local_id == ItemLocalId::ZERO {
57 self.current_owner_nodes = None;
59 self.map.tcx.hir_owner_parent(owner)
60 } else {
61 let owner_nodes =
62 self.current_owner_nodes.get_or_insert_with(|| self.map.tcx.hir_owner_nodes(owner));
63 let parent_local_id = owner_nodes.nodes[local_id].parent;
64 debug_assert_ne!(parent_local_id, local_id);
66 HirId { owner, local_id: parent_local_id }
67 };
68
69 debug_assert_ne!(parent_id, self.current_id);
70
71 self.current_id = parent_id;
72 Some(parent_id)
73 }
74}
75
76pub struct ParentOwnerIterator<'hir> {
79 current_id: HirId,
80 map: Map<'hir>,
81}
82
83impl<'hir> Iterator for ParentOwnerIterator<'hir> {
84 type Item = (OwnerId, OwnerNode<'hir>);
85
86 fn next(&mut self) -> Option<Self::Item> {
87 if self.current_id.local_id.index() != 0 {
88 self.current_id.local_id = ItemLocalId::ZERO;
89 let node = self.map.tcx.hir_owner_node(self.current_id.owner);
90 return Some((self.current_id.owner, node));
91 }
92 if self.current_id == CRATE_HIR_ID {
93 return None;
94 }
95
96 let parent_id = self.map.def_key(self.current_id.owner.def_id).parent;
97 let parent_id = parent_id.map_or(CRATE_OWNER_ID, |local_def_index| {
98 let def_id = LocalDefId { local_def_index };
99 self.map.tcx.local_def_id_to_hir_id(def_id).owner
100 });
101 self.current_id = HirId::make_owner(parent_id.def_id);
102
103 let node = self.map.tcx.hir_owner_node(self.current_id.owner);
104 Some((self.current_id.owner, node))
105 }
106}
107
108impl<'tcx> TyCtxt<'tcx> {
109 #[inline]
110 fn expect_hir_owner_nodes(self, def_id: LocalDefId) -> &'tcx OwnerNodes<'tcx> {
111 self.opt_hir_owner_nodes(def_id)
112 .unwrap_or_else(|| span_bug!(self.def_span(def_id), "{def_id:?} is not an owner"))
113 }
114
115 #[inline]
116 pub fn hir_owner_nodes(self, owner_id: OwnerId) -> &'tcx OwnerNodes<'tcx> {
117 self.expect_hir_owner_nodes(owner_id.def_id)
118 }
119
120 #[inline]
121 fn opt_hir_owner_node(self, def_id: LocalDefId) -> Option<OwnerNode<'tcx>> {
122 self.opt_hir_owner_nodes(def_id).map(|nodes| nodes.node())
123 }
124
125 #[inline]
126 pub fn expect_hir_owner_node(self, def_id: LocalDefId) -> OwnerNode<'tcx> {
127 self.expect_hir_owner_nodes(def_id).node()
128 }
129
130 #[inline]
131 pub fn hir_owner_node(self, owner_id: OwnerId) -> OwnerNode<'tcx> {
132 self.hir_owner_nodes(owner_id).node()
133 }
134
135 pub fn hir_node(self, id: HirId) -> Node<'tcx> {
137 self.hir_owner_nodes(id.owner).nodes[id.local_id].node
138 }
139
140 #[inline]
142 pub fn hir_node_by_def_id(self, id: LocalDefId) -> Node<'tcx> {
143 self.hir_node(self.local_def_id_to_hir_id(id))
144 }
145
146 pub fn parent_hir_id(self, hir_id: HirId) -> HirId {
151 let HirId { owner, local_id } = hir_id;
152 if local_id == ItemLocalId::ZERO {
153 self.hir_owner_parent(owner)
154 } else {
155 let parent_local_id = self.hir_owner_nodes(owner).nodes[local_id].parent;
156 debug_assert_ne!(parent_local_id, local_id);
158 HirId { owner, local_id: parent_local_id }
159 }
160 }
161
162 pub fn parent_hir_node(self, hir_id: HirId) -> Node<'tcx> {
165 self.hir_node(self.parent_hir_id(hir_id))
166 }
167}
168
169impl<'hir> Map<'hir> {
170 #[inline]
171 pub fn krate(self) -> &'hir Crate<'hir> {
172 self.tcx.hir_crate(())
173 }
174
175 #[inline]
176 pub fn root_module(self) -> &'hir Mod<'hir> {
177 match self.tcx.hir_owner_node(CRATE_OWNER_ID) {
178 OwnerNode::Crate(item) => item,
179 _ => bug!(),
180 }
181 }
182
183 #[inline]
184 pub fn items(self) -> impl Iterator<Item = ItemId> + 'hir {
185 self.tcx.hir_crate_items(()).free_items.iter().copied()
186 }
187
188 #[inline]
189 pub fn module_items(self, module: LocalModDefId) -> impl Iterator<Item = ItemId> + 'hir {
190 self.tcx.hir_module_items(module).free_items()
191 }
192
193 pub fn def_key(self, def_id: LocalDefId) -> DefKey {
194 self.tcx.definitions_untracked().def_key(def_id)
196 }
197
198 pub fn def_path(self, def_id: LocalDefId) -> DefPath {
199 self.tcx.definitions_untracked().def_path(def_id)
201 }
202
203 #[inline]
204 pub fn def_path_hash(self, def_id: LocalDefId) -> DefPathHash {
205 self.tcx.definitions_untracked().def_path_hash(def_id)
207 }
208
209 pub fn get_if_local(self, id: DefId) -> Option<Node<'hir>> {
210 id.as_local().map(|id| self.tcx.hir_node_by_def_id(id))
211 }
212
213 pub fn get_generics(self, id: LocalDefId) -> Option<&'hir Generics<'hir>> {
214 self.tcx.opt_hir_owner_node(id)?.generics()
215 }
216
217 pub fn item(self, id: ItemId) -> &'hir Item<'hir> {
218 self.tcx.hir_owner_node(id.owner_id).expect_item()
219 }
220
221 pub fn trait_item(self, id: TraitItemId) -> &'hir TraitItem<'hir> {
222 self.tcx.hir_owner_node(id.owner_id).expect_trait_item()
223 }
224
225 pub fn impl_item(self, id: ImplItemId) -> &'hir ImplItem<'hir> {
226 self.tcx.hir_owner_node(id.owner_id).expect_impl_item()
227 }
228
229 pub fn foreign_item(self, id: ForeignItemId) -> &'hir ForeignItem<'hir> {
230 self.tcx.hir_owner_node(id.owner_id).expect_foreign_item()
231 }
232
233 pub fn body(self, id: BodyId) -> &'hir Body<'hir> {
234 self.tcx.hir_owner_nodes(id.hir_id.owner).bodies[&id.hir_id.local_id]
235 }
236
237 #[track_caller]
238 pub fn fn_decl_by_hir_id(self, hir_id: HirId) -> Option<&'hir FnDecl<'hir>> {
239 self.tcx.hir_node(hir_id).fn_decl()
240 }
241
242 #[track_caller]
243 pub fn fn_sig_by_hir_id(self, hir_id: HirId) -> Option<&'hir FnSig<'hir>> {
244 self.tcx.hir_node(hir_id).fn_sig()
245 }
246
247 #[track_caller]
248 pub fn enclosing_body_owner(self, hir_id: HirId) -> LocalDefId {
249 for (_, node) in self.parent_iter(hir_id) {
250 if let Some((def_id, _)) = node.associated_body() {
251 return def_id;
252 }
253 }
254
255 bug!("no `enclosing_body_owner` for hir_id `{}`", hir_id);
256 }
257
258 pub fn body_owner(self, BodyId { hir_id }: BodyId) -> HirId {
262 let parent = self.tcx.parent_hir_id(hir_id);
263 assert_eq!(self.tcx.hir_node(parent).body_id().unwrap().hir_id, hir_id, "{hir_id:?}");
264 parent
265 }
266
267 pub fn body_owner_def_id(self, BodyId { hir_id }: BodyId) -> LocalDefId {
268 self.tcx.parent_hir_node(hir_id).associated_body().unwrap().0
269 }
270
271 pub fn maybe_body_owned_by(self, id: LocalDefId) -> Option<&'hir Body<'hir>> {
274 Some(self.body(self.tcx.hir_node_by_def_id(id).body_id()?))
275 }
276
277 #[track_caller]
279 pub fn body_owned_by(self, id: LocalDefId) -> &'hir Body<'hir> {
280 self.maybe_body_owned_by(id).unwrap_or_else(|| {
281 let hir_id = self.tcx.local_def_id_to_hir_id(id);
282 span_bug!(
283 self.span(hir_id),
284 "body_owned_by: {} has no associated body",
285 self.node_to_string(hir_id)
286 );
287 })
288 }
289
290 pub fn body_param_names(self, id: BodyId) -> impl Iterator<Item = Ident> + 'hir {
291 self.body(id).params.iter().map(|arg| match arg.pat.kind {
292 PatKind::Binding(_, _, ident, _) => ident,
293 _ => Ident::empty(),
294 })
295 }
296
297 pub fn body_owner_kind(self, def_id: impl Into<DefId>) -> BodyOwnerKind {
301 let def_id = def_id.into();
302 match self.tcx.def_kind(def_id) {
303 DefKind::Const | DefKind::AssocConst | DefKind::AnonConst => {
304 BodyOwnerKind::Const { inline: false }
305 }
306 DefKind::InlineConst => BodyOwnerKind::Const { inline: true },
307 DefKind::Ctor(..) | DefKind::Fn | DefKind::AssocFn => BodyOwnerKind::Fn,
308 DefKind::Closure | DefKind::SyntheticCoroutineBody => BodyOwnerKind::Closure,
309 DefKind::Static { safety: _, mutability, nested: false } => {
310 BodyOwnerKind::Static(mutability)
311 }
312 dk => bug!("{:?} is not a body node: {:?}", def_id, dk),
313 }
314 }
315
316 pub fn body_const_context(self, def_id: impl Into<DefId>) -> Option<ConstContext> {
324 let def_id = def_id.into();
325 let ccx = match self.body_owner_kind(def_id) {
326 BodyOwnerKind::Const { inline } => ConstContext::Const { inline },
327 BodyOwnerKind::Static(mutability) => ConstContext::Static(mutability),
328
329 BodyOwnerKind::Fn if self.tcx.is_constructor(def_id) => return None,
330 BodyOwnerKind::Fn | BodyOwnerKind::Closure if self.tcx.is_const_fn(def_id) => {
331 ConstContext::ConstFn
332 }
333 BodyOwnerKind::Fn if self.tcx.is_const_default_method(def_id) => ConstContext::ConstFn,
334 BodyOwnerKind::Fn | BodyOwnerKind::Closure => return None,
335 };
336
337 Some(ccx)
338 }
339
340 #[inline]
344 pub fn body_owners(self) -> impl Iterator<Item = LocalDefId> + 'hir {
345 self.tcx.hir_crate_items(()).body_owners.iter().copied()
346 }
347
348 #[inline]
349 pub fn par_body_owners(self, f: impl Fn(LocalDefId) + DynSend + DynSync) {
350 par_for_each_in(&self.tcx.hir_crate_items(()).body_owners[..], |&def_id| f(def_id));
351 }
352
353 pub fn ty_param_owner(self, def_id: LocalDefId) -> LocalDefId {
354 let def_kind = self.tcx.def_kind(def_id);
355 match def_kind {
356 DefKind::Trait | DefKind::TraitAlias => def_id,
357 DefKind::LifetimeParam | DefKind::TyParam | DefKind::ConstParam => {
358 self.tcx.local_parent(def_id)
359 }
360 _ => bug!("ty_param_owner: {:?} is a {:?} not a type parameter", def_id, def_kind),
361 }
362 }
363
364 pub fn ty_param_name(self, def_id: LocalDefId) -> Symbol {
365 let def_kind = self.tcx.def_kind(def_id);
366 match def_kind {
367 DefKind::Trait | DefKind::TraitAlias => kw::SelfUpper,
368 DefKind::LifetimeParam | DefKind::TyParam | DefKind::ConstParam => {
369 self.tcx.item_name(def_id.to_def_id())
370 }
371 _ => bug!("ty_param_name: {:?} is a {:?} not a type parameter", def_id, def_kind),
372 }
373 }
374
375 pub fn trait_impls(self, trait_did: DefId) -> &'hir [LocalDefId] {
376 self.tcx.all_local_trait_impls(()).get(&trait_did).map_or(&[], |xs| &xs[..])
377 }
378
379 pub fn krate_attrs(self) -> &'hir [Attribute] {
383 self.attrs(CRATE_HIR_ID)
384 }
385
386 pub fn rustc_coherence_is_core(self) -> bool {
387 self.krate_attrs().iter().any(|attr| attr.has_name(sym::rustc_coherence_is_core))
388 }
389
390 pub fn get_module(self, module: LocalModDefId) -> (&'hir Mod<'hir>, Span, HirId) {
391 let hir_id = HirId::make_owner(module.to_local_def_id());
392 match self.tcx.hir_owner_node(hir_id.owner) {
393 OwnerNode::Item(&Item { span, kind: ItemKind::Mod(m), .. }) => (m, span, hir_id),
394 OwnerNode::Crate(item) => (item, item.spans.inner_span, hir_id),
395 node => panic!("not a module: {node:?}"),
396 }
397 }
398
399 pub fn walk_toplevel_module<V>(self, visitor: &mut V) -> V::Result
401 where
402 V: Visitor<'hir>,
403 {
404 let (top_mod, span, hir_id) = self.get_module(LocalModDefId::CRATE_DEF_ID);
405 visitor.visit_mod(top_mod, span, hir_id)
406 }
407
408 pub fn walk_attributes<V>(self, visitor: &mut V) -> V::Result
410 where
411 V: Visitor<'hir>,
412 {
413 let krate = self.krate();
414 for info in krate.owners.iter() {
415 if let MaybeOwner::Owner(info) = info {
416 for attrs in info.attrs.map.values() {
417 walk_list!(visitor, visit_attribute, *attrs);
418 }
419 }
420 }
421 V::Result::output()
422 }
423
424 pub fn visit_all_item_likes_in_crate<V>(self, visitor: &mut V) -> V::Result
435 where
436 V: Visitor<'hir>,
437 {
438 let krate = self.tcx.hir_crate_items(());
439 walk_list!(visitor, visit_item, krate.free_items().map(|id| self.item(id)));
440 walk_list!(visitor, visit_trait_item, krate.trait_items().map(|id| self.trait_item(id)));
441 walk_list!(visitor, visit_impl_item, krate.impl_items().map(|id| self.impl_item(id)));
442 walk_list!(
443 visitor,
444 visit_foreign_item,
445 krate.foreign_items().map(|id| self.foreign_item(id))
446 );
447 V::Result::output()
448 }
449
450 pub fn visit_item_likes_in_module<V>(self, module: LocalModDefId, visitor: &mut V) -> V::Result
453 where
454 V: Visitor<'hir>,
455 {
456 let module = self.tcx.hir_module_items(module);
457 walk_list!(visitor, visit_item, module.free_items().map(|id| self.item(id)));
458 walk_list!(visitor, visit_trait_item, module.trait_items().map(|id| self.trait_item(id)));
459 walk_list!(visitor, visit_impl_item, module.impl_items().map(|id| self.impl_item(id)));
460 walk_list!(
461 visitor,
462 visit_foreign_item,
463 module.foreign_items().map(|id| self.foreign_item(id))
464 );
465 V::Result::output()
466 }
467
468 pub fn for_each_module(self, mut f: impl FnMut(LocalModDefId)) {
469 let crate_items = self.tcx.hir_crate_items(());
470 for module in crate_items.submodules.iter() {
471 f(LocalModDefId::new_unchecked(module.def_id))
472 }
473 }
474
475 #[inline]
476 pub fn par_for_each_module(self, f: impl Fn(LocalModDefId) + DynSend + DynSync) {
477 let crate_items = self.tcx.hir_crate_items(());
478 par_for_each_in(&crate_items.submodules[..], |module| {
479 f(LocalModDefId::new_unchecked(module.def_id))
480 })
481 }
482
483 #[inline]
484 pub fn try_par_for_each_module(
485 self,
486 f: impl Fn(LocalModDefId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
487 ) -> Result<(), ErrorGuaranteed> {
488 let crate_items = self.tcx.hir_crate_items(());
489 try_par_for_each_in(&crate_items.submodules[..], |module| {
490 f(LocalModDefId::new_unchecked(module.def_id))
491 })
492 }
493
494 #[inline]
497 pub fn parent_id_iter(self, current_id: HirId) -> impl Iterator<Item = HirId> + 'hir {
498 ParentHirIterator::new(self, current_id)
499 }
500
501 #[inline]
504 pub fn parent_iter(self, current_id: HirId) -> impl Iterator<Item = (HirId, Node<'hir>)> {
505 self.parent_id_iter(current_id).map(move |id| (id, self.tcx.hir_node(id)))
506 }
507
508 #[inline]
511 pub fn parent_owner_iter(self, current_id: HirId) -> ParentOwnerIterator<'hir> {
512 ParentOwnerIterator { current_id, map: self }
513 }
514
515 pub fn is_lhs(self, id: HirId) -> bool {
517 match self.tcx.parent_hir_node(id) {
518 Node::Expr(expr) => match expr.kind {
519 ExprKind::Assign(lhs, _rhs, _span) => lhs.hir_id == id,
520 _ => false,
521 },
522 _ => false,
523 }
524 }
525
526 pub fn is_inside_const_context(self, hir_id: HirId) -> bool {
529 self.body_const_context(self.enclosing_body_owner(hir_id)).is_some()
530 }
531
532 pub fn get_fn_id_for_return_block(self, id: HirId) -> Option<HirId> {
555 let enclosing_body_owner = self.tcx.local_def_id_to_hir_id(self.enclosing_body_owner(id));
556
557 let mut iter = [id].into_iter().chain(self.parent_id_iter(id)).peekable();
559 while let Some(cur_id) = iter.next() {
560 if enclosing_body_owner == cur_id {
561 break;
562 }
563
564 if let Node::Expr(Expr { kind: ExprKind::Ret(_), .. }) = self.tcx.hir_node(cur_id) {
567 break;
568 }
569
570 if let Some(&parent_id) = iter.peek() {
572 match self.tcx.hir_node(parent_id) {
573 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, .. })
583 | Node::Expr(Expr { kind: ExprKind::Loop(..), .. })
584 | Node::LetStmt(..) => return None,
585
586 _ => {}
587 }
588 }
589 }
590
591 Some(enclosing_body_owner)
592 }
593
594 pub fn get_parent_item(self, hir_id: HirId) -> OwnerId {
599 if hir_id.local_id != ItemLocalId::ZERO {
600 hir_id.owner
602 } else if let Some((def_id, _node)) = self.parent_owner_iter(hir_id).next() {
603 def_id
604 } else {
605 CRATE_OWNER_ID
606 }
607 }
608
609 pub fn get_if_cause(self, hir_id: HirId) -> Option<&'hir Expr<'hir>> {
615 for (_, node) in self.parent_iter(hir_id) {
616 match node {
617 Node::Item(_)
618 | Node::ForeignItem(_)
619 | Node::TraitItem(_)
620 | Node::ImplItem(_)
621 | Node::Stmt(Stmt { kind: StmtKind::Let(_), .. }) => break,
622 Node::Expr(expr @ Expr { kind: ExprKind::If(..) | ExprKind::Match(..), .. }) => {
623 return Some(expr);
624 }
625 _ => {}
626 }
627 }
628 None
629 }
630
631 pub fn get_enclosing_scope(self, hir_id: HirId) -> Option<HirId> {
633 for (hir_id, node) in self.parent_iter(hir_id) {
634 if let Node::Item(Item {
635 kind:
636 ItemKind::Fn { .. }
637 | ItemKind::Const(..)
638 | ItemKind::Static(..)
639 | ItemKind::Mod(..)
640 | ItemKind::Enum(..)
641 | ItemKind::Struct(..)
642 | ItemKind::Union(..)
643 | ItemKind::Trait(..)
644 | ItemKind::Impl { .. },
645 ..
646 })
647 | Node::ForeignItem(ForeignItem { kind: ForeignItemKind::Fn(..), .. })
648 | Node::TraitItem(TraitItem { kind: TraitItemKind::Fn(..), .. })
649 | Node::ImplItem(ImplItem { kind: ImplItemKind::Fn(..), .. })
650 | Node::Block(_) = node
651 {
652 return Some(hir_id);
653 }
654 }
655 None
656 }
657
658 pub fn get_defining_scope(self, id: HirId) -> HirId {
660 let mut scope = id;
661 loop {
662 scope = self.get_enclosing_scope(scope).unwrap_or(CRATE_HIR_ID);
663 if scope == CRATE_HIR_ID || !matches!(self.tcx.hir_node(scope), Node::Block(_)) {
664 return scope;
665 }
666 }
667 }
668
669 pub fn get_foreign_abi(self, hir_id: HirId) -> ExternAbi {
670 let parent = self.get_parent_item(hir_id);
671 if let OwnerNode::Item(Item { kind: ItemKind::ForeignMod { abi, .. }, .. }) =
672 self.tcx.hir_owner_node(parent)
673 {
674 return *abi;
675 }
676 bug!(
677 "expected foreign mod or inlined parent, found {}",
678 self.node_to_string(HirId::make_owner(parent.def_id))
679 )
680 }
681
682 pub fn expect_item(self, id: LocalDefId) -> &'hir Item<'hir> {
683 match self.tcx.expect_hir_owner_node(id) {
684 OwnerNode::Item(item) => item,
685 _ => bug!("expected item, found {}", self.node_to_string(HirId::make_owner(id))),
686 }
687 }
688
689 pub fn expect_impl_item(self, id: LocalDefId) -> &'hir ImplItem<'hir> {
690 match self.tcx.expect_hir_owner_node(id) {
691 OwnerNode::ImplItem(item) => item,
692 _ => bug!("expected impl item, found {}", self.node_to_string(HirId::make_owner(id))),
693 }
694 }
695
696 pub fn expect_trait_item(self, id: LocalDefId) -> &'hir TraitItem<'hir> {
697 match self.tcx.expect_hir_owner_node(id) {
698 OwnerNode::TraitItem(item) => item,
699 _ => bug!("expected trait item, found {}", self.node_to_string(HirId::make_owner(id))),
700 }
701 }
702
703 pub fn get_fn_output(self, def_id: LocalDefId) -> Option<&'hir FnRetTy<'hir>> {
704 Some(&self.tcx.opt_hir_owner_node(def_id)?.fn_decl()?.output)
705 }
706
707 pub fn expect_variant(self, id: HirId) -> &'hir Variant<'hir> {
708 match self.tcx.hir_node(id) {
709 Node::Variant(variant) => variant,
710 _ => bug!("expected variant, found {}", self.node_to_string(id)),
711 }
712 }
713
714 pub fn expect_field(self, id: HirId) -> &'hir FieldDef<'hir> {
715 match self.tcx.hir_node(id) {
716 Node::Field(field) => field,
717 _ => bug!("expected field, found {}", self.node_to_string(id)),
718 }
719 }
720
721 pub fn expect_foreign_item(self, id: OwnerId) -> &'hir ForeignItem<'hir> {
722 match self.tcx.hir_owner_node(id) {
723 OwnerNode::ForeignItem(item) => item,
724 _ => {
725 bug!(
726 "expected foreign item, found {}",
727 self.node_to_string(HirId::make_owner(id.def_id))
728 )
729 }
730 }
731 }
732
733 #[track_caller]
734 pub fn expect_opaque_ty(self, id: LocalDefId) -> &'hir OpaqueTy<'hir> {
735 match self.tcx.hir_node_by_def_id(id) {
736 Node::OpaqueTy(opaq) => opaq,
737 _ => {
738 bug!(
739 "expected opaque type definition, found {}",
740 self.node_to_string(self.tcx.local_def_id_to_hir_id(id))
741 )
742 }
743 }
744 }
745
746 pub fn expect_expr(self, id: HirId) -> &'hir Expr<'hir> {
747 match self.tcx.hir_node(id) {
748 Node::Expr(expr) => expr,
749 _ => bug!("expected expr, found {}", self.node_to_string(id)),
750 }
751 }
752
753 pub fn opt_delegation_sig_id(self, def_id: LocalDefId) -> Option<DefId> {
754 self.tcx.opt_hir_owner_node(def_id)?.fn_decl()?.opt_delegation_sig_id()
755 }
756
757 #[inline]
758 fn opt_ident(self, id: HirId) -> Option<Ident> {
759 match self.tcx.hir_node(id) {
760 Node::Pat(&Pat { kind: PatKind::Binding(_, _, ident, _), .. }) => Some(ident),
761 Node::Ctor(..) => match self.tcx.parent_hir_node(id) {
764 Node::Item(item) => Some(item.ident),
765 Node::Variant(variant) => Some(variant.ident),
766 _ => unreachable!(),
767 },
768 node => node.ident(),
769 }
770 }
771
772 #[inline]
773 pub(super) fn opt_ident_span(self, id: HirId) -> Option<Span> {
774 self.opt_ident(id).map(|ident| ident.span)
775 }
776
777 #[inline]
778 pub fn ident(self, id: HirId) -> Ident {
779 self.opt_ident(id).unwrap()
780 }
781
782 #[inline]
783 pub fn opt_name(self, id: HirId) -> Option<Symbol> {
784 self.opt_ident(id).map(|ident| ident.name)
785 }
786
787 pub fn name(self, id: HirId) -> Symbol {
788 self.opt_name(id).unwrap_or_else(|| bug!("no name for {}", self.node_to_string(id)))
789 }
790
791 pub fn attrs(self, id: HirId) -> &'hir [Attribute] {
794 self.tcx.hir_attrs(id.owner).get(id.local_id)
795 }
796
797 pub fn span(self, hir_id: HirId) -> Span {
800 fn until_within(outer: Span, end: Span) -> Span {
801 if let Some(end) = end.find_ancestor_inside(outer) {
802 outer.with_hi(end.hi())
803 } else {
804 outer
805 }
806 }
807
808 fn named_span(item_span: Span, ident: Ident, generics: Option<&Generics<'_>>) -> Span {
809 if ident.name != kw::Empty {
810 let mut span = until_within(item_span, ident.span);
811 if let Some(g) = generics
812 && !g.span.is_dummy()
813 && let Some(g_span) = g.span.find_ancestor_inside(item_span)
814 {
815 span = span.to(g_span);
816 }
817 span
818 } else {
819 item_span
820 }
821 }
822
823 let span = match self.tcx.hir_node(hir_id) {
824 Node::Item(Item { kind: ItemKind::Fn { sig, .. }, span: outer_span, .. })
826 | Node::TraitItem(TraitItem {
827 kind: TraitItemKind::Fn(sig, ..),
828 span: outer_span,
829 ..
830 })
831 | Node::ImplItem(ImplItem {
832 kind: ImplItemKind::Fn(sig, ..), span: outer_span, ..
833 })
834 | Node::ForeignItem(ForeignItem {
835 kind: ForeignItemKind::Fn(sig, ..),
836 span: outer_span,
837 ..
838 }) => {
839 sig.span.find_ancestor_in_same_ctxt(*outer_span).unwrap_or(*outer_span)
842 }
843 Node::Item(Item {
845 kind: ItemKind::Impl(Impl { generics, .. }),
846 span: outer_span,
847 ..
848 }) => until_within(*outer_span, generics.where_clause_span),
849 Node::Item(Item {
851 kind: ItemKind::Const(ty, ..) | ItemKind::Static(ty, ..),
852 span: outer_span,
853 ..
854 })
855 | Node::TraitItem(TraitItem {
856 kind: TraitItemKind::Const(ty, ..),
857 span: outer_span,
858 ..
859 })
860 | Node::ImplItem(ImplItem {
861 kind: ImplItemKind::Const(ty, ..),
862 span: outer_span,
863 ..
864 })
865 | Node::ForeignItem(ForeignItem {
866 kind: ForeignItemKind::Static(ty, ..),
867 span: outer_span,
868 ..
869 }) => until_within(*outer_span, ty.span),
870 Node::Item(Item {
872 kind: ItemKind::Trait(_, _, generics, bounds, _),
873 span: outer_span,
874 ..
875 })
876 | Node::TraitItem(TraitItem {
877 kind: TraitItemKind::Type(bounds, _),
878 generics,
879 span: outer_span,
880 ..
881 }) => {
882 let end = if let Some(b) = bounds.last() { b.span() } else { generics.span };
883 until_within(*outer_span, end)
884 }
885 Node::Item(item) => match &item.kind {
887 ItemKind::Use(path, _) => {
888 path.span.find_ancestor_in_same_ctxt(item.span).unwrap_or(item.span)
891 }
892 _ => named_span(item.span, item.ident, item.kind.generics()),
893 },
894 Node::Variant(variant) => named_span(variant.span, variant.ident, None),
895 Node::ImplItem(item) => named_span(item.span, item.ident, Some(item.generics)),
896 Node::ForeignItem(item) => named_span(item.span, item.ident, None),
897 Node::Ctor(_) => return self.span(self.tcx.parent_hir_id(hir_id)),
898 Node::Expr(Expr {
899 kind: ExprKind::Closure(Closure { fn_decl_span, .. }),
900 span,
901 ..
902 }) => {
903 fn_decl_span.find_ancestor_inside(*span).unwrap_or(*span)
905 }
906 _ => self.span_with_body(hir_id),
907 };
908 debug_assert_eq!(span.ctxt(), self.span_with_body(hir_id).ctxt());
909 span
910 }
911
912 pub fn span_with_body(self, hir_id: HirId) -> Span {
915 match self.tcx.hir_node(hir_id) {
916 Node::Param(param) => param.span,
917 Node::Item(item) => item.span,
918 Node::ForeignItem(foreign_item) => foreign_item.span,
919 Node::TraitItem(trait_item) => trait_item.span,
920 Node::ImplItem(impl_item) => impl_item.span,
921 Node::Variant(variant) => variant.span,
922 Node::Field(field) => field.span,
923 Node::AnonConst(constant) => constant.span,
924 Node::ConstBlock(constant) => self.body(constant.body).value.span,
925 Node::ConstArg(const_arg) => const_arg.span(),
926 Node::Expr(expr) => expr.span,
927 Node::ExprField(field) => field.span,
928 Node::Stmt(stmt) => stmt.span,
929 Node::PathSegment(seg) => {
930 let ident_span = seg.ident.span;
931 ident_span
932 .with_hi(seg.args.map_or_else(|| ident_span.hi(), |args| args.span_ext.hi()))
933 }
934 Node::Ty(ty) => ty.span,
935 Node::AssocItemConstraint(constraint) => constraint.span,
936 Node::TraitRef(tr) => tr.path.span,
937 Node::OpaqueTy(op) => op.span,
938 Node::Pat(pat) => pat.span,
939 Node::TyPat(pat) => pat.span,
940 Node::PatField(field) => field.span,
941 Node::PatExpr(lit) => lit.span,
942 Node::Arm(arm) => arm.span,
943 Node::Block(block) => block.span,
944 Node::Ctor(..) => self.span_with_body(self.tcx.parent_hir_id(hir_id)),
945 Node::Lifetime(lifetime) => lifetime.ident.span,
946 Node::GenericParam(param) => param.span,
947 Node::Infer(i) => i.span,
948 Node::LetStmt(local) => local.span,
949 Node::Crate(item) => item.spans.inner_span,
950 Node::WherePredicate(pred) => pred.span,
951 Node::PreciseCapturingNonLifetimeArg(param) => param.ident.span,
952 Node::Synthetic => unreachable!(),
953 Node::Err(span) => span,
954 }
955 }
956
957 pub fn span_if_local(self, id: DefId) -> Option<Span> {
958 id.is_local().then(|| self.tcx.def_span(id))
959 }
960
961 pub fn res_span(self, res: Res) -> Option<Span> {
962 match res {
963 Res::Err => None,
964 Res::Local(id) => Some(self.span(id)),
965 res => self.span_if_local(res.opt_def_id()?),
966 }
967 }
968
969 pub fn node_to_string(self, id: HirId) -> String {
972 hir_id_to_string(self, id)
973 }
974
975 pub fn opt_const_param_default_param_def_id(self, anon_const: HirId) -> Option<LocalDefId> {
978 let const_arg = self.tcx.parent_hir_id(anon_const);
979 match self.tcx.parent_hir_node(const_arg) {
980 Node::GenericParam(GenericParam {
981 def_id: param_id,
982 kind: GenericParamKind::Const { .. },
983 ..
984 }) => Some(*param_id),
985 _ => None,
986 }
987 }
988
989 pub fn maybe_get_struct_pattern_shorthand_field(&self, expr: &Expr<'_>) -> Option<Symbol> {
990 let local = match expr {
991 Expr {
992 kind:
993 ExprKind::Path(QPath::Resolved(
994 None,
995 Path {
996 res: def::Res::Local(_), segments: [PathSegment { ident, .. }], ..
997 },
998 )),
999 ..
1000 } => Some(ident),
1001 _ => None,
1002 }?;
1003
1004 match self.tcx.parent_hir_node(expr.hir_id) {
1005 Node::ExprField(field) => {
1006 if field.ident.name == local.name && field.is_shorthand {
1007 return Some(local.name);
1008 }
1009 }
1010 _ => {}
1011 }
1012
1013 None
1014 }
1015}
1016
1017impl<'hir> intravisit::Map<'hir> for Map<'hir> {
1018 fn hir_node(&self, hir_id: HirId) -> Node<'hir> {
1019 self.tcx.hir_node(hir_id)
1020 }
1021
1022 fn hir_node_by_def_id(&self, def_id: LocalDefId) -> Node<'hir> {
1023 self.tcx.hir_node_by_def_id(def_id)
1024 }
1025
1026 fn body(&self, id: BodyId) -> &'hir Body<'hir> {
1027 (*self).body(id)
1028 }
1029
1030 fn item(&self, id: ItemId) -> &'hir Item<'hir> {
1031 (*self).item(id)
1032 }
1033
1034 fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir> {
1035 (*self).trait_item(id)
1036 }
1037
1038 fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir> {
1039 (*self).impl_item(id)
1040 }
1041
1042 fn foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir> {
1043 (*self).foreign_item(id)
1044 }
1045}
1046
1047impl<'tcx> pprust_hir::PpAnn for TyCtxt<'tcx> {
1048 fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
1049 pprust_hir::PpAnn::nested(&(&self.hir() as &dyn intravisit::Map<'_>), state, nested)
1050 }
1051}
1052
1053pub(super) fn crate_hash(tcx: TyCtxt<'_>, _: LocalCrate) -> Svh {
1054 let krate = tcx.hir_crate(());
1055 let hir_body_hash = krate.opt_hir_hash.expect("HIR hash missing while computing crate hash");
1056
1057 let upstream_crates = upstream_crates(tcx);
1058
1059 let resolutions = tcx.resolutions(());
1060
1061 let mut source_file_names: Vec<_> = tcx
1067 .sess
1068 .source_map()
1069 .files()
1070 .iter()
1071 .filter(|source_file| source_file.cnum == LOCAL_CRATE)
1072 .map(|source_file| source_file.stable_id)
1073 .collect();
1074
1075 source_file_names.sort_unstable();
1076
1077 let debugger_visualizers: Vec<_> = tcx
1083 .debugger_visualizers(LOCAL_CRATE)
1084 .iter()
1085 .map(DebuggerVisualizerFile::path_erased)
1089 .collect();
1090
1091 let crate_hash: Fingerprint = tcx.with_stable_hashing_context(|mut hcx| {
1092 let mut stable_hasher = StableHasher::new();
1093 hir_body_hash.hash_stable(&mut hcx, &mut stable_hasher);
1094 upstream_crates.hash_stable(&mut hcx, &mut stable_hasher);
1095 source_file_names.hash_stable(&mut hcx, &mut stable_hasher);
1096 debugger_visualizers.hash_stable(&mut hcx, &mut stable_hasher);
1097 if tcx.sess.opts.incremental.is_some() {
1098 let definitions = tcx.untracked().definitions.freeze();
1099 let mut owner_spans: Vec<_> = krate
1100 .owners
1101 .iter_enumerated()
1102 .filter_map(|(def_id, info)| {
1103 let _ = info.as_owner()?;
1104 let def_path_hash = definitions.def_path_hash(def_id);
1105 let span = tcx.source_span(def_id);
1106 debug_assert_eq!(span.parent(), None);
1107 Some((def_path_hash, span))
1108 })
1109 .collect();
1110 owner_spans.sort_unstable_by_key(|bn| bn.0);
1111 owner_spans.hash_stable(&mut hcx, &mut stable_hasher);
1112 }
1113 tcx.sess.opts.dep_tracking_hash(true).hash_stable(&mut hcx, &mut stable_hasher);
1114 tcx.stable_crate_id(LOCAL_CRATE).hash_stable(&mut hcx, &mut stable_hasher);
1115 resolutions.visibilities_for_hashing.hash_stable(&mut hcx, &mut stable_hasher);
1120 with_metavar_spans(|mspans| {
1121 mspans.freeze_and_get_read_spans().hash_stable(&mut hcx, &mut stable_hasher);
1122 });
1123 stable_hasher.finish()
1124 });
1125
1126 Svh::new(crate_hash)
1127}
1128
1129fn upstream_crates(tcx: TyCtxt<'_>) -> Vec<(StableCrateId, Svh)> {
1130 let mut upstream_crates: Vec<_> = tcx
1131 .crates(())
1132 .iter()
1133 .map(|&cnum| {
1134 let stable_crate_id = tcx.stable_crate_id(cnum);
1135 let hash = tcx.crate_hash(cnum);
1136 (stable_crate_id, hash)
1137 })
1138 .collect();
1139 upstream_crates.sort_unstable_by_key(|&(stable_crate_id, _)| stable_crate_id);
1140 upstream_crates
1141}
1142
1143fn hir_id_to_string(map: Map<'_>, id: HirId) -> String {
1144 let path_str = |def_id: LocalDefId| map.tcx.def_path_str(def_id);
1145
1146 let span_str = || map.tcx.sess.source_map().span_to_snippet(map.span(id)).unwrap_or_default();
1147 let node_str = |prefix| format!("{id} ({prefix} `{}`)", span_str());
1148
1149 match map.tcx.hir_node(id) {
1150 Node::Item(item) => {
1151 let item_str = match item.kind {
1152 ItemKind::ExternCrate(..) => "extern crate",
1153 ItemKind::Use(..) => "use",
1154 ItemKind::Static(..) => "static",
1155 ItemKind::Const(..) => "const",
1156 ItemKind::Fn { .. } => "fn",
1157 ItemKind::Macro(..) => "macro",
1158 ItemKind::Mod(..) => "mod",
1159 ItemKind::ForeignMod { .. } => "foreign mod",
1160 ItemKind::GlobalAsm(..) => "global asm",
1161 ItemKind::TyAlias(..) => "ty",
1162 ItemKind::Enum(..) => "enum",
1163 ItemKind::Struct(..) => "struct",
1164 ItemKind::Union(..) => "union",
1165 ItemKind::Trait(..) => "trait",
1166 ItemKind::TraitAlias(..) => "trait alias",
1167 ItemKind::Impl { .. } => "impl",
1168 };
1169 format!("{id} ({item_str} {})", path_str(item.owner_id.def_id))
1170 }
1171 Node::ForeignItem(item) => {
1172 format!("{id} (foreign item {})", path_str(item.owner_id.def_id))
1173 }
1174 Node::ImplItem(ii) => {
1175 let kind = match ii.kind {
1176 ImplItemKind::Const(..) => "associated constant",
1177 ImplItemKind::Fn(fn_sig, _) => match fn_sig.decl.implicit_self {
1178 ImplicitSelfKind::None => "associated function",
1179 _ => "method",
1180 },
1181 ImplItemKind::Type(_) => "associated type",
1182 };
1183 format!("{id} ({kind} `{}` in {})", ii.ident, path_str(ii.owner_id.def_id))
1184 }
1185 Node::TraitItem(ti) => {
1186 let kind = match ti.kind {
1187 TraitItemKind::Const(..) => "associated constant",
1188 TraitItemKind::Fn(fn_sig, _) => match fn_sig.decl.implicit_self {
1189 ImplicitSelfKind::None => "associated function",
1190 _ => "trait method",
1191 },
1192 TraitItemKind::Type(..) => "associated type",
1193 };
1194
1195 format!("{id} ({kind} `{}` in {})", ti.ident, path_str(ti.owner_id.def_id))
1196 }
1197 Node::Variant(variant) => {
1198 format!("{id} (variant `{}` in {})", variant.ident, path_str(variant.def_id))
1199 }
1200 Node::Field(field) => {
1201 format!("{id} (field `{}` in {})", field.ident, path_str(field.def_id))
1202 }
1203 Node::AnonConst(_) => node_str("const"),
1204 Node::ConstBlock(_) => node_str("const"),
1205 Node::ConstArg(_) => node_str("const"),
1206 Node::Expr(_) => node_str("expr"),
1207 Node::ExprField(_) => node_str("expr field"),
1208 Node::Stmt(_) => node_str("stmt"),
1209 Node::PathSegment(_) => node_str("path segment"),
1210 Node::Ty(_) => node_str("type"),
1211 Node::AssocItemConstraint(_) => node_str("assoc item constraint"),
1212 Node::TraitRef(_) => node_str("trait ref"),
1213 Node::OpaqueTy(_) => node_str("opaque type"),
1214 Node::Pat(_) => node_str("pat"),
1215 Node::TyPat(_) => node_str("pat ty"),
1216 Node::PatField(_) => node_str("pattern field"),
1217 Node::PatExpr(_) => node_str("pattern literal"),
1218 Node::Param(_) => node_str("param"),
1219 Node::Arm(_) => node_str("arm"),
1220 Node::Block(_) => node_str("block"),
1221 Node::Infer(_) => node_str("infer"),
1222 Node::LetStmt(_) => node_str("local"),
1223 Node::Ctor(ctor) => format!(
1224 "{id} (ctor {})",
1225 ctor.ctor_def_id().map_or("<missing path>".into(), |def_id| path_str(def_id)),
1226 ),
1227 Node::Lifetime(_) => node_str("lifetime"),
1228 Node::GenericParam(param) => {
1229 format!("{id} (generic_param {})", path_str(param.def_id))
1230 }
1231 Node::Crate(..) => String::from("(root_crate)"),
1232 Node::WherePredicate(_) => node_str("where predicate"),
1233 Node::Synthetic => unreachable!(),
1234 Node::Err(_) => node_str("error"),
1235 Node::PreciseCapturingNonLifetimeArg(_param) => node_str("parameter"),
1236 }
1237}
1238
1239pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalModDefId) -> ModuleItems {
1240 let mut collector = ItemCollector::new(tcx, false);
1241
1242 let (hir_mod, span, hir_id) = tcx.hir().get_module(module_id);
1243 collector.visit_mod(hir_mod, span, hir_id);
1244
1245 let ItemCollector {
1246 submodules,
1247 items,
1248 trait_items,
1249 impl_items,
1250 foreign_items,
1251 body_owners,
1252 opaques,
1253 nested_bodies,
1254 ..
1255 } = collector;
1256 ModuleItems {
1257 submodules: submodules.into_boxed_slice(),
1258 free_items: items.into_boxed_slice(),
1259 trait_items: trait_items.into_boxed_slice(),
1260 impl_items: impl_items.into_boxed_slice(),
1261 foreign_items: foreign_items.into_boxed_slice(),
1262 body_owners: body_owners.into_boxed_slice(),
1263 opaques: opaques.into_boxed_slice(),
1264 nested_bodies: nested_bodies.into_boxed_slice(),
1265 }
1266}
1267
1268pub(crate) fn hir_crate_items(tcx: TyCtxt<'_>, _: ()) -> ModuleItems {
1269 let mut collector = ItemCollector::new(tcx, true);
1270
1271 collector.submodules.push(CRATE_OWNER_ID);
1275 tcx.hir().walk_toplevel_module(&mut collector);
1276
1277 let ItemCollector {
1278 submodules,
1279 items,
1280 trait_items,
1281 impl_items,
1282 foreign_items,
1283 body_owners,
1284 opaques,
1285 nested_bodies,
1286 ..
1287 } = collector;
1288
1289 ModuleItems {
1290 submodules: submodules.into_boxed_slice(),
1291 free_items: items.into_boxed_slice(),
1292 trait_items: trait_items.into_boxed_slice(),
1293 impl_items: impl_items.into_boxed_slice(),
1294 foreign_items: foreign_items.into_boxed_slice(),
1295 body_owners: body_owners.into_boxed_slice(),
1296 opaques: opaques.into_boxed_slice(),
1297 nested_bodies: nested_bodies.into_boxed_slice(),
1298 }
1299}
1300
1301struct ItemCollector<'tcx> {
1302 crate_collector: bool,
1305 tcx: TyCtxt<'tcx>,
1306 submodules: Vec<OwnerId>,
1307 items: Vec<ItemId>,
1308 trait_items: Vec<TraitItemId>,
1309 impl_items: Vec<ImplItemId>,
1310 foreign_items: Vec<ForeignItemId>,
1311 body_owners: Vec<LocalDefId>,
1312 opaques: Vec<LocalDefId>,
1313 nested_bodies: Vec<LocalDefId>,
1314}
1315
1316impl<'tcx> ItemCollector<'tcx> {
1317 fn new(tcx: TyCtxt<'tcx>, crate_collector: bool) -> ItemCollector<'tcx> {
1318 ItemCollector {
1319 crate_collector,
1320 tcx,
1321 submodules: Vec::default(),
1322 items: Vec::default(),
1323 trait_items: Vec::default(),
1324 impl_items: Vec::default(),
1325 foreign_items: Vec::default(),
1326 body_owners: Vec::default(),
1327 opaques: Vec::default(),
1328 nested_bodies: Vec::default(),
1329 }
1330 }
1331}
1332
1333impl<'hir> Visitor<'hir> for ItemCollector<'hir> {
1334 type NestedFilter = nested_filter::All;
1335
1336 fn nested_visit_map(&mut self) -> Self::Map {
1337 self.tcx.hir()
1338 }
1339
1340 fn visit_item(&mut self, item: &'hir Item<'hir>) {
1341 if Node::Item(item).associated_body().is_some() {
1342 self.body_owners.push(item.owner_id.def_id);
1343 }
1344
1345 self.items.push(item.item_id());
1346
1347 if let ItemKind::Mod(module) = &item.kind {
1349 self.submodules.push(item.owner_id);
1350 if self.crate_collector {
1352 intravisit::walk_mod(self, module, item.hir_id());
1353 }
1354 } else {
1355 intravisit::walk_item(self, item)
1356 }
1357 }
1358
1359 fn visit_foreign_item(&mut self, item: &'hir ForeignItem<'hir>) {
1360 self.foreign_items.push(item.foreign_item_id());
1361 intravisit::walk_foreign_item(self, item)
1362 }
1363
1364 fn visit_anon_const(&mut self, c: &'hir AnonConst) {
1365 self.body_owners.push(c.def_id);
1366 intravisit::walk_anon_const(self, c)
1367 }
1368
1369 fn visit_inline_const(&mut self, c: &'hir ConstBlock) {
1370 self.body_owners.push(c.def_id);
1371 self.nested_bodies.push(c.def_id);
1372 intravisit::walk_inline_const(self, c)
1373 }
1374
1375 fn visit_opaque_ty(&mut self, o: &'hir OpaqueTy<'hir>) {
1376 self.opaques.push(o.def_id);
1377 intravisit::walk_opaque_ty(self, o)
1378 }
1379
1380 fn visit_expr(&mut self, ex: &'hir Expr<'hir>) {
1381 if let ExprKind::Closure(closure) = ex.kind {
1382 self.body_owners.push(closure.def_id);
1383 self.nested_bodies.push(closure.def_id);
1384 }
1385 intravisit::walk_expr(self, ex)
1386 }
1387
1388 fn visit_trait_item(&mut self, item: &'hir TraitItem<'hir>) {
1389 if Node::TraitItem(item).associated_body().is_some() {
1390 self.body_owners.push(item.owner_id.def_id);
1391 }
1392
1393 self.trait_items.push(item.trait_item_id());
1394 intravisit::walk_trait_item(self, item)
1395 }
1396
1397 fn visit_impl_item(&mut self, item: &'hir ImplItem<'hir>) {
1398 if Node::ImplItem(item).associated_body().is_some() {
1399 self.body_owners.push(item.owner_id.def_id);
1400 }
1401
1402 self.impl_items.push(item.impl_item_id());
1403 intravisit::walk_impl_item(self, item)
1404 }
1405}