Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions compiler/rustc_ast_lowering/src/delegation/resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use ast::visit::Visitor;
use hir::def::DefKind;
use rustc_ast::{self as ast, AssocItemKind, Delegation, DelegationSource, Item, ItemKind, NodeId};
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
use rustc_data_structures::steal::Steal;
use rustc_hir as hir;
use rustc_middle::middle::resolve::{
self as mid_res, AstOwner, DelegationInherentFnKind, TypeRelativeDelegationRes,
Expand Down Expand Up @@ -53,9 +52,10 @@ pub(crate) fn resolve_type_relative_delegations(
// Also record resolutions for cases when signature is resolved but call path is not.
mid_res::DelegationResolution::Partial
| mid_res::DelegationResolution::PartialCall(_) => {
let Some(r_and_owner) = ast_index.get(def_id).map(Steal::borrow) else {
unreachable!("ast index must contain delegations");
};
let r_and_owner = ast_index[def_id]
.as_ref()
.expect("ast index must contain delegations")
.borrow();

let (r, owner) = &*r_and_owner;

Expand Down
21 changes: 11 additions & 10 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ enum TryBlockScope {
fn index_ast<'tcx>(
tcx: TyCtxt<'tcx>,
(): (),
) -> &'tcx IndexSlice<LocalDefId, Steal<(Arc<ResolverAstLowering<'tcx>>, AstOwner)>> {
) -> &'tcx IndexSlice<LocalDefId, Option<Steal<(Arc<ResolverAstLowering<'tcx>>, AstOwner)>>> {
// Queries that borrow `resolver_for_lowering`.
tcx.ensure_done().output_filenames(());
tcx.ensure_done().early_lint_checks(());
Expand All @@ -610,20 +610,19 @@ fn index_ast<'tcx>(
let index = indexer.index;
let resolver = Arc::new(resolver);
return tcx.arena.alloc_index_slice_from_iter::<LocalDefId, _, _>(
index.into_iter().map(|owner| Steal::new((Arc::clone(&resolver), owner))),
index.into_iter().map(|owner| owner.map(|o| Steal::new((Arc::clone(&resolver), o)))),
);

struct Indexer<'s, 'hir> {
owners: &'s NodeMap<PerOwnerResolverData<'hir>>,
index: IndexVec<LocalDefId, AstOwner>,
index: IndexVec<LocalDefId, Option<AstOwner>>,
next_node_id: NodeId,
}

impl Indexer<'_, '_> {
fn insert(&mut self, id: NodeId, node: AstOwner) {
let def_id = self.owners[&id].def_id;
self.index.ensure_contains_elem(def_id, || AstOwner::NonOwner);
self.index[def_id] = node;
self.index.insert(def_id, node);
}

fn make_dummy<K>(
Expand Down Expand Up @@ -705,7 +704,7 @@ fn lower_to_hir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::MaybeOwner<'_> {
tcx.ensure_done().resolve_type_relative_delegations(());

let ast_index = tcx.index_ast(());
let resolver_and_node = ast_index.get(def_id).map(Steal::steal);
let resolver_and_node = ast_index.get(def_id);

let fallback_to_ancestor = || {
// The item did not exist in the AST, it was created while lowering another item.
Expand All @@ -732,10 +731,13 @@ fn lower_to_hir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::MaybeOwner<'_> {
})
};

let Some((resolver, node)) = resolver_and_node else {
let Some(Some(r_and_node)) = resolver_and_node else {
// `ast_index` does not contain all definitions, only up-to the highest
// `LocalDefId` which has a non-trivial `AstOwner`. Gracefully handle
// other definitions, in particular those nested inside this highest definition.
// OR
// The item existed in the AST, but is not a HIR owner.
// Fetch the correct information from its parent.
return fallback_to_ancestor();
};

Expand All @@ -750,6 +752,8 @@ fn lower_to_hir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::MaybeOwner<'_> {
hir::MaybeOwner::Owner(lctx.curr_owner.into_owner_info(tcx, item))
}

let (resolver, node) = r_and_node.steal();

let item = match &node {
// The item existed in the AST.
AstOwner::Crate(c) => with_lctx(tcx, &*resolver, CRATE_NODE_ID, |lctx| {
Expand All @@ -770,9 +774,6 @@ fn lower_to_hir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::MaybeOwner<'_> {
AstOwner::ForeignItem(item) => with_lctx(tcx, &*resolver, item.id, |lctx| {
hir::OwnerNode::ForeignItem(lctx.lower_foreign_item(item))
}),
// The item existed in the AST, but is not a HIR owner.
// Fetch the correct information from its parent.
AstOwner::NonOwner => fallback_to_ancestor(),
};

tcx.sess.time("drop_ast", || mem::drop(node));
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ rustc_arena::declare_arena! {
rustc_middle::middle::resolve::ResolverAstLowering<'tcx>
>,
index_ast:
rustc_data_structures::steal::Steal<(
Option<rustc_data_structures::steal::Steal<(
std::sync::Arc<rustc_middle::middle::resolve::ResolverAstLowering<'tcx>>,
rustc_middle::middle::resolve::AstOwner
)>,
)>>,
crate_alone: rustc_data_structures::steal::Steal<rustc_ast::Crate>,
crate_for_resolver: rustc_data_structures::steal::Steal<(rustc_ast::Crate, rustc_ast::AttrVec)>,
resolutions: rustc_middle::middle::resolve::ResolverGlobalCtxt,
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_middle/src/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,6 @@ pub type DocLinkResMap = FxIndexMap<(Symbol, Namespace), Option<Res<NodeId>>>;
/// is as good a place as any for it.
#[derive(Debug)]
pub enum AstOwner {
/// This definition does not correspond to a HIR owner.
NonOwner,
Crate(Box<ast::Crate>),
Item(Box<ast::Item>),
TraitItem(Box<ast::AssocItem>),
Expand Down
12 changes: 5 additions & 7 deletions compiler/rustc_middle/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,11 @@ rustc_queries! {
desc { "getting the resolver for lowering" }
}

query index_ast(_: ()) -> &'tcx IndexSlice<LocalDefId, Steal<(
// There is only a single `ResolverAstLowering` for all owners.
// We want to drop it once the whole HIR has been lowered.
// We rely on reference counting to know when all definitions have been stolen.
Arc<ResolverAstLowering<'tcx>>,
AstOwner,
)>> {
// There is only a single `ResolverAstLowering` for all owners.
// We want to drop it once the whole HIR has been lowered.
// We rely on reference counting to know when all definitions have been stolen.
// None if the `LocalDefId` is not an owner.
query index_ast(_: ()) -> &'tcx IndexSlice<LocalDefId, Option<Steal<(Arc<ResolverAstLowering<'tcx>>, AstOwner)>>> {
eval_always
no_hash
desc { "getting the AST for lowering" }
Expand Down
Loading