template-haskell 2.23.0.0 → 2.24.0.0
raw patch · 9 files changed
+606/−68 lines, 9 filesdep +ghc-internaldep ~basedep ~ghc-boot-th
Dependencies added: ghc-internal
Dependency ranges changed: base, ghc-boot-th
Files
- Language/Haskell/TH/Lib.hs +66/−3
- Language/Haskell/TH/Lib/Internal.hs +0/−20
- Language/Haskell/TH/Ppr.hs +88/−6
- Language/Haskell/TH/PprLib.hs +52/−4
- Language/Haskell/TH/Quote.hs +4/−4
- Language/Haskell/TH/Syntax.hs +380/−25
- changelog.md +9/−0
- template-haskell.cabal +7/−4
- vendored-filepath/System/FilePath.hs +0/−2
Language/Haskell/TH/Lib.hs view
@@ -122,7 +122,9 @@ -- **** Pragmas ruleVar, typedRuleVar, valueAnnotation, typeAnnotation, moduleAnnotation,- pragInlD, pragSpecD, pragSpecInlD, pragSpecInstD, pragRuleD, pragAnnD,+ pragInlD, pragSpecD, pragSpecInlD,+ pragSpecED, pragSpecInlED,+ pragSpecInstD, pragRuleD, pragAnnD, pragLineD, pragCompleteD, -- **** Pattern Synonyms@@ -141,7 +143,7 @@ ) where -import GHC.Internal.TH.Lib hiding+import GHC.Boot.TH.Lib hiding ( tySynD , dataD , newtypeD@@ -181,7 +183,7 @@ , Role , InjectivityAnn )-import qualified GHC.Internal.TH.Lib as Internal+import qualified GHC.Boot.TH.Lib as Internal import Language.Haskell.TH.Syntax import Control.Applicative (Applicative(..))@@ -393,3 +395,64 @@ conP :: Quote m => Name -> [m Pat] -> m Pat conP n xs = Internal.conP n [] xs+++--------------------------------------------------------------------------------+-- * Constraint predicates (deprecated)++{-# DEPRECATED classP "As of template-haskell-2.10, constraint predicates (Pred) are just types (Type), in keeping with ConstraintKinds. Please use 'conT' and 'appT'." #-}+classP :: Quote m => Name -> [m Type] -> m Pred+classP cla tys+ = do+ tysl <- sequenceA tys+ pure (foldl AppT (ConT cla) tysl)++{-# DEPRECATED equalP "As of template-haskell-2.10, constraint predicates (Pred) are just types (Type), in keeping with ConstraintKinds. Please see 'equalityT'." #-}+equalP :: Quote m => m Type -> m Type -> m Pred+equalP tleft tright+ = do+ tleft1 <- tleft+ tright1 <- tright+ eqT <- equalityT+ pure (foldl AppT eqT [tleft1, tright1])++--------------------------------------------------------------------------------+-- * Strictness queries (deprecated)+{-# DEPRECATED isStrict+ ["Use 'bang'. See https://gitlab.haskell.org/ghc/ghc/wikis/migration/8.0. ",+ "Example usage: 'bang noSourceUnpackedness sourceStrict'"] #-}+{-# DEPRECATED notStrict+ ["Use 'bang'. See https://gitlab.haskell.org/ghc/ghc/wikis/migration/8.0. ",+ "Example usage: 'bang noSourceUnpackedness noSourceStrictness'"] #-}+{-# DEPRECATED unpacked+ ["Use 'bang'. See https://gitlab.haskell.org/ghc/ghc/wikis/migration/8.0. ",+ "Example usage: 'bang sourceUnpack sourceStrict'"] #-}+isStrict, notStrict, unpacked :: Quote m => m Strict+isStrict = bang noSourceUnpackedness sourceStrict+notStrict = bang noSourceUnpackedness noSourceStrictness+unpacked = bang sourceUnpack sourceStrict++{-# DEPRECATED strictType+ "As of @template-haskell-2.11.0.0@, 'StrictType' has been replaced by 'BangType'. Please use 'bangType' instead." #-}+strictType :: Quote m => m Strict -> m Type -> m StrictType+strictType = bangType++{-# DEPRECATED varStrictType+ "As of @template-haskell-2.11.0.0@, 'VarStrictType' has been replaced by 'VarBangType'. Please use 'varBangType' instead." #-}+varStrictType :: Quote m => Name -> m StrictType -> m VarStrictType+varStrictType = varBangType++--------------------------------------------------------------------------------+-- * Specialisation pragmas (backwards compatibility)++pragSpecD :: Quote m => Name -> m Type -> Phases -> m Dec+pragSpecD n ty phases+ = do+ ty1 <- ty+ pure $ PragmaD $ SpecialiseP n ty1 Nothing phases++pragSpecInlD :: Quote m => Name -> m Type -> Inline -> Phases -> m Dec+pragSpecInlD n ty inline phases+ = do+ ty1 <- ty+ pure $ PragmaD $ SpecialiseP n ty1 (Just inline) phases
− Language/Haskell/TH/Lib/Internal.hs
@@ -1,20 +0,0 @@-{-# LANGUAGE Safe #-}--- |--- Language.Haskell.TH.Lib.Internal exposes some additional functionality that--- is used internally in GHC's integration with Template Haskell. This is not a--- part of the public API, and as such, there are no API guarantees for this--- module from version to version.---- Why do we have both GHC.Internal.TH.Lib and--- Language.Haskell.TH.Lib? Ultimately, it's because the functions in the--- former (which are tailored for GHC's use) need different type signatures--- than the ones in the latter. Syncing up the Internal type signatures would--- involve a massive amount of breaking changes, so for the time being, we--- relegate as many changes as we can to just the Internal module, where it--- is safe to break things.--module Language.Haskell.TH.Lib.Internal- ( module GHC.Internal.TH.Lib )- where--import GHC.Internal.TH.Lib
Language/Haskell/TH/Ppr.hs view
@@ -1,9 +1,91 @@ {-# LANGUAGE Safe #-} --- | contains a prettyprinter for the--- Template Haskell datatypes-module Language.Haskell.TH.Ppr- ( module GHC.Internal.TH.Ppr )- where+{- | contains a prettyprinter for the+Template Haskell datatypes+-}+module Language.Haskell.TH.Ppr (+ appPrec,+ bar,+ bytesToString,+ commaSep,+ commaSepApplied,+ commaSepWith,+ fromTANormal,+ funPrec,+ hashParens,+ isStarT,+ isSymOcc,+ nestDepth,+ noPrec,+ opPrec,+ parensIf,+ pprBangType,+ pprBndrVis,+ pprBody,+ pprClause,+ pprCtxWith,+ pprCxt,+ pprExp,+ pprFields,+ pprFixity,+ pprForall,+ pprForall',+ pprForallVis,+ pprFunArgType,+ pprGadtRHS,+ pprGuarded,+ pprInfixExp,+ pprInfixT,+ pprLit,+ pprMatchPat,+ pprMaybeExp,+ pprNamespaceSpecifier,+ pprParendType,+ pprParendTypeArg,+ pprPat,+ pprPatSynSig,+ pprPatSynType,+ pprPrefixOcc,+ pprRecFields,+ pprStrictType,+ pprString,+ pprTyApp,+ pprTyLit,+ pprType,+ pprVarBangType,+ pprVarStrictType,+ ppr_bndrs,+ ppr_ctx_preds_with,+ ppr_cxt_preds,+ ppr_data,+ ppr_dec,+ ppr_deriv_clause,+ ppr_deriv_strategy,+ ppr_newtype,+ ppr_overlap,+ ppr_sig,+ ppr_tf_head,+ ppr_tySyn,+ ppr_type_data,+ ppr_typedef,+ pprint,+ qualPrec,+ quoteParens,+ semiSep,+ semiSepWith,+ sepWith,+ showtextl,+ sigPrec,+ split,+ unboxedSumBars,+ unopPrec,+ where_clause,+ ForallVisFlag (..),+ Ppr (..),+ PprFlag (..),+ Precedence,+ TypeArg (..),+)+where -import GHC.Internal.TH.Ppr+import GHC.Boot.TH.Ppr
Language/Haskell/TH/PprLib.hs view
@@ -1,8 +1,56 @@ {-# LANGUAGE Safe #-} -- | Monadic front-end to Text.PrettyPrint-module Language.Haskell.TH.PprLib- ( module GHC.Internal.TH.PprLib )- where+module Language.Haskell.TH.PprLib (+ ($$),+ ($+$),+ (<+>),+ (<>),+ arrow,+ braces,+ brackets,+ cat,+ char,+ colon,+ comma,+ dcolon,+ double,+ doubleQuotes,+ empty,+ equals,+ fcat,+ float,+ fsep,+ hang,+ hcat,+ hsep,+ int,+ integer,+ isEmpty,+ lbrace,+ lbrack,+ lparen,+ nest,+ parens,+ pprName,+ pprName',+ ptext,+ punctuate,+ quotes,+ rational,+ rbrace,+ rbrack,+ rparen,+ semi,+ sep,+ space,+ text,+ to_HPJ_Doc,+ vcat,+ Doc,+ PprM,+)+where -import GHC.Internal.TH.PprLib+import Prelude hiding ((<>))+import GHC.Boot.TH.PprLib
Language/Haskell/TH/Quote.hs view
@@ -17,12 +17,12 @@ ( QuasiQuoter(..) , quoteFile -- * For backwards compatibility- ,dataToQa, dataToExpQ, dataToPatQ+ , dataToQa, dataToExpQ, dataToPatQ ) where -import GHC.Internal.TH.Syntax-import GHC.Internal.TH.Quote-import GHC.Internal.TH.Lift+import GHC.Boot.TH.Syntax+import GHC.Boot.TH.Quote+import Language.Haskell.TH.Syntax (dataToQa, dataToExpQ, dataToPatQ) -- | 'quoteFile' takes a 'QuasiQuoter' and lifts it into one that read
Language/Haskell/TH/Syntax.hs view
@@ -1,26 +1,218 @@ {-# LANGUAGE MagicHash #-}-{-# LANGUAGE UnboxedTuples #-}+{-# LANGUAGE PatternSynonyms #-}+{-# LANGUAGE RankNTypes #-}+{-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TemplateHaskellQuotes #-} {-# LANGUAGE Trustworthy #-}-module Language.Haskell.TH.Syntax- ( module GHC.Internal.TH.Syntax- , makeRelativeToProject- , module GHC.Internal.TH.Lift- , addrToByteArrayName- , addrToByteArray- )+{-# LANGUAGE UnboxedTuples #-}++module Language.Haskell.TH.Syntax (+ Quote (..),+ Exp (..),+ Match (..),+ Clause (..),+ Q (..),+ Pat (..),+ Stmt (..),+ Con (..),+ Type (..),+ Dec (..),+ BangType,+ VarBangType,+ FieldExp,+ FieldPat,+ Name (..),+ FunDep (..),+ Pred,+ RuleBndr (..),+ TySynEqn (..),+ InjectivityAnn (..),+ Kind,+ Overlap (..),+ DerivClause (..),+ DerivStrategy (..),+ Code (..),+ ModName (..),+ addCorePlugin,+ addDependentFile,+ addForeignFile,+ addForeignFilePath,+ addForeignSource,+ addModFinalizer,+ addTempFile,+ addTopDecls,+ badIO,+ bindCode,+ bindCode_,+ cmpEq,+ compareBytes,+ counter,+ defaultFixity,+ eqBytes,+ extsEnabled,+ getDoc,+ getPackageRoot,+ getQ,+ get_cons_names,+ hoistCode,+ isExtEnabled,+ isInstance,+ joinCode,+ liftCode,+ location,+ lookupName,+ lookupTypeName,+ lookupValueName,+ manyName,+ maxPrecedence,+ memcmp,+ mkNameG,+ mkNameU,+ mkOccName,+ mkPkgName,+ mk_tup_name,+ mkName,+ mkNameG_v,+ mkNameG_d,+ mkNameG_tc,+ mkNameL,+ mkNameS,+ unTypeCode,+ mkModName,+ unsafeCodeCoerce,+ mkNameQ,+ mkNameG_fld,+ modString,+ nameBase,+ nameModule,+ namePackage,+ nameSpace,+ newDeclarationGroup,+ newNameIO,+ occString,+ oneName,+ pkgString,+ putDoc,+ putQ,+ recover,+ reify,+ reifyAnnotations,+ reifyConStrictness,+ reifyFixity,+ reifyInstances,+ reifyModule,+ reifyRoles,+ reifyType,+ report,+ reportError,+ reportWarning,+ runIO,+ sequenceQ,+ runQ,+ showName,+ showName',+ thenCmp,+ tupleDataName,+ tupleTypeName,+ unTypeQ,+ unboxedSumDataName,+ unboxedSumTypeName,+ unboxedTupleDataName,+ unboxedTupleTypeName,+ unsafeTExpCoerce,+ ForeignSrcLang (..),+ Extension (..),+ AnnLookup (..),+ AnnTarget (..),+ Arity,+ Bang (..),+ BndrVis (..),+ Body (..),+ Bytes (..),+ Callconv (..),+ CharPos,+ Cxt,+ DecidedStrictness (..),+ DocLoc (..),+ FamilyResultSig (..),+ Fixity (..),+ FixityDirection (..),+ Foreign (..),+ Guard (..),+ Info (..),+ Inline (..),+ InstanceDec,+ Lit (..),+ Loc (..),+ Module (..),+ ModuleInfo (..),+ NameFlavour (..),+ NameIs (..),+ NameSpace (..),+ NamespaceSpecifier (..),+ OccName (..),+ ParentName,+ PatSynArgs (..),+ PatSynDir (..),+ PatSynType,+ Phases (..),+ PkgName (..),+ Pragma (SpecialiseP, ..),+ Quasi (..),+ Range (..),+ Role (..),+ RuleMatch (..),+ Safety (..),+ SourceStrictness (..),+ SourceUnpackedness (..),+ Specificity (..),+ Strict,+ StrictType,+ SumAlt,+ SumArity,+ TExp (..),+ TyLit (..),+ TyVarBndr (..),+ TypeFamilyHead (..),+ Uniq,+ Unlifted,+ VarStrictType,+ makeRelativeToProject,+ liftString,+ Lift (..),+ dataToCodeQ,+ dataToExpQ,+ dataToPatQ,+ dataToQa,+ falseName,+ justName,+ leftName,+ liftData,+ liftDataTyped,+ nonemptyName,+ nothingName,+ rightName,+ trueName,+) where -import GHC.Internal.TH.Syntax-import GHC.Internal.TH.Lift+import GHC.Boot.TH.Lift+import GHC.Boot.TH.Syntax import System.FilePath-import Data.Array.Byte-import GHC.Exts-import GHC.ST+import Data.Data hiding (Fixity(..))+import Data.List.NonEmpty (NonEmpty(..))+import GHC.Lexeme ( startsVarSym, startsVarId ) --- This module completely re-exports 'GHC.Internal.TH.Syntax',+-- This module completely re-exports 'GHC.Boot.TH.Syntax', -- and exports additionally functions that depend on filepath. +-- |+addForeignFile :: ForeignSrcLang -> String -> Q ()+addForeignFile = addForeignSource+{-# DEPRECATED addForeignFile+ "Use 'Language.Haskell.TH.Syntax.addForeignSource' instead"+ #-} -- deprecated in 8.6+ -- | The input is a filepath, which if relative is offset by the package root. makeRelativeToProject :: FilePath -> Q FilePath makeRelativeToProject fp | isRelative fp = do@@ -28,17 +220,180 @@ return (root </> fp) makeRelativeToProject fp = return fp --- The following two defintions are copied from 'Data.Byte.Array'--- in order to preserve the old export list of 'TH.Syntax'.--- They will soon be removed as part of #24782.+trueName, falseName :: Name+trueName = 'True+falseName = 'False -addrToByteArrayName :: Name-addrToByteArrayName = 'addrToByteArray+nothingName, justName :: Name+nothingName = 'Nothing+justName = 'Just -addrToByteArray :: Int -> Addr# -> ByteArray-addrToByteArray (I# len) addr = runST $ ST $- \s -> case newByteArray# len s of- (# s', mb #) -> case copyAddrToByteArray# addr mb 0# len s' of- s'' -> case unsafeFreezeByteArray# mb s'' of- (# s''', ret #) -> (# s''', ByteArray ret #)+leftName, rightName :: Name+leftName = 'Left+rightName = 'Right +nonemptyName :: Name+nonemptyName = '(:|)++-----------------------------------------------------+--+-- Generic Lift implementations+--+-----------------------------------------------------++-- | 'dataToQa' is an internal utility function for constructing generic+-- conversion functions from types with 'Data' instances to various+-- quasi-quoting representations. See the source of 'dataToExpQ' and+-- 'dataToPatQ' for two example usages: @mkCon@, @mkLit@+-- and @appQ@ are overloadable to account for different syntax for+-- expressions and patterns; @antiQ@ allows you to override type-specific+-- cases, a common usage is just @const Nothing@, which results in+-- no overloading.+dataToQa :: forall m a k q. (Quote m, Data a)+ => (Name -> k)+ -> (Lit -> m q)+ -> (k -> [m q] -> m q)+ -> (forall b . Data b => b -> Maybe (m q))+ -> a+ -> m q+dataToQa mkCon mkLit appCon antiQ t =+ case antiQ t of+ Nothing ->+ case constrRep constr of+ AlgConstr _ ->+ appCon (mkCon funOrConName) conArgs+ where+ funOrConName :: Name+ funOrConName =+ case showConstr constr of+ "(:)" -> Name (mkOccName ":")+ (NameG DataName+ (mkPkgName "ghc-internal")+ (mkModName "GHC.Internal.Types"))+ con@"[]" -> Name (mkOccName con)+ (NameG DataName+ (mkPkgName "ghc-internal")+ (mkModName "GHC.Internal.Types"))+ con@('(':_) -> Name (mkOccName con)+ (NameG DataName+ (mkPkgName "ghc-internal")+ (mkModName "GHC.Internal.Tuple"))++ -- Tricky case: see Note [Data for non-algebraic types]+ fun@(x:_) | startsVarSym x || startsVarId x+ -> mkNameG_v tyconPkg tyconMod fun+ con -> mkNameG_d tyconPkg tyconMod con++ where+ tycon :: TyCon+ tycon = (typeRepTyCon . typeOf) t++ tyconPkg, tyconMod :: String+ tyconPkg = tyConPackage tycon+ tyconMod = tyConModule tycon++ conArgs :: [m q]+ conArgs = gmapQ (dataToQa mkCon mkLit appCon antiQ) t+ IntConstr n ->+ mkLit $ IntegerL n+ FloatConstr n ->+ mkLit $ RationalL n+ CharConstr c ->+ mkLit $ CharL c+ where+ constr :: Constr+ constr = toConstr t++ Just y -> y+++{- Note [Data for non-algebraic types]+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+Class Data was originally intended for algebraic data types. But+it is possible to use it for abstract types too. For example, in+package `text` we find++ instance Data Text where+ ...+ toConstr _ = packConstr++ packConstr :: Constr+ packConstr = mkConstr textDataType "pack" [] Prefix++Here `packConstr` isn't a real data constructor, it's an ordinary+function. Two complications++* In such a case, we must take care to build the Name using+ mkNameG_v (for values), not mkNameG_d (for data constructors).+ See #10796.++* The pseudo-constructor is named only by its string, here "pack".+ But 'dataToQa' needs the TyCon of its defining module, and has+ to assume it's defined in the same module as the TyCon itself.+ But nothing enforces that; #12596 shows what goes wrong if+ "pack" is defined in a different module than the data type "Text".+ -}++-- | A typed variant of 'dataToExpQ'.+dataToCodeQ :: (Quote m, Data a)+ => (forall b . Data b => b -> Maybe (Code m b))+ -> a -> Code m a+dataToCodeQ f = unsafeCodeCoerce . dataToExpQ (fmap unTypeCode . f)++-- | 'dataToExpQ' converts a value to a 'Exp' representation of the+-- same value, in the SYB style. It is generalized to take a function+-- override type-specific cases; see 'liftData' for a more commonly+-- used variant.+dataToExpQ :: (Quote m, Data a)+ => (forall b . Data b => b -> Maybe (m Exp))+ -> a+ -> m Exp+dataToExpQ = dataToQa varOrConE litE (foldl appE)+ where+ -- Make sure that VarE is used if the Constr value relies on a+ -- function underneath the surface (instead of a constructor).+ -- See #10796.+ varOrConE s =+ case nameSpace s of+ Just VarName -> return (VarE s)+ Just (FldName {}) -> return (VarE s)+ Just DataName -> return (ConE s)+ _ -> error $ "Can't construct an expression from name "+ ++ showName s+ appE x y = do { a <- x; b <- y; return (AppE a b)}+ litE c = return (LitE c)++-- | A typed variant of 'liftData'.+liftDataTyped :: (Quote m, Data a) => a -> Code m a+liftDataTyped = dataToCodeQ (const Nothing)++-- | 'liftData' is a variant of 'lift' in the 'Lift' type class which+-- works for any type with a 'Data' instance.+liftData :: (Quote m, Data a) => a -> m Exp+liftData = dataToExpQ (const Nothing)++-- | 'dataToPatQ' converts a value to a 'Pat' representation of the same+-- value, in the SYB style. It takes a function to handle type-specific cases,+-- alternatively, pass @const Nothing@ to get default behavior.+dataToPatQ :: (Quote m, Data a)+ => (forall b . Data b => b -> Maybe (m Pat))+ -> a+ -> m Pat+dataToPatQ = dataToQa id litP conP+ where litP l = return (LitP l)+ conP n ps =+ case nameSpace n of+ Just DataName -> do+ ps' <- sequence ps+ return (ConP n [] ps')+ _ -> error $ "Can't construct a pattern from name "+ ++ showName n++--------------------------------------------------------------------------------+-- Back-compat for Specialise pragmas++-- | Old-form specialise pragma @{ {\-\# SPECIALISE [INLINE] [phases] (var :: ty) #-} }@.+--+-- Subsumed by the more general 'SpecialiseEP' constructor.+pattern SpecialiseP :: Name -> Type -> (Maybe Inline) -> Phases -> Pragma+pattern SpecialiseP nm ty inl phases = SpecialiseEP Nothing [] (SigE (VarE nm) ty) inl phases
changelog.md view
@@ -1,5 +1,14 @@ # Changelog for [`template-haskell` package](http://hackage.haskell.org/package/template-haskell) +## 2.24.0.0++ * Introduce `dataToCodeQ` and `liftDataTyped`, typed variants of `dataToExpQ` and `liftData` respectively.++ * Remove the `Language.Haskell.TH.Lib.Internal` module. This module has long been deprecated, and exposes compiler internals.+ Users should use `Language.Haskell.TH.Lib` instead, which exposes a more stable version of this API.++ * Remove `addrToByteArrayName` and `addrToByteArray` from `Language.Haskell.TH.Syntax`. These were part of the implementation of the `Lift ByteArray` instance and were accidentally exported because this module lacked an explicit export list. They have no usages on Hackage.+ ## 2.23.0.0 * Extend `Exp` with `ForallE`, `ForallVisE`, `ConstraintedE`,
template-haskell.cabal view
@@ -3,7 +3,7 @@ -- template-haskell.cabal. name: template-haskell-version: 2.23.0.0+version: 2.24.0.0 -- NOTE: Don't forget to update ./changelog.md license: BSD3 license-file: LICENSE@@ -43,7 +43,6 @@ exposed-modules: Language.Haskell.TH Language.Haskell.TH.Lib- Language.Haskell.TH.Lib.Internal Language.Haskell.TH.Ppr Language.Haskell.TH.PprLib Language.Haskell.TH.Quote@@ -52,8 +51,12 @@ Language.Haskell.TH.CodeDo build-depends:- base >= 4.11 && < 4.22,- ghc-boot-th == 9.12.1+ base >= 4.11 && < 4.23,+ -- We don't directly depend on any of the modules from `ghc-internal`+ -- But we need to depend on it to work around a hadrian bug.+ -- See: https://gitlab.haskell.org/ghc/ghc/-/issues/25705+ ghc-internal == 9.1401.*,+ ghc-boot-th == 9.14.1 other-modules: System.FilePath
vendored-filepath/System/FilePath.hs view
@@ -1,9 +1,7 @@ -- Vendored from filepath v1.4.2.2 {-# LANGUAGE CPP #-}-#if __GLASGOW_HASKELL__ >= 704 {-# LANGUAGE Safe #-}-#endif {- | Module : System.FilePath Copyright : (c) Neil Mitchell 2005-2014