mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-26 21:39:07 +00:00
Add support for hash key of tuples
This commit is contained in:
parent
f4f9ae7a5d
commit
e6cac71ca5
3 changed files with 16 additions and 3 deletions
|
@ -30,6 +30,7 @@ use crate::{synth_var, util::Env, DerivedBody};
|
||||||
pub(crate) fn derive_hash(env: &mut Env<'_>, key: FlatHashKey, def_symbol: Symbol) -> DerivedBody {
|
pub(crate) fn derive_hash(env: &mut Env<'_>, key: FlatHashKey, def_symbol: Symbol) -> DerivedBody {
|
||||||
let (body_type, body) = match key {
|
let (body_type, body) = match key {
|
||||||
FlatHashKey::Record(fields) => hash_record(env, def_symbol, fields),
|
FlatHashKey::Record(fields) => hash_record(env, def_symbol, fields),
|
||||||
|
FlatHashKey::Tuple(_fields) => todo!(),
|
||||||
FlatHashKey::TagUnion(tags) => {
|
FlatHashKey::TagUnion(tags) => {
|
||||||
if tags.len() == 1 {
|
if tags.len() == 1 {
|
||||||
hash_newtype_tag_union(env, def_symbol, tags.into_iter().next().unwrap())
|
hash_newtype_tag_union(env, def_symbol, tags.into_iter().next().unwrap())
|
||||||
|
|
|
@ -5,7 +5,7 @@ use roc_module::{
|
||||||
use roc_types::subs::{Content, FlatType, GetSubsSlice, Subs, Variable};
|
use roc_types::subs::{Content, FlatType, GetSubsSlice, Subs, Variable};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
util::{check_derivable_ext_var, debug_name_record, debug_name_tag},
|
util::{check_derivable_ext_var, debug_name_record, debug_name_tag, debug_name_tuple},
|
||||||
DeriveError,
|
DeriveError,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -21,6 +21,7 @@ pub enum FlatHash {
|
||||||
pub enum FlatHashKey {
|
pub enum FlatHashKey {
|
||||||
// Unfortunate that we must allocate here, c'est la vie
|
// Unfortunate that we must allocate here, c'est la vie
|
||||||
Record(Vec<Lowercase>),
|
Record(Vec<Lowercase>),
|
||||||
|
Tuple(u32),
|
||||||
TagUnion(Vec<(TagName, u16)>),
|
TagUnion(Vec<(TagName, u16)>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,6 +29,7 @@ impl FlatHashKey {
|
||||||
pub(crate) fn debug_name(&self) -> String {
|
pub(crate) fn debug_name(&self) -> String {
|
||||||
match self {
|
match self {
|
||||||
FlatHashKey::Record(fields) => debug_name_record(fields),
|
FlatHashKey::Record(fields) => debug_name_record(fields),
|
||||||
|
FlatHashKey::Tuple(arity) => debug_name_tuple(*arity),
|
||||||
FlatHashKey::TagUnion(tags) => debug_name_tag(tags),
|
FlatHashKey::TagUnion(tags) => debug_name_tag(tags),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -65,8 +67,14 @@ impl FlatHash {
|
||||||
|
|
||||||
Ok(Key(FlatHashKey::Record(field_names)))
|
Ok(Key(FlatHashKey::Record(field_names)))
|
||||||
}
|
}
|
||||||
FlatType::Tuple(_elems, _ext) => {
|
FlatType::Tuple(elems, ext) => {
|
||||||
todo!();
|
let (elems_iter, ext) = elems.sorted_iterator_and_ext(subs, ext);
|
||||||
|
|
||||||
|
check_derivable_ext_var(subs, ext, |ext| {
|
||||||
|
matches!(ext, Content::Structure(FlatType::EmptyTuple))
|
||||||
|
})?;
|
||||||
|
|
||||||
|
Ok(Key(FlatHashKey::Tuple(elems_iter.count() as _)))
|
||||||
}
|
}
|
||||||
FlatType::TagUnion(tags, ext) | FlatType::RecursiveTagUnion(_, tags, ext) => {
|
FlatType::TagUnion(tags, ext) | FlatType::RecursiveTagUnion(_, tags, ext) => {
|
||||||
// The recursion var doesn't matter, because the derived implementation will only
|
// The recursion var doesn't matter, because the derived implementation will only
|
||||||
|
|
|
@ -43,6 +43,10 @@ pub(crate) fn debug_name_record(fields: &[Lowercase]) -> String {
|
||||||
str
|
str
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn debug_name_tuple(arity: u32) -> String {
|
||||||
|
format!("(arity:{arity})")
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn debug_name_tag(tags: &[(TagName, u16)]) -> String {
|
pub(crate) fn debug_name_tag(tags: &[(TagName, u16)]) -> String {
|
||||||
let mut str = String::from('[');
|
let mut str = String::from('[');
|
||||||
tags.iter().enumerate().for_each(|(i, (tag, arity))| {
|
tags.iter().enumerate().for_each(|(i, (tag, arity))| {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue