-
Notifications
You must be signed in to change notification settings - Fork 632
[CDEC-385] For types with deriveSimpleBi
instances, remove partial field accessors from sum types and convert to deriveIndexedBi
#3153
Conversation
Named fields in sum types result in accessors that are partial functions. I remove those from 3 types, and then switch their derived `Bi` instances to use `deriveIndexedBi`, which doesn't require named fields.
Class.TH We now error out when `deriveSimpleBi` encounteres named fields in a sumtype. Modifying `deriveSimpleBi` in this way causes some modules to break; this commit also modifies or eliminates those breakages. BiSerialize Because `deriveSimpleBi` no longer supports sum types, the comparison translation done previously gives us weaker guarantees (it only compares structs). In light of that, the most effective tests seem to be golden tests for checking that the old behavior of `deriveSimpleBi` is kept aligned with the future behavior of `deriveIndexedBi`.
@@ -368,6 +368,12 @@ deriveSimpleBiInternal predsMB headTy constrs = do | |||
when (length realFields /= length dcFields) $ templateHaskellError $ | |||
sformat ("Some field of "%shown | |||
%" constructor doesn't have an explicit name") cName | |||
when (length constrs > 1 && not (null realFields)) $ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's a sum type, and any of the constructors take values, it could have partial field accessors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool! Doing this was a good idea 👍
Field [| 0 :: TestIndexed |], | ||
Field [| 1 :: TestIndexed |] | ||
] | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the same as before, it was just moved, and git can't tell that.
tests :: IO Bool | ||
tests = | ||
H.checkParallel $$discover | ||
-} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could delete this block comment. I think it gives context for anyone wondering about the transition from deriveSimpleBi
to deriveIndexedBi` in the future. But it could also be left in git history.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given what we're testing here and how the tests were generated, I like the idea of leaving this in (it'd be especially helpful if I wanted to write another golden test for some reason).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
tests :: IO Bool | ||
tests = | ||
H.checkParallel $$discover | ||
-} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given what we're testing here and how the tests were generated, I like the idea of leaving this in (it'd be especially helpful if I wanted to write another golden test for some reason).
@@ -368,6 +368,12 @@ deriveSimpleBiInternal predsMB headTy constrs = do | |||
when (length realFields /= length dcFields) $ templateHaskellError $ | |||
sformat ("Some field of "%shown | |||
%" constructor doesn't have an explicit name") cName | |||
when (length constrs > 1 && not (null realFields)) $ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool! Doing this was a good idea 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just need a a mention of deriveIndexedBi
in that error message and a comment on its implementation.
When thats done, please feel free to dismiss this review and merge it.
Thanks!
binary/src/Pos/Binary/Class/TH.hs
Outdated
sformat ("`deriveSimpleBi` no longer supports sum types \ | ||
\with named fields.\n Constructor "%shown%" has \ | ||
\named fields: "%shown%".") | ||
cName (map fst realFields) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a mention of deriveIndexedBi
in that error message so people don't have to go looking?
As simple as "Use derivedIndexedBiinstead. Probably a good idea to add a comment explaining
deriveIndexedBi` as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's what the error message will look like
/Users/mhueschen/Projects/IOHK-work/cardano-sl/binary/test/Test/Pos/Binary/Cbor/CborSpec.hs:70:1: error:
`deriveSimpleBi` no longer supports sum types with named fields.
Constructor Test.Pos.Binary.Cbor.CborSpec.Login has named fields: [Test.Pos.Binary.Cbor.CborSpec.login,Test.Pos.Binary.Cbor.CborSpec.age].
Please use `deriveIndexedBi` instead.
-- Since `deriveSimpleBi` no longer works on sum types, we cannot do a simple | ||
-- comparison property between `deriveSimpleBi` and `deriveIndexedBi`. Instead, | ||
-- this module contains golden tests. The tests were generated by the following | ||
-- process (done before sumtypes were disallowed from `deriveSimpleBi`): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
- Spruce up example derivations for `deriveSimpleBi` and `deriveIndexedBi`. - Document `deriveIndexedBi` & the motivation behind it.
Description
For types with
deriveSimpleBi
instances, remove partial field accessors from sum types and convert toderiveIndexedBi
.I also modify
deriveSimpleBi
to error out on sum-types where constructors have more than 0 fields. This is to discourage partial field accessors.The module
Test.Pos.Binary.Helpers.GoldenRoundTrip
must be added tocardano-sl-binary
's test-suite, because we cannot importcardano-sl-binary-test
from the testsuite. So some dependencies are added.Linked issue
https://iohk.myjetbrains.com/youtrack/issue/CDEC-385
Type of change
Developer checklist