2025-01-11 19:12:36 +00:00
|
|
|
use intravisit::InferKind;
|
2021-10-21 21:08:57 +00:00
|
|
|
use rustc_data_structures::sorted_map::SortedMap;
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir as hir;
|
2023-12-18 20:02:21 +00:00
|
|
|
use rustc_hir::def_id::{LocalDefId, LocalDefIdMap};
|
2023-11-10 02:11:24 +00:00
|
|
|
use rustc_hir::intravisit::Visitor;
|
2020-01-07 13:38:27 +00:00
|
|
|
use rustc_hir::*;
|
2024-04-03 14:49:59 +00:00
|
|
|
use rustc_index::IndexVec;
|
2021-07-05 20:26:23 +00:00
|
|
|
use rustc_middle::span_bug;
|
2023-08-31 21:14:47 +00:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2021-02-28 17:58:50 +00:00
|
|
|
use rustc_span::{DUMMY_SP, Span};
|
2024-04-29 06:24:06 +00:00
|
|
|
use tracing::{debug, instrument};
|
2015-09-10 19:53:08 +00:00
|
|
|
|
2019-02-28 22:43:53 +00:00
|
|
|
/// A visitor that walks over the HIR and collects `Node`s into a HIR map.
|
2023-11-25 19:46:10 +00:00
|
|
|
struct NodeCollector<'a, 'hir> {
|
2023-08-31 21:14:47 +00:00
|
|
|
tcx: TyCtxt<'hir>,
|
|
|
|
|
2021-10-21 21:08:57 +00:00
|
|
|
bodies: &'a SortedMap<ItemLocalId, &'hir Body<'hir>>,
|
2018-11-21 18:35:14 +00:00
|
|
|
|
2021-09-12 01:19:18 +00:00
|
|
|
/// Outputs
|
2024-01-20 12:21:27 +00:00
|
|
|
nodes: IndexVec<ItemLocalId, ParentedNode<'hir>>,
|
2023-12-18 20:02:21 +00:00
|
|
|
parenting: LocalDefIdMap<ItemLocalId>,
|
2020-02-07 10:45:04 +00:00
|
|
|
|
2015-12-21 21:24:15 +00:00
|
|
|
/// The parent of this node
|
2024-04-04 11:43:49 +00:00
|
|
|
parent_node: ItemLocalId,
|
2019-02-03 08:14:31 +00:00
|
|
|
|
2022-09-20 05:11:23 +00:00
|
|
|
owner: OwnerId,
|
2018-12-22 11:40:23 +00:00
|
|
|
}
|
|
|
|
|
2023-08-31 21:14:47 +00:00
|
|
|
#[instrument(level = "debug", skip(tcx, bodies))]
|
2021-09-12 01:19:18 +00:00
|
|
|
pub(super) fn index_hir<'hir>(
|
2023-08-31 21:14:47 +00:00
|
|
|
tcx: TyCtxt<'hir>,
|
2021-09-12 01:19:18 +00:00
|
|
|
item: hir::OwnerNode<'hir>,
|
2021-10-21 21:08:57 +00:00
|
|
|
bodies: &SortedMap<ItemLocalId, &'hir Body<'hir>>,
|
2024-01-20 12:21:27 +00:00
|
|
|
num_nodes: usize,
|
|
|
|
) -> (IndexVec<ItemLocalId, ParentedNode<'hir>>, LocalDefIdMap<ItemLocalId>) {
|
2024-04-04 11:43:49 +00:00
|
|
|
let err_node = ParentedNode { parent: ItemLocalId::ZERO, node: Node::Err(item.span()) };
|
2024-01-20 12:21:27 +00:00
|
|
|
let mut nodes = IndexVec::from_elem_n(err_node, num_nodes);
|
2021-10-12 06:34:38 +00:00
|
|
|
// This node's parent should never be accessed: the owner's parent is computed by the
|
2022-11-16 20:34:16 +00:00
|
|
|
// hir_owner_parent query. Make it invalid (= ItemLocalId::MAX) to force an ICE whenever it is
|
2021-10-12 06:34:38 +00:00
|
|
|
// used.
|
2024-04-04 11:43:49 +00:00
|
|
|
nodes[ItemLocalId::ZERO] = ParentedNode { parent: ItemLocalId::INVALID, node: item.into() };
|
2021-02-28 19:23:10 +00:00
|
|
|
let mut collector = NodeCollector {
|
2023-08-31 21:14:47 +00:00
|
|
|
tcx,
|
2021-09-12 01:19:18 +00:00
|
|
|
owner: item.def_id(),
|
2024-04-04 11:43:49 +00:00
|
|
|
parent_node: ItemLocalId::ZERO,
|
2021-09-12 01:19:18 +00:00
|
|
|
nodes,
|
|
|
|
bodies,
|
2023-12-18 20:02:21 +00:00
|
|
|
parenting: Default::default(),
|
2021-02-28 19:23:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
match item {
|
2022-03-18 21:13:38 +00:00
|
|
|
OwnerNode::Crate(citem) => {
|
2023-11-21 19:07:32 +00:00
|
|
|
collector.visit_mod(citem, citem.spans.inner_span, hir::CRATE_HIR_ID)
|
2022-03-18 21:13:38 +00:00
|
|
|
}
|
2021-02-28 19:23:10 +00:00
|
|
|
OwnerNode::Item(item) => collector.visit_item(item),
|
|
|
|
OwnerNode::TraitItem(item) => collector.visit_trait_item(item),
|
|
|
|
OwnerNode::ImplItem(item) => collector.visit_impl_item(item),
|
|
|
|
OwnerNode::ForeignItem(item) => collector.visit_foreign_item(item),
|
2024-03-19 08:37:53 +00:00
|
|
|
OwnerNode::Synthetic => unreachable!(),
|
2021-02-28 19:23:10 +00:00
|
|
|
};
|
|
|
|
|
2024-01-20 12:21:27 +00:00
|
|
|
for (local_id, node) in collector.nodes.iter_enumerated() {
|
|
|
|
if let Node::Err(span) = node.node {
|
|
|
|
let hir_id = HirId { owner: item.def_id(), local_id };
|
|
|
|
let msg = format!("ID {hir_id} not encountered when visiting item HIR");
|
2024-04-26 13:09:04 +00:00
|
|
|
tcx.dcx().span_delayed_bug(span, msg);
|
2024-01-20 12:21:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-12 01:19:18 +00:00
|
|
|
(collector.nodes, collector.parenting)
|
2021-02-28 19:23:10 +00:00
|
|
|
}
|
2016-04-13 23:55:34 +00:00
|
|
|
|
2021-02-28 19:23:10 +00:00
|
|
|
impl<'a, 'hir> NodeCollector<'a, 'hir> {
|
2022-08-31 13:01:10 +00:00
|
|
|
#[instrument(level = "debug", skip(self))]
|
2019-02-15 09:30:44 +00:00
|
|
|
fn insert(&mut self, span: Span, hir_id: HirId, node: Node<'hir>) {
|
2021-02-28 19:23:10 +00:00
|
|
|
debug_assert_eq!(self.owner, hir_id.owner);
|
2021-03-27 12:21:26 +00:00
|
|
|
debug_assert_ne!(hir_id.local_id.as_u32(), 0);
|
2022-09-11 13:11:58 +00:00
|
|
|
debug_assert_ne!(hir_id.local_id, self.parent_node);
|
2017-08-18 18:24:19 +00:00
|
|
|
|
|
|
|
// Make sure that the DepNode of some node coincides with the HirId
|
|
|
|
// owner of that node.
|
2025-01-22 10:57:59 +00:00
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
if hir_id.owner != self.owner {
|
|
|
|
span_bug!(
|
|
|
|
span,
|
|
|
|
"inconsistent HirId at `{:?}` for `{node:?}`: \
|
2020-04-13 16:52:40 +00:00
|
|
|
current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?})",
|
2025-01-22 10:57:59 +00:00
|
|
|
self.tcx.sess.source_map().span_to_diagnostic_string(span),
|
|
|
|
self.tcx
|
|
|
|
.definitions_untracked()
|
|
|
|
.def_path(self.owner.def_id)
|
|
|
|
.to_string_no_crate_verbose(),
|
|
|
|
self.owner,
|
|
|
|
self.tcx
|
|
|
|
.definitions_untracked()
|
|
|
|
.def_path(hir_id.owner.def_id)
|
|
|
|
.to_string_no_crate_verbose(),
|
|
|
|
hir_id.owner,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if self.tcx.sess.opts.incremental.is_some()
|
|
|
|
&& span.parent().is_none()
|
|
|
|
&& !span.is_dummy()
|
|
|
|
{
|
|
|
|
span_bug!(span, "span without a parent: {:#?}, {node:?}", span.data())
|
|
|
|
}
|
2017-08-18 18:24:19 +00:00
|
|
|
}
|
|
|
|
|
2024-01-20 12:21:27 +00:00
|
|
|
self.nodes[hir_id.local_id] = ParentedNode { parent: self.parent_node, node };
|
2016-04-13 23:55:34 +00:00
|
|
|
}
|
2016-04-14 05:24:30 +00:00
|
|
|
|
2019-02-06 13:16:11 +00:00
|
|
|
fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_node_id: HirId, f: F) {
|
2021-02-28 19:23:10 +00:00
|
|
|
debug_assert_eq!(parent_node_id.owner, self.owner);
|
2019-02-15 14:21:56 +00:00
|
|
|
let parent_node = self.parent_node;
|
2021-02-28 19:23:10 +00:00
|
|
|
self.parent_node = parent_node_id.local_id;
|
2016-04-14 05:24:30 +00:00
|
|
|
f(self);
|
2019-02-15 14:21:56 +00:00
|
|
|
self.parent_node = parent_node;
|
2016-04-14 05:24:30 +00:00
|
|
|
}
|
2017-08-18 18:24:19 +00:00
|
|
|
|
2021-03-05 19:09:33 +00:00
|
|
|
fn insert_nested(&mut self, item: LocalDefId) {
|
2024-04-04 11:43:49 +00:00
|
|
|
if self.parent_node != ItemLocalId::ZERO {
|
2024-04-03 14:23:08 +00:00
|
|
|
self.parenting.insert(item, self.parent_node);
|
|
|
|
}
|
2021-03-05 19:09:33 +00:00
|
|
|
}
|
2016-04-13 23:55:34 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 19:23:10 +00:00
|
|
|
impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
|
2016-04-13 23:55:34 +00:00
|
|
|
/// Because we want to track parent items and so forth, enable
|
|
|
|
/// deep walking so that we walk nested items in the context of
|
|
|
|
/// their outer items.
|
2016-11-09 21:45:26 +00:00
|
|
|
|
2016-04-13 23:55:34 +00:00
|
|
|
fn visit_nested_item(&mut self, item: ItemId) {
|
|
|
|
debug!("visit_nested_item: {:?}", item);
|
2022-10-27 03:02:18 +00:00
|
|
|
self.insert_nested(item.owner_id.def_id);
|
2016-04-13 23:55:34 +00:00
|
|
|
}
|
|
|
|
|
2016-12-04 02:21:06 +00:00
|
|
|
fn visit_nested_trait_item(&mut self, item_id: TraitItemId) {
|
2022-10-27 03:02:18 +00:00
|
|
|
self.insert_nested(item_id.owner_id.def_id);
|
2016-12-04 02:21:06 +00:00
|
|
|
}
|
|
|
|
|
2016-11-04 22:20:15 +00:00
|
|
|
fn visit_nested_impl_item(&mut self, item_id: ImplItemId) {
|
2022-10-27 03:02:18 +00:00
|
|
|
self.insert_nested(item_id.owner_id.def_id);
|
2016-11-04 22:20:15 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 20:57:54 +00:00
|
|
|
fn visit_nested_foreign_item(&mut self, foreign_id: ForeignItemId) {
|
2022-10-27 03:02:18 +00:00
|
|
|
self.insert_nested(foreign_id.owner_id.def_id);
|
2020-11-11 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 10:32:59 +00:00
|
|
|
fn visit_nested_body(&mut self, id: BodyId) {
|
2021-02-28 19:23:10 +00:00
|
|
|
debug_assert_eq!(id.hir_id.owner, self.owner);
|
2021-10-21 21:08:57 +00:00
|
|
|
let body = self.bodies[&id.hir_id.local_id];
|
2021-02-28 19:23:10 +00:00
|
|
|
self.visit_body(body);
|
2016-10-28 20:58:32 +00:00
|
|
|
}
|
|
|
|
|
2019-11-29 12:43:03 +00:00
|
|
|
fn visit_param(&mut self, param: &'hir Param<'hir>) {
|
2019-08-27 11:24:32 +00:00
|
|
|
let node = Node::Param(param);
|
|
|
|
self.insert(param.pat.span, param.hir_id, node);
|
|
|
|
self.with_parent(param.hir_id, |this| {
|
|
|
|
intravisit::walk_param(this, param);
|
2019-07-26 22:52:37 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-31 13:01:10 +00:00
|
|
|
#[instrument(level = "debug", skip(self))]
|
2019-11-28 18:28:50 +00:00
|
|
|
fn visit_item(&mut self, i: &'hir Item<'hir>) {
|
2022-10-27 03:02:18 +00:00
|
|
|
debug_assert_eq!(i.owner_id, self.owner);
|
2021-02-28 19:23:10 +00:00
|
|
|
self.with_parent(i.hir_id(), |this| {
|
Move `hir::Item::ident` into `hir::ItemKind`.
`hir::Item` has an `ident` field.
- It's always non-empty for these item kinds: `ExternCrate`, `Static`,
`Const`, `Fn`, `Macro`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`,
Trait`, TraitAalis`.
- It's always empty for these item kinds: `ForeignMod`, `GlobalAsm`,
`Impl`.
- For `Use`, it is non-empty for `UseKind::Single` and empty for
`UseKind::{Glob,ListStem}`.
All of this is quite non-obvious; the only documentation is a single
comment saying "The name might be a dummy name in case of anonymous
items". Some sites that handle items check for an empty ident, some
don't. This is a very C-like way of doing things, but this is Rust, we
have sum types, we can do this properly and never forget to check for
the exceptional case and never YOLO possibly empty identifiers (or
possibly dummy spans) around and hope that things will work out.
The commit is large but it's mostly obvious plumbing work. Some notable
things.
- A similar transformation makes sense for `ast::Item`, but this is
already a big change. That can be done later.
- Lots of assertions are added to item lowering to ensure that
identifiers are empty/non-empty as expected. These will be removable
when `ast::Item` is done later.
- `ItemKind::Use` doesn't get an `Ident`, but `UseKind::Single` does.
- `lower_use_tree` is significantly simpler. No more confusing `&mut
Ident` to deal with.
- `ItemKind::ident` is a new method, it returns an `Option<Ident>`. It's
used with `unwrap` in a few places; sometimes it's hard to tell
exactly which item kinds might occur. None of these unwraps fail on
the test suite. It's conceivable that some might fail on alternative
input. We can deal with those if/when they happen.
- In `trait_path` the `find_map`/`if let` is replaced with a loop, and
things end up much clearer that way.
- `named_span` no longer checks for an empty name; instead the call site
now checks for a missing identifier if necessary.
- `maybe_inline_local` doesn't need the `glob` argument, it can be
computed in-function from the `renamed` argument.
- `arbitrary_source_item_ordering::check_mod` had a big `if` statement
that was just getting the ident from the item kinds that had one. It
could be mostly replaced by a single call to the new `ItemKind::ident`
method.
- `ItemKind` grows from 56 to 64 bytes, but `Item` stays the same size,
and that's what matters, because `ItemKind` only occurs within `Item`.
2025-03-06 08:07:36 +00:00
|
|
|
if let ItemKind::Struct(_, struct_def, _) = &i.kind {
|
2021-03-27 12:21:26 +00:00
|
|
|
// If this is a tuple or unit-like struct, register the constructor.
|
|
|
|
if let Some(ctor_hir_id) = struct_def.ctor_hir_id() {
|
|
|
|
this.insert(i.span, ctor_hir_id, Node::Ctor(struct_def));
|
2016-04-13 23:55:34 +00:00
|
|
|
}
|
2021-03-27 12:21:26 +00:00
|
|
|
}
|
|
|
|
intravisit::walk_item(this, i);
|
2016-04-14 05:24:30 +00:00
|
|
|
});
|
2016-04-13 23:55:34 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 13:01:10 +00:00
|
|
|
#[instrument(level = "debug", skip(self))]
|
2020-11-11 20:57:54 +00:00
|
|
|
fn visit_foreign_item(&mut self, fi: &'hir ForeignItem<'hir>) {
|
2022-10-27 03:02:18 +00:00
|
|
|
debug_assert_eq!(fi.owner_id, self.owner);
|
2021-02-28 19:23:10 +00:00
|
|
|
self.with_parent(fi.hir_id(), |this| {
|
2021-03-27 12:21:26 +00:00
|
|
|
intravisit::walk_foreign_item(this, fi);
|
2016-04-14 05:24:30 +00:00
|
|
|
});
|
2016-04-13 23:55:34 +00:00
|
|
|
}
|
|
|
|
|
2019-11-30 16:46:46 +00:00
|
|
|
fn visit_generic_param(&mut self, param: &'hir GenericParam<'hir>) {
|
2021-03-23 21:47:22 +00:00
|
|
|
self.insert(param.span, param.hir_id, Node::GenericParam(param));
|
|
|
|
intravisit::walk_generic_param(self, param);
|
2016-04-13 23:55:34 +00:00
|
|
|
}
|
|
|
|
|
2024-06-08 03:26:50 +00:00
|
|
|
fn visit_const_param_default(&mut self, param: HirId, ct: &'hir ConstArg<'hir>) {
|
2021-03-27 12:21:26 +00:00
|
|
|
self.with_parent(param, |this| {
|
|
|
|
intravisit::walk_const_param_default(this, ct);
|
|
|
|
})
|
2021-03-01 11:50:09 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 13:01:10 +00:00
|
|
|
#[instrument(level = "debug", skip(self))]
|
2019-11-28 20:47:10 +00:00
|
|
|
fn visit_trait_item(&mut self, ti: &'hir TraitItem<'hir>) {
|
2022-10-27 03:02:18 +00:00
|
|
|
debug_assert_eq!(ti.owner_id, self.owner);
|
2021-02-28 19:23:10 +00:00
|
|
|
self.with_parent(ti.hir_id(), |this| {
|
2021-03-27 12:21:26 +00:00
|
|
|
intravisit::walk_trait_item(this, ti);
|
2016-04-14 05:24:30 +00:00
|
|
|
});
|
2016-04-13 23:55:34 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 13:01:10 +00:00
|
|
|
#[instrument(level = "debug", skip(self))]
|
2019-11-28 21:16:44 +00:00
|
|
|
fn visit_impl_item(&mut self, ii: &'hir ImplItem<'hir>) {
|
2022-10-27 03:02:18 +00:00
|
|
|
debug_assert_eq!(ii.owner_id, self.owner);
|
2021-02-28 19:23:10 +00:00
|
|
|
self.with_parent(ii.hir_id(), |this| {
|
2021-03-27 12:21:26 +00:00
|
|
|
intravisit::walk_impl_item(this, ii);
|
2016-04-14 05:24:30 +00:00
|
|
|
});
|
2016-04-13 23:55:34 +00:00
|
|
|
}
|
|
|
|
|
2019-11-29 12:43:03 +00:00
|
|
|
fn visit_pat(&mut self, pat: &'hir Pat<'hir>) {
|
2022-06-28 18:15:30 +00:00
|
|
|
self.insert(pat.span, pat.hir_id, Node::Pat(pat));
|
2015-09-10 19:53:08 +00:00
|
|
|
|
2019-02-15 11:14:36 +00:00
|
|
|
self.with_parent(pat.hir_id, |this| {
|
2016-04-14 05:24:30 +00:00
|
|
|
intravisit::walk_pat(this, pat);
|
2022-05-23 01:34:37 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-12-11 16:50:45 +00:00
|
|
|
fn visit_pat_expr(&mut self, expr: &'hir PatExpr<'hir>) {
|
|
|
|
self.insert(expr.span, expr.hir_id, Node::PatExpr(expr));
|
|
|
|
|
|
|
|
self.with_parent(expr.hir_id, |this| {
|
|
|
|
intravisit::walk_pat_expr(this, expr);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-23 01:34:37 +00:00
|
|
|
fn visit_pat_field(&mut self, field: &'hir PatField<'hir>) {
|
|
|
|
self.insert(field.span, field.hir_id, Node::PatField(field));
|
|
|
|
self.with_parent(field.hir_id, |this| {
|
|
|
|
intravisit::walk_pat_field(this, field);
|
2016-04-14 05:24:30 +00:00
|
|
|
});
|
2015-09-10 19:53:08 +00:00
|
|
|
}
|
|
|
|
|
2019-11-29 12:43:03 +00:00
|
|
|
fn visit_arm(&mut self, arm: &'hir Arm<'hir>) {
|
2019-03-30 22:54:29 +00:00
|
|
|
let node = Node::Arm(arm);
|
|
|
|
|
|
|
|
self.insert(arm.span, arm.hir_id, node);
|
|
|
|
|
|
|
|
self.with_parent(arm.hir_id, |this| {
|
|
|
|
intravisit::walk_arm(this, arm);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-08-10 03:43:30 +00:00
|
|
|
fn visit_opaque_ty(&mut self, opaq: &'hir OpaqueTy<'hir>) {
|
|
|
|
self.insert(opaq.span, opaq.hir_id, Node::OpaqueTy(opaq));
|
|
|
|
|
|
|
|
self.with_parent(opaq.hir_id, |this| {
|
|
|
|
intravisit::walk_opaque_ty(this, opaq);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-17 18:28:50 +00:00
|
|
|
fn visit_anon_const(&mut self, constant: &'hir AnonConst) {
|
2024-11-28 16:33:22 +00:00
|
|
|
self.insert(constant.span, constant.hir_id, Node::AnonConst(constant));
|
2018-05-17 18:28:50 +00:00
|
|
|
|
2019-02-15 11:14:36 +00:00
|
|
|
self.with_parent(constant.hir_id, |this| {
|
2018-05-17 18:28:50 +00:00
|
|
|
intravisit::walk_anon_const(this, constant);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-03 09:11:58 +00:00
|
|
|
fn visit_inline_const(&mut self, constant: &'hir ConstBlock) {
|
|
|
|
self.insert(DUMMY_SP, constant.hir_id, Node::ConstBlock(constant));
|
|
|
|
|
|
|
|
self.with_parent(constant.hir_id, |this| {
|
|
|
|
intravisit::walk_inline_const(this, constant);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-29 12:43:03 +00:00
|
|
|
fn visit_expr(&mut self, expr: &'hir Expr<'hir>) {
|
2019-02-15 09:30:44 +00:00
|
|
|
self.insert(expr.span, expr.hir_id, Node::Expr(expr));
|
2015-09-10 19:53:08 +00:00
|
|
|
|
2019-02-15 11:14:36 +00:00
|
|
|
self.with_parent(expr.hir_id, |this| {
|
2016-04-14 05:24:30 +00:00
|
|
|
intravisit::walk_expr(this, expr);
|
|
|
|
});
|
2015-09-10 19:53:08 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 00:42:39 +00:00
|
|
|
fn visit_expr_field(&mut self, field: &'hir ExprField<'hir>) {
|
|
|
|
self.insert(field.span, field.hir_id, Node::ExprField(field));
|
|
|
|
self.with_parent(field.hir_id, |this| {
|
|
|
|
intravisit::walk_expr_field(this, field);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-29 12:43:03 +00:00
|
|
|
fn visit_stmt(&mut self, stmt: &'hir Stmt<'hir>) {
|
2019-02-15 09:30:44 +00:00
|
|
|
self.insert(stmt.span, stmt.hir_id, Node::Stmt(stmt));
|
2016-04-14 05:24:30 +00:00
|
|
|
|
2019-02-15 11:14:36 +00:00
|
|
|
self.with_parent(stmt.hir_id, |this| {
|
2016-04-14 05:24:30 +00:00
|
|
|
intravisit::walk_stmt(this, stmt);
|
|
|
|
});
|
2015-09-10 19:53:08 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 00:43:34 +00:00
|
|
|
fn visit_path_segment(&mut self, path_segment: &'hir PathSegment<'hir>) {
|
2024-01-31 18:55:10 +00:00
|
|
|
// FIXME: walk path segment with `path_segment.hir_id` parent.
|
2022-09-12 00:43:34 +00:00
|
|
|
self.insert(path_segment.ident.span, path_segment.hir_id, Node::PathSegment(path_segment));
|
|
|
|
intravisit::walk_path_segment(self, path_segment);
|
2018-09-11 03:08:47 +00:00
|
|
|
}
|
|
|
|
|
2025-01-11 19:12:36 +00:00
|
|
|
fn visit_ty(&mut self, ty: &'hir Ty<'hir, AmbigArg>) {
|
|
|
|
self.insert(ty.span, ty.hir_id, Node::Ty(ty.as_unambig_ty()));
|
2016-08-06 00:12:20 +00:00
|
|
|
|
2019-02-15 11:14:36 +00:00
|
|
|
self.with_parent(ty.hir_id, |this| {
|
2016-08-06 00:12:20 +00:00
|
|
|
intravisit::walk_ty(this, ty);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-01-11 19:12:36 +00:00
|
|
|
fn visit_const_arg(&mut self, const_arg: &'hir ConstArg<'hir, AmbigArg>) {
|
|
|
|
self.insert(
|
|
|
|
const_arg.as_unambig_ct().span(),
|
|
|
|
const_arg.hir_id,
|
|
|
|
Node::ConstArg(const_arg.as_unambig_ct()),
|
|
|
|
);
|
2021-04-24 21:41:57 +00:00
|
|
|
|
2025-01-11 19:12:36 +00:00
|
|
|
self.with_parent(const_arg.hir_id, |this| {
|
|
|
|
intravisit::walk_ambig_const_arg(this, const_arg);
|
2021-04-24 21:41:57 +00:00
|
|
|
});
|
2016-08-06 00:12:20 +00:00
|
|
|
}
|
|
|
|
|
2025-01-11 19:12:36 +00:00
|
|
|
fn visit_infer(
|
|
|
|
&mut self,
|
|
|
|
inf_id: HirId,
|
|
|
|
inf_span: Span,
|
|
|
|
kind: InferKind<'hir>,
|
|
|
|
) -> Self::Result {
|
|
|
|
match kind {
|
|
|
|
InferKind::Ty(ty) => self.insert(inf_span, inf_id, Node::Ty(ty)),
|
|
|
|
InferKind::Const(ct) => self.insert(inf_span, inf_id, Node::ConstArg(ct)),
|
|
|
|
InferKind::Ambig(inf) => self.insert(inf_span, inf_id, Node::Infer(inf)),
|
|
|
|
}
|
|
|
|
|
|
|
|
self.visit_id(inf_id);
|
|
|
|
}
|
|
|
|
|
2019-11-30 16:46:46 +00:00
|
|
|
fn visit_trait_ref(&mut self, tr: &'hir TraitRef<'hir>) {
|
2019-02-15 09:30:44 +00:00
|
|
|
self.insert(tr.path.span, tr.hir_ref_id, Node::TraitRef(tr));
|
2016-10-30 06:04:52 +00:00
|
|
|
|
2019-02-15 11:14:36 +00:00
|
|
|
self.with_parent(tr.hir_ref_id, |this| {
|
2016-10-30 06:04:52 +00:00
|
|
|
intravisit::walk_trait_ref(this, tr);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-29 12:43:03 +00:00
|
|
|
fn visit_block(&mut self, block: &'hir Block<'hir>) {
|
2019-02-15 09:30:44 +00:00
|
|
|
self.insert(block.span, block.hir_id, Node::Block(block));
|
2019-02-15 11:14:36 +00:00
|
|
|
self.with_parent(block.hir_id, |this| {
|
2016-04-14 05:24:30 +00:00
|
|
|
intravisit::walk_block(this, block);
|
|
|
|
});
|
2015-09-10 19:53:08 +00:00
|
|
|
}
|
|
|
|
|
2024-03-20 16:50:31 +00:00
|
|
|
fn visit_local(&mut self, l: &'hir LetStmt<'hir>) {
|
2024-03-22 17:06:20 +00:00
|
|
|
self.insert(l.span, l.hir_id, Node::LetStmt(l));
|
2021-03-27 12:21:26 +00:00
|
|
|
self.with_parent(l.hir_id, |this| {
|
|
|
|
intravisit::walk_local(this, l);
|
|
|
|
})
|
2017-08-18 06:56:11 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 01:21:50 +00:00
|
|
|
fn visit_lifetime(&mut self, lifetime: &'hir Lifetime) {
|
2022-11-05 22:41:07 +00:00
|
|
|
self.insert(lifetime.ident.span, lifetime.hir_id, Node::Lifetime(lifetime));
|
2015-09-10 19:53:08 +00:00
|
|
|
}
|
2016-10-28 06:52:45 +00:00
|
|
|
|
2022-08-10 01:22:01 +00:00
|
|
|
fn visit_variant(&mut self, v: &'hir Variant<'hir>) {
|
2022-11-06 19:46:55 +00:00
|
|
|
self.insert(v.span, v.hir_id, Node::Variant(v));
|
|
|
|
self.with_parent(v.hir_id, |this| {
|
2019-03-21 22:38:50 +00:00
|
|
|
// Register the constructor of this variant.
|
2019-08-14 00:40:21 +00:00
|
|
|
if let Some(ctor_hir_id) = v.data.ctor_hir_id() {
|
|
|
|
this.insert(v.span, ctor_hir_id, Node::Ctor(&v.data));
|
2019-03-21 22:38:50 +00:00
|
|
|
}
|
2022-08-10 01:22:01 +00:00
|
|
|
intravisit::walk_variant(this, v);
|
2016-12-21 10:32:59 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-15 21:36:07 +00:00
|
|
|
fn visit_field_def(&mut self, field: &'hir FieldDef<'hir>) {
|
2019-02-15 09:30:44 +00:00
|
|
|
self.insert(field.span, field.hir_id, Node::Field(field));
|
2019-02-15 11:14:36 +00:00
|
|
|
self.with_parent(field.hir_id, |this| {
|
2021-03-15 21:36:07 +00:00
|
|
|
intravisit::walk_field_def(this, field);
|
2016-11-09 18:57:48 +00:00
|
|
|
});
|
|
|
|
}
|
2017-08-18 18:24:19 +00:00
|
|
|
|
2024-05-27 21:53:46 +00:00
|
|
|
fn visit_assoc_item_constraint(&mut self, constraint: &'hir AssocItemConstraint<'hir>) {
|
|
|
|
self.insert(constraint.span, constraint.hir_id, Node::AssocItemConstraint(constraint));
|
|
|
|
self.with_parent(constraint.hir_id, |this| {
|
|
|
|
intravisit::walk_assoc_item_constraint(this, constraint)
|
2022-05-07 20:41:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-08-18 18:24:19 +00:00
|
|
|
fn visit_trait_item_ref(&mut self, ii: &'hir TraitItemRef) {
|
|
|
|
// Do not visit the duplicate information in TraitItemRef. We want to
|
|
|
|
// map the actual nodes, not the duplicate ones in the *Ref.
|
2022-03-12 18:36:11 +00:00
|
|
|
let TraitItemRef { id, ident: _, kind: _, span: _ } = *ii;
|
2017-08-18 18:24:19 +00:00
|
|
|
|
|
|
|
self.visit_nested_trait_item(id);
|
|
|
|
}
|
|
|
|
|
2021-07-15 20:19:39 +00:00
|
|
|
fn visit_impl_item_ref(&mut self, ii: &'hir ImplItemRef) {
|
2017-08-18 18:24:19 +00:00
|
|
|
// Do not visit the duplicate information in ImplItemRef. We want to
|
|
|
|
// map the actual nodes, not the duplicate ones in the *Ref.
|
2022-03-12 18:36:11 +00:00
|
|
|
let ImplItemRef { id, ident: _, kind: _, span: _, trait_item_def_id: _ } = *ii;
|
2017-08-18 18:24:19 +00:00
|
|
|
|
|
|
|
self.visit_nested_impl_item(id);
|
|
|
|
}
|
2020-11-28 17:08:11 +00:00
|
|
|
|
2021-07-15 20:19:39 +00:00
|
|
|
fn visit_foreign_item_ref(&mut self, fi: &'hir ForeignItemRef) {
|
2020-11-28 17:08:11 +00:00
|
|
|
// Do not visit the duplicate information in ForeignItemRef. We want to
|
|
|
|
// map the actual nodes, not the duplicate ones in the *Ref.
|
2021-07-15 20:19:39 +00:00
|
|
|
let ForeignItemRef { id, ident: _, span: _ } = *fi;
|
2020-11-28 17:08:11 +00:00
|
|
|
|
|
|
|
self.visit_nested_foreign_item(id);
|
|
|
|
}
|
2024-01-20 12:21:27 +00:00
|
|
|
|
|
|
|
fn visit_where_predicate(&mut self, predicate: &'hir WherePredicate<'hir>) {
|
2024-11-25 08:38:35 +00:00
|
|
|
self.insert(predicate.span, predicate.hir_id, Node::WherePredicate(predicate));
|
|
|
|
self.with_parent(predicate.hir_id, |this| {
|
|
|
|
intravisit::walk_where_predicate(this, predicate)
|
|
|
|
});
|
2024-01-20 12:21:27 +00:00
|
|
|
}
|
|
|
|
|
2025-01-07 10:24:16 +00:00
|
|
|
fn visit_pattern_type_pattern(&mut self, pat: &'hir hir::TyPat<'hir>) {
|
|
|
|
self.insert(pat.span, pat.hir_id, Node::TyPat(pat));
|
|
|
|
|
|
|
|
self.with_parent(pat.hir_id, |this| {
|
|
|
|
intravisit::walk_ty_pat(this, pat);
|
|
|
|
});
|
2023-02-02 13:57:36 +00:00
|
|
|
}
|
2024-04-04 18:46:26 +00:00
|
|
|
|
|
|
|
fn visit_precise_capturing_arg(
|
|
|
|
&mut self,
|
|
|
|
arg: &'hir PreciseCapturingArg<'hir>,
|
|
|
|
) -> Self::Result {
|
|
|
|
match arg {
|
|
|
|
PreciseCapturingArg::Lifetime(_) => {
|
|
|
|
// This is represented as a `Node::Lifetime`, intravisit will get to it below.
|
|
|
|
}
|
|
|
|
PreciseCapturingArg::Param(param) => self.insert(
|
|
|
|
param.ident.span,
|
|
|
|
param.hir_id,
|
|
|
|
Node::PreciseCapturingNonLifetimeArg(param),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
intravisit::walk_precise_capturing_arg(self, arg);
|
|
|
|
}
|
2015-09-10 19:53:08 +00:00
|
|
|
}
|