Skip to content

Commit 5b00e0b

Browse files
authored
Rollup merge of #137202 - Mark-Simulacrum:interned-is-hash, r=saethlin
Enforce T: Hash for Interned<...> This adds panicking Hash impls for several resolver types that don't actually satisfy this condition. It's not obvious to me that rustc_resolve actually upholds the Interned guarantees but fixing that seems pretty hard (the structures have at minimum some interior mutability, so it's not really recursively hashable in place...). FIXME comments as such on those impls. cc #137196 (comment) r? ``@saethlin``
2 parents 626fcdf + 9fc7590 commit 5b00e0b

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

compiler/rustc_data_structures/src/intern.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ impl<'a, T: Ord> Ord for Interned<'a, T> {
9292
}
9393
}
9494

95-
impl<'a, T> Hash for Interned<'a, T> {
95+
impl<'a, T> Hash for Interned<'a, T>
96+
where
97+
T: Hash,
98+
{
9699
#[inline]
97100
fn hash<H: Hasher>(&self, s: &mut H) {
98101
// Pointer hashing is sufficient, due to the uniqueness constraint.

compiler/rustc_resolve/src/imports.rs

+13
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,19 @@ pub(crate) struct ImportData<'ra> {
182182
/// so we can use referential equality to compare them.
183183
pub(crate) type Import<'ra> = Interned<'ra, ImportData<'ra>>;
184184

185+
// Allows us to use Interned without actually enforcing (via Hash/PartialEq/...) uniqueness of the
186+
// contained data.
187+
// FIXME: We may wish to actually have at least debug-level assertions that Interned's guarantees
188+
// are upheld.
189+
impl std::hash::Hash for ImportData<'_> {
190+
fn hash<H>(&self, _: &mut H)
191+
where
192+
H: std::hash::Hasher,
193+
{
194+
unreachable!()
195+
}
196+
}
197+
185198
impl<'ra> ImportData<'ra> {
186199
pub(crate) fn is_glob(&self) -> bool {
187200
matches!(self.kind, ImportKind::Glob { .. })

compiler/rustc_resolve/src/lib.rs

+26
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,19 @@ struct ModuleData<'ra> {
589589
#[rustc_pass_by_value]
590590
struct Module<'ra>(Interned<'ra, ModuleData<'ra>>);
591591

592+
// Allows us to use Interned without actually enforcing (via Hash/PartialEq/...) uniqueness of the
593+
// contained data.
594+
// FIXME: We may wish to actually have at least debug-level assertions that Interned's guarantees
595+
// are upheld.
596+
impl std::hash::Hash for ModuleData<'_> {
597+
fn hash<H>(&self, _: &mut H)
598+
where
599+
H: std::hash::Hasher,
600+
{
601+
unreachable!()
602+
}
603+
}
604+
592605
impl<'ra> ModuleData<'ra> {
593606
fn new(
594607
parent: Option<Module<'ra>>,
@@ -739,6 +752,19 @@ struct NameBindingData<'ra> {
739752
/// so we can use referential equality to compare them.
740753
type NameBinding<'ra> = Interned<'ra, NameBindingData<'ra>>;
741754

755+
// Allows us to use Interned without actually enforcing (via Hash/PartialEq/...) uniqueness of the
756+
// contained data.
757+
// FIXME: We may wish to actually have at least debug-level assertions that Interned's guarantees
758+
// are upheld.
759+
impl std::hash::Hash for NameBindingData<'_> {
760+
fn hash<H>(&self, _: &mut H)
761+
where
762+
H: std::hash::Hasher,
763+
{
764+
unreachable!()
765+
}
766+
}
767+
742768
trait ToNameBinding<'ra> {
743769
fn to_name_binding(self, arenas: &'ra ResolverArenas<'ra>) -> NameBinding<'ra>;
744770
}

0 commit comments

Comments
 (0)