Skip to content
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

[WIP] Add HashStableEq trait, and use it in query code #93467

Closed
wants to merge 18 commits into from
7 changes: 7 additions & 0 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ pub struct Crate {
///
/// E.g., the '..' in `#[name(..)]`.
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
#[stable_hasher(no_hash_stable_eq)]
pub enum NestedMetaItem {
/// A full MetaItem, for recursive meta items.
MetaItem(MetaItem),
Expand All @@ -534,6 +535,7 @@ pub enum NestedMetaItem {
///
/// E.g., `#[test]`, `#[derive(..)]`, `#[rustfmt::skip]` or `#[feature = "foo"]`.
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
#[stable_hasher(no_hash_stable_eq)]
pub struct MetaItem {
pub path: Path,
pub kind: MetaItemKind,
Expand All @@ -544,6 +546,7 @@ pub struct MetaItem {
///
/// E.g., `#[test]`, `#[derive(..)]` or `#[feature = "foo"]`.
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
#[stable_hasher(no_hash_stable_eq)]
pub enum MetaItemKind {
/// Word meta item.
///
Expand Down Expand Up @@ -1527,6 +1530,7 @@ impl MacCall {

/// Arguments passed to an attribute or a function-like macro.
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
#[stable_hasher(no_hash_stable_eq)]
pub enum MacArgs {
/// No arguments - `#[attr]`.
Empty,
Expand Down Expand Up @@ -1602,6 +1606,7 @@ impl MacDelimiter {

/// Represents a macro definition.
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
#[stable_hasher(no_hash_stable_eq)]
pub struct MacroDef {
pub body: P<MacArgs>,
/// `true` if macro was defined with `macro_rules`.
Expand All @@ -1621,6 +1626,7 @@ pub enum StrStyle {

/// An AST literal.
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
#[stable_hasher(no_hash_stable_eq)]
pub struct Lit {
/// The original literal token as written in source code.
pub token: token::Lit,
Expand Down Expand Up @@ -2425,6 +2431,7 @@ impl<D: Decoder> rustc_serialize::Decodable<D> for AttrId {
}

#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
#[stable_hasher(no_hash_stable_eq)]
pub struct AttrItem {
pub path: Path,
pub args: MacArgs,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_ast/src/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};

use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
/// An owned smart pointer.
#[derive(HashStableEq)]
pub struct P<T: ?Sized> {
ptr: Box<T>,
}
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_ast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ fn ident_can_begin_type(name: Symbol, span: Span, is_raw: bool) -> bool {
}

#[derive(Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
#[stable_hasher(no_hash_stable_eq)]
pub enum TokenKind {
/* Expression-operator symbols. */
Eq,
Expand Down Expand Up @@ -246,6 +247,7 @@ pub enum TokenKind {
rustc_data_structures::static_assert_size!(TokenKind, 16);

#[derive(Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
#[stable_hasher(no_hash_stable_eq)]
pub struct Token {
pub kind: TokenKind,
pub span: Span,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_ast/src/tokenstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use std::{fmt, iter, mem};
/// The RHS of an MBE macro is the only place `SubstNt`s are substituted.
/// Nothing special happens to misnamed or misplaced `SubstNt`s.
#[derive(Debug, Clone, PartialEq, Encodable, Decodable, HashStable_Generic)]
#[stable_hasher(no_hash_stable_eq)]
pub enum TokenTree {
/// A single token.
Token(Token),
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_codegen_ssa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ bitflags::bitflags! {
}

#[derive(Clone, Debug, Encodable, Decodable, HashStable)]
#[stable_hasher(no_hash_stable_eq)]
pub struct NativeLib {
pub kind: NativeLibKind,
pub name: Option<Symbol>,
Expand Down
10 changes: 10 additions & 0 deletions compiler/rustc_data_structures/src/intern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::ptr;

use crate::stable_hasher::HashStableEq;

mod private {
#[derive(Clone, Copy, Debug)]
pub struct PrivateZst;
Expand Down Expand Up @@ -52,6 +54,14 @@ impl<'a, T> Deref for Interned<'a, T> {
}
}

// FIXME - is this right? Should we try to enforce this somehow?
impl<'a, T: HashStableEq> HashStableEq for Interned<'a, T> {
fn hash_stable_eq(&self, other: &Self) -> bool {
// Pointer equality implies equality, due to the uniqueness constraint.
ptr::eq(self.0, other.0)
}
}

impl<'a, T> PartialEq for Interned<'a, T> {
#[inline]
fn eq(&self, other: &Self) -> bool {
Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_data_structures/src/sorted_map.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::stable_hasher::{HashStable, StableHasher};
use crate::stable_hasher::{HashStable, HashStableEq, StableHasher};
use std::borrow::Borrow;
use std::cmp::Ordering;
use std::iter::FromIterator;
Expand Down Expand Up @@ -298,5 +298,11 @@ impl<K: HashStable<CTX>, V: HashStable<CTX>, CTX> HashStable<CTX> for SortedMap<
}
}

impl<K: HashStableEq, V: HashStableEq> HashStableEq for SortedMap<K, V> {
fn hash_stable_eq(&self, other: &Self) -> bool {
self.data.hash_stable_eq(&other.data)
}
}

#[cfg(test)]
mod tests;
Loading