[commit: ghc] master: Fail instead of panic-ing when qAddTopDecls has conversion error (774f366)

git at git.haskell.org git at git.haskell.org
Fri Jul 27 16:29:03 UTC 2018


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

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/774f366ebe58023fc50ba346894227b14816fe67/ghc

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

commit 774f366ebe58023fc50ba346894227b14816fe67
Author: Michael Sloan <mgsloan at gmail.com>
Date:   Thu Jul 26 17:18:22 2018 -0400

    Fail instead of panic-ing when qAddTopDecls has conversion error
    
    See https://ghc.haskell.org/trac/ghc/ticket/14627 for an example where
    GHC panics when using qAddTopDecls on [d| f = Bool |]. Instead, it
    should be a normal error message, and that's what this change is for. It
    does not entirely resolve Trac#14627, since "Illegal variable name:
    'bool'" isn't a very good error for this cirumstance.
    
    Test Plan: Manually tested.
    
    Reviewers: goldfire, bgamari
    
    Reviewed By: goldfire
    
    Subscribers: rwbarton, thomie, carter
    
    Differential Revision: https://phabricator.haskell.org/D4914


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

774f366ebe58023fc50ba346894227b14816fe67
 compiler/typecheck/TcSplice.hs                    | 4 +++-
 testsuite/tests/th/TH_invalid_add_top_decl.hs     | 8 ++++++++
 testsuite/tests/th/TH_invalid_add_top_decl.stderr | 5 +++++
 testsuite/tests/th/all.T                          | 1 +
 4 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/compiler/typecheck/TcSplice.hs b/compiler/typecheck/TcSplice.hs
index 31695e3..5e50dac 100644
--- a/compiler/typecheck/TcSplice.hs
+++ b/compiler/typecheck/TcSplice.hs
@@ -894,7 +894,9 @@ instance TH.Quasi TcM where
       l <- getSrcSpanM
       let either_hval = convertToHsDecls l thds
       ds <- case either_hval of
-              Left exn -> pprPanic "qAddTopDecls: can't convert top-level declarations" exn
+              Left exn -> failWithTc $
+                hang (text "Error in a declaration passed to addTopDecls:")
+                   2 exn
               Right ds -> return ds
       mapM_ (checkTopDecl . unLoc) ds
       th_topdecls_var <- fmap tcg_th_topdecls getGblEnv
diff --git a/testsuite/tests/th/TH_invalid_add_top_decl.hs b/testsuite/tests/th/TH_invalid_add_top_decl.hs
new file mode 100644
index 0000000..b13d873
--- /dev/null
+++ b/testsuite/tests/th/TH_invalid_add_top_decl.hs
@@ -0,0 +1,8 @@
+{-# LANGUAGE TemplateHaskell #-}
+import Language.Haskell.TH
+import Language.Haskell.TH.Syntax
+
+$(do
+  invalidDecl <- valD (varP (mkName "emptyDo")) (normalB (doE [])) []
+  addTopDecls [invalidDecl]
+  return [])
diff --git a/testsuite/tests/th/TH_invalid_add_top_decl.stderr b/testsuite/tests/th/TH_invalid_add_top_decl.stderr
new file mode 100644
index 0000000..9124c2d
--- /dev/null
+++ b/testsuite/tests/th/TH_invalid_add_top_decl.stderr
@@ -0,0 +1,5 @@
+
+TH_invalid_add_top_decl.hs:5:3:
+    Error in a declaration passed to addTopDecls:
+      Empty stmt list in do-block
+      When splicing a TH declaration: emptyDo = do
diff --git a/testsuite/tests/th/all.T b/testsuite/tests/th/all.T
index cd80a6c..0fddd44 100644
--- a/testsuite/tests/th/all.T
+++ b/testsuite/tests/th/all.T
@@ -424,3 +424,4 @@ test('T9693', expect_broken(9693), ghci_script, ['T9693.script'])
 test('T14471', normal, compile, [''])
 test('TH_rebindableAdo', normal, compile, [''])
 test('T14627', normal, compile_fail, [''])
+test('TH_invalid_add_top_decl', normal, compile_fail, [''])



More information about the ghc-commits mailing list