[commit: ghc] master: Check if -XStaticPointers is enabled when renaming static expressions (9ff9c35)

git at git.haskell.org git at git.haskell.org
Wed Sep 13 13:44:33 UTC 2017


Repository : ssh://git@git.haskell.org/ghc

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/9ff9c35895ecc072f289c93addd1faad884bf122/ghc

>---------------------------------------------------------------

commit 9ff9c35895ecc072f289c93addd1faad884bf122
Author: Ryan Scott <ryan.gl.scott at gmail.com>
Date:   Wed Sep 13 09:37:13 2017 -0400

    Check if -XStaticPointers is enabled when renaming static expressions
    
    Summary:
    Trying to use `static` expressions without the `-XStaticPointers`
    extension enabled can lead to runtime errors. Normally, such a situation isn't
    possible, but Template Haskell provides a backdoor that allows it to happen,
    as shown in #14204.
    
    To prevent this, we ensure that `-XStaticPointers` is enabled when renaming
    `static` expressions.
    
    Test Plan: make test TEST=T14204
    
    Reviewers: facundominguez, austin, bgamari, simonpj
    
    Reviewed By: facundominguez, simonpj
    
    Subscribers: simonpj, rwbarton, thomie
    
    GHC Trac Issues: #14204
    
    Differential Revision: https://phabricator.haskell.org/D3931


>---------------------------------------------------------------

9ff9c35895ecc072f289c93addd1faad884bf122
 compiler/rename/RnExpr.hs        | 10 ++++++++++
 testsuite/tests/th/T14204.hs     |  8 ++++++++
 testsuite/tests/th/T14204.stderr |  5 +++++
 testsuite/tests/th/all.T         |  1 +
 4 files changed, 24 insertions(+)

diff --git a/compiler/rename/RnExpr.hs b/compiler/rename/RnExpr.hs
index 5ccefb8..99e3f70 100644
--- a/compiler/rename/RnExpr.hs
+++ b/compiler/rename/RnExpr.hs
@@ -375,6 +375,16 @@ wired-in. See the Notes about the NameSorts in Name.hs.
 -}
 
 rnExpr e@(HsStatic _ expr) = do
+    -- Normally, you wouldn't be able to construct a static expression without
+    -- first enabling -XStaticPointers in the first place, since that extension
+    -- is what makes the parser treat `static` as a keyword. But this is not a
+    -- sufficient safeguard, as one can construct static expressions by another
+    -- mechanism: Template Haskell (see #14204). To ensure that GHC is
+    -- absolutely prepared to cope with static forms, we check for
+    -- -XStaticPointers here as well.
+    unlessXOptM LangExt.StaticPointers $
+      addErr $ hang (text "Illegal static expression:" <+> ppr e)
+                  2 (text "Use StaticPointers to enable this extension")
     (expr',fvExpr) <- rnLExpr expr
     stage <- getStage
     case stage of
diff --git a/testsuite/tests/th/T14204.hs b/testsuite/tests/th/T14204.hs
new file mode 100644
index 0000000..e952dbd
--- /dev/null
+++ b/testsuite/tests/th/T14204.hs
@@ -0,0 +1,8 @@
+{-# LANGUAGE TemplateHaskell #-}
+module T14204 where
+
+import GHC.StaticPtr
+import Language.Haskell.TH
+
+main :: IO ()
+main = putStrLn (deRefStaticPtr $(pure (StaticE (LitE (StringL "wat")))))
diff --git a/testsuite/tests/th/T14204.stderr b/testsuite/tests/th/T14204.stderr
new file mode 100644
index 0000000..90150e2
--- /dev/null
+++ b/testsuite/tests/th/T14204.stderr
@@ -0,0 +1,5 @@
+
+T14204.hs:8:35: error:
+    • Illegal static expression: static "wat"
+        Use StaticPointers to enable this extension
+    • In the untyped splice: $(pure (StaticE (LitE (StringL "wat"))))
diff --git a/testsuite/tests/th/all.T b/testsuite/tests/th/all.T
index 1e737ac..aa973f7 100644
--- a/testsuite/tests/th/all.T
+++ b/testsuite/tests/th/all.T
@@ -394,4 +394,5 @@ test('T13856', normal, compile, ['-v0 -ddump-splices -dsuppress-uniques'])
 test('T13885', normal, compile_and_run, ['-v0'])
 test('T13887', normal, compile_and_run, ['-v0'])
 test('T13968', normal, compile_fail, ['-v0'])
+test('T14204', normal, compile_fail, ['-v0'])
 test('T14060', normal, compile_and_run, ['-v0'])



More information about the ghc-commits mailing list