mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-03 16:44:33 +00:00
Support newtypes and unit tag union hash deriving
This commit is contained in:
parent
251b3865d9
commit
41c9985c53
2 changed files with 42 additions and 7 deletions
|
@ -27,13 +27,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::TagUnion(tags) => {
|
FlatHashKey::TagUnion(tags) => hash_tag_union(env, def_symbol, tags),
|
||||||
if tags.len() > 1 {
|
|
||||||
hash_tag_union(env, def_symbol, tags)
|
|
||||||
} else {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let specialization_lambda_sets =
|
let specialization_lambda_sets =
|
||||||
|
|
|
@ -201,6 +201,47 @@ fn two_field_record() {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tag_one_label_no_payloads() {
|
||||||
|
derive_test(Hash, v!([A]), |golden| {
|
||||||
|
assert_snapshot!(golden, @r###"
|
||||||
|
# derived for [A]
|
||||||
|
# a, [A] -[[hash_[A 0](0)]]-> a | a has Hasher
|
||||||
|
# a, [A] -[[hash_[A 0](0)]]-> a | a has Hasher
|
||||||
|
# Specialization lambda sets:
|
||||||
|
# @<1>: [[hash_[A 0](0)]]
|
||||||
|
#Derived.hash_[A 0] =
|
||||||
|
\#Derived.hasher, #Derived.union ->
|
||||||
|
#Derived.discrHasher =
|
||||||
|
Hash.hash #Derived.hasher (@tag_discriminant #Derived.union)
|
||||||
|
when #Derived.union is
|
||||||
|
A -> #Derived.discrHasher
|
||||||
|
"###
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tag_one_label_newtype() {
|
||||||
|
derive_test(Hash, v!([A v!(U8) v!(STR)]), |golden| {
|
||||||
|
assert_snapshot!(golden, @r###"
|
||||||
|
# derived for [A U8 Str]
|
||||||
|
# a, [A a1 a2] -[[hash_[A 2](0)]]-> a | a has Hasher, a1 has Hash, a2 has Hash
|
||||||
|
# a, [A a1 a2] -[[hash_[A 2](0)]]-> a | a has Hasher, a1 has Hash, a2 has Hash
|
||||||
|
# Specialization lambda sets:
|
||||||
|
# @<1>: [[hash_[A 2](0)]]
|
||||||
|
#Derived.hash_[A 2] =
|
||||||
|
\#Derived.hasher, #Derived.union ->
|
||||||
|
#Derived.discrHasher =
|
||||||
|
Hash.hash #Derived.hasher (@tag_discriminant #Derived.union)
|
||||||
|
when #Derived.union is
|
||||||
|
A #Derived.4 #Derived.5 ->
|
||||||
|
Hash.hash (Hash.hash #Derived.discrHasher #Derived.4) #Derived.5
|
||||||
|
"###
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn tag_two_labels() {
|
fn tag_two_labels() {
|
||||||
derive_test(Hash, v!([A v!(U8) v!(STR) v!(U16), B v!(STR)]), |golden| {
|
derive_test(Hash, v!([A v!(U8) v!(STR) v!(U16), B v!(STR)]), |golden| {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue