Skip to content
Draft
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
18 changes: 11 additions & 7 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ use rustc_hir::{
self as hir, AngleBrackets, CRATE_OWNER_ID, ConstArg, GenericArg, HirId, ItemLocalMap,
LifetimeSource, LifetimeSyntax, MissingLifetimeKind, ParamName, TraitCandidate,
};
use rustc_index::{Idx, IndexSlice, IndexVec};
use rustc_index::Idx;
use rustc_index::dense_map::{DenseMap, DenseMapRef};
use rustc_macros::extension;
use rustc_middle::middle::resolve::{
AstOwner, LifetimeRes, PartialRes, PerOwnerResolverData, ResolverAstLowering,
Expand Down Expand Up @@ -590,7 +591,7 @@ enum TryBlockScope {
fn index_ast<'tcx>(
tcx: TyCtxt<'tcx>,
(): (),
) -> &'tcx IndexSlice<LocalDefId, Option<Steal<(Arc<ResolverAstLowering<'tcx>>, AstOwner)>>> {
) -> DenseMapRef<'tcx, 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 @@ -603,7 +604,7 @@ fn index_ast<'tcx>(

let mut indexer = Indexer {
owners: &resolver.owners,
index: IndexVec::new(),
index: Default::default(),
next_node_id: resolver.next_node_id,
};
indexer.visit_crate(&mut krate);
Expand All @@ -612,20 +613,23 @@ fn index_ast<'tcx>(

let index = indexer.index;
let resolver = Arc::new(resolver);
return tcx.arena.alloc_index_slice_from_iter::<LocalDefId, _, _>(
return DenseMapRef::from_raw(tcx.arena.alloc_from_iter(
// We are delegating handling of stable iteration to the users of the returned
// `DenseMapRef`, it also has a warning for unstable iteration
#[allow(rustc::potential_query_instability)]
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, Option<AstOwner>>,
index: DenseMap<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.insert(def_id, node);
self.index.insert(def_id, Some(node));
}

fn make_dummy<K>(
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_crate_store/src/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::sync::OnceLock;
use rustc_abi::ExternAbi;
use rustc_attr_ir::{CfgEntry, PeImportNameType};
use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::{self, AppendOnlyIndexVec, FreezeLock};
use rustc_data_structures::sync::{self, AppendOnlyDenseMap, FreezeLock};
use rustc_hir_id::definitions::{DefKey, DefPath, Definitions};
use rustc_macros::{BlobDecodable, Decodable, Encodable, StableHash};
use rustc_span::def_id::{
Expand Down Expand Up @@ -218,7 +218,7 @@ pub type CrateStoreDyn = dyn CrateStore + sync::DynSync + sync::DynSend;
pub struct Untracked {
pub cstore: FreezeLock<Box<CrateStoreDyn>>,
/// Reference span for definitions.
pub source_span: AppendOnlyIndexVec<LocalDefId, Span>,
pub source_span: AppendOnlyDenseMap<LocalDefId, Span>,
pub definitions: FreezeLock<Definitions>,
/// The interned [StableCrateId]s.
pub stable_crate_ids: FreezeLock<StableCrateIdMap>,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_data_structures/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub use self::parallel::{
broadcast, par_fns, par_for_each_in, par_for_each_slice, par_join, par_map, parallel_guard,
spawn, try_par_for_each_in,
};
pub use self::vec::{AppendOnlyIndexVec, AppendOnlyVec};
pub use self::vec::{AppendOnlyDenseMap, AppendOnlyVec};
pub use self::worker_local::{Registry, WorkerLocal};
pub use crate::marker::*;

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_data_structures/src/sync/vec.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::marker::PhantomData;

use rustc_index::Idx;
use rustc_index::dense_map::DenseKey;

#[derive(Default)]
pub struct AppendOnlyIndexVec<I: Idx, T: Copy> {
pub struct AppendOnlyDenseMap<I: DenseKey, T: Copy> {
vec: elsa::sync::LockFreeFrozenVec<T>,
_marker: PhantomData<fn(&I)>,
}

impl<I: Idx, T: Copy> AppendOnlyIndexVec<I, T> {
impl<I: DenseKey, T: Copy> AppendOnlyDenseMap<I, T> {
pub fn new() -> Self {
Self { vec: elsa::sync::LockFreeFrozenVec::new(), _marker: PhantomData }
}
Expand Down
11 changes: 6 additions & 5 deletions compiler/rustc_data_structures/src/vec_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::ops::{Index, IndexMut};
use std::sync::atomic::{AtomicPtr, AtomicU32, AtomicUsize, Ordering};

use rustc_index::Idx;
use rustc_index::dense_map::DenseKey;

#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -221,7 +222,7 @@ impl SlotIndex {
/// query dependency tracking.
///
/// [#124780]: https://github.com/rust-lang/rust/pull/124780
pub struct VecCache<K: Idx, V, I> {
pub struct VecCache<K: DenseKey, V, I> {
// Entries per bucket:
// Bucket 0: 4096 2^12
// Bucket 1: 4096 2^12
Expand All @@ -243,7 +244,7 @@ pub struct VecCache<K: Idx, V, I> {
key: PhantomData<(K, I)>,
}

impl<K: Idx, V, I> Default for VecCache<K, V, I> {
impl<K: DenseKey, V, I> Default for VecCache<K, V, I> {
fn default() -> Self {
VecCache {
buckets: Default::default(),
Expand All @@ -255,7 +256,7 @@ impl<K: Idx, V, I> Default for VecCache<K, V, I> {
}
}

impl<K: Idx, V, I> VecCache<K, V, I> {
impl<K: DenseKey, V, I> VecCache<K, V, I> {
pub fn without_track_present() -> Self {
let mut this = VecCache::default();
this.track_present = false;
Expand All @@ -264,7 +265,7 @@ impl<K: Idx, V, I> VecCache<K, V, I> {
}

// SAFETY: No access to `V` is made.
unsafe impl<K: Idx, #[may_dangle] V, I> Drop for VecCache<K, V, I> {
unsafe impl<K: DenseKey, #[may_dangle] V, I> Drop for VecCache<K, V, I> {
fn drop(&mut self) {
// We have unique ownership, so no locks etc. are needed. Since `K` and `V` are both `Copy`,
// we are also guaranteed to just need to deallocate any large arrays (not iterate over
Expand Down Expand Up @@ -303,7 +304,7 @@ unsafe impl<K: Idx, #[may_dangle] V, I> Drop for VecCache<K, V, I> {

impl<K, V, I> VecCache<K, V, I>
where
K: Eq + Idx + Copy + Debug,
K: Eq + DenseKey + Copy + Debug,
V: Copy,
I: Idx + Copy,
{
Expand Down
9 changes: 6 additions & 3 deletions compiler/rustc_hir_id/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::sorted_map::SortedMap;
use rustc_data_structures::stable_hash::StableHasher;
use rustc_hashes::Hash64;
use rustc_index::IndexVec;
use rustc_index::dense_map::DenseMap;
use rustc_macros::{BlobDecodable, Decodable, Encodable, extension};
use rustc_serialize::Encodable;
pub use rustc_span::def_id::DefPathHash;
Expand Down Expand Up @@ -96,9 +96,9 @@ impl DefPathToIndexMap {
#[derive(Debug)]
pub struct Definitions {
stable_crate_id: StableCrateId,
def_id_to_key: IndexVec<LocalDefId, DefKey>,
def_id_to_key: DenseMap<LocalDefId, DefKey>,
// We do only store the local hash, as all the definitions are from the current crate.
def_path_hashes: IndexVec<LocalDefId, Hash64>,
def_path_hashes: DenseMap<LocalDefId, Hash64>,
// The first non deterministic def index, may correspond to non-allocated
// def id, in this case `first_non_det_index.as_usize() == num_definitions`.
first_non_det_index: Option<DefIndex>,
Expand Down Expand Up @@ -420,9 +420,12 @@ impl Definitions {
def_id
}

#[rustc_lint_query_instability]
pub fn enumerated_keys_and_path_hashes(
&self,
) -> impl Iterator<Item = (DefIndex, &DefKey, DefPathHash)> + ExactSizeIterator {
// propagated to the caller
#[allow(rustc::potential_query_instability)]
self.def_id_to_key
.iter_enumerated()
.map(move |(def_id, key)| (def_id.local_def_index, key, self.def_path_hash(def_id)))
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_hir_id/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::fmt::{self, Debug};
use rustc_data_structures::stable_hash::{
StableHash, StableHashCtxt, StableHasher, StableOrd, ToStableHashKey,
};
use rustc_index::dense_map::DenseKey;
use rustc_macros::{Decodable, Encodable, StableHash};
use rustc_serialize::ContextOrd;
use rustc_span::def_id::{CRATE_DEF_ID, DefId, DefIndex, DefPathHash, LocalDefId, LocalModId};
Expand Down Expand Up @@ -56,7 +57,7 @@ impl OwnerId {
}
}

impl rustc_index::Idx for OwnerId {
impl DenseKey for OwnerId {
#[inline]
fn new(idx: usize) -> Self {
OwnerId { def_id: LocalDefId { local_def_index: DefIndex::from_usize(idx) } }
Expand Down Expand Up @@ -106,6 +107,7 @@ pub struct HirId {
// See https://github.com/rust-lang/rust/issues/90317.
impl !Ord for HirId {}
impl !PartialOrd for HirId {}
impl !rustc_index::Idx for OwnerId {}

impl Debug for HirId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down
Loading
Loading