Skip to content

Commit

Permalink
feat(types): impl PartialEq and Hash for Ident instead of `Modu…
Browse files Browse the repository at this point in the history
…lePath`

This follows the original implementation. 2 `Ident`s should be equal if their values are equal,
even if their `Span`s are different. This also includes manually implementing `Hash` to allow
for this.
  • Loading branch information
bing committed Jun 8, 2024
1 parent 83900b1 commit a0cca5f
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/parser/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::{fmt::Display, str::FromStr};
use std::{
fmt::Display,
hash::{Hash, Hasher},
str::FromStr,
};

use ark_ff::Field;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -153,7 +157,7 @@ pub struct Ty {

/// The module preceding structs, functions, or variables.
// TODO: Hash should probably be implemented manually, right now two alias might have different span and so this will give different hashes
#[derive(Default, Debug, Clone, Serialize, Deserialize, Hash, Eq)]
#[derive(Default, Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
pub enum ModulePath {
#[default]
/// This is a local type, not imported from another module.
Expand All @@ -167,18 +171,6 @@ pub enum ModulePath {
Absolute(UserRepo),
}

// TODO: do we want to implement this on Ident instead?
impl PartialEq for ModulePath {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(ModulePath::Alias(a), ModulePath::Alias(b)) => a.value == b.value,
(ModulePath::Local, ModulePath::Local) => true,
(ModulePath::Absolute(a), ModulePath::Absolute(b)) => a == b,
_ => false,
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum TyKind {
/// The main primitive type. 'Nuf said.
Expand Down Expand Up @@ -401,12 +393,25 @@ impl FnSig {
}

/// Any kind of text that can represent a type, a variable, a function name, etc.
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[derive(Debug, Default, Clone, Eq, Serialize, Deserialize)]
pub struct Ident {
pub value: String,
pub span: Span,
}

impl PartialEq for Ident {
fn eq(&self, other: &Self) -> bool {
self.value == other.value
}
}

impl Hash for Ident {
fn hash<H: Hasher>(&self, state: &mut H) {
self.value.hash(state);
self.span.hash(state);
}
}

impl Ident {
pub fn new(value: String, span: Span) -> Self {
Self { value, span }
Expand Down

0 comments on commit a0cca5f

Please sign in to comment.