Skip to content

Enforce T: Hash for Interned<...> #137202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2025
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
5 changes: 4 additions & 1 deletion compiler/rustc_data_structures/src/intern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ impl<'a, T: Ord> Ord for Interned<'a, T> {
}
}

impl<'a, T> Hash for Interned<'a, T> {
impl<'a, T> Hash for Interned<'a, T>
where
T: Hash,
{
#[inline]
fn hash<H: Hasher>(&self, s: &mut H) {
// Pointer hashing is sufficient, due to the uniqueness constraint.
Expand Down
13 changes: 13 additions & 0 deletions compiler/rustc_resolve/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,19 @@ pub(crate) struct ImportData<'ra> {
/// so we can use referential equality to compare them.
pub(crate) type Import<'ra> = Interned<'ra, ImportData<'ra>>;

// Allows us to use Interned without actually enforcing (via Hash/PartialEq/...) uniqueness of the
// contained data.
// FIXME: We may wish to actually have at least debug-level assertions that Interned's guarantees
// are upheld.
impl std::hash::Hash for ImportData<'_> {
fn hash<H>(&self, _: &mut H)
where
H: std::hash::Hasher,
{
unreachable!()
}
}

impl<'ra> ImportData<'ra> {
pub(crate) fn is_glob(&self) -> bool {
matches!(self.kind, ImportKind::Glob { .. })
Expand Down
26 changes: 26 additions & 0 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,19 @@ struct ModuleData<'ra> {
#[rustc_pass_by_value]
struct Module<'ra>(Interned<'ra, ModuleData<'ra>>);

// Allows us to use Interned without actually enforcing (via Hash/PartialEq/...) uniqueness of the
// contained data.
// FIXME: We may wish to actually have at least debug-level assertions that Interned's guarantees
// are upheld.
impl std::hash::Hash for ModuleData<'_> {
fn hash<H>(&self, _: &mut H)
where
H: std::hash::Hasher,
{
unreachable!()
}
}

impl<'ra> ModuleData<'ra> {
fn new(
parent: Option<Module<'ra>>,
Expand Down Expand Up @@ -739,6 +752,19 @@ struct NameBindingData<'ra> {
/// so we can use referential equality to compare them.
type NameBinding<'ra> = Interned<'ra, NameBindingData<'ra>>;

// Allows us to use Interned without actually enforcing (via Hash/PartialEq/...) uniqueness of the
// contained data.
// FIXME: We may wish to actually have at least debug-level assertions that Interned's guarantees
// are upheld.
impl std::hash::Hash for NameBindingData<'_> {
fn hash<H>(&self, _: &mut H)
where
H: std::hash::Hasher,
{
unreachable!()
}
}

trait ToNameBinding<'ra> {
fn to_name_binding(self, arenas: &'ra ResolverArenas<'ra>) -> NameBinding<'ra>;
}
Expand Down
Loading