mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 14:55:26 +00:00
Auto merge of #120943 - petrochenkov:somehir3, r=oli-obk
Create some minimal HIR for associated opaque types `LocalDefId`s for opaque types in traits and impls are created after AST -> HIR lowering, so they don't have corresponding HIR and return their various properties through fed queries. In this PR I also feed some core HIR-related queries for these `LocalDefId`s (which happen to be HIR owners). As a result all `LocalDefId`s now have corresponding `HirId`s and HIR nodes, and "optional" methods like `opt_local_def_id_to_hir_id` and `opt_hir_node_by_def_id` can be removed. Follow up to https://github.com/rust-lang/rust/pull/120206.
This commit is contained in:
commit
fe61575228
@ -55,6 +55,7 @@ pub(super) fn index_hir<'hir>(
|
||||
OwnerNode::TraitItem(item) => collector.visit_trait_item(item),
|
||||
OwnerNode::ImplItem(item) => collector.visit_impl_item(item),
|
||||
OwnerNode::ForeignItem(item) => collector.visit_foreign_item(item),
|
||||
OwnerNode::AssocOpaqueTy(..) => unreachable!(),
|
||||
};
|
||||
|
||||
for (local_id, node) in collector.nodes.iter_enumerated() {
|
||||
|
@ -2553,6 +2553,11 @@ pub struct OpaqueTy<'hir> {
|
||||
pub in_trait: bool,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, HashStable_Generic)]
|
||||
pub struct AssocOpaqueTy {
|
||||
// Add some data if necessary
|
||||
}
|
||||
|
||||
/// From whence the opaque type came.
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Debug, HashStable_Generic)]
|
||||
pub enum OpaqueTyOrigin {
|
||||
@ -3363,6 +3368,7 @@ pub enum OwnerNode<'hir> {
|
||||
TraitItem(&'hir TraitItem<'hir>),
|
||||
ImplItem(&'hir ImplItem<'hir>),
|
||||
Crate(&'hir Mod<'hir>),
|
||||
AssocOpaqueTy(&'hir AssocOpaqueTy),
|
||||
}
|
||||
|
||||
impl<'hir> OwnerNode<'hir> {
|
||||
@ -3372,7 +3378,7 @@ impl<'hir> OwnerNode<'hir> {
|
||||
| OwnerNode::ForeignItem(ForeignItem { ident, .. })
|
||||
| OwnerNode::ImplItem(ImplItem { ident, .. })
|
||||
| OwnerNode::TraitItem(TraitItem { ident, .. }) => Some(*ident),
|
||||
OwnerNode::Crate(..) => None,
|
||||
OwnerNode::Crate(..) | OwnerNode::AssocOpaqueTy(..) => None,
|
||||
}
|
||||
}
|
||||
|
||||
@ -3385,6 +3391,7 @@ impl<'hir> OwnerNode<'hir> {
|
||||
| OwnerNode::ImplItem(ImplItem { span, .. })
|
||||
| OwnerNode::TraitItem(TraitItem { span, .. }) => span,
|
||||
OwnerNode::Crate(Mod { spans: ModSpans { inner_span, .. }, .. }) => inner_span,
|
||||
OwnerNode::AssocOpaqueTy(..) => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -3443,6 +3450,7 @@ impl<'hir> OwnerNode<'hir> {
|
||||
| OwnerNode::ImplItem(ImplItem { owner_id, .. })
|
||||
| OwnerNode::ForeignItem(ForeignItem { owner_id, .. }) => *owner_id,
|
||||
OwnerNode::Crate(..) => crate::CRATE_HIR_ID.owner,
|
||||
OwnerNode::AssocOpaqueTy(..) => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -3486,6 +3494,7 @@ impl<'hir> Into<Node<'hir>> for OwnerNode<'hir> {
|
||||
OwnerNode::ImplItem(n) => Node::ImplItem(n),
|
||||
OwnerNode::TraitItem(n) => Node::TraitItem(n),
|
||||
OwnerNode::Crate(n) => Node::Crate(n),
|
||||
OwnerNode::AssocOpaqueTy(n) => Node::AssocOpaqueTy(n),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -3523,6 +3532,7 @@ pub enum Node<'hir> {
|
||||
WhereBoundPredicate(&'hir WhereBoundPredicate<'hir>),
|
||||
// FIXME: Merge into `Node::Infer`.
|
||||
ArrayLenInfer(&'hir InferArg),
|
||||
AssocOpaqueTy(&'hir AssocOpaqueTy),
|
||||
// Span by reference to minimize `Node`'s size
|
||||
#[allow(rustc::pass_by_value)]
|
||||
Err(&'hir Span),
|
||||
@ -3573,6 +3583,7 @@ impl<'hir> Node<'hir> {
|
||||
| Node::Infer(..)
|
||||
| Node::WhereBoundPredicate(..)
|
||||
| Node::ArrayLenInfer(..)
|
||||
| Node::AssocOpaqueTy(..)
|
||||
| Node::Err(..) => None,
|
||||
}
|
||||
}
|
||||
@ -3678,6 +3689,7 @@ impl<'hir> Node<'hir> {
|
||||
Node::TraitItem(i) => Some(OwnerNode::TraitItem(i)),
|
||||
Node::ImplItem(i) => Some(OwnerNode::ImplItem(i)),
|
||||
Node::Crate(i) => Some(OwnerNode::Crate(i)),
|
||||
Node::AssocOpaqueTy(i) => Some(OwnerNode::AssocOpaqueTy(i)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
@ -196,6 +196,7 @@ fn check_well_formed(tcx: TyCtxt<'_>, def_id: hir::OwnerId) -> Result<(), ErrorG
|
||||
hir::OwnerNode::TraitItem(item) => check_trait_item(tcx, item),
|
||||
hir::OwnerNode::ImplItem(item) => check_impl_item(tcx, item),
|
||||
hir::OwnerNode::ForeignItem(item) => check_foreign_item(tcx, item),
|
||||
hir::OwnerNode::AssocOpaqueTy(..) => unreachable!(),
|
||||
};
|
||||
|
||||
if let Some(generics) = node.generics() {
|
||||
|
@ -262,6 +262,7 @@ fn resolve_bound_vars(tcx: TyCtxt<'_>, local_def_id: hir::OwnerId) -> ResolveBou
|
||||
visitor.visit_impl_item(item)
|
||||
}
|
||||
hir::OwnerNode::Crate(_) => {}
|
||||
hir::OwnerNode::AssocOpaqueTy(..) => unreachable!(),
|
||||
}
|
||||
|
||||
let mut rl = ResolveBoundVars::default();
|
||||
|
@ -121,6 +121,7 @@ impl<'a> State<'a> {
|
||||
self.print_bounds(":", pred.bounds);
|
||||
}
|
||||
Node::ArrayLenInfer(_) => self.word("_"),
|
||||
Node::AssocOpaqueTy(..) => unreachable!(),
|
||||
Node::Err(_) => self.word("/*ERROR*/"),
|
||||
}
|
||||
}
|
||||
|
@ -2174,7 +2174,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||
let mut call_finder = FindClosureArg { tcx: self.tcx, calls: vec![] };
|
||||
let node = self
|
||||
.tcx
|
||||
.opt_local_def_id_to_hir_id(self.tcx.hir().get_parent_item(call_expr.hir_id))
|
||||
.opt_local_def_id_to_hir_id(
|
||||
self.tcx.hir().get_parent_item(call_expr.hir_id).def_id,
|
||||
)
|
||||
.map(|hir_id| self.tcx.hir_node(hir_id));
|
||||
match node {
|
||||
Some(hir::Node::Item(item)) => call_finder.visit_item(item),
|
||||
|
@ -2554,6 +2554,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
||||
hir::OwnerNode::ImplItem(i) => visitor.visit_impl_item(i),
|
||||
hir::OwnerNode::TraitItem(i) => visitor.visit_trait_item(i),
|
||||
hir::OwnerNode::Crate(_) => bug!("OwnerNode::Crate doesn't not have generics"),
|
||||
hir::OwnerNode::AssocOpaqueTy(..) => unreachable!(),
|
||||
}
|
||||
|
||||
let ast_generics = self.tcx.hir().get_generics(lifetime_scope).unwrap();
|
||||
|
@ -356,7 +356,7 @@ pub fn late_lint_mod<'tcx, T: LateLintPass<'tcx> + 'tcx>(
|
||||
cached_typeck_results: Cell::new(None),
|
||||
param_env: ty::ParamEnv::empty(),
|
||||
effective_visibilities: tcx.effective_visibilities(()),
|
||||
last_node_with_lint_attrs: tcx.local_def_id_to_hir_id(module_def_id.into()),
|
||||
last_node_with_lint_attrs: tcx.local_def_id_to_hir_id(module_def_id),
|
||||
generics: None,
|
||||
only_module: true,
|
||||
};
|
||||
|
@ -191,6 +191,7 @@ fn shallow_lint_levels_on(tcx: TyCtxt<'_>, owner: hir::OwnerId) -> ShallowLintLe
|
||||
levels.add_id(hir::CRATE_HIR_ID);
|
||||
levels.visit_mod(mod_, mod_.spans.inner_span, hir::CRATE_HIR_ID)
|
||||
}
|
||||
hir::OwnerNode::AssocOpaqueTy(..) => unreachable!(),
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -115,6 +115,7 @@ macro_rules! arena_types {
|
||||
[] features: rustc_feature::Features,
|
||||
[decode] specialization_graph: rustc_middle::traits::specialization_graph::Graph,
|
||||
[] crate_inherent_impls: rustc_middle::ty::CrateInherentImpls,
|
||||
[] hir_owner_nodes: rustc_hir::OwnerNodes<'tcx>,
|
||||
]);
|
||||
)
|
||||
}
|
||||
|
@ -161,7 +161,7 @@ impl<'tcx> TyCtxt<'tcx> {
|
||||
/// Retrieves the `hir::Node` corresponding to `id`, returning `None` if cannot be found.
|
||||
#[inline]
|
||||
pub fn opt_hir_node_by_def_id(self, id: LocalDefId) -> Option<Node<'tcx>> {
|
||||
Some(self.hir_node(self.opt_local_def_id_to_hir_id(id)?))
|
||||
Some(self.hir_node_by_def_id(id))
|
||||
}
|
||||
|
||||
/// Retrieves the `hir::Node` corresponding to `id`.
|
||||
@ -169,12 +169,10 @@ impl<'tcx> TyCtxt<'tcx> {
|
||||
self.hir_owner_nodes(id.owner).nodes[id.local_id].node
|
||||
}
|
||||
|
||||
/// Retrieves the `hir::Node` corresponding to `id`, panicking if it cannot be found.
|
||||
/// Retrieves the `hir::Node` corresponding to `id`.
|
||||
#[inline]
|
||||
#[track_caller]
|
||||
pub fn hir_node_by_def_id(self, id: LocalDefId) -> Node<'tcx> {
|
||||
self.opt_hir_node_by_def_id(id)
|
||||
.unwrap_or_else(|| bug!("couldn't find HIR node for def id {id:?}"))
|
||||
self.hir_node(self.local_def_id_to_hir_id(id))
|
||||
}
|
||||
|
||||
/// Returns `HirId` of the parent HIR node of node with this `hir_id`.
|
||||
@ -963,6 +961,7 @@ impl<'hir> Map<'hir> {
|
||||
Node::Crate(item) => item.spans.inner_span,
|
||||
Node::WhereBoundPredicate(pred) => pred.span,
|
||||
Node::ArrayLenInfer(inf) => inf.span,
|
||||
Node::AssocOpaqueTy(..) => unreachable!(),
|
||||
Node::Err(span) => *span,
|
||||
}
|
||||
}
|
||||
@ -1227,6 +1226,7 @@ fn hir_id_to_string(map: Map<'_>, id: HirId) -> String {
|
||||
Node::Crate(..) => String::from("(root_crate)"),
|
||||
Node::WhereBoundPredicate(_) => node_str("where bound predicate"),
|
||||
Node::ArrayLenInfer(_) => node_str("array len infer"),
|
||||
Node::AssocOpaqueTy(..) => unreachable!(),
|
||||
Node::Err(_) => node_str("error"),
|
||||
}
|
||||
}
|
||||
|
@ -127,12 +127,10 @@ pub fn provide(providers: &mut Providers) {
|
||||
providers.hir_crate_items = map::hir_crate_items;
|
||||
providers.crate_hash = map::crate_hash;
|
||||
providers.hir_module_items = map::hir_module_items;
|
||||
providers.opt_local_def_id_to_hir_id = |tcx, def_id| {
|
||||
Some(match tcx.hir_crate(()).owners[def_id] {
|
||||
MaybeOwner::Owner(_) => HirId::make_owner(def_id),
|
||||
MaybeOwner::NonOwner(hir_id) => hir_id,
|
||||
MaybeOwner::Phantom => bug!("No HirId for {:?}", def_id),
|
||||
})
|
||||
providers.local_def_id_to_hir_id = |tcx, def_id| match tcx.hir_crate(()).owners[def_id] {
|
||||
MaybeOwner::Owner(_) => HirId::make_owner(def_id),
|
||||
MaybeOwner::NonOwner(hir_id) => hir_id,
|
||||
MaybeOwner::Phantom => bug!("No HirId for {:?}", def_id),
|
||||
};
|
||||
providers.opt_hir_owner_nodes =
|
||||
|tcx, id| tcx.hir_crate(()).owners.get(id)?.as_owner().map(|i| &i.nodes);
|
||||
|
@ -174,10 +174,8 @@ rustc_queries! {
|
||||
cache_on_disk_if { true }
|
||||
}
|
||||
|
||||
/// Gives access to the HIR ID for the given `LocalDefId` owner `key` if any.
|
||||
///
|
||||
/// Definitions that were generated with no HIR, would be fed to return `None`.
|
||||
query opt_local_def_id_to_hir_id(key: LocalDefId) -> Option<hir::HirId>{
|
||||
/// Returns HIR ID for the given `LocalDefId`.
|
||||
query local_def_id_to_hir_id(key: LocalDefId) -> hir::HirId {
|
||||
desc { |tcx| "getting HIR ID of `{}`", tcx.def_path_str(key) }
|
||||
feedable
|
||||
}
|
||||
@ -196,6 +194,7 @@ rustc_queries! {
|
||||
/// Avoid calling this query directly.
|
||||
query opt_hir_owner_nodes(key: LocalDefId) -> Option<&'tcx hir::OwnerNodes<'tcx>> {
|
||||
desc { |tcx| "getting HIR owner items in `{}`", tcx.def_path_str(key) }
|
||||
feedable
|
||||
}
|
||||
|
||||
/// Gives access to the HIR attributes inside the HIR owner `key`.
|
||||
@ -204,6 +203,7 @@ rustc_queries! {
|
||||
/// Avoid calling this query directly.
|
||||
query hir_attrs(key: hir::OwnerId) -> &'tcx hir::AttributeMap<'tcx> {
|
||||
desc { |tcx| "getting HIR owner attributes in `{}`", tcx.def_path_str(key) }
|
||||
feedable
|
||||
}
|
||||
|
||||
/// Given the def_id of a const-generic parameter, computes the associated default const
|
||||
|
@ -589,6 +589,11 @@ impl<'tcx> TyCtxtFeed<'tcx, LocalDefId> {
|
||||
pub fn def_id(&self) -> LocalDefId {
|
||||
self.key
|
||||
}
|
||||
|
||||
// Caller must ensure that `self.key` ID is indeed an owner.
|
||||
pub fn feed_owner_id(&self) -> TyCtxtFeed<'tcx, hir::OwnerId> {
|
||||
TyCtxtFeed { tcx: self.tcx, key: hir::OwnerId { def_id: self.key } }
|
||||
}
|
||||
}
|
||||
|
||||
/// The central data structure of the compiler. It stores references
|
||||
@ -2350,8 +2355,8 @@ impl<'tcx> TyCtxt<'tcx> {
|
||||
self.intrinsic_raw(def_id)
|
||||
}
|
||||
|
||||
pub fn local_def_id_to_hir_id(self, local_def_id: LocalDefId) -> HirId {
|
||||
self.opt_local_def_id_to_hir_id(local_def_id).unwrap()
|
||||
pub fn opt_local_def_id_to_hir_id(self, local_def_id: LocalDefId) -> Option<HirId> {
|
||||
Some(self.local_def_id_to_hir_id(local_def_id))
|
||||
}
|
||||
|
||||
pub fn next_trait_solver_globally(self) -> bool {
|
||||
|
@ -270,7 +270,8 @@ impl<'tcx> ReachableContext<'tcx> {
|
||||
| Node::Ctor(..)
|
||||
| Node::Field(_)
|
||||
| Node::Ty(_)
|
||||
| Node::Crate(_) => {}
|
||||
| Node::Crate(_)
|
||||
| Node::AssocOpaqueTy(..) => {}
|
||||
_ => {
|
||||
bug!(
|
||||
"found unexpected node kind in worklist: {} ({:?})",
|
||||
|
@ -1,10 +1,11 @@
|
||||
use rustc_data_structures::fx::FxIndexSet;
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::def::DefKind;
|
||||
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId};
|
||||
use rustc_hir::intravisit::{self, Visitor};
|
||||
use rustc_hir::{self as hir, HirId};
|
||||
use rustc_index::IndexVec;
|
||||
use rustc_middle::query::Providers;
|
||||
use rustc_middle::ty::{self, ImplTraitInTraitData, TyCtxt};
|
||||
use rustc_middle::ty::{self, ImplTraitInTraitData, TyCtxt, TyCtxtFeed};
|
||||
use rustc_span::symbol::kw;
|
||||
|
||||
pub(crate) fn provide(providers: &mut Providers) {
|
||||
@ -237,6 +238,22 @@ fn associated_types_for_impl_traits_in_associated_fn(
|
||||
}
|
||||
}
|
||||
|
||||
fn feed_hir(feed: &TyCtxtFeed<'_, LocalDefId>) {
|
||||
feed.local_def_id_to_hir_id(HirId::make_owner(feed.def_id()));
|
||||
feed.opt_hir_owner_nodes(Some(feed.tcx.arena.alloc(hir::OwnerNodes {
|
||||
opt_hash_including_bodies: None,
|
||||
nodes: IndexVec::from_elem_n(
|
||||
hir::ParentedNode {
|
||||
parent: hir::ItemLocalId::INVALID,
|
||||
node: hir::Node::AssocOpaqueTy(&hir::AssocOpaqueTy {}),
|
||||
},
|
||||
1,
|
||||
),
|
||||
bodies: Default::default(),
|
||||
})));
|
||||
feed.feed_owner_id().hir_attrs(hir::AttributeMap::EMPTY);
|
||||
}
|
||||
|
||||
/// Given an `opaque_ty_def_id` corresponding to an `impl Trait` in an associated
|
||||
/// function from a trait, synthesize an associated type for that `impl Trait`
|
||||
/// that inherits properties that we infer from the method and the opaque type.
|
||||
@ -258,9 +275,7 @@ fn associated_type_for_impl_trait_in_trait(
|
||||
let local_def_id = trait_assoc_ty.def_id();
|
||||
let def_id = local_def_id.to_def_id();
|
||||
|
||||
// There's no HIR associated with this new synthesized `def_id`, so feed
|
||||
// `opt_local_def_id_to_hir_id` with `None`.
|
||||
trait_assoc_ty.opt_local_def_id_to_hir_id(None);
|
||||
feed_hir(&trait_assoc_ty);
|
||||
|
||||
// Copy span of the opaque.
|
||||
trait_assoc_ty.def_ident_span(Some(span));
|
||||
@ -318,9 +333,7 @@ fn associated_type_for_impl_trait_in_impl(
|
||||
let local_def_id = impl_assoc_ty.def_id();
|
||||
let def_id = local_def_id.to_def_id();
|
||||
|
||||
// There's no HIR associated with this new synthesized `def_id`, so feed
|
||||
// `opt_local_def_id_to_hir_id` with `None`.
|
||||
impl_assoc_ty.opt_local_def_id_to_hir_id(None);
|
||||
feed_hir(&impl_assoc_ty);
|
||||
|
||||
// Copy span of the opaque.
|
||||
impl_assoc_ty.def_ident_span(Some(span));
|
||||
|
Loading…
Reference in New Issue
Block a user