Meanwhile, a more disciplined (than directly working with Addr# literals) construct for compile-time ByteString literals can be something like the below (for demo purposes stashed away in .../Data/ByteString/Literal.hs):
module Data.ByteString.Literal (byteStringLiteral) where
import qualified Data.ByteString.Char8 as B
import qualified Language.Haskell.TH.Syntax as TH
byteStringLiteral :: (MonadFail m, TH.Quote m) => String -> TH.Code m B.ByteString
byteStringLiteral s = TH.liftCode $ fmap TH.TExp $
if all (<= '\xff') s
then TH.lift (B.pack s)
else fail $ "Input code points exceed 0xFF in: " ++ show s
which then makes possible:
$ cat test.hs
module Foo where
import Data.ByteString.Literal
$ ghci -v0 -XTemplateHaskell -ddump-splices -i... test.hs
λ> import qualified Data.ByteString as B
λ>
λ> bs = $$(byteStringLiteral "foo\0bar\xA0\&baz")
<interactive>:3:8-46: Splicing expression
byteStringLiteral "foo\0bar\xA0\&baz"
======>
bytestring-0.12.2.0-inplace:Data.ByteString.Internal.Type.unsafePackLenLiteral
11 "foo\NULbar\\160baz"#
λ>
λ> B.unpack bs
[102,111,111,0,98,97,114,160,98,97,122]
λ>
λ> bs = $$(byteStringLiteral "foo\0βαρ")
<interactive>:5:8: error: [GHC-39584]
• Input code points exceed 0xFF in: "foo\NUL\946\945\961"
• In the Template Haskell splice $$(byteStringLiteral "foo\0βαρ")
In the expression: $$(byteStringLiteral "foo\0βαρ")
In an equation for ‘bs’: bs = $$(byteStringLiteral "foo\0βαρ")