From 5340a71c58fd5172be29ba7cb44a008307766e81 Mon Sep 17 00:00:00 2001 From: Teo Camarasu Date: Sat, 10 Jan 2026 21:13:40 +0000 Subject: [PATCH] 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 --- src/Data/Text.hs | 26 +++++++++++++++++++++----- src/Data/Text/Lazy.hs | 16 +++++++++++++--- tests/Tests/Lift.hs | 5 +++++ tests/Tests/RebindableSyntaxTest.hs | 6 +++++- tests/Tests/ShareEmpty.hs | 5 +++++ text.cabal | 14 ++++++++++++-- 6 files changed, 61 insertions(+), 11 deletions(-) diff --git a/src/Data/Text.hs b/src/Data/Text.hs index 361b6dda5..efd302b2b 100644 --- a/src/Data/Text.hs +++ b/src/Data/Text.hs @@ -274,12 +274,16 @@ import GHC.Base (eqInt, neInt, gtInt, geInt, ltInt, leInt) import qualified GHC.Exts as Exts import GHC.Int (Int8) import GHC.Stack (HasCallStack) +#if __GLASGOW_HASKELL__ >= 914 +import qualified Language.Haskell.TH.Lift as TH +#else import qualified Language.Haskell.TH.Lib as TH import qualified Language.Haskell.TH.Syntax as TH +#endif import Text.Printf (PrintfArg, formatArg, formatString) import System.Posix.Types (CSsize(..)) -#if MIN_VERSION_template_haskell(2,16,0) +#if __GLASGOW_HASKELL__ >= 810 import Data.Text.Foreign (asForeignPtr) import System.IO.Unsafe (unsafePerformIO) #endif @@ -451,7 +455,17 @@ instance Data Text where -- | @since 1.2.4.0 instance TH.Lift Text where -#if MIN_VERSION_template_haskell(2,16,0) +#if __GLASGOW_HASKELL__ >= 914 + lift txt = do + let (ptr, len) = unsafePerformIO $ asForeignPtr txt + case len of + 0 -> [| empty |] + _ -> + let + bytesQ = TH.liftAddrCompat ptr 0 (P.fromIntegral len) + lenQ = TH.liftIntCompat (P.fromIntegral len) + in [| unpackCStringLen# $bytesQ $lenQ |] +#elif __GLASGOW_HASKELL__ >= 810 lift txt = do let (ptr, len) = unsafePerformIO $ asForeignPtr txt case len of @@ -465,13 +479,15 @@ instance TH.Lift Text where #else lift = TH.appE (TH.varE 'pack) . TH.stringE . unpack #endif -#if MIN_VERSION_template_haskell(2,17,0) +#if __GLASGOW_HASKELL__ >= 914 + liftTyped = TH.defaultLiftTyped +#elif __GLASGOW_HASKELL__ >= 900 liftTyped = TH.unsafeCodeCoerce . TH.lift -#elif MIN_VERSION_template_haskell(2,16,0) +#elif __GLASGOW_HASKELL__ >= 810 liftTyped = TH.unsafeTExpCoerce . TH.lift #endif -#if MIN_VERSION_template_haskell(2,16,0) +#if __GLASGOW_HASKELL__ >= 810 unpackCStringLen# :: Exts.Addr# -> Int -> Text unpackCStringLen# addr# l = Text ba 0 l where diff --git a/src/Data/Text/Lazy.hs b/src/Data/Text/Lazy.hs index 2a3591576..ff9859c65 100644 --- a/src/Data/Text/Lazy.hs +++ b/src/Data/Text/Lazy.hs @@ -255,8 +255,12 @@ import qualified GHC.CString as GHC import qualified GHC.Exts as Exts import GHC.Prim (Addr#) import GHC.Stack (HasCallStack) -import qualified Language.Haskell.TH.Lib as TH +#if __GLASGOW_HASKELL__ >= 914 +import qualified Language.Haskell.TH.Lift as TH +#else import qualified Language.Haskell.TH.Syntax as TH +import qualified Language.Haskell.TH.Lib as TH +#endif import Text.Printf (PrintfArg, formatArg, formatString) -- $fusion @@ -393,10 +397,16 @@ instance Data Text where -- | @since 1.2.4.0 instance TH.Lift Text where +#if __GLASGOW_HASKELL__ >= 900 + lift x = [| fromStrict $(TH.lift . toStrict $ x) |] +#else lift = TH.appE (TH.varE 'fromStrict) . TH.lift . toStrict -#if MIN_VERSION_template_haskell(2,17,0) +#endif +#if __GLASGOW_HASKELL__ >= 914 + liftTyped = TH.defaultLiftTyped +#elif __GLASGOW_HASKELL__ >= 900 liftTyped = TH.unsafeCodeCoerce . TH.lift -#elif MIN_VERSION_template_haskell(2,16,0) +#elif __GLASGOW_HASKELL__ >= 810 liftTyped = TH.unsafeTExpCoerce . TH.lift #endif diff --git a/tests/Tests/Lift.hs b/tests/Tests/Lift.hs index 676f23def..09c671e08 100644 --- a/tests/Tests/Lift.hs +++ b/tests/Tests/Lift.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TemplateHaskell #-} module Tests.Lift @@ -7,7 +8,11 @@ module Tests.Lift import qualified Data.Text as S import qualified Data.Text.Lazy as L +#if __GLASGOW_HASKELL__ >= 914 +import Language.Haskell.TH.Lift (lift) +#else import Language.Haskell.TH.Syntax (lift) +#endif import Test.Tasty.HUnit (testCase, assertEqual) import Test.Tasty (TestTree, testGroup) diff --git a/tests/Tests/RebindableSyntaxTest.hs b/tests/Tests/RebindableSyntaxTest.hs index ffe90b6a6..f4b6464c4 100644 --- a/tests/Tests/RebindableSyntaxTest.hs +++ b/tests/Tests/RebindableSyntaxTest.hs @@ -1,9 +1,13 @@ -{-# LANGUAGE RebindableSyntax, TemplateHaskell #-} +{-# LANGUAGE CPP, RebindableSyntax, TemplateHaskell #-} module Tests.RebindableSyntaxTest where import qualified Data.Text as Text +#if __GLASGOW_HASKELL__ >= 914 +import Language.Haskell.TH.Lift (lift) +#else import Language.Haskell.TH.Syntax (lift) +#endif import Test.Tasty.HUnit (testCase, assertEqual) import Test.Tasty (TestTree, testGroup) import Prelude (($)) diff --git a/tests/Tests/ShareEmpty.hs b/tests/Tests/ShareEmpty.hs index bfbf7cdb2..81bf9a90a 100644 --- a/tests/Tests/ShareEmpty.hs +++ b/tests/Tests/ShareEmpty.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} {-# LANGUAGE MagicHash #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TemplateHaskell #-} @@ -12,7 +13,11 @@ module Tests.ShareEmpty import Control.Exception (evaluate) import Data.Text +#if __GLASGOW_HASKELL__ >= 914 +import Language.Haskell.TH.Lift (lift) +#else import Language.Haskell.TH.Syntax (lift) +#endif import Test.Tasty.HUnit (testCase, assertFailure, assertEqual) import Test.Tasty (TestTree, testGroup) import GHC.Exts diff --git a/text.cabal b/text.cabal index d9953277c..8928d6852 100644 --- a/text.cabal +++ b/text.cabal @@ -227,7 +227,14 @@ library bytestring >= 0.10.4 && < 0.13, deepseq >= 1.1 && < 1.6, ghc-prim >= 0.2 && < 0.15, - template-haskell >= 2.5 && < 2.25 + + -- template-haskell-lift was added as a boot library in GHC-9.14 + -- once we no longer wish to backport releases to older major releases of GHC, + -- this conditional can be dropped + if impl(ghc < 9.14) + build-depends: template-haskell >= 2.5 && < 3 + else + build-depends: template-haskell-lift >= 0.1 && <0.2 if impl(ghc < 9.4) build-depends: data-array-byte >= 0.1 && < 0.2 @@ -304,12 +311,15 @@ test-suite tests tasty, tasty-hunit, tasty-quickcheck, - template-haskell, temporary, transformers, text if impl(ghc < 9.4) build-depends: data-array-byte >= 0.1 && < 0.2 + if impl(ghc < 9.14) + build-depends: template-haskell + else + build-depends: template-haskell-lift -- Plugin infrastructure does not work properly in 8.6.1, and -- ghc-9.2.1 library depends on parsec, which causes a circular dependency.