Skip to content

Commit

Permalink
rustc_error, rustc_private, rustc_ast: Switch to stable hash containers
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasJonsson committed Sep 10, 2022
1 parent db9d86b commit 8d3c30c
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 16 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/node_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ rustc_index::newtype_index! {
}
}

rustc_data_structures::define_id_collections!(NodeMap, NodeSet, NodeId);
rustc_data_structures::define_id_collections!(NodeMap, NodeSet, NodeMapEntry, NodeId);

/// The [`NodeId`] used to represent the root of the crate.
pub const CRATE_NODE_ID: NodeId = NodeId::from_u32(0);
Expand Down
15 changes: 14 additions & 1 deletion compiler/rustc_data_structures/src/fx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,26 @@ use std::hash::BuildHasherDefault;

pub use rustc_hash::{FxHashMap, FxHashSet, FxHasher};

pub type StdEntry<'a, K, V> = std::collections::hash_map::Entry<'a, K, V>;

pub type FxIndexMap<K, V> = indexmap::IndexMap<K, V, BuildHasherDefault<FxHasher>>;
pub type FxIndexSet<V> = indexmap::IndexSet<V, BuildHasherDefault<FxHasher>>;
pub type IndexEntry<'a, K, V> = indexmap::map::Entry<'a, K, V>;

#[macro_export]
macro_rules! define_id_collections {
($map_name:ident, $set_name:ident, $key:ty) => {
($map_name:ident, $set_name:ident, $entry_name:ident, $key:ty) => {
pub type $map_name<T> = $crate::fx::FxHashMap<$key, T>;
pub type $set_name = $crate::fx::FxHashSet<$key>;
pub type $entry_name<'a, T> = $crate::fx::StdEntry<'a, $key, T>;
};
}

#[macro_export]
macro_rules! define_stable_id_collections {
($map_name:ident, $set_name:ident, $entry_name:ident, $key:ty) => {
pub type $map_name<T> = $crate::fx::FxIndexMap<$key, T>;
pub type $set_name = $crate::fx::FxIndexSet<$key>;
pub type $entry_name<'a, T> = $crate::fx::IndexEntry<'a, $key, T>;
};
}
4 changes: 2 additions & 2 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{

use rustc_lint_defs::pluralize;

use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
use rustc_data_structures::sync::Lrc;
use rustc_error_messages::FluentArgs;
use rustc_span::hygiene::{ExpnKind, MacroKind};
Expand Down Expand Up @@ -1487,7 +1487,7 @@ impl EmitterWriter {
);

// Contains the vertical lines' positions for active multiline annotations
let mut multilines = FxHashMap::default();
let mut multilines = FxIndexMap::default();

// Get the left-side margin to remove it
let mut whitespace_margin = usize::MAX;
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#![feature(result_option_inspect)]
#![feature(rustc_attrs)]
#![allow(incomplete_features)]
#![allow(rustc::potential_query_instability)]

#[macro_use]
extern crate rustc_macros;
Expand All @@ -27,7 +26,7 @@ use Level::*;

use emitter::{is_case_difference, Emitter, EmitterWriter};
use registry::Registry;
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
use rustc_data_structures::stable_hasher::StableHasher;
use rustc_data_structures::sync::{self, Lock, Lrc};
use rustc_data_structures::AtomicRef;
Expand Down Expand Up @@ -413,7 +412,7 @@ struct HandlerInner {
taught_diagnostics: FxHashSet<DiagnosticId>,

/// Used to suggest rustc --explain <error code>
emitted_diagnostic_codes: FxHashSet<DiagnosticId>,
emitted_diagnostic_codes: FxIndexSet<DiagnosticId>,

/// This set contains a hash of every diagnostic that has been emitted by
/// this handler. These hashes is used to avoid emitting the same error
Expand Down
9 changes: 7 additions & 2 deletions compiler/rustc_hir/src/hir_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,13 @@ impl PartialOrd for HirId {
}
}

rustc_data_structures::define_id_collections!(HirIdMap, HirIdSet, HirId);
rustc_data_structures::define_id_collections!(ItemLocalMap, ItemLocalSet, ItemLocalId);
rustc_data_structures::define_stable_id_collections!(HirIdMap, HirIdSet, HirIdMapEntry, HirId);
rustc_data_structures::define_id_collections!(
ItemLocalMap,
ItemLocalSet,
ItemLocalMapEntry,
ItemLocalId
);

rustc_index::newtype_index! {
/// An `ItemLocalId` uniquely identifies something within a given "item-like";
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_privacy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#![feature(rustc_private)]
#![feature(try_blocks)]
#![recursion_limit = "256"]
#![allow(rustc::potential_query_instability)]
#![deny(rustc::untranslatable_diagnostic)]
#![deny(rustc::diagnostic_outside_of_impl)]

Expand Down
9 changes: 7 additions & 2 deletions compiler/rustc_span/src/def_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ impl fmt::Debug for DefId {
}
}

rustc_data_structures::define_id_collections!(DefIdMap, DefIdSet, DefId);
rustc_data_structures::define_id_collections!(DefIdMap, DefIdSet, DefIdMapEntry, DefId);

/// A `LocalDefId` is equivalent to a `DefId` with `krate == LOCAL_CRATE`. Since
/// we encode this information in the type, we can ensure at compile time that
Expand Down Expand Up @@ -399,7 +399,12 @@ impl<D: Decoder> Decodable<D> for LocalDefId {
}
}

rustc_data_structures::define_id_collections!(LocalDefIdMap, LocalDefIdSet, LocalDefId);
rustc_data_structures::define_id_collections!(
LocalDefIdMap,
LocalDefIdSet,
LocalDefIdMapEntry,
LocalDefId
);

impl<CTX: HashStableContext> HashStable<CTX> for DefId {
#[inline]
Expand Down
7 changes: 3 additions & 4 deletions src/tools/clippy/clippy_lints/src/matches/match_same_arms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ use rustc_arena::DroplessArena;
use rustc_ast::ast::LitKind;
use rustc_errors::Applicability;
use rustc_hir::def_id::DefId;
use rustc_hir::{Arm, Expr, ExprKind, HirId, HirIdMap, HirIdSet, Pat, PatKind, RangeEnd};
use rustc_hir::{Arm, Expr, ExprKind, HirId, HirIdMap, HirIdMapEntry, HirIdSet, Pat, PatKind, RangeEnd};
use rustc_lint::LateContext;
use rustc_middle::ty;
use rustc_span::Symbol;
use std::collections::hash_map::Entry;

use super::MATCH_SAME_ARMS;

Expand Down Expand Up @@ -71,9 +70,9 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>]) {
if let Some(a_id) = path_to_local(a);
if let Some(b_id) = path_to_local(b);
let entry = match local_map.entry(a_id) {
Entry::Vacant(entry) => entry,
HirIdMapEntry::Vacant(entry) => entry,
// check if using the same bindings as before
Entry::Occupied(entry) => return *entry.get() == b_id,
HirIdMapEntry::Occupied(entry) => return *entry.get() == b_id,
};
// the names technically don't have to match; this makes the lint more conservative
if cx.tcx.hir().name(a_id) == cx.tcx.hir().name(b_id);
Expand Down

0 comments on commit 8d3c30c

Please sign in to comment.