Optimize derived hash implementation for newtypes

When we have a newtype tag union, there is no reason to hash its
discriminant.
This commit is contained in:
Ayaz Hafiz 2022-10-05 12:19:35 -05:00
parent 2517695ce4
commit a308ebb38c
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
3 changed files with 109 additions and 26 deletions

View file

@ -206,16 +206,11 @@ 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
# hasher, [A] -[[hash_[A 0](0)]]-> hasher | hasher has Hasher
# hasher, [A] -[[hash_[A 0](0)]]-> hasher | hasher 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
#Derived.hash_[A 0] = \#Derived.hasher, A -> #Derived.hasher
"###
)
})
@ -226,17 +221,13 @@ 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
# hasher, [A a a1] -[[hash_[A 2](0)]]-> hasher | a has Hash, a1 has Hash, hasher has Hasher
# hasher, [A a a1] -[[hash_[A 2](0)]]-> hasher | a has Hash, a1 has Hash, hasher has Hasher
# 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
\#Derived.hasher, A #Derived.2 #Derived.3 ->
Hash.hash (Hash.hash #Derived.hasher #Derived.2) #Derived.3
"###
)
})