Skip to content

Commit 763cc19

Browse files
TeofilCLysxia
authored andcommitted
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 f6a35e5 commit 763cc19

File tree

6 files changed

+61
-11
lines changed

6 files changed

+61
-11
lines changed

src/Data/Text.hs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,16 @@ import GHC.Base (eqInt, neInt, gtInt, geInt, ltInt, leInt)
274274
import qualified GHC.Exts as Exts
275275
import GHC.Int (Int8)
276276
import GHC.Stack (HasCallStack)
277+
#if __GLASGOW_HASKELL__ >= 914
278+
import qualified Language.Haskell.TH.Lift as TH
279+
#else
277280
import qualified Language.Haskell.TH.Lib as TH
278281
import qualified Language.Haskell.TH.Syntax as TH
282+
#endif
279283
import Text.Printf (PrintfArg, formatArg, formatString)
280284
import System.Posix.Types (CSsize(..))
281285

282-
#if MIN_VERSION_template_haskell(2,16,0)
286+
#if __GLASGOW_HASKELL__ >= 810
283287
import Data.Text.Foreign (asForeignPtr)
284288
import System.IO.Unsafe (unsafePerformIO)
285289
#endif
@@ -451,7 +455,17 @@ instance Data Text where
451455

452456
-- | @since 1.2.4.0
453457
instance TH.Lift Text where
454-
#if MIN_VERSION_template_haskell(2,16,0)
458+
#if __GLASGOW_HASKELL__ >= 914
459+
lift txt = do
460+
let (ptr, len) = unsafePerformIO $ asForeignPtr txt
461+
case len of
462+
0 -> [| empty |]
463+
_ ->
464+
let
465+
bytesQ = TH.liftAddrCompat ptr 0 (P.fromIntegral len)
466+
lenQ = TH.liftIntCompat (P.fromIntegral len)
467+
in [| unpackCStringLen# $bytesQ $lenQ |]
468+
#elif __GLASGOW_HASKELL__ >= 810
455469
lift txt = do
456470
let (ptr, len) = unsafePerformIO $ asForeignPtr txt
457471
case len of
@@ -465,13 +479,15 @@ instance TH.Lift Text where
465479
#else
466480
lift = TH.appE (TH.varE 'pack) . TH.stringE . unpack
467481
#endif
468-
#if MIN_VERSION_template_haskell(2,17,0)
482+
#if __GLASGOW_HASKELL__ >= 914
483+
liftTyped = TH.defaultLiftTyped
484+
#elif __GLASGOW_HASKELL__ >= 900
469485
liftTyped = TH.unsafeCodeCoerce . TH.lift
470-
#elif MIN_VERSION_template_haskell(2,16,0)
486+
#elif __GLASGOW_HASKELL__ >= 810
471487
liftTyped = TH.unsafeTExpCoerce . TH.lift
472488
#endif
473489

474-
#if MIN_VERSION_template_haskell(2,16,0)
490+
#if __GLASGOW_HASKELL__ >= 810
475491
unpackCStringLen# :: Exts.Addr# -> Int -> Text
476492
unpackCStringLen# addr# l = Text ba 0 l
477493
where

src/Data/Text/Lazy.hs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,12 @@ import qualified GHC.CString as GHC
255255
import qualified GHC.Exts as Exts
256256
import GHC.Prim (Addr#)
257257
import GHC.Stack (HasCallStack)
258-
import qualified Language.Haskell.TH.Lib as TH
258+
#if __GLASGOW_HASKELL__ >= 914
259+
import qualified Language.Haskell.TH.Lift as TH
260+
#else
259261
import qualified Language.Haskell.TH.Syntax as TH
262+
import qualified Language.Haskell.TH.Lib as TH
263+
#endif
260264
import Text.Printf (PrintfArg, formatArg, formatString)
261265

262266
-- $fusion
@@ -393,10 +397,16 @@ instance Data Text where
393397

394398
-- | @since 1.2.4.0
395399
instance TH.Lift Text where
400+
#if __GLASGOW_HASKELL__ >= 900
401+
lift x = [| fromStrict $(TH.lift . toStrict $ x) |]
402+
#else
396403
lift = TH.appE (TH.varE 'fromStrict) . TH.lift . toStrict
397-
#if MIN_VERSION_template_haskell(2,17,0)
404+
#endif
405+
#if __GLASGOW_HASKELL__ >= 914
406+
liftTyped = TH.defaultLiftTyped
407+
#elif __GLASGOW_HASKELL__ >= 900
398408
liftTyped = TH.unsafeCodeCoerce . TH.lift
399-
#elif MIN_VERSION_template_haskell(2,16,0)
409+
#elif __GLASGOW_HASKELL__ >= 810
400410
liftTyped = TH.unsafeTExpCoerce . TH.lift
401411
#endif
402412

tests/Tests/Lift.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE CPP #-}
12
{-# LANGUAGE OverloadedStrings #-}
23
{-# LANGUAGE TemplateHaskell #-}
34
module Tests.Lift
@@ -7,7 +8,11 @@ module Tests.Lift
78

89
import qualified Data.Text as S
910
import qualified Data.Text.Lazy as L
11+
#if __GLASGOW_HASKELL__ >= 914
12+
import Language.Haskell.TH.Lift (lift)
13+
#else
1014
import Language.Haskell.TH.Syntax (lift)
15+
#endif
1116
import Test.Tasty.HUnit (testCase, assertEqual)
1217
import Test.Tasty (TestTree, testGroup)
1318

tests/Tests/RebindableSyntaxTest.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
{-# LANGUAGE RebindableSyntax, TemplateHaskell #-}
1+
{-# LANGUAGE CPP, RebindableSyntax, TemplateHaskell #-}
22

33
module Tests.RebindableSyntaxTest where
44

55
import qualified Data.Text as Text
6+
#if __GLASGOW_HASKELL__ >= 914
7+
import Language.Haskell.TH.Lift (lift)
8+
#else
69
import Language.Haskell.TH.Syntax (lift)
10+
#endif
711
import Test.Tasty.HUnit (testCase, assertEqual)
812
import Test.Tasty (TestTree, testGroup)
913
import Prelude (($))

tests/Tests/ShareEmpty.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE CPP #-}
12
{-# LANGUAGE MagicHash #-}
23
{-# LANGUAGE OverloadedStrings #-}
34
{-# LANGUAGE TemplateHaskell #-}
@@ -12,7 +13,11 @@ module Tests.ShareEmpty
1213

1314
import Control.Exception (evaluate)
1415
import Data.Text
16+
#if __GLASGOW_HASKELL__ >= 914
17+
import Language.Haskell.TH.Lift (lift)
18+
#else
1519
import Language.Haskell.TH.Syntax (lift)
20+
#endif
1621
import Test.Tasty.HUnit (testCase, assertFailure, assertEqual)
1722
import Test.Tasty (TestTree, testGroup)
1823
import GHC.Exts

text.cabal

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,14 @@ library
227227
bytestring >= 0.10.4 && < 0.13,
228228
deepseq >= 1.1 && < 1.6,
229229
ghc-prim >= 0.2 && < 0.15,
230-
template-haskell >= 2.5 && < 2.25
230+
231+
-- template-haskell-lift was added as a boot library in GHC-9.14
232+
-- once we no longer wish to backport releases to older major releases of GHC,
233+
-- this conditional can be dropped
234+
if impl(ghc < 9.14)
235+
build-depends: template-haskell
236+
else
237+
build-depends: template-haskell-lift >= 0.1 && <0.2
231238

232239
if impl(ghc < 9.4)
233240
build-depends: data-array-byte >= 0.1 && < 0.2
@@ -304,12 +311,15 @@ test-suite tests
304311
tasty,
305312
tasty-hunit,
306313
tasty-quickcheck,
307-
template-haskell,
308314
temporary,
309315
transformers,
310316
text
311317
if impl(ghc < 9.4)
312318
build-depends: data-array-byte >= 0.1 && < 0.2
319+
if impl(ghc < 9.14)
320+
build-depends: template-haskell
321+
else
322+
build-depends: template-haskell-lift >= 0.1 && <0.2
313323

314324
-- Plugin infrastructure does not work properly in 8.6.1, and
315325
-- ghc-9.2.1 library depends on parsec, which causes a circular dependency.

0 commit comments

Comments
 (0)