1212{-# LANGUAGE ViewPatterns #-}
1313{-# LANGUAGE TypeOperators #-}
1414
15+ #if __GLASGOW_HASKELL__ <914
16+ #define data pattern
17+ #else
18+ {-# LANGUAGE ExplicitNamespaces #-}
19+ #endif
20+
1521-- | Thin compatibility layer around GHC
1622--
1723-- This should be the only module with GHC-specific CPP directives, and the
@@ -26,7 +32,7 @@ module Data.Record.Internal.GHC.Shim (
2632 , mkFunBind
2733 , HsModule
2834 , LHsModule
29- , pattern GHC. HsModule
35+ , data GHC. HsModule
3036
3137 -- * Annotations
3238#if __GLASGOW_HASKELL__ < 902
@@ -77,7 +83,14 @@ module Data.Record.Internal.GHC.Shim (
7783
7884 -- * Compat
7985#if __GLASGOW_HASKELL__ >= 910
80- , pattern LambdaExpr
86+ , data LambdaExpr
87+ #endif
88+ , wrapBangTy
89+ , Singleton (.. )
90+
91+ #if __GLASGOW_HASKELL__ >= 914
92+ , getBangType
93+ , getBangStrictness
8194#endif
8295
8396 -- * Re-exports
@@ -107,6 +120,8 @@ module Data.Record.Internal.GHC.Shim (
107120#endif
108121 ) where
109122
123+ #undef data
124+
110125import Control.Monad
111126import Data.List.NonEmpty (NonEmpty (.. ))
112127import Data.Generics (Data , GenericQ , cast , toConstr , gzipWithQ )
@@ -144,7 +159,7 @@ import GHC.Driver.Main (getHscEnv)
144159import GHC.Tc.Types.Evidence (HsWrapper (WpHole ))
145160import GHC.Utils.Error (Severity (SevError , SevWarning ))
146161
147- import GHC.Plugins hiding ((<>) , getHscEnv , unLoc
162+ import GHC.Plugins hiding ((<>) , getHscEnv , unLoc , singleton
148163#if __GLASGOW_HASKELL__ >= 902
149164 , AnnType , AnnLet , AnnRec , AnnLam , AnnCase
150165 , Exception
@@ -298,6 +313,9 @@ importDecl qualified name = noLocA $ ImportDecl {
298313#endif
299314#if __GLASGOW_HASKELL__ >= 906
300315 , ideclImportList = Nothing
316+ #endif
317+ #if __GLASGOW_HASKELL__ >= 914
318+ , ideclLevelSpec = NotLevelled
301319#endif
302320 }
303321
@@ -483,8 +501,10 @@ hsFunTy = HsFunTy
483501hsFunTy ext = HsFunTy ext (HsUnrestrictedArrow NormalSyntax )
484502#elif __GLASGOW_HASKELL__ < 910
485503hsFunTy ext = HsFunTy ext (HsUnrestrictedArrow (L NoTokenLoc HsNormalTok ))
486- #else
504+ #elif __GLASGOW_HASKELL__ < 914
487505hsFunTy ext = HsFunTy ext (HsUnrestrictedArrow NoEpUniTok )
506+ #else
507+ hsFunTy ext = HsFunTy ext (HsUnannotated (EpArrow defExt))
488508#endif
489509
490510userTyVar :: LIdP GhcPs -> HsTyVarBndr GhcPs
@@ -726,7 +746,7 @@ simpleRecordUpdates =
726746 isSingleLabel (FieldLabelStrings labels) =
727747 case labels of
728748#if __GLASGOW_HASKELL__ >= 906
729- [ L _ (DotFieldOcc _ (L l (FieldLabelString label)))] ->
749+ (matchSingleton -> Just ( L _ (DotFieldOcc _ (L l (FieldLabelString label))))) ->
730750#else
731751 [L _ (DotFieldOcc _ (L l label))] ->
732752#endif
@@ -856,10 +876,54 @@ issueWarning l errMsg = do
856876 bag :: a -> Bag a
857877 bag = listToBag . (: [] )
858878
879+
880+ {- ------------------------------------------------------------------------------
881+ Strictness
882+ -------------------------------------------------------------------------------}
883+
884+ #if __GLASGOW_HASKELL__ >= 914
885+ getBangType :: LHsType GhcPs -> LHsType GhcPs
886+ getBangType (L _ (XHsType (HsBangTy _ _ lty))) = lty
887+ -- getBangType (L _ (HsDocTy x (L _ (HsBangTy _ _ lty)) lds)) =
888+ -- addCLocA lty lds (HsDocTy x lty lds)
889+ getBangType lty = lty
890+
891+ getBangStrictness :: LHsType GhcPs -> HsSrcBang
892+ getBangStrictness (L _ (XHsType (HsBangTy _ s _ty))) = s
893+ -- getBangStrictness (L _ (HsDocTy _ (L _ (HsBangTy (_, s) b _)) _)) = HsSrcBang s b
894+ getBangStrictness _ = HsSrcBang NoSourceText NoSrcUnpack NoSrcStrict
895+ #endif
896+
897+ {- ------------------------------------------------------------------------------
898+ Singleton
899+ -------------------------------------------------------------------------------}
900+
901+ class Singleton f where
902+ singleton :: a -> f a
903+ matchSingleton :: f a -> Maybe a
904+
905+ instance Singleton [] where
906+ singleton x = [x]
907+ matchSingleton [x] = Just x
908+ matchSingleton _ = Nothing
909+
910+ instance Singleton NonEmpty where
911+ singleton x = x :| []
912+ matchSingleton (x :| [] ) = Just x
913+ matchSingleton _ = Nothing
914+
859915{- ------------------------------------------------------------------------------
860916 Compat
861917-------------------------------------------------------------------------------}
862918
919+ #if __GLASGOW_HASKELL__ >= 914
920+ wrapBangTy :: HsTypeGhcPsExt -> HsType GhcPs
921+ wrapBangTy = XHsType
922+ #else
923+ wrapBangTy :: HsType GhcPs -> HsType GhcPs
924+ wrapBangTy = id
925+ #endif
926+
863927#if __GLASGOW_HASKELL__ >= 910
864928pattern LambdaExpr :: HsMatchContext fn
865929pattern LambdaExpr = LamAlt LamSingle
0 commit comments