bytestring
bytestring copied to clipboard
Error calling 'toConstr' method for ByteString
I'm using gshow from syb to display a data structure that has Data instance but not Show instance (specifically, for GHC types).
However, it looks like some of the fields contain ByteString and I see the following runtime error:
(ghc: panic! (the 'impossible' happened)
(GHC version 9.2.2:
Data.ByteString.ByteString.toConstr
CallStack (from HasCallStack):
error, called at libraries/bytestring/Data/ByteString/Internal.hs:270:20 in bytestring-0.11.3.0:Data.ByteString.Internal
Please report this as a GHC bug: https://www.haskell.org/ghc/reportabug
Apparently, this error happens because toConstr method for ByteString defined using error:
https://github.com/haskell/bytestring/blob/acfe93480a15ecd373a5de5e423b1460749e52e1/Data/ByteString/Internal.hs#L283-L287
Would it be possible to change it to something that doesn't throw errors so I can use gshow on nested datastructures that can potentially contain ByteString?
I'm not really familiar with Data.
What constructor would you (or other users) expect toConstr to produce? The docs for toConstr say
For proper terms, this is meant to be the top-level constructor. Primitive datatypes are here viewed as potentially infinite sets of values (i.e., constructors).
That seems to suggest that we return the BS constructor.
However other instances where the constructors are internal typically return something like a fromList or pack method. The Data Text instance has some documentation on this: https://hackage.haskell.org/package/text-2.0/docs/src/Data.Text.html#line-369
So maybe we should use pack :: [Word8] -> ByteString here?!
(The instances for LazyByteString and ShortByteString will need updating too)
@sjakobi I don't have a preference here and I'm happy with everything that doesn't throw an error 🙂
I think we should implement the same instance as in instance Data Text. A PR would be welcome.
I created a PR in attempt to resolve this issue #519