Skip to content

Commit d8c2d02

Browse files
committed
fixup! fixup! fixup! Implemented TH splices for validated ByteString literals
tweak TH type signatures
1 parent d4d62f0 commit d8c2d02

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

Data/ByteString/Internal/Type.hs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
{-# OPTIONS_HADDOCK not-home #-}
55

6+
{-# LANGUAGE ConstraintKinds #-}
67
{-# LANGUAGE TemplateHaskellQuotes #-}
78
{-# LANGUAGE TypeFamilies #-}
89
{-# LANGUAGE UnliftedFFITypes #-}
@@ -199,6 +200,14 @@ import GHC.ForeignPtr (unsafeWithForeignPtr)
199200

200201
import qualified Language.Haskell.TH.Lib as TH
201202
import qualified Language.Haskell.TH.Syntax as TH
203+
import Language.Haskell.TH.Syntax (Lift, TExp)
204+
#if __GLASGOW_HASKELL__ >= 900
205+
import Language.Haskell.TH.Syntax (Code, Quote)
206+
#endif
207+
208+
#if !MIN_VERSION_base(4,13,0)
209+
import Control.Monad.Fail (MonadFail)
210+
#endif
202211

203212
#if !HS_unsafeWithForeignPtr_AVAILABLE
204213
unsafeWithForeignPtr :: ForeignPtr a -> (Ptr a -> IO b) -> IO b
@@ -361,7 +370,7 @@ byteStringDataType :: DataType
361370
byteStringDataType = mkDataType "Data.ByteString.ByteString" [packConstr]
362371

363372
-- | @since 0.11.2.0
364-
instance TH.Lift ByteString where
373+
instance Lift ByteString where
365374
#if MIN_VERSION_template_haskell(2,16,0)
366375
-- template-haskell-2.16 first ships with ghc-8.10
367376
lift (BS ptr len) = [| unsafePackLenLiteral |]
@@ -532,21 +541,21 @@ packUptoLenChars len cs0 =
532541
go !p (c:cs) = pokeFp p (c2w c) >> go (p `plusForeignPtr` 1) cs
533542
in go p0 cs0
534543

535-
#if MIN_VERSION_template_haskell(2,17,0)
536-
type THLift a = forall m. (MonadFail m, TH.Quote m) => TH.Code m a
544+
#if __GLASGOW_HASKELL__ < 900
545+
type Quote m = (TH.Q ~ m)
546+
type Code m a = m (TExp a)
547+
#endif
537548

538-
liftTyped :: forall a m. (MonadFail m, TH.Quote m, TH.Lift a) => a -> TH.Code m a
549+
liftTyped :: forall a m. (MonadFail m, Quote m, Lift a) => a -> Code m a
550+
#if MIN_VERSION_template_haskell(2,17,0)
539551
liftTyped = TH.liftTyped
540552

541-
liftCode :: forall a m. (MonadFail m, TH.Quote m) => m (TH.TExp a) -> TH.Code m a
553+
liftCode :: forall a m. (MonadFail m, Quote m) => m (TExp a) -> Code m a
542554
liftCode = TH.liftCode
543555
#else
544-
type THLift a = TH.Q (TH.TExp a)
545-
546-
liftTyped :: forall a. TH.Lift a => a -> TH.Q (TH.TExp a)
547556
liftTyped = TH.unsafeTExpCoerce . TH.lift
548557

549-
liftCode :: forall a. TH.Q TH.Exp -> TH.Q (TH.TExp a)
558+
liftCode :: forall a m. (MonadFail m, Quote m) => m TH.Exp -> Code m a
550559
liftCode = TH.unsafeTExpCoerce
551560
#endif
552561

@@ -573,7 +582,7 @@ data H2W = Hex {-# UNPACK #-} !Int [Word8]
573582
-- > ehloCmd :: ByteString
574583
-- > ehloCmd = $$(literalFromChar8 "EHLO")
575584
--
576-
literalFromChar8 :: String -> THLift ByteString
585+
literalFromChar8 :: (MonadFail m, Quote m) => String -> Code m ByteString
577586
literalFromChar8 "" = [||empty||]
578587
literalFromChar8 s = case foldr' op (Octets 0 []) s of
579588
Octets n ws -> liftTyped (unsafePackLenBytes n ws)
@@ -597,7 +606,7 @@ literalFromChar8 s = case foldr' op (Octets 0 []) s of
597606
-- > ehloCmd :: ByteString
598607
-- > ehloCmd = $$(literalFromHex "45484c4F")
599608
--
600-
literalFromHex :: String -> THLift ByteString
609+
literalFromHex :: (MonadFail m, Quote m) => String -> Code m ByteString
601610
literalFromHex "" = [||empty||]
602611
literalFromHex s =
603612
case foldr' op (Hex 0 []) s of

0 commit comments

Comments
 (0)