Skip to content

Commit ee19745

Browse files
committed
Use template-haskell-lift for GHC>=9.14
This new boot library should be more stable than template-haskell and should eventually allow us to remove much of the CPP around TH. It will also make it easier for end-users to reinstall template-haskell as it will no longer be used by any boot libraries
1 parent da6f41a commit ee19745

5 files changed

Lines changed: 57 additions & 22 deletions

File tree

Data/ByteString/Internal/Type.hs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,17 @@ import GHC.Int (Int (..))
199199
import GHC.ForeignPtr (unsafeWithForeignPtr)
200200
#endif
201201

202-
import qualified Language.Haskell.TH.Lib as TH
202+
#if __GLASGOW_HASKELL__ >= 914
203+
import qualified Language.Haskell.TH.Lift as TH
204+
import Language.Haskell.TH.Lift (Code, Quote)
205+
#else
203206
import qualified Language.Haskell.TH.Syntax as TH
204-
import Language.Haskell.TH.Syntax (Lift, TExp)
207+
import qualified Language.Haskell.TH.Lib as TH
208+
import Language.Haskell.TH.Syntax (TExp)
205209
#if __GLASGOW_HASKELL__ >= 900
206210
import Language.Haskell.TH.Syntax (Code, Quote)
207211
#endif
212+
#endif
208213

209214
#if !MIN_VERSION_base(4,13,0)
210215
import Control.Monad.Fail (MonadFail)
@@ -371,8 +376,14 @@ byteStringDataType :: DataType
371376
byteStringDataType = mkDataType "Data.ByteString.ByteString" [packConstr]
372377

373378
-- | @since 0.11.2.0
374-
instance Lift ByteString where
375-
#if MIN_VERSION_template_haskell(2,16,0)
379+
instance TH.Lift ByteString where
380+
#if __GLASGOW_HASKELL__ >= 914
381+
lift (BS ptr len) =
382+
[| unsafePackLenLiteral
383+
$(TH.lift len)
384+
$(TH.liftAddrCompat ptr 0 (fromIntegral len))
385+
|]
386+
#elif __GLASGOW_HASKELL__ >= 810
376387
-- template-haskell-2.16 first ships with ghc-8.10
377388
lift (BS ptr len) = [| unsafePackLenLiteral |]
378389
`TH.appE` TH.litE (TH.integerL (fromIntegral len))
@@ -383,10 +394,13 @@ instance Lift ByteString where
383394
`TH.appE` TH.litE (TH.StringPrimL $ unpackBytes bs)
384395
#endif
385396

386-
#if MIN_VERSION_template_haskell(2,17,0)
397+
#if __GLASGOW_HASKELL__ >= 914
398+
-- template-haskell-lift-0.1 first ships with ghc-9.16
399+
liftTyped = TH.defaultLiftTyped
400+
#elif __GLASGOW_HASKELL__ >= 900
387401
-- template-haskell-2.17 first ships with ghc-9.0
388402
liftTyped = TH.unsafeCodeCoerce . TH.lift
389-
#elif MIN_VERSION_template_haskell(2,16,0)
403+
#elif __GLASGOW_HASKELL__ >= 810
390404
-- template-haskell-2.16 first ships with ghc-8.10
391405
liftTyped = TH.unsafeTExpCoerce . TH.lift
392406
#endif
@@ -559,17 +573,13 @@ type Quote m = (TH.Q ~ m)
559573
type Code m a = m (TExp a)
560574
#endif
561575

562-
liftTyped :: forall a m. (MonadFail m, Quote m, Lift a) => a -> Code m a
563-
#if MIN_VERSION_template_haskell(2,17,0)
576+
liftTyped :: forall a m. (MonadFail m, Quote m, TH.Lift a) => a -> Code m a
577+
#if __GLASGOW_HASKELL__ >= 914
578+
liftTyped = TH.defaultLiftTyped
579+
#elif __GLASGOW_HASKELL__ >= 900
564580
liftTyped = TH.liftTyped
565-
566-
liftCode :: forall a m. (MonadFail m, Quote m) => m (TExp a) -> Code m a
567-
liftCode = TH.liftCode
568581
#else
569582
liftTyped = TH.unsafeTExpCoerce . TH.lift
570-
571-
liftCode :: forall a m. (MonadFail m, Quote m) => m TH.Exp -> Code m a
572-
liftCode = TH.unsafeTExpCoerce
573583
#endif
574584

