2021-09-12 03:19:18 +02:00
|
|
|
use crate::hir::{ModuleItems, Owner};
|
2022-04-07 22:24:07 +02:00
|
|
|
use crate::ty::{DefIdTree, TyCtxt};
|
2020-04-27 23:26:11 +05:30
|
|
|
use rustc_ast as ast;
|
2021-02-28 18:58:50 +01:00
|
|
|
use rustc_data_structures::fingerprint::Fingerprint;
|
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
2018-08-03 12:22:22 -06:00
|
|
|
use rustc_data_structures::svh::Svh;
|
2021-09-12 01:11:22 +02:00
|
|
|
use rustc_data_structures::sync::{par_for_each_in, Send, Sync};
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir::def::{DefKind, Res};
|
2021-07-14 19:53:57 +02:00
|
|
|
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
|
2021-05-20 20:17:45 +02:00
|
|
|
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
|
2021-08-26 18:42:08 +02:00
|
|
|
use rustc_hir::intravisit::{self, Visitor};
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir::*;
|
2021-03-03 21:50:45 +01:00
|
|
|
use rustc_index::vec::Idx;
|
2021-11-03 18:03:12 -05:00
|
|
|
use rustc_middle::hir::nested_filter;
|
2021-06-08 18:36:30 +02:00
|
|
|
use rustc_span::def_id::StableCrateId;
|
2021-07-10 11:16:03 +08:00
|
|
|
use rustc_span::symbol::{kw, sym, Ident, Symbol};
|
2020-01-07 14:38:27 +01:00
|
|
|
use rustc_span::Span;
|
2018-04-25 19:30:39 +03:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2020-02-07 15:34:39 +01:00
|
|
|
fn fn_decl<'hir>(node: Node<'hir>) -> Option<&'hir FnDecl<'hir>> {
|
|
|
|
match node {
|
2020-03-24 06:17:44 +01:00
|
|
|
Node::Item(Item { kind: ItemKind::Fn(sig, _, _), .. })
|
|
|
|
| Node::TraitItem(TraitItem { kind: TraitItemKind::Fn(sig, _), .. })
|
|
|
|
| Node::ImplItem(ImplItem { kind: ImplItemKind::Fn(sig, _), .. }) => Some(&sig.decl),
|
2022-07-11 23:39:53 +04:00
|
|
|
Node::Expr(Expr { kind: ExprKind::Closure(Closure { fn_decl, .. }), .. })
|
Support HIR wf checking for function signatures
During function type-checking, we normalize any associated types in
the function signature (argument types + return type), and then
create WF obligations for each of the normalized types. The HIR wf code
does not currently support this case, so any errors that we get have
imprecise spans.
This commit extends `ObligationCauseCode::WellFormed` to support
recording a function parameter, allowing us to get the corresponding
HIR type if an error occurs. Function typechecking is modified to
pass this information during signature normalization and WF checking.
The resulting code is fairly verbose, due to the fact that we can
no longer normalize the entire signature with a single function call.
As part of the refactoring, we now perform HIR-based WF checking
for several other 'typed items' (statics, consts, and inherent impls).
As a result, WF and projection errors in a function signature now
have a precise span, which points directly at the responsible type.
If a function signature is constructed via a macro, this will allow
the error message to point at the code 'most responsible' for the error
(e.g. a user-supplied macro argument).
2021-07-18 11:33:49 -05:00
|
|
|
| Node::ForeignItem(ForeignItem { kind: ForeignItemKind::Fn(fn_decl, ..), .. }) => {
|
|
|
|
Some(fn_decl)
|
|
|
|
}
|
2020-02-07 15:34:39 +01:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2019-11-06 11:43:33 -08:00
|
|
|
|
2020-11-30 23:58:08 -08:00
|
|
|
pub fn fn_sig<'hir>(node: Node<'hir>) -> Option<&'hir FnSig<'hir>> {
|
2020-02-07 15:34:39 +01:00
|
|
|
match &node {
|
2020-03-24 06:17:44 +01:00
|
|
|
Node::Item(Item { kind: ItemKind::Fn(sig, _, _), .. })
|
|
|
|
| Node::TraitItem(TraitItem { kind: TraitItemKind::Fn(sig, _), .. })
|
|
|
|
| Node::ImplItem(ImplItem { kind: ImplItemKind::Fn(sig, _), .. }) => Some(sig),
|
2020-02-07 15:34:39 +01:00
|
|
|
_ => None,
|
2019-11-06 11:43:33 -08:00
|
|
|
}
|
2020-02-07 15:34:39 +01:00
|
|
|
}
|
2019-11-06 11:43:33 -08:00
|
|
|
|
2022-07-06 18:23:38 -04:00
|
|
|
#[inline]
|
2020-06-22 15:54:28 -07:00
|
|
|
pub fn associated_body<'hir>(node: Node<'hir>) -> Option<BodyId> {
|
2020-02-07 16:10:50 +01:00
|
|
|
match node {
|
2020-03-24 06:17:44 +01:00
|
|
|
Node::Item(Item {
|
|
|
|
kind: ItemKind::Const(_, body) | ItemKind::Static(.., body) | ItemKind::Fn(.., body),
|
|
|
|
..
|
|
|
|
})
|
|
|
|
| Node::TraitItem(TraitItem {
|
|
|
|
kind:
|
|
|
|
TraitItemKind::Const(_, Some(body)) | TraitItemKind::Fn(_, TraitFn::Provided(body)),
|
|
|
|
..
|
|
|
|
})
|
|
|
|
| Node::ImplItem(ImplItem {
|
|
|
|
kind: ImplItemKind::Const(_, body) | ImplItemKind::Fn(_, body),
|
|
|
|
..
|
|
|
|
})
|
2022-07-11 23:39:53 +04:00
|
|
|
| Node::Expr(Expr { kind: ExprKind::Closure(Closure { body, .. }), .. }) => Some(*body),
|
2018-05-17 21:28:50 +03:00
|
|
|
|
2020-02-07 16:10:50 +01:00
|
|
|
Node::AnonConst(constant) => Some(constant.body),
|
2016-12-20 23:05:21 +02:00
|
|
|
|
2020-02-07 16:10:50 +01:00
|
|
|
_ => None,
|
2017-02-20 21:18:16 -05:00
|
|
|
}
|
2020-02-07 16:10:50 +01:00
|
|
|
}
|
2017-02-20 21:18:16 -05:00
|
|
|
|
2020-02-07 16:10:50 +01:00
|
|
|
fn is_body_owner<'hir>(node: Node<'hir>, hir_id: HirId) -> bool {
|
|
|
|
match associated_body(node) {
|
|
|
|
Some(b) => b.hir_id == hir_id,
|
|
|
|
None => false,
|
2016-12-20 23:05:21 +02:00
|
|
|
}
|
2013-08-31 17:13:57 +10:00
|
|
|
}
|
|
|
|
|
2020-02-09 15:32:00 +01:00
|
|
|
#[derive(Copy, Clone)]
|
2017-01-26 03:21:50 +02:00
|
|
|
pub struct Map<'hir> {
|
2020-02-07 13:13:35 +01:00
|
|
|
pub(super) tcx: TyCtxt<'hir>,
|
2014-01-17 23:23:09 +11:00
|
|
|
}
|
|
|
|
|
2020-02-25 05:30:11 +01:00
|
|
|
/// An iterator that walks up the ancestor tree of a given `HirId`.
|
|
|
|
/// Constructed using `tcx.hir().parent_iter(hir_id)`.
|
2021-09-18 15:48:07 -05:00
|
|
|
pub struct ParentHirIterator<'hir> {
|
2019-09-25 17:05:30 -07:00
|
|
|
current_id: HirId,
|
2021-09-18 15:48:07 -05:00
|
|
|
map: Map<'hir>,
|
2019-09-25 17:05:30 -07:00
|
|
|
}
|
|
|
|
|
2021-09-18 15:48:07 -05:00
|
|
|
impl<'hir> Iterator for ParentHirIterator<'hir> {
|
2019-11-29 14:08:03 +01:00
|
|
|
type Item = (HirId, Node<'hir>);
|
2019-09-25 17:05:30 -07:00
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if self.current_id == CRATE_HIR_ID {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
loop {
|
|
|
|
// There are nodes that do not have entries, so we need to skip them.
|
|
|
|
let parent_id = self.map.get_parent_node(self.current_id);
|
|
|
|
|
|
|
|
if parent_id == self.current_id {
|
|
|
|
self.current_id = CRATE_HIR_ID;
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.current_id = parent_id;
|
2021-03-03 21:50:45 +01:00
|
|
|
if let Some(node) = self.map.find(parent_id) {
|
|
|
|
return Some((parent_id, node));
|
|
|
|
}
|
|
|
|
// If this `HirId` doesn't have an entry, skip it and look for its `parent_id`.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An iterator that walks up the ancestor tree of a given `HirId`.
|
|
|
|
/// Constructed using `tcx.hir().parent_owner_iter(hir_id)`.
|
2021-09-18 15:48:07 -05:00
|
|
|
pub struct ParentOwnerIterator<'hir> {
|
2021-03-03 21:50:45 +01:00
|
|
|
current_id: HirId,
|
2021-09-18 15:48:07 -05:00
|
|
|
map: Map<'hir>,
|
2021-03-03 21:50:45 +01:00
|
|
|
}
|
|
|
|
|
2021-09-18 15:48:07 -05:00
|
|
|
impl<'hir> Iterator for ParentOwnerIterator<'hir> {
|
2021-10-21 19:41:47 +02:00
|
|
|
type Item = (LocalDefId, OwnerNode<'hir>);
|
2021-03-03 21:50:45 +01:00
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if self.current_id.local_id.index() != 0 {
|
|
|
|
self.current_id.local_id = ItemLocalId::new(0);
|
2022-01-28 14:58:27 -03:00
|
|
|
if let Some(node) = self.map.tcx.hir_owner(self.current_id.owner) {
|
2021-10-21 19:41:47 +02:00
|
|
|
return Some((self.current_id.owner, node.node));
|
2021-03-03 21:50:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if self.current_id == CRATE_HIR_ID {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
loop {
|
|
|
|
// There are nodes that do not have entries, so we need to skip them.
|
|
|
|
let parent_id = self.map.def_key(self.current_id.owner).parent;
|
|
|
|
|
|
|
|
let parent_id = parent_id.map_or(CRATE_HIR_ID.owner, |local_def_index| {
|
|
|
|
let def_id = LocalDefId { local_def_index };
|
|
|
|
self.map.local_def_id_to_hir_id(def_id).owner
|
|
|
|
});
|
|
|
|
self.current_id = HirId::make_owner(parent_id);
|
|
|
|
|
|
|
|
// If this `HirId` doesn't have an entry, skip it and look for its `parent_id`.
|
2022-01-28 14:58:27 -03:00
|
|
|
if let Some(node) = self.map.tcx.hir_owner(self.current_id.owner) {
|
2021-10-21 19:41:47 +02:00
|
|
|
return Some((self.current_id.owner, node.node));
|
2019-09-25 17:05:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
impl<'hir> Map<'hir> {
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn krate(self) -> &'hir Crate<'hir> {
|
2021-05-11 11:42:01 +02:00
|
|
|
self.tcx.hir_crate(())
|
2020-02-06 13:41:37 +01:00
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn root_module(self) -> &'hir Mod<'hir> {
|
2021-09-12 01:11:22 +02:00
|
|
|
match self.tcx.hir_owner(CRATE_DEF_ID).map(|o| o.node) {
|
2022-01-28 14:58:27 -03:00
|
|
|
Some(OwnerNode::Crate(item)) => item,
|
2021-09-13 20:43:43 +02:00
|
|
|
_ => bug!(),
|
2021-09-12 01:11:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-03 15:50:33 -04:00
|
|
|
pub fn items(self) -> impl Iterator<Item = ItemId> + 'hir {
|
2022-04-09 13:49:36 -04:00
|
|
|
self.tcx.hir_crate_items(()).items.iter().copied()
|
2022-04-03 15:50:33 -04:00
|
|
|
}
|
|
|
|
|
2022-05-07 14:45:30 -04:00
|
|
|
pub fn module_items(self, module: LocalDefId) -> impl Iterator<Item = ItemId> + 'hir {
|
|
|
|
self.tcx.hir_module_items(module).items()
|
|
|
|
}
|
|
|
|
|
2022-04-09 15:55:06 -04:00
|
|
|
pub fn par_for_each_item(self, f: impl Fn(ItemId) + Sync + Send) {
|
2022-04-09 13:49:36 -04:00
|
|
|
par_for_each_in(&self.tcx.hir_crate_items(()).items[..], |id| f(*id));
|
2021-09-12 01:11:22 +02:00
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn def_key(self, def_id: LocalDefId) -> DefKey {
|
2021-05-20 20:17:45 +02:00
|
|
|
// Accessing the DefKey is ok, since it is part of DefPathHash.
|
2021-07-14 00:48:51 +02:00
|
|
|
self.tcx.definitions_untracked().def_key(def_id)
|
2015-09-17 14:29:59 -04:00
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn def_path_from_hir_id(self, id: HirId) -> Option<DefPath> {
|
2020-04-08 12:33:38 +01:00
|
|
|
self.opt_local_def_id(id).map(|def_id| self.def_path(def_id))
|
2015-09-17 14:29:59 -04:00
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn def_path(self, def_id: LocalDefId) -> DefPath {
|
2021-05-20 20:17:45 +02:00
|
|
|
// Accessing the DefPath is ok, since it is part of DefPathHash.
|
2021-07-14 00:48:51 +02:00
|
|
|
self.tcx.definitions_untracked().def_path(def_id)
|
2015-09-17 14:29:59 -04:00
|
|
|
}
|
|
|
|
|
2021-05-20 20:17:45 +02:00
|
|
|
#[inline]
|
|
|
|
pub fn def_path_hash(self, def_id: LocalDefId) -> DefPathHash {
|
|
|
|
// Accessing the DefPathHash is ok, it is incr. comp. stable.
|
2021-07-14 00:48:51 +02:00
|
|
|
self.tcx.definitions_untracked().def_path_hash(def_id)
|
2021-05-20 20:17:45 +02:00
|
|
|
}
|
|
|
|
|
2019-02-03 09:14:31 +01:00
|
|
|
#[inline]
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn local_def_id(self, hir_id: HirId) -> LocalDefId {
|
2020-04-09 09:43:00 +01:00
|
|
|
self.opt_local_def_id(hir_id).unwrap_or_else(|| {
|
|
|
|
bug!(
|
|
|
|
"local_def_id: no entry for `{:?}`, which has a map of `{:?}`",
|
|
|
|
hir_id,
|
2021-03-03 21:50:45 +01:00
|
|
|
self.find(hir_id)
|
2020-04-09 09:43:00 +01:00
|
|
|
)
|
|
|
|
})
|
2019-02-03 09:14:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn opt_local_def_id(self, hir_id: HirId) -> Option<LocalDefId> {
|
2021-12-05 11:18:16 +01:00
|
|
|
if hir_id.local_id == ItemLocalId::new(0) {
|
|
|
|
Some(hir_id.owner)
|
|
|
|
} else {
|
2021-11-21 19:04:47 +01:00
|
|
|
self.tcx
|
2021-09-22 19:28:20 +02:00
|
|
|
.hir_owner_nodes(hir_id.owner)
|
|
|
|
.as_owner()?
|
2021-11-21 19:04:47 +01:00
|
|
|
.local_id_to_def_id
|
|
|
|
.get(&hir_id.local_id)
|
|
|
|
.copied()
|
2021-12-05 11:18:16 +01:00
|
|
|
}
|
2015-09-02 16:11:32 -04:00
|
|
|
}
|
|
|
|
|
2017-11-16 14:04:01 +01:00
|
|
|
#[inline]
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn local_def_id_to_hir_id(self, def_id: LocalDefId) -> HirId {
|
2022-01-29 08:40:23 -03:00
|
|
|
self.tcx.local_def_id_to_hir_id(def_id)
|
2017-11-16 14:04:01 +01:00
|
|
|
}
|
|
|
|
|
2022-07-06 17:02:58 +08:00
|
|
|
/// Do not call this function directly. The query should be called.
|
2022-04-29 19:43:15 +02:00
|
|
|
pub(super) fn opt_def_kind(self, local_def_id: LocalDefId) -> Option<DefKind> {
|
2020-03-16 10:01:03 -05:00
|
|
|
let hir_id = self.local_def_id_to_hir_id(local_def_id);
|
2021-01-16 14:35:16 +01:00
|
|
|
let def_kind = match self.find(hir_id)? {
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Item(item) => match item.kind {
|
2022-03-29 17:11:12 +02:00
|
|
|
ItemKind::Static(_, mt, _) => DefKind::Static(mt),
|
2019-04-20 18:26:26 +03:00
|
|
|
ItemKind::Const(..) => DefKind::Const,
|
|
|
|
ItemKind::Fn(..) => DefKind::Fn,
|
2021-12-11 19:52:23 +08:00
|
|
|
ItemKind::Macro(_, macro_kind) => DefKind::Macro(macro_kind),
|
2019-04-20 18:26:26 +03:00
|
|
|
ItemKind::Mod(..) => DefKind::Mod,
|
2019-08-01 00:41:54 +01:00
|
|
|
ItemKind::OpaqueTy(..) => DefKind::OpaqueTy,
|
2019-08-02 11:02:08 +01:00
|
|
|
ItemKind::TyAlias(..) => DefKind::TyAlias,
|
2019-04-20 18:26:26 +03:00
|
|
|
ItemKind::Enum(..) => DefKind::Enum,
|
|
|
|
ItemKind::Struct(..) => DefKind::Struct,
|
|
|
|
ItemKind::Union(..) => DefKind::Union,
|
|
|
|
ItemKind::Trait(..) => DefKind::Trait,
|
|
|
|
ItemKind::TraitAlias(..) => DefKind::TraitAlias,
|
2020-03-16 10:01:03 -05:00
|
|
|
ItemKind::ExternCrate(_) => DefKind::ExternCrate,
|
|
|
|
ItemKind::Use(..) => DefKind::Use,
|
2020-11-11 22:40:09 +01:00
|
|
|
ItemKind::ForeignMod { .. } => DefKind::ForeignMod,
|
2020-03-16 10:01:03 -05:00
|
|
|
ItemKind::GlobalAsm(..) => DefKind::GlobalAsm,
|
|
|
|
ItemKind::Impl { .. } => DefKind::Impl,
|
2018-02-19 16:46:40 +01:00
|
|
|
},
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::ForeignItem(item) => match item.kind {
|
2019-04-20 18:26:26 +03:00
|
|
|
ForeignItemKind::Fn(..) => DefKind::Fn,
|
2022-03-29 17:11:12 +02:00
|
|
|
ForeignItemKind::Static(_, mt) => DefKind::Static(mt),
|
2019-04-20 18:26:26 +03:00
|
|
|
ForeignItemKind::Type => DefKind::ForeignTy,
|
2018-02-19 16:46:40 +01:00
|
|
|
},
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::TraitItem(item) => match item.kind {
|
2019-05-19 16:26:08 +08:00
|
|
|
TraitItemKind::Const(..) => DefKind::AssocConst,
|
2020-03-03 12:46:22 -06:00
|
|
|
TraitItemKind::Fn(..) => DefKind::AssocFn,
|
2019-05-19 16:26:08 +08:00
|
|
|
TraitItemKind::Type(..) => DefKind::AssocTy,
|
2018-02-19 16:46:40 +01:00
|
|
|
},
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::ImplItem(item) => match item.kind {
|
2019-05-19 16:26:08 +08:00
|
|
|
ImplItemKind::Const(..) => DefKind::AssocConst,
|
2020-03-05 09:57:34 -06:00
|
|
|
ImplItemKind::Fn(..) => DefKind::AssocFn,
|
2019-08-02 20:59:07 +01:00
|
|
|
ImplItemKind::TyAlias(..) => DefKind::AssocTy,
|
2018-02-19 16:46:40 +01:00
|
|
|
},
|
2019-04-20 18:26:26 +03:00
|
|
|
Node::Variant(_) => DefKind::Variant,
|
2019-03-24 18:21:59 +01:00
|
|
|
Node::Ctor(variant_data) => {
|
2019-04-20 18:26:26 +03:00
|
|
|
// FIXME(eddyb) is this even possible, if we have a `Node::Ctor`?
|
2020-04-17 21:55:17 +03:00
|
|
|
assert_ne!(variant_data.ctor_hir_id(), None);
|
2020-03-01 22:04:42 +01:00
|
|
|
|
2019-06-24 09:58:49 +02:00
|
|
|
let ctor_of = match self.find(self.get_parent_node(hir_id)) {
|
2019-03-24 18:54:56 +01:00
|
|
|
Some(Node::Item(..)) => def::CtorOf::Struct,
|
|
|
|
Some(Node::Variant(..)) => def::CtorOf::Variant,
|
2019-03-24 18:21:59 +01:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2019-04-20 18:26:26 +03:00
|
|
|
DefKind::Ctor(ctor_of, def::CtorKind::from_hir(variant_data))
|
2019-02-03 14:09:56 +01:00
|
|
|
}
|
2021-10-02 12:59:26 +01:00
|
|
|
Node::AnonConst(_) => {
|
|
|
|
let inline = match self.find(self.get_parent_node(hir_id)) {
|
|
|
|
Some(Node::Expr(&Expr {
|
|
|
|
kind: ExprKind::ConstBlock(ref anon_const), ..
|
|
|
|
})) if anon_const.hir_id == hir_id => true,
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
if inline { DefKind::InlineConst } else { DefKind::AnonConst }
|
|
|
|
}
|
2020-03-16 10:01:03 -05:00
|
|
|
Node::Field(_) => DefKind::Field,
|
|
|
|
Node::Expr(expr) => match expr.kind {
|
2022-07-11 23:39:53 +04:00
|
|
|
ExprKind::Closure(Closure { movability: None, .. }) => DefKind::Closure,
|
|
|
|
ExprKind::Closure(Closure { movability: Some(_), .. }) => DefKind::Generator,
|
2020-03-16 10:01:03 -05:00
|
|
|
_ => bug!("def_kind: unsupported node: {}", self.node_to_string(hir_id)),
|
|
|
|
},
|
|
|
|
Node::GenericParam(param) => match param.kind {
|
|
|
|
GenericParamKind::Lifetime { .. } => DefKind::LifetimeParam,
|
|
|
|
GenericParamKind::Type { .. } => DefKind::TyParam,
|
|
|
|
GenericParamKind::Const { .. } => DefKind::ConstParam,
|
|
|
|
},
|
2021-01-18 19:15:53 +01:00
|
|
|
Node::Crate(_) => DefKind::Mod,
|
2020-03-16 10:01:03 -05:00
|
|
|
Node::Stmt(_)
|
2018-09-11 15:08:47 +12:00
|
|
|
| Node::PathSegment(_)
|
2018-08-25 15:56:16 +01:00
|
|
|
| Node::Ty(_)
|
2022-05-07 22:41:53 +02:00
|
|
|
| Node::TypeBinding(_)
|
2021-04-26 18:19:23 +00:00
|
|
|
| Node::Infer(_)
|
2018-08-25 15:56:16 +01:00
|
|
|
| Node::TraitRef(_)
|
|
|
|
| Node::Pat(_)
|
2019-04-20 19:46:19 +03:00
|
|
|
| Node::Local(_)
|
2019-08-27 13:24:32 +02:00
|
|
|
| Node::Param(_)
|
2019-03-30 22:54:29 +00:00
|
|
|
| Node::Arm(_)
|
2018-08-25 15:56:16 +01:00
|
|
|
| Node::Lifetime(_)
|
2021-01-18 19:15:53 +01:00
|
|
|
| Node::Block(_) => return None,
|
2021-01-16 14:35:16 +01:00
|
|
|
};
|
|
|
|
Some(def_kind)
|
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn find_parent_node(self, id: HirId) -> Option<HirId> {
|
2020-03-10 13:44:53 -07:00
|
|
|
if id.local_id == ItemLocalId::from_u32(0) {
|
2021-03-06 11:25:41 +01:00
|
|
|
Some(self.tcx.hir_owner_parent(id.owner))
|
2020-02-07 16:43:36 +01:00
|
|
|
} else {
|
2021-09-22 19:28:20 +02:00
|
|
|
let owner = self.tcx.hir_owner_nodes(id.owner).as_owner()?;
|
2021-03-03 21:50:45 +01:00
|
|
|
let node = owner.nodes[id.local_id].as_ref()?;
|
|
|
|
let hir_id = HirId { owner: id.owner, local_id: node.parent };
|
|
|
|
Some(hir_id)
|
2020-02-07 16:43:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn get_parent_node(self, hir_id: HirId) -> HirId {
|
2022-05-07 22:41:53 +02:00
|
|
|
self.find_parent_node(hir_id)
|
|
|
|
.unwrap_or_else(|| bug!("No parent for node {:?}", self.node_to_string(hir_id)))
|
2021-03-03 21:50:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn find(self, id: HirId) -> Option<Node<'hir>> {
|
2021-03-03 21:50:45 +01:00
|
|
|
if id.local_id == ItemLocalId::from_u32(0) {
|
2022-01-28 14:58:27 -03:00
|
|
|
let owner = self.tcx.hir_owner(id.owner)?;
|
2021-03-27 13:21:26 +01:00
|
|
|
Some(owner.node.into())
|
2021-03-03 21:50:45 +01:00
|
|
|
} else {
|
2021-09-22 19:28:20 +02:00
|
|
|
let owner = self.tcx.hir_owner_nodes(id.owner).as_owner()?;
|
2021-03-03 21:50:45 +01:00
|
|
|
let node = owner.nodes[id.local_id].as_ref()?;
|
|
|
|
Some(node.node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 19:41:47 +02:00
|
|
|
/// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
|
|
|
|
#[inline]
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn find_by_def_id(self, id: LocalDefId) -> Option<Node<'hir>> {
|
2021-10-21 19:41:47 +02:00
|
|
|
self.find(self.local_def_id_to_hir_id(id))
|
|
|
|
}
|
|
|
|
|
2021-03-03 21:50:45 +01:00
|
|
|
/// Retrieves the `Node` corresponding to `id`, panicking if it cannot be found.
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn get(self, id: HirId) -> Node<'hir> {
|
2021-03-03 21:50:45 +01:00
|
|
|
self.find(id).unwrap_or_else(|| bug!("couldn't find hir id {} in the HIR map", id))
|
|
|
|
}
|
|
|
|
|
2021-10-21 19:41:47 +02:00
|
|
|
/// Retrieves the `Node` corresponding to `id`, panicking if it cannot be found.
|
|
|
|
#[inline]
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn get_by_def_id(self, id: LocalDefId) -> Node<'hir> {
|
2021-10-21 19:41:47 +02:00
|
|
|
self.find_by_def_id(id).unwrap_or_else(|| bug!("couldn't find {:?} in the HIR map", id))
|
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn get_if_local(self, id: DefId) -> Option<Node<'hir>> {
|
2021-03-03 21:50:45 +01:00
|
|
|
id.as_local().and_then(|id| self.find(self.local_def_id_to_hir_id(id)))
|
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn get_generics(self, id: LocalDefId) -> Option<&'hir Generics<'hir>> {
|
2022-01-28 14:58:27 -03:00
|
|
|
let node = self.tcx.hir_owner(id)?;
|
2022-05-27 09:35:28 +02:00
|
|
|
node.node.generics()
|
2020-03-16 19:17:40 +01:00
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn item(self, id: ItemId) -> &'hir Item<'hir> {
|
2021-03-27 13:56:22 +01:00
|
|
|
self.tcx.hir_owner(id.def_id).unwrap().node.expect_item()
|
2020-01-07 15:51:38 +01:00
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn trait_item(self, id: TraitItemId) -> &'hir TraitItem<'hir> {
|
2021-03-27 13:56:22 +01:00
|
|
|
self.tcx.hir_owner(id.def_id).unwrap().node.expect_trait_item()
|
2016-12-04 04:21:06 +02:00
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn impl_item(self, id: ImplItemId) -> &'hir ImplItem<'hir> {
|
2021-03-27 13:56:22 +01:00
|
|
|
self.tcx.hir_owner(id.def_id).unwrap().node.expect_impl_item()
|
2016-11-02 18:25:31 -04:00
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn foreign_item(self, id: ForeignItemId) -> &'hir ForeignItem<'hir> {
|
2021-03-27 13:56:22 +01:00
|
|
|
self.tcx.hir_owner(id.def_id).unwrap().node.expect_foreign_item()
|
2020-11-11 21:57:54 +01:00
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn body(self, id: BodyId) -> &'hir Body<'hir> {
|
2021-10-21 23:08:57 +02:00
|
|
|
self.tcx.hir_owner_nodes(id.hir_id.owner).unwrap().bodies[&id.hir_id.local_id]
|
2016-12-21 12:32:59 +02:00
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn fn_decl_by_hir_id(self, hir_id: HirId) -> Option<&'hir FnDecl<'hir>> {
|
2020-02-07 15:34:39 +01:00
|
|
|
if let Some(node) = self.find(hir_id) {
|
|
|
|
fn_decl(node)
|
2019-03-09 08:57:35 +01:00
|
|
|
} else {
|
2020-02-07 15:34:39 +01:00
|
|
|
bug!("no node for hir_id `{}`", hir_id)
|
2019-03-09 08:57:35 +01:00
|
|
|
}
|
2019-02-04 11:09:32 +01:00
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn fn_sig_by_hir_id(self, hir_id: HirId) -> Option<&'hir FnSig<'hir>> {
|
2020-02-07 15:34:39 +01:00
|
|
|
if let Some(node) = self.find(hir_id) {
|
|
|
|
fn_sig(node)
|
2019-11-06 11:43:33 -08:00
|
|
|
} else {
|
2020-02-07 15:34:39 +01:00
|
|
|
bug!("no node for hir_id `{}`", hir_id)
|
2019-11-06 11:43:33 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn enclosing_body_owner(self, hir_id: HirId) -> HirId {
|
2020-05-20 14:57:16 +02:00
|
|
|
for (parent, _) in self.parent_iter(hir_id) {
|
2022-07-15 23:13:04 -04:00
|
|
|
if let Some(local_did) = parent.as_owner() && let Some(body) = self.maybe_body_owned_by(local_did) {
|
2020-05-20 14:57:16 +02:00
|
|
|
return self.body_owner(body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bug!("no `enclosing_body_owner` for hir_id `{}`", hir_id);
|
|
|
|
}
|
|
|
|
|
2019-06-16 17:30:53 +02:00
|
|
|
/// Returns the `HirId` that corresponds to the definition of
|
2018-11-27 02:59:49 +00:00
|
|
|
/// which this is the body of, i.e., a `fn`, `const` or `static`
|
2018-05-17 21:28:50 +03:00
|
|
|
/// item (possibly associated), a closure, or a `hir::AnonConst`.
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn body_owner(self, BodyId { hir_id }: BodyId) -> HirId {
|
2019-06-24 09:46:38 +02:00
|
|
|
let parent = self.get_parent_node(hir_id);
|
2020-02-07 16:10:50 +01:00
|
|
|
assert!(self.find(parent).map_or(false, |n| is_body_owner(n, hir_id)));
|
2019-06-14 12:28:47 +02:00
|
|
|
parent
|
2016-12-20 23:05:21 +02:00
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn body_owner_def_id(self, id: BodyId) -> LocalDefId {
|
2020-04-09 09:43:00 +01:00
|
|
|
self.local_def_id(self.body_owner(id))
|
2016-12-20 23:05:21 +02:00
|
|
|
}
|
|
|
|
|
2022-07-15 23:13:04 -04:00
|
|
|
/// Given a `LocalDefId`, returns the `BodyId` associated with it,
|
2017-04-24 22:03:47 +03:00
|
|
|
/// if the node is a body owner, otherwise returns `None`.
|
2022-07-15 23:13:04 -04:00
|
|
|
pub fn maybe_body_owned_by(self, id: LocalDefId) -> Option<BodyId> {
|
|
|
|
self.get_if_local(id.to_def_id()).map(associated_body).flatten()
|
2017-04-04 12:06:35 -04:00
|
|
|
}
|
|
|
|
|
2017-04-24 22:03:47 +03:00
|
|
|
/// Given a body owner's id, returns the `BodyId` associated with it.
|
2022-07-15 23:13:04 -04:00
|
|
|
pub fn body_owned_by(self, id: LocalDefId) -> BodyId {
|
2019-06-14 18:58:55 +02:00
|
|
|
self.maybe_body_owned_by(id).unwrap_or_else(|| {
|
2022-07-15 23:13:04 -04:00
|
|
|
let hir_id = self.local_def_id_to_hir_id(id);
|
2019-06-14 18:58:55 +02:00
|
|
|
span_bug!(
|
2022-07-15 23:13:04 -04:00
|
|
|
self.span(hir_id),
|
2019-06-14 18:58:55 +02:00
|
|
|
"body_owned_by: {} has no associated body",
|
2022-07-15 23:13:04 -04:00
|
|
|
self.node_to_string(hir_id)
|
2019-06-16 17:30:02 +02:00
|
|
|
);
|
2017-04-24 22:03:47 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn body_param_names(self, id: BodyId) -> impl Iterator<Item = Ident> + 'hir {
|
2020-06-11 13:42:22 -04:00
|
|
|
self.body(id).params.iter().map(|arg| match arg.pat.kind {
|
|
|
|
PatKind::Binding(_, _, ident, _) => ident,
|
2021-10-17 23:20:30 +03:00
|
|
|
_ => Ident::empty(),
|
2020-06-11 13:42:22 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-07 10:22:35 -07:00
|
|
|
/// Returns the `BodyOwnerKind` of this `LocalDefId`.
|
|
|
|
///
|
|
|
|
/// Panics if `LocalDefId` does not have an associated body.
|
2022-03-29 17:11:12 +02:00
|
|
|
pub fn body_owner_kind(self, def_id: LocalDefId) -> BodyOwnerKind {
|
|
|
|
match self.tcx.def_kind(def_id) {
|
|
|
|
DefKind::Const | DefKind::AssocConst | DefKind::InlineConst | DefKind::AnonConst => {
|
|
|
|
BodyOwnerKind::Const
|
|
|
|
}
|
|
|
|
DefKind::Ctor(..) | DefKind::Fn | DefKind::AssocFn => BodyOwnerKind::Fn,
|
|
|
|
DefKind::Closure | DefKind::Generator => BodyOwnerKind::Closure,
|
|
|
|
DefKind::Static(mt) => BodyOwnerKind::Static(mt),
|
|
|
|
dk => bug!("{:?} is not a body node: {:?}", def_id, dk),
|
2017-11-10 19:20:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 10:22:35 -07:00
|
|
|
/// Returns the `ConstContext` of the body associated with this `LocalDefId`.
|
|
|
|
///
|
|
|
|
/// Panics if `LocalDefId` does not have an associated body.
|
2021-07-10 11:16:03 +08:00
|
|
|
///
|
|
|
|
/// This should only be used for determining the context of a body, a return
|
2021-12-12 12:34:46 +08:00
|
|
|
/// value of `Some` does not always suggest that the owner of the body is `const`,
|
|
|
|
/// just that it has to be checked as if it were.
|
2022-03-29 17:11:12 +02:00
|
|
|
pub fn body_const_context(self, def_id: LocalDefId) -> Option<ConstContext> {
|
|
|
|
let ccx = match self.body_owner_kind(def_id) {
|
2020-05-07 10:22:35 -07:00
|
|
|
BodyOwnerKind::Const => ConstContext::Const,
|
|
|
|
BodyOwnerKind::Static(mt) => ConstContext::Static(mt),
|
|
|
|
|
2022-03-29 17:11:12 +02:00
|
|
|
BodyOwnerKind::Fn if self.tcx.is_constructor(def_id.to_def_id()) => return None,
|
|
|
|
BodyOwnerKind::Fn if self.tcx.is_const_fn_raw(def_id.to_def_id()) => {
|
|
|
|
ConstContext::ConstFn
|
|
|
|
}
|
2022-05-11 16:02:20 +00:00
|
|
|
BodyOwnerKind::Fn if self.tcx.is_const_default_method(def_id.to_def_id()) => {
|
2021-07-10 11:16:03 +08:00
|
|
|
ConstContext::ConstFn
|
|
|
|
}
|
2020-05-07 10:22:35 -07:00
|
|
|
BodyOwnerKind::Fn | BodyOwnerKind::Closure => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(ccx)
|
|
|
|
}
|
|
|
|
|
2021-09-12 11:33:16 +02:00
|
|
|
/// Returns an iterator of the `DefId`s for all body-owners in this
|
|
|
|
/// crate. If you would prefer to iterate over the bodies
|
|
|
|
/// themselves, you can do `self.hir().krate().body_ids.iter()`.
|
|
|
|
pub fn body_owners(self) -> impl Iterator<Item = LocalDefId> + 'hir {
|
2022-06-15 22:29:59 -04:00
|
|
|
self.tcx.hir_crate_items(()).body_owners.iter().copied()
|
2021-09-12 11:33:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn par_body_owners<F: Fn(LocalDefId) + Sync + Send>(self, f: F) {
|
2022-07-19 17:00:51 +08:00
|
|
|
par_for_each_in(&self.tcx.hir_crate_items(()).body_owners[..], |&def_id| f(def_id));
|
2021-09-12 11:33:16 +02:00
|
|
|
}
|
|
|
|
|
2022-04-07 22:24:07 +02:00
|
|
|
pub fn ty_param_owner(self, def_id: LocalDefId) -> LocalDefId {
|
|
|
|
let def_kind = self.tcx.def_kind(def_id);
|
|
|
|
match def_kind {
|
|
|
|
DefKind::Trait | DefKind::TraitAlias => def_id,
|
2022-04-25 22:08:45 +03:00
|
|
|
DefKind::TyParam | DefKind::ConstParam => self.tcx.local_parent(def_id),
|
2022-04-07 22:24:07 +02:00
|
|
|
_ => bug!("ty_param_owner: {:?} is a {:?} not a type parameter", def_id, def_kind),
|
2017-02-11 19:26:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-08 23:06:20 +02:00
|
|
|
pub fn ty_param_name(self, def_id: LocalDefId) -> Symbol {
|
|
|
|
let def_kind = self.tcx.def_kind(def_id);
|
|
|
|
match def_kind {
|
|
|
|
DefKind::Trait | DefKind::TraitAlias => kw::SelfUpper,
|
|
|
|
DefKind::TyParam | DefKind::ConstParam => self.tcx.item_name(def_id.to_def_id()),
|
|
|
|
_ => bug!("ty_param_name: {:?} is a {:?} not a type parameter", def_id, def_kind),
|
2017-02-11 19:26:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn trait_impls(self, trait_did: DefId) -> &'hir [LocalDefId] {
|
2021-05-11 11:42:01 +02:00
|
|
|
self.tcx.all_local_trait_impls(()).get(&trait_did).map_or(&[], |xs| &xs[..])
|
2017-02-19 14:46:29 +02:00
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Gets the attributes on the crate. This is preferable to
|
2016-03-16 05:53:45 -04:00
|
|
|
/// invoking `krate.attrs` because it registers a tighter
|
|
|
|
/// dep-graph access.
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn krate_attrs(self) -> &'hir [ast::Attribute] {
|
2020-11-24 18:12:42 +01:00
|
|
|
self.attrs(CRATE_HIR_ID)
|
2016-03-16 05:53:45 -04:00
|
|
|
}
|
|
|
|
|
2022-03-15 16:30:30 +01:00
|
|
|
pub fn rustc_coherence_is_core(self) -> bool {
|
|
|
|
self.krate_attrs().iter().any(|attr| attr.has_name(sym::rustc_coherence_is_core))
|
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn get_module(self, module: LocalDefId) -> (&'hir Mod<'hir>, Span, HirId) {
|
2021-03-27 13:56:22 +01:00
|
|
|
let hir_id = HirId::make_owner(module);
|
|
|
|
match self.tcx.hir_owner(module).map(|o| o.node) {
|
2022-01-28 14:58:27 -03:00
|
|
|
Some(OwnerNode::Item(&Item { span, kind: ItemKind::Mod(ref m), .. })) => {
|
|
|
|
(m, span, hir_id)
|
|
|
|
}
|
2022-03-18 17:13:38 -04:00
|
|
|
Some(OwnerNode::Crate(item)) => (item, item.spans.inner_span, hir_id),
|
2019-08-21 16:11:01 -07:00
|
|
|
node => panic!("not a module: {:?}", node),
|
2018-06-08 19:14:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-03 15:28:57 +02:00
|
|
|
/// Walks the contents of the local crate. See also `visit_all_item_likes_in_crate`.
|
2021-09-02 19:22:24 +02:00
|
|
|
pub fn walk_toplevel_module(self, visitor: &mut impl Visitor<'hir>) {
|
2021-08-26 18:42:08 +02:00
|
|
|
let (top_mod, span, hir_id) = self.get_module(CRATE_DEF_ID);
|
|
|
|
visitor.visit_mod(top_mod, span, hir_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Walks the attributes in a crate.
|
|
|
|
pub fn walk_attributes(self, visitor: &mut impl Visitor<'hir>) {
|
|
|
|
let krate = self.krate();
|
2022-06-16 07:54:03 +10:00
|
|
|
for info in krate.owners.iter() {
|
2021-09-22 19:28:20 +02:00
|
|
|
if let MaybeOwner::Owner(info) = info {
|
2022-06-16 07:54:03 +10:00
|
|
|
for attrs in info.attrs.map.values() {
|
2021-07-16 14:42:26 +02:00
|
|
|
for a in *attrs {
|
2022-06-16 07:54:03 +10:00
|
|
|
visitor.visit_attribute(a)
|
2021-07-16 14:42:26 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-26 18:42:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-03 15:28:57 +02:00
|
|
|
/// Visits all item-likes in the crate in some deterministic (but unspecified) order. If you
|
|
|
|
/// need to process every item-like, and don't care about visiting nested items in a particular
|
|
|
|
/// order then this method is the best choice. If you do care about this nesting, you should
|
|
|
|
/// use the `tcx.hir().walk_toplevel_module`.
|
|
|
|
///
|
|
|
|
/// Note that this function will access HIR for all the item-likes in the crate. If you only
|
|
|
|
/// need to access some of them, it is usually better to manually loop on the iterators
|
|
|
|
/// provided by `tcx.hir_crate_items(())`.
|
2021-09-12 01:11:22 +02:00
|
|
|
///
|
2022-05-07 16:35:38 -04:00
|
|
|
/// Please see the notes in `intravisit.rs` for more information.
|
2022-07-03 15:28:57 +02:00
|
|
|
pub fn visit_all_item_likes_in_crate<V>(self, visitor: &mut V)
|
2021-09-12 01:11:22 +02:00
|
|
|
where
|
2022-05-07 15:43:10 -04:00
|
|
|
V: Visitor<'hir>,
|
2021-09-12 01:11:22 +02:00
|
|
|
{
|
2022-07-03 15:28:57 +02:00
|
|
|
let krate = self.tcx.hir_crate_items(());
|
|
|
|
|
|
|
|
for id in krate.items() {
|
|
|
|
visitor.visit_item(self.item(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
for id in krate.trait_items() {
|
|
|
|
visitor.visit_trait_item(self.trait_item(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
for id in krate.impl_items() {
|
|
|
|
visitor.visit_impl_item(self.impl_item(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
for id in krate.foreign_items() {
|
|
|
|
visitor.visit_foreign_item(self.foreign_item(id));
|
2021-09-12 01:11:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-03 15:28:57 +02:00
|
|
|
/// This method is the equivalent of `visit_all_item_likes_in_crate` but restricted to
|
|
|
|
/// item-likes in a single module.
|
|
|
|
pub fn visit_item_likes_in_module<V>(self, module: LocalDefId, visitor: &mut V)
|
2018-06-06 22:13:52 +02:00
|
|
|
where
|
2022-05-07 15:43:10 -04:00
|
|
|
V: Visitor<'hir>,
|
2018-06-06 22:13:52 +02:00
|
|
|
{
|
2020-06-27 13:09:54 +02:00
|
|
|
let module = self.tcx.hir_module_items(module);
|
2018-06-06 22:13:52 +02:00
|
|
|
|
2022-07-03 15:28:57 +02:00
|
|
|
for id in module.items() {
|
|
|
|
visitor.visit_item(self.item(id));
|
2018-06-06 22:13:52 +02:00
|
|
|
}
|
|
|
|
|
2022-07-03 15:28:57 +02:00
|
|
|
for id in module.trait_items() {
|
|
|
|
visitor.visit_trait_item(self.trait_item(id));
|
2018-06-06 22:13:52 +02:00
|
|
|
}
|
|
|
|
|
2022-07-03 15:28:57 +02:00
|
|
|
for id in module.impl_items() {
|
|
|
|
visitor.visit_impl_item(self.impl_item(id));
|
2018-06-06 22:13:52 +02:00
|
|
|
}
|
2020-11-11 21:57:54 +01:00
|
|
|
|
2022-07-03 15:28:57 +02:00
|
|
|
for id in module.foreign_items() {
|
|
|
|
visitor.visit_foreign_item(self.foreign_item(id));
|
2020-11-11 21:57:54 +01:00
|
|
|
}
|
2018-06-06 22:13:52 +02:00
|
|
|
}
|
|
|
|
|
2022-05-09 18:44:04 -04:00
|
|
|
pub fn for_each_module(self, mut f: impl FnMut(LocalDefId)) {
|
2022-04-03 15:50:33 -04:00
|
|
|
let crate_items = self.tcx.hir_crate_items(());
|
|
|
|
for module in crate_items.submodules.iter() {
|
|
|
|
f(*module)
|
2021-07-18 18:12:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 17:24:03 +02:00
|
|
|
#[cfg(not(parallel_compiler))]
|
|
|
|
#[inline]
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn par_for_each_module(self, f: impl Fn(LocalDefId)) {
|
2021-09-08 17:24:03 +02:00
|
|
|
self.for_each_module(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(parallel_compiler)]
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn par_for_each_module(self, f: impl Fn(LocalDefId) + Sync) {
|
2021-09-08 17:24:03 +02:00
|
|
|
use rustc_data_structures::sync::{par_iter, ParallelIterator};
|
2021-07-18 18:12:17 +02:00
|
|
|
par_iter_submodules(self.tcx, CRATE_DEF_ID, &f);
|
|
|
|
|
|
|
|
fn par_iter_submodules<F>(tcx: TyCtxt<'_>, module: LocalDefId, f: &F)
|
|
|
|
where
|
2021-09-08 17:24:03 +02:00
|
|
|
F: Fn(LocalDefId) + Sync,
|
2021-07-18 18:12:17 +02:00
|
|
|
{
|
|
|
|
(*f)(module);
|
|
|
|
let items = tcx.hir_module_items(module);
|
2021-09-07 20:07:33 +02:00
|
|
|
par_iter(&items.submodules[..]).for_each(|&sm| par_iter_submodules(tcx, sm, f));
|
2021-07-18 18:12:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 05:30:11 +01:00
|
|
|
/// Returns an iterator for the nodes in the ancestor tree of the `current_id`
|
|
|
|
/// until the crate root is reached. Prefer this over your own loop using `get_parent_node`.
|
2021-09-18 15:48:07 -05:00
|
|
|
pub fn parent_iter(self, current_id: HirId) -> ParentHirIterator<'hir> {
|
2020-02-25 05:30:11 +01:00
|
|
|
ParentHirIterator { current_id, map: self }
|
|
|
|
}
|
|
|
|
|
2021-03-03 21:50:45 +01:00
|
|
|
/// Returns an iterator for the nodes in the ancestor tree of the `current_id`
|
|
|
|
/// until the crate root is reached. Prefer this over your own loop using `get_parent_node`.
|
2021-09-18 15:48:07 -05:00
|
|
|
pub fn parent_owner_iter(self, current_id: HirId) -> ParentOwnerIterator<'hir> {
|
2021-03-03 21:50:45 +01:00
|
|
|
ParentOwnerIterator { current_id, map: self }
|
|
|
|
}
|
|
|
|
|
2021-01-24 10:32:24 +03:00
|
|
|
/// Checks if the node is left-hand side of an assignment.
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn is_lhs(self, id: HirId) -> bool {
|
2021-01-24 10:32:24 +03:00
|
|
|
match self.find(self.get_parent_node(id)) {
|
|
|
|
Some(Node::Expr(expr)) => match expr.kind {
|
|
|
|
ExprKind::Assign(lhs, _rhs, _span) => lhs.hir_id == id,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-13 11:24:08 -07:00
|
|
|
/// Whether the expression pointed at by `hir_id` belongs to a `const` evaluation context.
|
|
|
|
/// Used exclusively for diagnostics, to avoid suggestion function calls.
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn is_inside_const_context(self, hir_id: HirId) -> bool {
|
2020-05-20 14:57:16 +02:00
|
|
|
self.body_const_context(self.local_def_id(self.enclosing_body_owner(hir_id))).is_some()
|
2019-04-29 17:14:31 -07:00
|
|
|
}
|
|
|
|
|
2019-06-16 17:44:19 +02:00
|
|
|
/// Retrieves the `HirId` for `id`'s enclosing method, unless there's a
|
2017-08-01 19:13:47 -07:00
|
|
|
/// `while` or `loop` before reaching it, as block tail returns are not
|
2017-06-27 13:34:56 -07:00
|
|
|
/// available in them.
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// fn foo(x: usize) -> bool {
|
|
|
|
/// if x == 1 {
|
2019-09-06 03:57:44 +01:00
|
|
|
/// true // If `get_return_block` gets passed the `id` corresponding
|
2019-06-16 17:44:19 +02:00
|
|
|
/// } else { // to this, it will return `foo`'s `HirId`.
|
2017-06-27 13:34:56 -07:00
|
|
|
/// false
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2022-04-15 15:04:34 -07:00
|
|
|
/// ```compile_fail,E0308
|
2017-06-27 13:34:56 -07:00
|
|
|
/// fn foo(x: usize) -> bool {
|
|
|
|
/// loop {
|
2019-09-06 03:57:44 +01:00
|
|
|
/// true // If `get_return_block` gets passed the `id` corresponding
|
2017-06-27 13:34:56 -07:00
|
|
|
/// } // to this, it will return `None`.
|
|
|
|
/// false
|
|
|
|
/// }
|
|
|
|
/// ```
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn get_return_block(self, id: HirId) -> Option<HirId> {
|
2020-02-25 05:30:11 +01:00
|
|
|
let mut iter = self.parent_iter(id).peekable();
|
2019-09-25 23:01:01 -07:00
|
|
|
let mut ignore_tail = false;
|
2021-03-03 21:50:45 +01:00
|
|
|
if let Some(node) = self.find(id) {
|
|
|
|
if let Node::Expr(Expr { kind: ExprKind::Ret(_), .. }) = node {
|
2019-09-25 23:01:01 -07:00
|
|
|
// When dealing with `return` statements, we don't care about climbing only tail
|
|
|
|
// expressions.
|
|
|
|
ignore_tail = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while let Some((hir_id, node)) = iter.next() {
|
|
|
|
if let (Some((_, next_node)), false) = (iter.peek(), ignore_tail) {
|
|
|
|
match next_node {
|
|
|
|
Node::Block(Block { expr: None, .. }) => return None,
|
2020-03-24 06:17:44 +01:00
|
|
|
// The current node is not the tail expression of its parent.
|
|
|
|
Node::Block(Block { expr: Some(e), .. }) if hir_id != e.hir_id => return None,
|
2019-09-25 23:01:01 -07:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2019-09-25 17:05:30 -07:00
|
|
|
match node {
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Item(_)
|
|
|
|
| Node::ForeignItem(_)
|
|
|
|
| Node::TraitItem(_)
|
2022-06-11 21:25:25 +02:00
|
|
|
| Node::Expr(Expr { kind: ExprKind::Closure { .. }, .. })
|
2019-09-25 17:05:30 -07:00
|
|
|
| Node::ImplItem(_) => return Some(hir_id),
|
2020-03-24 06:17:44 +01:00
|
|
|
// Ignore `return`s on the first iteration
|
|
|
|
Node::Expr(Expr { kind: ExprKind::Loop(..) | ExprKind::Ret(..), .. })
|
|
|
|
| Node::Local(_) => {
|
|
|
|
return None;
|
2017-06-24 00:57:39 -07:00
|
|
|
}
|
2019-09-25 17:05:30 -07:00
|
|
|
_ => {}
|
2017-06-24 00:57:39 -07:00
|
|
|
}
|
2019-09-25 17:05:30 -07:00
|
|
|
}
|
|
|
|
None
|
2017-06-24 00:57:39 -07:00
|
|
|
}
|
|
|
|
|
2019-06-14 12:28:47 +02:00
|
|
|
/// Retrieves the `HirId` for `id`'s parent item, or `id` itself if no
|
2015-07-01 08:58:48 +12:00
|
|
|
/// parent item is in this map. The "parent item" is the closest parent node
|
2018-06-13 09:11:23 +02:00
|
|
|
/// in the HIR which is recorded by the map and is an item, either an item
|
2015-07-01 08:58:48 +12:00
|
|
|
/// in a module, trait, or impl.
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn get_parent_item(self, hir_id: HirId) -> LocalDefId {
|
2021-10-21 19:41:47 +02:00
|
|
|
if let Some((def_id, _node)) = self.parent_owner_iter(hir_id).next() {
|
|
|
|
def_id
|
2021-03-27 13:56:22 +01:00
|
|
|
} else {
|
2021-10-21 19:41:47 +02:00
|
|
|
CRATE_DEF_ID
|
2015-07-01 08:58:48 +12:00
|
|
|
}
|
2015-06-16 21:56:33 +12:00
|
|
|
}
|
|
|
|
|
2019-03-09 08:57:35 +01:00
|
|
|
/// Returns the `HirId` of `id`'s nearest module parent, or `id` itself if no
|
2018-09-19 09:28:49 -05:00
|
|
|
/// module parent is in this map.
|
2022-02-10 11:42:02 +01:00
|
|
|
pub(super) fn get_module_parent_node(self, hir_id: HirId) -> LocalDefId {
|
2021-10-21 19:41:47 +02:00
|
|
|
for (def_id, node) in self.parent_owner_iter(hir_id) {
|
2021-03-27 13:56:22 +01:00
|
|
|
if let OwnerNode::Item(&Item { kind: ItemKind::Mod(_), .. }) = node {
|
2021-10-21 19:41:47 +02:00
|
|
|
return def_id;
|
2019-09-25 17:05:30 -07:00
|
|
|
}
|
2018-09-19 09:28:49 -05:00
|
|
|
}
|
2021-10-21 19:41:47 +02:00
|
|
|
CRATE_DEF_ID
|
2016-02-25 01:55:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 15:38:11 -03:00
|
|
|
/// When on an if expression, a match arm tail expression or a match arm, give back
|
|
|
|
/// the enclosing `if` or `match` expression.
|
2019-09-27 18:35:34 -07:00
|
|
|
///
|
2021-01-01 15:38:11 -03:00
|
|
|
/// Used by error reporting when there's a type error in an if or match arm caused by the
|
2019-09-27 18:35:34 -07:00
|
|
|
/// expression needing to be unit.
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn get_if_cause(self, hir_id: HirId) -> Option<&'hir Expr<'hir>> {
|
2020-02-25 05:30:11 +01:00
|
|
|
for (_, node) in self.parent_iter(hir_id) {
|
2019-09-26 17:16:34 -07:00
|
|
|
match node {
|
2020-03-24 06:17:44 +01:00
|
|
|
Node::Item(_)
|
|
|
|
| Node::ForeignItem(_)
|
|
|
|
| Node::TraitItem(_)
|
|
|
|
| Node::ImplItem(_)
|
|
|
|
| Node::Stmt(Stmt { kind: StmtKind::Local(_), .. }) => break,
|
2021-01-01 15:38:11 -03:00
|
|
|
Node::Expr(expr @ Expr { kind: ExprKind::If(..) | ExprKind::Match(..), .. }) => {
|
|
|
|
return Some(expr);
|
|
|
|
}
|
2019-09-26 17:16:34 -07:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2019-02-28 22:43:53 +00:00
|
|
|
/// Returns the nearest enclosing scope. A scope is roughly an item or block.
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn get_enclosing_scope(self, hir_id: HirId) -> Option<HirId> {
|
2020-02-25 05:30:11 +01:00
|
|
|
for (hir_id, node) in self.parent_iter(hir_id) {
|
2020-03-24 06:17:44 +01:00
|
|
|
if let Node::Item(Item {
|
|
|
|
kind:
|
2019-09-25 17:05:30 -07:00
|
|
|
ItemKind::Fn(..)
|
2020-05-10 11:57:58 +01:00
|
|
|
| ItemKind::Const(..)
|
|
|
|
| ItemKind::Static(..)
|
2019-09-25 17:05:30 -07:00
|
|
|
| ItemKind::Mod(..)
|
|
|
|
| ItemKind::Enum(..)
|
|
|
|
| ItemKind::Struct(..)
|
|
|
|
| ItemKind::Union(..)
|
|
|
|
| ItemKind::Trait(..)
|
2020-03-24 06:17:44 +01:00
|
|
|
| ItemKind::Impl { .. },
|
|
|
|
..
|
|
|
|
})
|
|
|
|
| Node::ForeignItem(ForeignItem { kind: ForeignItemKind::Fn(..), .. })
|
|
|
|
| Node::TraitItem(TraitItem { kind: TraitItemKind::Fn(..), .. })
|
|
|
|
| Node::ImplItem(ImplItem { kind: ImplItemKind::Fn(..), .. })
|
|
|
|
| Node::Block(_) = node
|
|
|
|
{
|
2019-09-25 17:05:30 -07:00
|
|
|
return Some(hir_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
2015-06-16 22:20:30 +12:00
|
|
|
}
|
|
|
|
|
2019-08-01 00:41:54 +01:00
|
|
|
/// Returns the defining scope for an opaque type definition.
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn get_defining_scope(self, id: HirId) -> HirId {
|
2019-02-28 22:43:53 +00:00
|
|
|
let mut scope = id;
|
|
|
|
loop {
|
2019-09-25 17:05:30 -07:00
|
|
|
scope = self.get_enclosing_scope(scope).unwrap_or(CRATE_HIR_ID);
|
2021-01-10 14:38:14 +01:00
|
|
|
if scope == CRATE_HIR_ID || !matches!(self.get(scope), Node::Block(_)) {
|
|
|
|
return scope;
|
2019-02-28 22:43:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn get_foreign_abi(self, hir_id: HirId) -> Abi {
|
2019-03-09 08:23:35 +01:00
|
|
|
let parent = self.get_parent_item(hir_id);
|
2022-01-28 14:58:27 -03:00
|
|
|
if let Some(node) = self.tcx.hir_owner(parent) {
|
2021-03-27 13:56:22 +01:00
|
|
|
if let OwnerNode::Item(Item { kind: ItemKind::ForeignMod { abi, .. }, .. }) = node.node
|
|
|
|
{
|
2020-11-11 22:40:09 +01:00
|
|
|
return *abi;
|
2014-05-18 02:38:13 +03:00
|
|
|
}
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
2021-10-21 19:41:47 +02:00
|
|
|
bug!(
|
|
|
|
"expected foreign mod or inlined parent, found {}",
|
|
|
|
self.node_to_string(HirId::make_owner(parent))
|
|
|
|
)
|
2019-02-04 11:09:32 +01:00
|
|
|
}
|
|
|
|
|
2022-06-12 00:47:21 +02:00
|
|
|
pub fn expect_owner(self, id: LocalDefId) -> OwnerNode<'hir> {
|
|
|
|
self.tcx.hir_owner(id).unwrap_or_else(|| bug!("expected owner for {:?}", id)).node
|
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn expect_item(self, id: LocalDefId) -> &'hir Item<'hir> {
|
2021-10-20 22:38:10 +02:00
|
|
|
match self.tcx.hir_owner(id) {
|
2022-01-28 14:58:27 -03:00
|
|
|
Some(Owner { node: OwnerNode::Item(item), .. }) => item,
|
2021-10-20 22:38:10 +02:00
|
|
|
_ => bug!("expected item, found {}", self.node_to_string(HirId::make_owner(id))),
|
2019-03-07 15:46:41 +01:00
|
|
|
}
|
2019-02-03 09:14:31 +01:00
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn expect_impl_item(self, id: LocalDefId) -> &'hir ImplItem<'hir> {
|
2021-10-20 22:38:10 +02:00
|
|
|
match self.tcx.hir_owner(id) {
|
2022-01-28 14:58:27 -03:00
|
|
|
Some(Owner { node: OwnerNode::ImplItem(item), .. }) => item,
|
2021-10-20 22:38:10 +02:00
|
|
|
_ => bug!("expected impl item, found {}", self.node_to_string(HirId::make_owner(id))),
|
2016-08-10 01:43:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn expect_trait_item(self, id: LocalDefId) -> &'hir TraitItem<'hir> {
|
2021-10-20 22:38:10 +02:00
|
|
|
match self.tcx.hir_owner(id) {
|
2022-01-28 14:58:27 -03:00
|
|
|
Some(Owner { node: OwnerNode::TraitItem(item), .. }) => item,
|
2021-10-20 22:38:10 +02:00
|
|
|
_ => bug!("expected trait item, found {}", self.node_to_string(HirId::make_owner(id))),
|
2014-04-03 13:38:45 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn expect_variant(self, id: HirId) -> &'hir Variant<'hir> {
|
2019-06-24 09:58:49 +02:00
|
|
|
match self.find(id) {
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Variant(variant)) => variant,
|
2019-06-16 17:30:02 +02:00
|
|
|
_ => bug!("expected variant, found {}", self.node_to_string(id)),
|
2014-04-08 21:00:20 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn expect_foreign_item(self, id: LocalDefId) -> &'hir ForeignItem<'hir> {
|
2021-10-20 22:38:10 +02:00
|
|
|
match self.tcx.hir_owner(id) {
|
2022-01-28 14:58:27 -03:00
|
|
|
Some(Owner { node: OwnerNode::ForeignItem(item), .. }) => item,
|
2021-10-20 22:38:10 +02:00
|
|
|
_ => {
|
|
|
|
bug!("expected foreign item, found {}", self.node_to_string(HirId::make_owner(id)))
|
|
|
|
}
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn expect_expr(self, id: HirId) -> &'hir Expr<'hir> {
|
2019-06-24 09:58:49 +02:00
|
|
|
match self.find(id) {
|
2019-03-09 08:57:35 +01:00
|
|
|
Some(Node::Expr(expr)) => expr,
|
2019-06-16 17:30:02 +02:00
|
|
|
_ => bug!("expected expr, found {}", self.node_to_string(id)),
|
2019-03-09 08:57:35 +01:00
|
|
|
}
|
2019-02-04 11:09:32 +01:00
|
|
|
}
|
|
|
|
|
2022-04-19 21:25:26 +02:00
|
|
|
#[inline]
|
|
|
|
fn opt_ident(self, id: HirId) -> Option<Ident> {
|
|
|
|
match self.get(id) {
|
2022-06-28 13:15:30 -05:00
|
|
|
Node::Pat(&Pat { kind: PatKind::Binding(_, _, ident, _), .. }) => Some(ident),
|
2022-04-08 18:53:16 +02:00
|
|
|
// A `Ctor` doesn't have an identifier itself, but its parent
|
|
|
|
// struct/variant does. Compare with `hir::Map::opt_span`.
|
|
|
|
Node::Ctor(..) => match self.find(self.get_parent_node(id))? {
|
|
|
|
Node::Item(item) => Some(item.ident),
|
|
|
|
Node::Variant(variant) => Some(variant.ident),
|
|
|
|
_ => unreachable!(),
|
|
|
|
},
|
|
|
|
node => node.ident(),
|
2022-04-19 21:25:26 +02:00
|
|
|
}
|
2022-04-08 18:53:16 +02:00
|
|
|
}
|
|
|
|
|
2022-04-19 21:25:26 +02:00
|
|
|
#[inline]
|
|
|
|
pub(super) fn opt_ident_span(self, id: HirId) -> Option<Span> {
|
|
|
|
self.opt_ident(id).map(|ident| ident.span)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn opt_name(self, id: HirId) -> Option<Symbol> {
|
2022-04-19 21:25:26 +02:00
|
|
|
self.opt_ident(id).map(|ident| ident.name)
|
2019-12-13 15:27:55 -08:00
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn name(self, id: HirId) -> Symbol {
|
2022-04-08 18:53:16 +02:00
|
|
|
self.opt_name(id).unwrap_or_else(|| bug!("no name for {}", self.node_to_string(id)))
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
|
|
|
|
2019-02-28 22:43:53 +00:00
|
|
|
/// Given a node ID, gets a list of attributes associated with the AST
|
|
|
|
/// corresponding to the node-ID.
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn attrs(self, id: HirId) -> &'hir [ast::Attribute] {
|
2021-01-24 17:14:17 +01:00
|
|
|
self.tcx.hir_attrs(id.owner).get(id.local_id)
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
|
|
|
|
2020-08-12 17:02:14 -04:00
|
|
|
/// Gets the span of the definition of the specified HIR node.
|
2022-02-13 16:27:59 +01:00
|
|
|
/// This is used by `tcx.def_span`.
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn span(self, hir_id: HirId) -> Span {
|
2020-12-23 10:32:00 +01:00
|
|
|
self.opt_span(hir_id)
|
|
|
|
.unwrap_or_else(|| bug!("hir::map::Map::span: id not in map: {:?}", hir_id))
|
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn opt_span(self, hir_id: HirId) -> Option<Span> {
|
2022-02-13 16:27:59 +01:00
|
|
|
fn until_within(outer: Span, end: Span) -> Span {
|
|
|
|
if let Some(end) = end.find_ancestor_inside(outer) {
|
|
|
|
outer.with_hi(end.hi())
|
|
|
|
} else {
|
|
|
|
outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn named_span(item_span: Span, ident: Ident, generics: Option<&Generics<'_>>) -> Span {
|
|
|
|
if ident.name != kw::Empty {
|
|
|
|
let mut span = until_within(item_span, ident.span);
|
|
|
|
if let Some(g) = generics
|
|
|
|
&& !g.span.is_dummy()
|
|
|
|
&& let Some(g_span) = g.span.find_ancestor_inside(item_span)
|
|
|
|
{
|
|
|
|
span = span.to(g_span);
|
|
|
|
}
|
|
|
|
span
|
|
|
|
} else {
|
|
|
|
item_span
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 21:50:45 +01:00
|
|
|
let span = match self.find(hir_id)? {
|
2022-02-13 16:27:59 +01:00
|
|
|
// Function-like.
|
|
|
|
Node::Item(Item { kind: ItemKind::Fn(sig, ..), .. })
|
|
|
|
| Node::TraitItem(TraitItem { kind: TraitItemKind::Fn(sig, ..), .. })
|
|
|
|
| Node::ImplItem(ImplItem { kind: ImplItemKind::Fn(sig, ..), .. }) => sig.span,
|
|
|
|
// Constants and Statics.
|
|
|
|
Node::Item(Item {
|
|
|
|
kind:
|
|
|
|
ItemKind::Const(ty, ..)
|
|
|
|
| ItemKind::Static(ty, ..)
|
|
|
|
| ItemKind::Impl(Impl { self_ty: ty, .. }),
|
|
|
|
span: outer_span,
|
|
|
|
..
|
|
|
|
})
|
|
|
|
| Node::TraitItem(TraitItem {
|
|
|
|
kind: TraitItemKind::Const(ty, ..),
|
|
|
|
span: outer_span,
|
|
|
|
..
|
|
|
|
})
|
|
|
|
| Node::ImplItem(ImplItem {
|
|
|
|
kind: ImplItemKind::Const(ty, ..),
|
|
|
|
span: outer_span,
|
|
|
|
..
|
|
|
|
})
|
|
|
|
| Node::ForeignItem(ForeignItem {
|
|
|
|
kind: ForeignItemKind::Static(ty, ..),
|
|
|
|
span: outer_span,
|
|
|
|
..
|
|
|
|
}) => until_within(*outer_span, ty.span),
|
|
|
|
// With generics and bounds.
|
|
|
|
Node::Item(Item {
|
|
|
|
kind: ItemKind::Trait(_, _, generics, bounds, _),
|
|
|
|
span: outer_span,
|
|
|
|
..
|
|
|
|
})
|
|
|
|
| Node::TraitItem(TraitItem {
|
|
|
|
kind: TraitItemKind::Type(bounds, _),
|
|
|
|
generics,
|
|
|
|
span: outer_span,
|
|
|
|
..
|
|
|
|
}) => {
|
|
|
|
let end = if let Some(b) = bounds.last() { b.span() } else { generics.span };
|
|
|
|
until_within(*outer_span, end)
|
|
|
|
}
|
|
|
|
// Other cases.
|
2020-12-23 10:32:00 +01:00
|
|
|
Node::Item(item) => match &item.kind {
|
2022-02-13 16:27:59 +01:00
|
|
|
ItemKind::Use(path, _) => path.span,
|
|
|
|
_ => named_span(item.span, item.ident, item.kind.generics()),
|
2020-08-12 17:02:14 -04:00
|
|
|
},
|
2022-07-04 18:13:16 +09:00
|
|
|
Node::Variant(variant) => named_span(variant.span, variant.ident, None),
|
2022-02-13 16:27:59 +01:00
|
|
|
Node::ImplItem(item) => named_span(item.span, item.ident, Some(item.generics)),
|
|
|
|
Node::ForeignItem(item) => match item.kind {
|
|
|
|
ForeignItemKind::Fn(decl, _, _) => until_within(item.span, decl.output.span()),
|
|
|
|
_ => named_span(item.span, item.ident, None),
|
2020-08-12 17:02:14 -04:00
|
|
|
},
|
2022-07-04 17:23:58 +09:00
|
|
|
Node::Ctor(_) => return self.opt_span(self.get_parent_node(hir_id)),
|
2022-07-11 23:39:53 +04:00
|
|
|
Node::Expr(Expr { kind: ExprKind::Closure(Closure { fn_decl_span, .. }), .. }) => {
|
|
|
|
*fn_decl_span
|
|
|
|
}
|
2022-02-13 16:27:59 +01:00
|
|
|
_ => self.span_with_body(hir_id),
|
|
|
|
};
|
|
|
|
Some(span)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Like `hir.span()`, but includes the body of items
|
|
|
|
/// (instead of just the item header)
|
|
|
|
pub fn span_with_body(self, hir_id: HirId) -> Span {
|
|
|
|
match self.get(hir_id) {
|
|
|
|
Node::Param(param) => param.span,
|
|
|
|
Node::Item(item) => item.span,
|
|
|
|
Node::ForeignItem(foreign_item) => foreign_item.span,
|
|
|
|
Node::TraitItem(trait_item) => trait_item.span,
|
|
|
|
Node::ImplItem(impl_item) => impl_item.span,
|
2020-12-23 10:32:00 +01:00
|
|
|
Node::Variant(variant) => variant.span,
|
|
|
|
Node::Field(field) => field.span,
|
|
|
|
Node::AnonConst(constant) => self.body(constant.body).value.span,
|
|
|
|
Node::Expr(expr) => expr.span,
|
|
|
|
Node::Stmt(stmt) => stmt.span,
|
2022-02-14 13:25:26 +01:00
|
|
|
Node::PathSegment(seg) => {
|
|
|
|
let ident_span = seg.ident.span;
|
|
|
|
ident_span
|
|
|
|
.with_hi(seg.args.map_or_else(|| ident_span.hi(), |args| args.span_ext.hi()))
|
|
|
|
}
|
2020-12-23 10:32:00 +01:00
|
|
|
Node::Ty(ty) => ty.span,
|
2022-05-07 22:41:53 +02:00
|
|
|
Node::TypeBinding(tb) => tb.span,
|
2020-12-23 10:32:00 +01:00
|
|
|
Node::TraitRef(tr) => tr.path.span,
|
|
|
|
Node::Pat(pat) => pat.span,
|
|
|
|
Node::Arm(arm) => arm.span,
|
|
|
|
Node::Block(block) => block.span,
|
2022-02-13 16:27:59 +01:00
|
|
|
Node::Ctor(..) => self.span_with_body(self.get_parent_node(hir_id)),
|
2020-12-23 10:32:00 +01:00
|
|
|
Node::Lifetime(lifetime) => lifetime.span,
|
|
|
|
Node::GenericParam(param) => param.span,
|
2021-04-24 21:41:57 +00:00
|
|
|
Node::Infer(i) => i.span,
|
2020-12-23 10:32:00 +01:00
|
|
|
Node::Local(local) => local.span,
|
2022-03-18 17:13:38 -04:00
|
|
|
Node::Crate(item) => item.spans.inner_span,
|
2020-08-12 17:02:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn span_if_local(self, id: DefId) -> Option<Span> {
|
2022-03-30 16:51:25 +02:00
|
|
|
if id.is_local() { Some(self.tcx.def_span(id)) } else { None }
|
2015-09-04 13:52:28 -04:00
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn res_span(self, res: Res) -> Option<Span> {
|
2019-09-28 02:30:48 +02:00
|
|
|
match res {
|
|
|
|
Res::Err => None,
|
|
|
|
Res::Local(id) => Some(self.span(id)),
|
|
|
|
res => self.span_if_local(res.opt_def_id()?),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 02:44:41 +01:00
|
|
|
/// Get a representation of this `id` for debugging purposes.
|
|
|
|
/// NOTE: Do NOT use this in diagnostics!
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn node_to_string(self, id: HirId) -> String {
|
2020-03-24 02:44:41 +01:00
|
|
|
hir_id_to_string(self, id)
|
2019-02-03 09:14:31 +01:00
|
|
|
}
|
2021-07-13 17:23:26 +01:00
|
|
|
|
|
|
|
/// Returns the HirId of `N` in `struct Foo<const N: usize = { ... }>` when
|
|
|
|
/// called with the HirId for the `{ ... }` anon const
|
2022-02-10 11:42:02 +01:00
|
|
|
pub fn opt_const_param_default_param_hir_id(self, anon_const: HirId) -> Option<HirId> {
|
2021-07-13 17:23:26 +01:00
|
|
|
match self.get(self.get_parent_node(anon_const)) {
|
|
|
|
Node::GenericParam(GenericParam {
|
|
|
|
hir_id: param_id,
|
|
|
|
kind: GenericParamKind::Const { .. },
|
|
|
|
..
|
|
|
|
}) => Some(*param_id),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2014-01-17 23:23:09 +11:00
|
|
|
}
|
2013-02-04 14:02:01 -08:00
|
|
|
|
2020-01-07 17:25:33 +01:00
|
|
|
impl<'hir> intravisit::Map<'hir> for Map<'hir> {
|
2020-03-24 02:44:41 +01:00
|
|
|
fn find(&self, hir_id: HirId) -> Option<Node<'hir>> {
|
2022-02-10 11:42:02 +01:00
|
|
|
(*self).find(hir_id)
|
2020-03-24 02:44:41 +01:00
|
|
|
}
|
|
|
|
|
2020-01-07 17:25:33 +01:00
|
|
|
fn body(&self, id: BodyId) -> &'hir Body<'hir> {
|
2022-02-10 11:42:02 +01:00
|
|
|
(*self).body(id)
|
2020-01-07 17:25:33 +01:00
|
|
|
}
|
|
|
|
|
2021-01-30 12:06:04 +01:00
|
|
|
fn item(&self, id: ItemId) -> &'hir Item<'hir> {
|
2022-02-10 11:42:02 +01:00
|
|
|
(*self).item(id)
|
2020-01-07 17:25:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir> {
|
2022-02-10 11:42:02 +01:00
|
|
|
(*self).trait_item(id)
|
2020-01-07 17:25:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir> {
|
2022-02-10 11:42:02 +01:00
|
|
|
(*self).impl_item(id)
|
2020-01-07 17:25:33 +01:00
|
|
|
}
|
2020-11-11 21:57:54 +01:00
|
|
|
|
|
|
|
fn foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir> {
|
2022-02-10 11:42:02 +01:00
|
|
|
(*self).foreign_item(id)
|
2020-11-11 21:57:54 +01:00
|
|
|
}
|
2020-01-07 17:25:33 +01:00
|
|
|
}
|
|
|
|
|
2021-02-28 18:58:50 +01:00
|
|
|
pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
|
2021-02-28 20:23:10 +01:00
|
|
|
debug_assert_eq!(crate_num, LOCAL_CRATE);
|
2021-09-19 22:07:12 +02:00
|
|
|
let krate = tcx.hir_crate(());
|
|
|
|
let hir_body_hash = krate.hir_hash;
|
2021-02-28 18:58:50 +01:00
|
|
|
|
2021-07-12 21:20:16 +02:00
|
|
|
let upstream_crates = upstream_crates(tcx);
|
2021-02-28 18:58:50 +01:00
|
|
|
|
2022-05-30 18:49:17 +02:00
|
|
|
let resolutions = tcx.resolutions(());
|
|
|
|
|
2021-02-28 18:58:50 +01:00
|
|
|
// We hash the final, remapped names of all local source files so we
|
|
|
|
// don't have to include the path prefix remapping commandline args.
|
|
|
|
// If we included the full mapping in the SVH, we could only have
|
|
|
|
// reproducible builds by compiling from the same directory. So we just
|
|
|
|
// hash the result of the mapping instead of the mapping itself.
|
|
|
|
let mut source_file_names: Vec<_> = tcx
|
|
|
|
.sess
|
|
|
|
.source_map()
|
|
|
|
.files()
|
|
|
|
.iter()
|
|
|
|
.filter(|source_file| source_file.cnum == LOCAL_CRATE)
|
|
|
|
.map(|source_file| source_file.name_hash)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
source_file_names.sort_unstable();
|
|
|
|
|
2021-07-12 22:19:25 +02:00
|
|
|
let crate_hash: Fingerprint = tcx.with_stable_hashing_context(|mut hcx| {
|
|
|
|
let mut stable_hasher = StableHasher::new();
|
|
|
|
hir_body_hash.hash_stable(&mut hcx, &mut stable_hasher);
|
|
|
|
upstream_crates.hash_stable(&mut hcx, &mut stable_hasher);
|
|
|
|
source_file_names.hash_stable(&mut hcx, &mut stable_hasher);
|
2022-07-06 07:44:47 -05:00
|
|
|
if tcx.sess.opts.unstable_opts.incremental_relative_spans {
|
2021-07-12 22:19:25 +02:00
|
|
|
let definitions = tcx.definitions_untracked();
|
|
|
|
let mut owner_spans: Vec<_> = krate
|
|
|
|
.owners
|
|
|
|
.iter_enumerated()
|
|
|
|
.filter_map(|(def_id, info)| {
|
|
|
|
let _ = info.as_owner()?;
|
|
|
|
let def_path_hash = definitions.def_path_hash(def_id);
|
|
|
|
let span = resolutions.source_span[def_id];
|
|
|
|
debug_assert_eq!(span.parent(), None);
|
|
|
|
Some((def_path_hash, span))
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
owner_spans.sort_unstable_by_key(|bn| bn.0);
|
|
|
|
owner_spans.hash_stable(&mut hcx, &mut stable_hasher);
|
|
|
|
}
|
|
|
|
tcx.sess.opts.dep_tracking_hash(true).hash_stable(&mut hcx, &mut stable_hasher);
|
|
|
|
tcx.sess.local_stable_crate_id().hash_stable(&mut hcx, &mut stable_hasher);
|
|
|
|
// Hash visibility information since it does not appear in HIR.
|
|
|
|
resolutions.visibilities.hash_stable(&mut hcx, &mut stable_hasher);
|
|
|
|
resolutions.has_pub_restricted.hash_stable(&mut hcx, &mut stable_hasher);
|
|
|
|
stable_hasher.finish()
|
|
|
|
});
|
2021-02-28 18:58:50 +01:00
|
|
|
|
|
|
|
Svh::new(crate_hash.to_smaller_hash())
|
|
|
|
}
|
|
|
|
|
2021-07-12 21:20:16 +02:00
|
|
|
fn upstream_crates(tcx: TyCtxt<'_>) -> Vec<(StableCrateId, Svh)> {
|
|
|
|
let mut upstream_crates: Vec<_> = tcx
|
|
|
|
.crates(())
|
2021-02-28 18:58:50 +01:00
|
|
|
.iter()
|
|
|
|
.map(|&cnum| {
|
2021-07-14 00:48:51 +02:00
|
|
|
let stable_crate_id = tcx.stable_crate_id(cnum);
|
2021-07-12 21:20:16 +02:00
|
|
|
let hash = tcx.crate_hash(cnum);
|
2021-06-08 18:36:30 +02:00
|
|
|
(stable_crate_id, hash)
|
2021-02-28 18:58:50 +01:00
|
|
|
})
|
|
|
|
.collect();
|
2021-06-08 18:36:30 +02:00
|
|
|
upstream_crates.sort_unstable_by_key(|&(stable_crate_id, _)| stable_crate_id);
|
2021-02-28 18:58:50 +01:00
|
|
|
upstream_crates
|
2011-06-19 22:41:21 +02:00
|
|
|
}
|
|
|
|
|
2022-02-10 11:42:02 +01:00
|
|
|
fn hir_id_to_string(map: Map<'_>, id: HirId) -> String {
|
2019-04-27 22:39:17 +02:00
|
|
|
let id_str = format!(" (hir_id={})", id);
|
2014-09-19 15:38:53 +02:00
|
|
|
|
2016-04-06 13:51:55 +03:00
|
|
|
let path_str = || {
|
2019-09-06 03:57:44 +01:00
|
|
|
// This functionality is used for debugging, try to use `TyCtxt` to get
|
|
|
|
// the user-friendly path, otherwise fall back to stringifying `DefPath`.
|
2019-02-05 11:20:45 -06:00
|
|
|
crate::ty::tls::with_opt(|tcx| {
|
2016-04-06 13:51:55 +03:00
|
|
|
if let Some(tcx) = tcx {
|
2019-06-27 11:28:14 +02:00
|
|
|
let def_id = map.local_def_id(id);
|
2020-04-09 09:43:00 +01:00
|
|
|
tcx.def_path_str(def_id.to_def_id())
|
2019-04-27 22:39:17 +02:00
|
|
|
} else if let Some(path) = map.def_path_from_hir_id(id) {
|
2020-08-31 22:57:48 +01:00
|
|
|
path.data.into_iter().map(|elem| elem.to_string()).collect::<Vec<_>>().join("::")
|
2016-04-06 13:51:55 +03:00
|
|
|
} else {
|
|
|
|
String::from("<missing path>")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2020-03-24 02:44:41 +01:00
|
|
|
let span_str = || map.tcx.sess.source_map().span_to_snippet(map.span(id)).unwrap_or_default();
|
|
|
|
let node_str = |prefix| format!("{} {}{}", prefix, span_str(), id_str);
|
|
|
|
|
2019-06-24 09:58:49 +02:00
|
|
|
match map.find(id) {
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Item(item)) => {
|
2019-09-26 17:51:36 +01:00
|
|
|
let item_str = match item.kind {
|
2018-07-11 23:36:06 +08:00
|
|
|
ItemKind::ExternCrate(..) => "extern crate",
|
|
|
|
ItemKind::Use(..) => "use",
|
|
|
|
ItemKind::Static(..) => "static",
|
|
|
|
ItemKind::Const(..) => "const",
|
|
|
|
ItemKind::Fn(..) => "fn",
|
2021-07-30 23:50:57 -07:00
|
|
|
ItemKind::Macro(..) => "macro",
|
2018-07-11 23:36:06 +08:00
|
|
|
ItemKind::Mod(..) => "mod",
|
2020-11-11 22:40:09 +01:00
|
|
|
ItemKind::ForeignMod { .. } => "foreign mod",
|
2018-07-11 23:36:06 +08:00
|
|
|
ItemKind::GlobalAsm(..) => "global asm",
|
2019-08-02 11:02:08 +01:00
|
|
|
ItemKind::TyAlias(..) => "ty",
|
2019-08-01 00:41:54 +01:00
|
|
|
ItemKind::OpaqueTy(..) => "opaque type",
|
2018-07-11 23:36:06 +08:00
|
|
|
ItemKind::Enum(..) => "enum",
|
|
|
|
ItemKind::Struct(..) => "struct",
|
|
|
|
ItemKind::Union(..) => "union",
|
|
|
|
ItemKind::Trait(..) => "trait",
|
|
|
|
ItemKind::TraitAlias(..) => "trait alias",
|
2020-01-17 16:14:29 -08:00
|
|
|
ItemKind::Impl { .. } => "impl",
|
2014-02-14 07:07:09 +02:00
|
|
|
};
|
2016-04-06 13:51:55 +03:00
|
|
|
format!("{} {}{}", item_str, path_str(), id_str)
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
2016-04-06 13:51:55 +03:00
|
|
|
Some(Node::ForeignItem(_)) => format!("foreign item {}{}", path_str(), id_str),
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::ImplItem(ii)) => match ii.kind {
|
2015-11-12 15:57:51 +01:00
|
|
|
ImplItemKind::Const(..) => {
|
2018-06-10 22:24:24 +03:00
|
|
|
format!("assoc const {} in {}{}", ii.ident, path_str(), id_str)
|
2014-08-04 13:56:56 -07:00
|
|
|
}
|
2020-03-05 09:57:34 -06:00
|
|
|
ImplItemKind::Fn(..) => format!("method {} in {}{}", ii.ident, path_str(), id_str),
|
2019-08-02 20:59:07 +01:00
|
|
|
ImplItemKind::TyAlias(_) => {
|
2018-06-10 22:24:24 +03:00
|
|
|
format!("assoc type {} in {}{}", ii.ident, path_str(), id_str)
|
2014-08-04 13:56:56 -07:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
},
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::TraitItem(ti)) => {
|
2019-09-26 17:07:54 +01:00
|
|
|
let kind = match ti.kind {
|
2016-12-04 04:21:06 +02:00
|
|
|
TraitItemKind::Const(..) => "assoc constant",
|
2020-03-03 12:46:22 -06:00
|
|
|
TraitItemKind::Fn(..) => "trait method",
|
2016-12-04 04:21:06 +02:00
|
|
|
TraitItemKind::Type(..) => "assoc type",
|
2015-03-10 12:28:44 +02:00
|
|
|
};
|
|
|
|
|
2018-06-10 22:24:24 +03:00
|
|
|
format!("{} {} in {}{}", kind, ti.ident, path_str(), id_str)
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Variant(ref variant)) => {
|
2014-09-19 15:38:53 +02:00
|
|
|
format!("variant {} in {}{}", variant.ident, path_str(), id_str)
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Field(ref field)) => {
|
2019-04-27 22:39:17 +02:00
|
|
|
format!("field {} in {}{}", field.ident, path_str(), id_str)
|
2014-02-14 07:07:09 +02:00
|
|
|
}
|
2020-03-24 02:44:41 +01:00
|
|
|
Some(Node::AnonConst(_)) => node_str("const"),
|
|
|
|
Some(Node::Expr(_)) => node_str("expr"),
|
|
|
|
Some(Node::Stmt(_)) => node_str("stmt"),
|
|
|
|
Some(Node::PathSegment(_)) => node_str("path segment"),
|
|
|
|
Some(Node::Ty(_)) => node_str("type"),
|
2022-05-07 22:41:53 +02:00
|
|
|
Some(Node::TypeBinding(_)) => node_str("type binding"),
|
2020-03-24 02:44:41 +01:00
|
|
|
Some(Node::TraitRef(_)) => node_str("trait ref"),
|
|
|
|
Some(Node::Pat(_)) => node_str("pat"),
|
|
|
|
Some(Node::Param(_)) => node_str("param"),
|
|
|
|
Some(Node::Arm(_)) => node_str("arm"),
|
|
|
|
Some(Node::Block(_)) => node_str("block"),
|
2021-04-24 21:41:57 +00:00
|
|
|
Some(Node::Infer(_)) => node_str("infer"),
|
2020-03-24 02:44:41 +01:00
|
|
|
Some(Node::Local(_)) => node_str("local"),
|
2019-03-21 23:38:50 +01:00
|
|
|
Some(Node::Ctor(..)) => format!("ctor {}{}", path_str(), id_str),
|
2020-03-24 02:44:41 +01:00
|
|
|
Some(Node::Lifetime(_)) => node_str("lifetime"),
|
2018-05-28 14:05:06 +01:00
|
|
|
Some(Node::GenericParam(ref param)) => format!("generic_param {:?}{}", param, id_str),
|
2020-02-07 16:43:36 +01:00
|
|
|
Some(Node::Crate(..)) => String::from("root_crate"),
|
2018-08-25 15:48:42 +01:00
|
|
|
None => format!("unknown node{}", id_str),
|
2013-10-29 06:03:32 -04:00
|
|
|
}
|
|
|
|
}
|
2021-07-18 18:12:17 +02:00
|
|
|
|
|
|
|
pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalDefId) -> ModuleItems {
|
2022-07-06 19:13:49 -04:00
|
|
|
let mut collector = ItemCollector::new(tcx, false);
|
2021-07-18 18:12:17 +02:00
|
|
|
|
|
|
|
let (hir_mod, span, hir_id) = tcx.hir().get_module(module_id);
|
|
|
|
collector.visit_mod(hir_mod, span, hir_id);
|
|
|
|
|
2022-07-06 19:13:49 -04:00
|
|
|
let ItemCollector {
|
2022-06-15 22:29:59 -04:00
|
|
|
submodules,
|
|
|
|
items,
|
|
|
|
trait_items,
|
|
|
|
impl_items,
|
|
|
|
foreign_items,
|
|
|
|
body_owners,
|
|
|
|
..
|
|
|
|
} = collector;
|
2021-09-07 20:07:33 +02:00
|
|
|
return ModuleItems {
|
|
|
|
submodules: submodules.into_boxed_slice(),
|
|
|
|
items: items.into_boxed_slice(),
|
|
|
|
trait_items: trait_items.into_boxed_slice(),
|
|
|
|
impl_items: impl_items.into_boxed_slice(),
|
|
|
|
foreign_items: foreign_items.into_boxed_slice(),
|
2022-06-15 22:29:59 -04:00
|
|
|
body_owners: body_owners.into_boxed_slice(),
|
2021-09-07 20:07:33 +02:00
|
|
|
};
|
2021-07-18 18:12:17 +02:00
|
|
|
}
|
2022-04-03 15:50:33 -04:00
|
|
|
|
|
|
|
pub(crate) fn hir_crate_items(tcx: TyCtxt<'_>, _: ()) -> ModuleItems {
|
2022-07-06 19:13:49 -04:00
|
|
|
let mut collector = ItemCollector::new(tcx, true);
|
2022-04-03 15:50:33 -04:00
|
|
|
|
2022-07-13 13:25:53 -04:00
|
|
|
// A "crate collector" and "module collector" start at a
|
|
|
|
// module item (the former starts at the crate root) but only
|
|
|
|
// the former needs to collect it. ItemCollector does not do this for us.
|
|
|
|
collector.submodules.push(CRATE_DEF_ID);
|
2022-04-03 15:50:33 -04:00
|
|
|
tcx.hir().walk_toplevel_module(&mut collector);
|
|
|
|
|
2022-07-06 19:13:49 -04:00
|
|
|
let ItemCollector {
|
2022-06-15 22:29:59 -04:00
|
|
|
submodules,
|
|
|
|
items,
|
|
|
|
trait_items,
|
|
|
|
impl_items,
|
|
|
|
foreign_items,
|
|
|
|
body_owners,
|
|
|
|
..
|
|
|
|
} = collector;
|
2022-04-03 15:50:33 -04:00
|
|
|
|
|
|
|
return ModuleItems {
|
|
|
|
submodules: submodules.into_boxed_slice(),
|
|
|
|
items: items.into_boxed_slice(),
|
|
|
|
trait_items: trait_items.into_boxed_slice(),
|
|
|
|
impl_items: impl_items.into_boxed_slice(),
|
|
|
|
foreign_items: foreign_items.into_boxed_slice(),
|
2022-06-15 22:29:59 -04:00
|
|
|
body_owners: body_owners.into_boxed_slice(),
|
2022-04-03 15:50:33 -04:00
|
|
|
};
|
2022-07-06 19:13:49 -04:00
|
|
|
}
|
2022-04-03 15:50:33 -04:00
|
|
|
|
2022-07-06 19:13:49 -04:00
|
|
|
struct ItemCollector<'tcx> {
|
|
|
|
// When true, it collects all items in the create,
|
|
|
|
// otherwise it collects items in some module.
|
|
|
|
crate_collector: bool,
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
submodules: Vec<LocalDefId>,
|
|
|
|
items: Vec<ItemId>,
|
|
|
|
trait_items: Vec<TraitItemId>,
|
|
|
|
impl_items: Vec<ImplItemId>,
|
|
|
|
foreign_items: Vec<ForeignItemId>,
|
|
|
|
body_owners: Vec<LocalDefId>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> ItemCollector<'tcx> {
|
|
|
|
fn new(tcx: TyCtxt<'tcx>, crate_collector: bool) -> ItemCollector<'tcx> {
|
|
|
|
ItemCollector {
|
|
|
|
crate_collector,
|
|
|
|
tcx,
|
|
|
|
submodules: Vec::default(),
|
|
|
|
items: Vec::default(),
|
|
|
|
trait_items: Vec::default(),
|
|
|
|
impl_items: Vec::default(),
|
|
|
|
foreign_items: Vec::default(),
|
|
|
|
body_owners: Vec::default(),
|
|
|
|
}
|
2022-04-03 15:50:33 -04:00
|
|
|
}
|
2022-07-06 19:13:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'hir> Visitor<'hir> for ItemCollector<'hir> {
|
|
|
|
type NestedFilter = nested_filter::All;
|
2022-04-03 15:50:33 -04:00
|
|
|
|
2022-07-06 19:13:49 -04:00
|
|
|
fn nested_visit_map(&mut self) -> Self::Map {
|
|
|
|
self.tcx.hir()
|
|
|
|
}
|
2022-04-03 15:50:33 -04:00
|
|
|
|
2022-07-06 19:13:49 -04:00
|
|
|
fn visit_item(&mut self, item: &'hir Item<'hir>) {
|
|
|
|
if associated_body(Node::Item(item)).is_some() {
|
|
|
|
self.body_owners.push(item.def_id);
|
2022-04-03 15:50:33 -04:00
|
|
|
}
|
|
|
|
|
2022-07-06 19:13:49 -04:00
|
|
|
self.items.push(item.item_id());
|
2022-06-15 22:29:59 -04:00
|
|
|
|
2022-07-13 13:25:53 -04:00
|
|
|
// Items that are modules are handled here instead of in visit_mod.
|
|
|
|
if let ItemKind::Mod(module) = &item.kind {
|
2022-07-06 19:13:49 -04:00
|
|
|
self.submodules.push(item.def_id);
|
2022-07-13 13:25:53 -04:00
|
|
|
// A module collector does not recurse inside nested modules.
|
|
|
|
if self.crate_collector {
|
|
|
|
intravisit::walk_mod(self, module, item.hir_id());
|
|
|
|
}
|
2022-07-06 19:13:49 -04:00
|
|
|
} else {
|
2022-04-03 15:50:33 -04:00
|
|
|
intravisit::walk_item(self, item)
|
|
|
|
}
|
2022-07-06 19:13:49 -04:00
|
|
|
}
|
2022-04-03 15:50:33 -04:00
|
|
|
|
2022-07-06 19:13:49 -04:00
|
|
|
fn visit_foreign_item(&mut self, item: &'hir ForeignItem<'hir>) {
|
|
|
|
self.foreign_items.push(item.foreign_item_id());
|
|
|
|
intravisit::walk_foreign_item(self, item)
|
|
|
|
}
|
2022-04-03 15:50:33 -04:00
|
|
|
|
2022-07-06 19:13:49 -04:00
|
|
|
fn visit_anon_const(&mut self, c: &'hir AnonConst) {
|
|
|
|
self.body_owners.push(self.tcx.hir().local_def_id(c.hir_id));
|
|
|
|
intravisit::walk_anon_const(self, c)
|
|
|
|
}
|
2022-04-03 15:50:33 -04:00
|
|
|
|
2022-07-06 19:13:49 -04:00
|
|
|
fn visit_expr(&mut self, ex: &'hir Expr<'hir>) {
|
|
|
|
if matches!(ex.kind, ExprKind::Closure { .. }) {
|
|
|
|
self.body_owners.push(self.tcx.hir().local_def_id(ex.hir_id));
|
2022-04-03 15:50:33 -04:00
|
|
|
}
|
2022-07-06 19:13:49 -04:00
|
|
|
intravisit::walk_expr(self, ex)
|
|
|
|
}
|
2022-04-03 15:50:33 -04:00
|
|
|
|
2022-07-06 19:13:49 -04:00
|
|
|
fn visit_trait_item(&mut self, item: &'hir TraitItem<'hir>) {
|
|
|
|
if associated_body(Node::TraitItem(item)).is_some() {
|
|
|
|
self.body_owners.push(item.def_id);
|
2022-04-03 15:50:33 -04:00
|
|
|
}
|
|
|
|
|
2022-07-06 19:13:49 -04:00
|
|
|
self.trait_items.push(item.trait_item_id());
|
|
|
|
intravisit::walk_trait_item(self, item)
|
|
|
|
}
|
2022-06-15 22:29:59 -04:00
|
|
|
|
2022-07-06 19:13:49 -04:00
|
|
|
fn visit_impl_item(&mut self, item: &'hir ImplItem<'hir>) {
|
|
|
|
if associated_body(Node::ImplItem(item)).is_some() {
|
|
|
|
self.body_owners.push(item.def_id);
|
2022-04-03 15:50:33 -04:00
|
|
|
}
|
2022-07-06 19:13:49 -04:00
|
|
|
|
|
|
|
self.impl_items.push(item.impl_item_id());
|
|
|
|
intravisit::walk_impl_item(self, item)
|
2022-04-03 15:50:33 -04:00
|
|
|
}
|
|
|
|
}
|