summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--compiler/GHC/ThToHs.hs4
-rw-r--r--libraries/template-haskell/Language/Haskell/TH/Lib.hs2
-rw-r--r--libraries/template-haskell/Language/Haskell/TH/Lib/Internal.hs6
-rw-r--r--libraries/template-haskell/Language/Haskell/TH/Ppr.hs2
-rw-r--r--libraries/template-haskell/Language/Haskell/TH/Syntax.hs2
-rw-r--r--libraries/template-haskell/changelog.md4
-rw-r--r--testsuite/tests/th/TH_typed1.hs7
-rw-r--r--testsuite/tests/th/TH_typed1.stdout1
-rw-r--r--testsuite/tests/th/TH_typed2.hs7
-rw-r--r--testsuite/tests/th/TH_typed2.stdout1
-rw-r--r--testsuite/tests/th/TH_typed3.hs10
-rw-r--r--testsuite/tests/th/TH_typed3.stderr9
-rw-r--r--testsuite/tests/th/TH_typed4.hs7
-rw-r--r--testsuite/tests/th/TH_typed4.stderr10
-rw-r--r--testsuite/tests/th/TH_typed5.hs10
-rw-r--r--testsuite/tests/th/TH_typed5.stdout2
-rw-r--r--testsuite/tests/th/all.T5
17 files changed, 88 insertions, 1 deletions
diff --git a/compiler/GHC/ThToHs.hs b/compiler/GHC/ThToHs.hs
index 724f15f602..39da7e0c51 100644
--- a/compiler/GHC/ThToHs.hs
+++ b/compiler/GHC/ThToHs.hs
@@ -1157,6 +1157,10 @@ cvtl e = wrapLA (cvt e)
(L noSrcSpanA (DotFieldOcc noAnn (L noSrcSpanA (FieldLabelString (fsLit f))))) }
cvt (ProjectionE xs) = return $ HsProjection noAnn $ fmap
(L noSrcSpanA . DotFieldOcc noAnn . L noSrcSpanA . FieldLabelString . fsLit) xs
+ cvt (TypedSpliceE e) = do { e' <- parenthesizeHsExpr appPrec <$> cvtl e
+ ; return $ HsTypedSplice (noAnn, noAnn) e' }
+ cvt (TypedBracketE e) = do { e' <- cvtl e
+ ; return $ HsTypedBracket noAnn e' }
{- | #16895 Ensure an infix expression's operator is a variable/constructor.
Consider this example:
diff --git a/libraries/template-haskell/Language/Haskell/TH/Lib.hs b/libraries/template-haskell/Language/Haskell/TH/Lib.hs
index b52de5b0d3..d6107f9dac 100644
--- a/libraries/template-haskell/Language/Haskell/TH/Lib.hs
+++ b/libraries/template-haskell/Language/Haskell/TH/Lib.hs
@@ -44,7 +44,7 @@ module Language.Haskell.TH.Lib (
appE, appTypeE, uInfixE, parensE, infixE, infixApp, sectionL, sectionR,
lamE, lam1E, lamCaseE, lamCasesE, tupE, unboxedTupE, unboxedSumE, condE,
multiIfE, letE, caseE, appsE, listE, sigE, recConE, recUpdE, stringE,
- fieldExp, getFieldE, projectionE,
+ fieldExp, getFieldE, projectionE, typedSpliceE, typedBracketE,
-- **** Ranges
fromE, fromThenE, fromToE, fromThenToE,
diff --git a/libraries/template-haskell/Language/Haskell/TH/Lib/Internal.hs b/libraries/template-haskell/Language/Haskell/TH/Lib/Internal.hs
index 8d0cf5adde..eeeff941fa 100644
--- a/libraries/template-haskell/Language/Haskell/TH/Lib/Internal.hs
+++ b/libraries/template-haskell/Language/Haskell/TH/Lib/Internal.hs
@@ -380,6 +380,12 @@ getFieldE e f = do
projectionE :: Quote m => NonEmpty String -> m Exp
projectionE xs = pure (ProjectionE xs)
+typedSpliceE :: Quote m => m Exp -> m Exp
+typedSpliceE = fmap TypedSpliceE
+
+typedBracketE :: Quote m => m Exp -> m Exp
+typedBracketE = fmap TypedBracketE
+
-- ** 'arithSeqE' Shortcuts
fromE :: Quote m => m Exp -> m Exp
fromE x = do { a <- x; pure (ArithSeqE (FromR a)) }
diff --git a/libraries/template-haskell/Language/Haskell/TH/Ppr.hs b/libraries/template-haskell/Language/Haskell/TH/Ppr.hs
index 034d2687b3..dbe3cb85df 100644
--- a/libraries/template-haskell/Language/Haskell/TH/Ppr.hs
+++ b/libraries/template-haskell/Language/Haskell/TH/Ppr.hs
@@ -232,6 +232,8 @@ pprExp _ (LabelE s) = text "#" <> text s
pprExp _ (ImplicitParamVarE n) = text ('?' : n)
pprExp _ (GetFieldE e f) = pprExp appPrec e <> text ('.': f)
pprExp _ (ProjectionE xs) = parens $ hcat $ map ((char '.'<>) . text) $ toList xs
+pprExp _ (TypedBracketE e) = text "[||" <> ppr e <> text "||]"
+pprExp _ (TypedSpliceE e) = text "$$" <> pprExp appPrec e
pprFields :: [(Name,Exp)] -> Doc
pprFields = sep . punctuate comma . map (\(s,e) -> pprName' Applied s <+> equals <+> ppr e)
diff --git a/libraries/template-haskell/Language/Haskell/TH/Syntax.hs b/libraries/template-haskell/Language/Haskell/TH/Syntax.hs
index 6d96d414c6..8398bafd53 100644
--- a/libraries/template-haskell/Language/Haskell/TH/Syntax.hs
+++ b/libraries/template-haskell/Language/Haskell/TH/Syntax.hs
@@ -2385,6 +2385,8 @@ data Exp
| ImplicitParamVarE String -- ^ @{ ?x }@ ( Implicit parameter )
| GetFieldE Exp String -- ^ @{ exp.field }@ ( Overloaded Record Dot )
| ProjectionE (NonEmpty String) -- ^ @(.x)@ or @(.x.y)@ (Record projections)
+ | TypedBracketE Exp -- ^ @[|| e ||]@
+ | TypedSpliceE Exp -- ^ @$$e@
deriving( Show, Eq, Ord, Data, Generic )
type FieldExp = (Name,Exp)
diff --git a/libraries/template-haskell/changelog.md b/libraries/template-haskell/changelog.md
index f6ed4d6b5f..5a62f6e124 100644
--- a/libraries/template-haskell/changelog.md
+++ b/libraries/template-haskell/changelog.md
@@ -8,6 +8,10 @@
This change enables TemplateHaskell support for `DuplicateRecordFields`.
+ * Add support for generating typed splices and brackets in untyped Template Haskell
+ Introduces `typedSpliceE :: Quote m => m Exp -> m Exp` and
+ `typedBracketE :: Quote m => m Exp -> m Exp`
+
## 2.20.0.0
* The `Ppr.pprInfixT` function has gained a `Precedence` argument.
diff --git a/testsuite/tests/th/TH_typed1.hs b/testsuite/tests/th/TH_typed1.hs
new file mode 100644
index 0000000000..f50131f88b
--- /dev/null
+++ b/testsuite/tests/th/TH_typed1.hs
@@ -0,0 +1,7 @@
+{-# LANGUAGE TemplateHaskell #-}
+
+module Main where
+
+import Language.Haskell.TH
+
+main = print $$( $(typedBracketE [| 'x' |]) )
diff --git a/testsuite/tests/th/TH_typed1.stdout b/testsuite/tests/th/TH_typed1.stdout
new file mode 100644
index 0000000000..44cf16f8da
--- /dev/null
+++ b/testsuite/tests/th/TH_typed1.stdout
@@ -0,0 +1 @@
+'x'
diff --git a/testsuite/tests/th/TH_typed2.hs b/testsuite/tests/th/TH_typed2.hs
new file mode 100644
index 0000000000..67f32766ce
--- /dev/null
+++ b/testsuite/tests/th/TH_typed2.hs
@@ -0,0 +1,7 @@
+{-# LANGUAGE TemplateHaskell #-}
+
+module Main where
+
+import Language.Haskell.TH
+
+main = print $( typedSpliceE $ typedBracketE [| 'y' |] )
diff --git a/testsuite/tests/th/TH_typed2.stdout b/testsuite/tests/th/TH_typed2.stdout
new file mode 100644
index 0000000000..5b548bb8b2
--- /dev/null
+++ b/testsuite/tests/th/TH_typed2.stdout
@@ -0,0 +1 @@
+'y'
diff --git a/testsuite/tests/th/TH_typed3.hs b/testsuite/tests/th/TH_typed3.hs
new file mode 100644
index 0000000000..b9477b27f0
--- /dev/null
+++ b/testsuite/tests/th/TH_typed3.hs
@@ -0,0 +1,10 @@
+{-# LANGUAGE TemplateHaskell #-}
+
+module Main where
+
+import Language.Haskell.TH
+
+-- test parenthesis around splice
+main = do
+ print $( typedSpliceE $ typedBracketE [| 'z' |] )
+ print $( typedSpliceE $ appE [| id |] (typedBracketE [| 'z' |]) )
diff --git a/testsuite/tests/th/TH_typed3.stderr b/testsuite/tests/th/TH_typed3.stderr
new file mode 100644
index 0000000000..bf5d8ec7c9
--- /dev/null
+++ b/testsuite/tests/th/TH_typed3.stderr
@@ -0,0 +1,9 @@
+TH_typed3.hs:9:12-53: Splicing expression
+ typedSpliceE $ typedBracketE [| 'z' |] ======> $$[|| 'z' ||]
+TH_typed3.hs:10:12-69: Splicing expression
+ typedSpliceE $ appE [| id |] (typedBracketE [| 'z' |])
+ ======>
+ $$(id [|| 'z' ||])
+TH_typed3.hs:9:12-53: Splicing expression [|| 'z' ||] ======> 'z'
+TH_typed3.hs:10:12-69: Splicing expression
+ id [|| 'z' ||] ======> 'z'
diff --git a/testsuite/tests/th/TH_typed4.hs b/testsuite/tests/th/TH_typed4.hs
new file mode 100644
index 0000000000..622b20bd2a
--- /dev/null
+++ b/testsuite/tests/th/TH_typed4.hs
@@ -0,0 +1,7 @@
+{-# LANGUAGE TemplateHaskell #-}
+
+module Main where
+
+import Language.Haskell.TH
+
+main = print $$( $$(unsafeCodeCoerce $ typedBracketE $ litE $ charL 'a' :: Code Q (Code Q Char)) )
diff --git a/testsuite/tests/th/TH_typed4.stderr b/testsuite/tests/th/TH_typed4.stderr
new file mode 100644
index 0000000000..9852f09b42
--- /dev/null
+++ b/testsuite/tests/th/TH_typed4.stderr
@@ -0,0 +1,10 @@
+TH_typed4.hs:7:20-96: Splicing expression
+ unsafeCodeCoerce $ typedBracketE $ litE $ charL 'a' ::
+ Code Q (Code Q Char)
+ ======>
+ [|| 'a' ||]
+TH_typed4.hs:7:16-98: Splicing expression
+ $$(unsafeCodeCoerce $ typedBracketE $ litE $ charL 'a' ::
+ Code Q (Code Q Char))
+ ======>
+ 'a'
diff --git a/testsuite/tests/th/TH_typed5.hs b/testsuite/tests/th/TH_typed5.hs
new file mode 100644
index 0000000000..e04b129c50
--- /dev/null
+++ b/testsuite/tests/th/TH_typed5.hs
@@ -0,0 +1,10 @@
+{-# LANGUAGE TemplateHaskell #-}
+
+module Main where
+
+import Language.Haskell.TH
+import Language.Haskell.TH.Ppr
+
+main = do
+ putStrLn =<< fmap pprint (typedSpliceE $ typedBracketE [| 'z' |])
+ putStrLn =<< fmap pprint (typedSpliceE $ appE [| id |] (typedBracketE [| 'z' |]))
diff --git a/testsuite/tests/th/TH_typed5.stdout b/testsuite/tests/th/TH_typed5.stdout
new file mode 100644
index 0000000000..62698d2161
--- /dev/null
+++ b/testsuite/tests/th/TH_typed5.stdout
@@ -0,0 +1,2 @@
+$$[||'z'||]
+$$(GHC.Base.id [||'z'||])
diff --git a/testsuite/tests/th/all.T b/testsuite/tests/th/all.T
index 20586f17b8..60f02a9c2e 100644
--- a/testsuite/tests/th/all.T
+++ b/testsuite/tests/th/all.T
@@ -559,3 +559,8 @@ test('T22819', normal, compile, ['-v0'])
test('TH_fun_par', normal, compile, [''])
test('T23036', normal, compile, ['-v0 -ddump-splices -dsuppress-uniques'])
test('T23203', normal, compile, ['-v0 -ddump-splices -dsuppress-uniques'])
+test('TH_typed1', normal, compile_and_run, [''])
+test('TH_typed2', normal, compile_and_run, [''])
+test('TH_typed3', normal, compile, ['-v0 -ddump-splices -dsuppress-uniques'])
+test('TH_typed4', normal, compile, ['-v0 -ddump-splices -dsuppress-uniques'])
+test('TH_typed5', normal, compile_and_run, [''])