Support newtypes and unit tag union hash deriving

This commit is contained in:
Ayaz Hafiz 2022-10-04 18:11:37 -05:00
parent 251b3865d9
commit 41c9985c53
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
2 changed files with 42 additions and 7 deletions

View file

@ -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 {
let (body_type, body) = match key {
FlatHashKey::Record(fields) => hash_record(env, def_symbol, fields),
FlatHashKey::TagUnion(tags) => {
if tags.len() > 1 {
hash_tag_union(env, def_symbol, tags)
} else {
todo!()
}
}
FlatHashKey::TagUnion(tags) => hash_tag_union(env, def_symbol, tags),
};
let specialization_lambda_sets =

View file

@ -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]
fn tag_two_labels() {
derive_test(Hash, v!([A v!(U8) v!(STR) v!(U16), B v!(STR)]), |golden| {