575585
data S2W = Octets {-# UNPACK #-} !Int [Word8]
@@ -599,7 +609,7 @@ literalFromOctetString :: (MonadFail m, Quote m) => String -> Code m ByteString
599609
literalFromOctetString "" = [||empty||]
600610
literalFromOctetString s = case foldr' op (Octets 0 []) s of
601611
Octets n ws -> liftTyped (unsafePackLenBytes n ws)
602-
Hichar i w -> liftCode $ fail $ "non-octet character '\\" ++
612+
Hichar i w -> error $ "non-octet character '\\" ++
603613
show w ++ "' at offset: " ++ show i
604614
where
605615
op :: Char -> S2W -> S2W
@@ -624,8 +634,8 @@ literalFromHex "" = [||empty||]
624634
literalFromHex s =
625635
case foldr' op (Hex 0 []) s of
626636
Hex n ws -> liftTyped (unsafePackLenBytes n ws)
627-
Odd i _ _ -> liftCode $ fail $ "Odd input length: " ++ show (1 + 2 * i)
628-
Bad i w -> liftCode $ fail $ "Non-hexadecimal character '\\" ++
637+
Odd i _ _ -> error $ "Odd input length: " ++ show (1 + 2 * i)
638+
Bad i w -> error $ "Non-hexadecimal character '\\" ++
629639
show w ++ "' at offset: " ++ show i
630640
where
631641
-- Convert char to decimal digit value if result in [0, 9].

Data/ByteString/Lazy/Internal.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ import Data.Data (Data(..), mkConstr ,mkDataType, Constr, DataTyp
6363

6464
import GHC.Exts (IsList(..))
6565

66+
#if __GLASGOW_HASKELL__ >= 914
67+
import qualified Language.Haskell.TH.Lift as TH
68+
#else
6669
import qualified Language.Haskell.TH.Syntax as TH
70+
#endif
6771

6872
#ifdef HS_BYTESTRING_ASSERTIONS
6973
import Control.Exception (assert)

Data/ByteString/Short/Internal.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,12 @@ import qualified Data.ByteString.Lazy.Internal as LBS
248248

249249
import qualified Data.List as List
250250
import qualified GHC.Exts
251+
#if __GLASGOW_HASKELL__ >= 914
252+
import qualified Language.Haskell.TH.Lift as TH
253+
#else
251254
import qualified Language.Haskell.TH.Syntax as TH
255+
#endif
256+
252257

253258
-- | A compact representation of a 'Word8' vector.
254259
--

bytestring.cabal

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,19 @@ common language
109109

110110
library
111111
import: language
112-
build-depends: base >= 4.12 && < 5, ghc-prim, deepseq, template-haskell
112+
build-depends: base >= 4.12 && < 5, ghc-prim, deepseq
113113

114114
if impl(ghc < 9.4)
115115
build-depends: data-array-byte >= 0.1 && < 0.2
116116

117+
-- template-haskell-lift was added as a boot library in GHC-9.14
118+
-- once we no longer wish to backport releases to older major releases of GHC,
119+
-- this conditional can be dropped
120+
if impl(ghc < 9.14)
121+
build-depends: template-haskell
122+
else
123+
build-depends: template-haskell-lift >= 0.1 && <0.2
124+
117125
exposed-modules: Data.ByteString
118126
Data.ByteString.Char8
119127
Data.ByteString.Unsafe
@@ -224,7 +232,6 @@ test-suite bytestring-tests
224232
QuickCheck,
225233
tasty,
226234
tasty-quickcheck >= 0.8.1,
227-
template-haskell,
228235
transformers >= 0.3,
229236
syb
230237

@@ -248,6 +255,11 @@ test-suite bytestring-tests
248255
if os(openbsd)
249256
build-depends: splitmix < 0.1.3 || > 0.1.3.1
250257

258+
if impl(ghc < 9.14)
259+
build-depends: template-haskell
260+
else
261+
build-depends: template-haskell-lift
262+
251263
benchmark bytestring-bench
252264
import: language
253265
main-is: BenchAll.hs

tests/Lift.hs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import Test.Tasty.QuickCheck (testProperty, (===))
1010
import qualified Data.ByteString as BS
1111
import qualified Data.ByteString.Lazy as LBS
1212
import qualified Data.ByteString.Short as SBS
13+
#if __GLASGOW_HASKELL__ >= 914
14+
import qualified Language.Haskell.TH.Lift as TH
15+
#else
1316
import qualified Language.Haskell.TH.Syntax as TH
17+
#endif
1418

1519
testSuite :: TestTree
1620
#ifdef wasm32_HOST_ARCH
@@ -26,7 +30,7 @@ testSuite = testGroup "Lift"
2630
let bs = "\0\1\2\3\0\1\2\3" :: BS.ByteString in
2731
bs === $(TH.lift $ BS.pack [0,1,2,3,0,1,2,3])
2832

29-
#if MIN_VERSION_template_haskell(2,16,0)
33+
#if __GLASGOW_HASKELL__ >= 810
3034
, testProperty "typed" $
3135
let bs = "\0\1\2\3\0\1\2\3" :: BS.ByteString in
3236
bs === $$(TH.liftTyped $ BS.pack [0,1,2,3,0,1,2,3])
@@ -50,7 +54,7 @@ testSuite = testGroup "Lift"
5054
let bs = "\0\1\2\3\0\1\2\3" :: LBS.ByteString in
5155
bs === $(TH.lift $ LBS.pack [0,1,2,3,0,1,2,3])
5256

53-
#if MIN_VERSION_template_haskell(2,16,0)
57+
#if __GLASGOW_HASKELL__ >= 810
5458
, testProperty "typed" $
5559
let bs = "\0\1\2\3\0\1\2\3" :: LBS.ByteString in
5660
bs === $$(TH.liftTyped $ LBS.pack [0,1,2,3,0,1,2,3])
@@ -66,7 +70,7 @@ testSuite = testGroup "Lift"
6670
let bs = "\0\1\2\3\0\1\2\3" :: SBS.ShortByteString in
6771
bs === $(TH.lift $ SBS.pack [0,1,2,3,0,1,2,3])
6872

69-
#if MIN_VERSION_template_haskell(2,16,0)
73+
#if __GLASGOW_HASKELL__ >= 810
7074
, testProperty "typed" $
7175
let bs = "\0\1\2\3\0\1\2\3" :: SBS.ShortByteString in
7276
bs === $$(TH.liftTyped $ SBS.pack [0,1,2,3,0,1,2,3])

0 commit comments

Comments
 (0)