diff --git a/compiler/rustc_ast_lowering/src/delegation/resolution.rs b/compiler/rustc_ast_lowering/src/delegation/resolution.rs index 7678418c47e56..8e35b05d7e870 100644 --- a/compiler/rustc_ast_lowering/src/delegation/resolution.rs +++ b/compiler/rustc_ast_lowering/src/delegation/resolution.rs @@ -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, @@ -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; diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index b6d1017665ec6..a99db03007b51 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -587,7 +587,7 @@ enum TryBlockScope { fn index_ast<'tcx>( tcx: TyCtxt<'tcx>, (): (), -) -> &'tcx IndexSlice>, AstOwner)>> { +) -> &'tcx IndexSlice>, AstOwner)>>> { // Queries that borrow `resolver_for_lowering`. tcx.ensure_done().output_filenames(()); tcx.ensure_done().early_lint_checks(()); @@ -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::( - 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>, - index: IndexVec, + index: IndexVec>, 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( @@ -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. @@ -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(); }; @@ -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| { @@ -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)); diff --git a/compiler/rustc_middle/src/arena.rs b/compiler/rustc_middle/src/arena.rs index a423b6bdca5b2..d2e6c4bbe2466 100644 --- a/compiler/rustc_middle/src/arena.rs +++ b/compiler/rustc_middle/src/arena.rs @@ -43,10 +43,10 @@ rustc_arena::declare_arena! { rustc_middle::middle::resolve::ResolverAstLowering<'tcx> >, index_ast: - rustc_data_structures::steal::Steal<( + Option>, rustc_middle::middle::resolve::AstOwner - )>, + )>>, crate_alone: rustc_data_structures::steal::Steal, crate_for_resolver: rustc_data_structures::steal::Steal<(rustc_ast::Crate, rustc_ast::AttrVec)>, resolutions: rustc_middle::middle::resolve::ResolverGlobalCtxt, diff --git a/compiler/rustc_middle/src/middle/resolve.rs b/compiler/rustc_middle/src/middle/resolve.rs index bafb45469a56e..013edbf24739a 100644 --- a/compiler/rustc_middle/src/middle/resolve.rs +++ b/compiler/rustc_middle/src/middle/resolve.rs @@ -332,8 +332,6 @@ pub type DocLinkResMap = FxIndexMap<(Symbol, Namespace), Option>>; /// 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), Item(Box), TraitItem(Box), diff --git a/compiler/rustc_middle/src/queries.rs b/compiler/rustc_middle/src/queries.rs index d9d618519446d..d0429df43d1dd 100644 --- a/compiler/rustc_middle/src/queries.rs +++ b/compiler/rustc_middle/src/queries.rs @@ -206,13 +206,11 @@ rustc_queries! { desc { "getting the resolver for lowering" } } - query index_ast(_: ()) -> &'tcx IndexSlice>, - 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>, AstOwner)>>> { eval_always no_hash desc { "getting the AST for lowering" }