2020-03-05 18:07:42 -03:00
|
|
|
//! HIR datatypes. See the [rustc dev guide] for more info.
|
2020-01-02 03:39:11 +01:00
|
|
|
//!
|
2020-03-09 18:33:04 -03:00
|
|
|
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
|
2020-01-02 03:39:11 +01:00
|
|
|
|
|
|
|
pub mod map;
|
2021-11-03 18:03:12 -05:00
|
|
|
pub mod nested_filter;
|
2020-07-05 22:06:37 -04:00
|
|
|
pub mod place;
|
2020-01-02 03:39:11 +01:00
|
|
|
|
2023-05-15 06:24:45 +02:00
|
|
|
use crate::query::Providers;
|
2023-04-13 15:23:54 -06:00
|
|
|
use crate::ty::{EarlyBinder, ImplSubject, TyCtxt};
|
2020-02-09 12:13:08 +01:00
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
2023-10-18 08:47:17 +00:00
|
|
|
use rustc_data_structures::sync::{try_par_for_each_in, DynSend, DynSync};
|
2023-05-17 15:31:46 +00:00
|
|
|
use rustc_hir::def::DefKind;
|
2023-04-26 20:53:51 +02:00
|
|
|
use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId};
|
2020-06-11 13:42:22 -04:00
|
|
|
use rustc_hir::*;
|
2020-11-14 16:48:54 +01:00
|
|
|
use rustc_query_system::ich::StableHashingContext;
|
2023-10-18 08:47:17 +00:00
|
|
|
use rustc_span::{ErrorGuaranteed, ExpnId, DUMMY_SP};
|
2020-02-06 11:59:29 +01:00
|
|
|
|
2021-05-28 21:14:11 +02:00
|
|
|
/// Top-level HIR node for current owner. This only contains the node for which
|
|
|
|
/// `HirId::local_id == 0`, and excludes bodies.
|
2021-03-27 13:21:26 +01:00
|
|
|
///
|
|
|
|
/// This struct exists to encapsulate all access to the hir_owner query in this module, and to
|
|
|
|
/// implement HashStable without hashing bodies.
|
2021-05-28 21:14:11 +02:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2020-03-18 03:48:17 +02:00
|
|
|
pub struct Owner<'tcx> {
|
2021-03-27 13:21:26 +01:00
|
|
|
node: OwnerNode<'tcx>,
|
2020-02-07 11:14:47 +01:00
|
|
|
}
|
|
|
|
|
2020-03-18 03:48:17 +02:00
|
|
|
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Owner<'tcx> {
|
2021-09-19 22:17:50 +02:00
|
|
|
#[inline]
|
2020-02-09 12:13:08 +01:00
|
|
|
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
|
2023-03-03 17:02:11 +11:00
|
|
|
// Perform a shallow hash instead using the deep hash saved in `OwnerNodes`. This lets us
|
|
|
|
// differentiate queries that depend on the full HIR tree from those that only depend on
|
|
|
|
// the item signature.
|
|
|
|
hcx.without_hir_bodies(|hcx| self.node.hash_stable(hcx, hasher));
|
2020-02-09 12:13:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-07 20:07:33 +02:00
|
|
|
/// Gather the LocalDefId for each item-like within a module, including items contained within
|
2022-11-16 20:34:16 +00:00
|
|
|
/// bodies. The Ids are in visitor order. This is used to partition a pass between modules.
|
2022-03-28 19:53:01 +02:00
|
|
|
#[derive(Debug, HashStable, Encodable, Decodable)]
|
2021-07-18 18:12:17 +02:00
|
|
|
pub struct ModuleItems {
|
2022-09-20 14:11:23 +09:00
|
|
|
submodules: Box<[OwnerId]>,
|
2021-09-07 20:07:33 +02:00
|
|
|
items: Box<[ItemId]>,
|
|
|
|
trait_items: Box<[TraitItemId]>,
|
|
|
|
impl_items: Box<[ImplItemId]>,
|
|
|
|
foreign_items: Box<[ForeignItemId]>,
|
2022-06-15 22:29:59 -04:00
|
|
|
body_owners: Box<[LocalDefId]>,
|
2021-07-18 18:12:17 +02:00
|
|
|
}
|
|
|
|
|
2022-04-06 19:04:15 -04:00
|
|
|
impl ModuleItems {
|
2022-04-09 13:56:56 -04:00
|
|
|
pub fn items(&self) -> impl Iterator<Item = ItemId> + '_ {
|
|
|
|
self.items.iter().copied()
|
2022-04-06 19:04:15 -04:00
|
|
|
}
|
2022-04-09 13:56:56 -04:00
|
|
|
|
|
|
|
pub fn trait_items(&self) -> impl Iterator<Item = TraitItemId> + '_ {
|
|
|
|
self.trait_items.iter().copied()
|
2022-04-06 19:04:15 -04:00
|
|
|
}
|
2022-04-09 13:56:56 -04:00
|
|
|
|
|
|
|
pub fn impl_items(&self) -> impl Iterator<Item = ImplItemId> + '_ {
|
|
|
|
self.impl_items.iter().copied()
|
2022-04-06 19:04:15 -04:00
|
|
|
}
|
2022-04-09 13:56:56 -04:00
|
|
|
|
|
|
|
pub fn foreign_items(&self) -> impl Iterator<Item = ForeignItemId> + '_ {
|
|
|
|
self.foreign_items.iter().copied()
|
2022-04-06 19:04:15 -04:00
|
|
|
}
|
2022-06-18 19:20:59 +02:00
|
|
|
|
2022-10-25 18:34:58 +00:00
|
|
|
pub fn owners(&self) -> impl Iterator<Item = OwnerId> + '_ {
|
2022-06-25 15:30:38 +02:00
|
|
|
self.items
|
|
|
|
.iter()
|
2022-10-25 18:34:58 +00:00
|
|
|
.map(|id| id.owner_id)
|
|
|
|
.chain(self.trait_items.iter().map(|id| id.owner_id))
|
|
|
|
.chain(self.impl_items.iter().map(|id| id.owner_id))
|
|
|
|
.chain(self.foreign_items.iter().map(|id| id.owner_id))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn definitions(&self) -> impl Iterator<Item = LocalDefId> + '_ {
|
|
|
|
self.owners().map(|id| id.def_id)
|
2022-06-25 15:30:38 +02:00
|
|
|
}
|
|
|
|
|
2023-10-18 08:47:17 +00:00
|
|
|
pub fn par_items(
|
|
|
|
&self,
|
|
|
|
f: impl Fn(ItemId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
|
|
|
|
) -> Result<(), ErrorGuaranteed> {
|
|
|
|
try_par_for_each_in(&self.items[..], |&id| f(id))
|
2022-06-18 19:20:59 +02:00
|
|
|
}
|
|
|
|
|
2023-10-18 08:47:17 +00:00
|
|
|
pub fn par_trait_items(
|
|
|
|
&self,
|
|
|
|
f: impl Fn(TraitItemId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
|
|
|
|
) -> Result<(), ErrorGuaranteed> {
|
|
|
|
try_par_for_each_in(&self.trait_items[..], |&id| f(id))
|
2022-06-18 19:20:59 +02:00
|
|
|
}
|
|
|
|
|
2023-10-18 08:47:17 +00:00
|
|
|
pub fn par_impl_items(
|
|
|
|
&self,
|
|
|
|
f: impl Fn(ImplItemId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
|
|
|
|
) -> Result<(), ErrorGuaranteed> {
|
|
|
|
try_par_for_each_in(&self.impl_items[..], |&id| f(id))
|
2022-06-18 19:20:59 +02:00
|
|
|
}
|
|
|
|
|
2023-10-18 08:47:17 +00:00
|
|
|
pub fn par_foreign_items(
|
|
|
|
&self,
|
|
|
|
f: impl Fn(ForeignItemId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync,
|
|
|
|
) -> Result<(), ErrorGuaranteed> {
|
|
|
|
try_par_for_each_in(&self.foreign_items[..], |&id| f(id))
|
2022-06-18 19:20:59 +02:00
|
|
|
}
|
2022-04-06 19:04:15 -04:00
|
|
|
}
|
|
|
|
|
2020-02-06 11:59:29 +01:00
|
|
|
impl<'tcx> TyCtxt<'tcx> {
|
|
|
|
#[inline(always)]
|
2020-02-09 15:32:00 +01:00
|
|
|
pub fn hir(self) -> map::Map<'tcx> {
|
|
|
|
map::Map { tcx: self }
|
2020-02-06 11:59:29 +01:00
|
|
|
}
|
2020-02-11 14:38:16 +01:00
|
|
|
|
2023-04-26 20:53:51 +02:00
|
|
|
pub fn parent_module(self, id: HirId) -> LocalModDefId {
|
2023-08-05 12:18:12 +00:00
|
|
|
if !id.is_owner() && self.def_kind(id.owner) == DefKind::Mod {
|
2023-04-26 20:53:51 +02:00
|
|
|
LocalModDefId::new_unchecked(id.owner.def_id)
|
2023-08-05 12:18:12 +00:00
|
|
|
} else {
|
|
|
|
self.parent_module_from_def_id(id.owner.def_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-26 20:53:51 +02:00
|
|
|
pub fn parent_module_from_def_id(self, mut id: LocalDefId) -> LocalModDefId {
|
2023-08-05 12:18:12 +00:00
|
|
|
while let Some(parent) = self.opt_local_parent(id) {
|
|
|
|
id = parent;
|
|
|
|
if self.def_kind(id) == DefKind::Mod {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-04-26 20:53:51 +02:00
|
|
|
LocalModDefId::new_unchecked(id)
|
2020-02-11 14:38:16 +01:00
|
|
|
}
|
2022-03-20 00:12:03 -03:00
|
|
|
|
2023-04-13 15:23:54 -06:00
|
|
|
pub fn impl_subject(self, def_id: DefId) -> EarlyBinder<ImplSubject<'tcx>> {
|
2023-04-13 16:43:34 -06:00
|
|
|
match self.impl_trait_ref(def_id) {
|
|
|
|
Some(t) => t.map_bound(ImplSubject::Trait),
|
|
|
|
None => self.type_of(def_id).map_bound(ImplSubject::Inherent),
|
|
|
|
}
|
2022-03-20 00:12:03 -03:00
|
|
|
}
|
2023-05-17 15:31:46 +00:00
|
|
|
|
|
|
|
/// Returns `true` if this is a foreign item (i.e., linked via `extern { ... }`).
|
|
|
|
pub fn is_foreign_item(self, def_id: impl Into<DefId>) -> bool {
|
|
|
|
self.opt_parent(def_id.into())
|
2023-05-24 14:19:22 +00:00
|
|
|
.is_some_and(|parent| matches!(self.def_kind(parent), DefKind::ForeignMod))
|
2023-05-17 15:31:46 +00:00
|
|
|
}
|
2020-02-06 11:59:29 +01:00
|
|
|
}
|
2020-01-02 03:39:11 +01:00
|
|
|
|
2020-07-05 23:00:14 +03:00
|
|
|
pub fn provide(providers: &mut Providers) {
|
2022-04-03 15:50:33 -04:00
|
|
|
providers.hir_crate_items = map::hir_crate_items;
|
2021-02-28 18:58:50 +01:00
|
|
|
providers.crate_hash = map::crate_hash;
|
2021-07-18 18:12:17 +02:00
|
|
|
providers.hir_module_items = map::hir_module_items;
|
2021-05-28 21:14:11 +02:00
|
|
|
providers.hir_owner = |tcx, id| {
|
2022-09-20 14:11:23 +09:00
|
|
|
let owner = tcx.hir_crate(()).owners.get(id.def_id)?.as_owner()?;
|
2022-01-28 14:58:27 -03:00
|
|
|
let node = owner.node();
|
2023-03-03 17:02:11 +11:00
|
|
|
Some(Owner { node })
|
2022-01-28 14:58:27 -03:00
|
|
|
};
|
2022-12-15 13:20:27 -03:00
|
|
|
providers.opt_local_def_id_to_hir_id = |tcx, id| {
|
2022-01-29 08:40:23 -03:00
|
|
|
let owner = tcx.hir_crate(()).owners[id].map(|_| ());
|
2022-12-15 13:20:27 -03:00
|
|
|
Some(match owner {
|
2022-01-29 08:40:23 -03:00
|
|
|
MaybeOwner::Owner(_) => HirId::make_owner(id),
|
|
|
|
MaybeOwner::Phantom => bug!("No HirId for {:?}", id),
|
|
|
|
MaybeOwner::NonOwner(hir_id) => hir_id,
|
2022-12-15 13:20:27 -03:00
|
|
|
})
|
2022-01-29 08:40:23 -03:00
|
|
|
};
|
2022-09-20 14:11:23 +09:00
|
|
|
providers.hir_owner_nodes = |tcx, id| tcx.hir_crate(()).owners[id.def_id].map(|i| &i.nodes);
|
2021-03-06 11:25:41 +01:00
|
|
|
providers.hir_owner_parent = |tcx, id| {
|
2021-07-14 00:48:51 +02:00
|
|
|
// Accessing the local_parent is ok since its value is hashed as part of `id`'s DefPathHash.
|
2022-09-20 14:11:23 +09:00
|
|
|
tcx.opt_local_parent(id.def_id).map_or(CRATE_HIR_ID, |parent| {
|
2021-07-14 00:48:51 +02:00
|
|
|
let mut parent_hir_id = tcx.hir().local_def_id_to_hir_id(parent);
|
2022-11-26 17:22:10 +00:00
|
|
|
parent_hir_id.local_id =
|
|
|
|
tcx.hir_crate(()).owners[parent_hir_id.owner.def_id].unwrap().parenting[&id.def_id];
|
2021-02-28 20:23:10 +01:00
|
|
|
parent_hir_id
|
2021-07-14 00:48:51 +02:00
|
|
|
})
|
2021-03-06 11:25:41 +01:00
|
|
|
};
|
2022-09-20 14:11:23 +09:00
|
|
|
providers.hir_attrs = |tcx, id| {
|
|
|
|
tcx.hir_crate(()).owners[id.def_id].as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs)
|
|
|
|
};
|
2022-03-30 16:51:25 +02:00
|
|
|
providers.def_span = |tcx, def_id| {
|
|
|
|
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
|
|
|
|
tcx.hir().opt_span(hir_id).unwrap_or(DUMMY_SP)
|
|
|
|
};
|
2022-04-08 18:53:16 +02:00
|
|
|
providers.def_ident_span = |tcx, def_id| {
|
|
|
|
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
|
|
|
|
tcx.hir().opt_ident_span(hir_id)
|
|
|
|
};
|
2023-08-19 14:09:30 +02:00
|
|
|
providers.fn_arg_names = |tcx, def_id| {
|
2020-06-11 13:42:22 -04:00
|
|
|
let hir = tcx.hir();
|
2022-07-19 17:06:52 -04:00
|
|
|
let hir_id = hir.local_def_id_to_hir_id(def_id);
|
|
|
|
if let Some(body_id) = hir.maybe_body_owned_by(def_id) {
|
2020-06-11 13:42:22 -04:00
|
|
|
tcx.arena.alloc_from_iter(hir.body_param_names(body_id))
|
|
|
|
} else if let Node::TraitItem(&TraitItem {
|
|
|
|
kind: TraitItemKind::Fn(_, TraitFn::Required(idents)),
|
|
|
|
..
|
2022-12-27 17:10:21 +00:00
|
|
|
})
|
|
|
|
| Node::ForeignItem(&ForeignItem {
|
|
|
|
kind: ForeignItemKind::Fn(_, idents, _),
|
|
|
|
..
|
2020-06-11 13:42:22 -04:00
|
|
|
}) = hir.get(hir_id)
|
|
|
|
{
|
2022-12-27 17:10:21 +00:00
|
|
|
idents
|
2020-06-11 13:42:22 -04:00
|
|
|
} else {
|
2023-08-19 14:09:30 +02:00
|
|
|
span_bug!(hir.span(hir_id), "fn_arg_names: unexpected item {:?}", def_id);
|
2020-06-11 13:42:22 -04:00
|
|
|
}
|
|
|
|
};
|
2023-03-13 18:54:05 +00:00
|
|
|
providers.opt_def_kind = |tcx, def_id| tcx.hir().opt_def_kind(def_id);
|
2021-07-16 21:55:10 +02:00
|
|
|
providers.all_local_trait_impls = |tcx, ()| &tcx.resolutions(()).trait_impls;
|
2023-03-13 18:54:05 +00:00
|
|
|
providers.expn_that_defined =
|
|
|
|
|tcx, id| tcx.resolutions(()).expn_that_defined.get(&id).copied().unwrap_or(ExpnId::root());
|
2022-09-20 14:11:23 +09:00
|
|
|
providers.in_scope_traits_map = |tcx, id| {
|
|
|
|
tcx.hir_crate(()).owners[id.def_id].as_owner().map(|owner_info| &owner_info.trait_map)
|
|
|
|
};
|
2020-01-02 03:39:11 +01:00
|
|
|
}
|