Fail instead of panic-ing when qAddTopDecls has conversion error
authorMichael Sloan <mgsloan@gmail.com>
Thu, 26 Jul 2018 21:18:22 +0000 (17:18 -0400)
committerBen Gamari <ben@smart-cactus.org>
Fri, 27 Jul 2018 15:40:43 +0000 (11:40 -0400)
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

compiler/typecheck/TcSplice.hs
testsuite/tests/th/TH_invalid_add_top_decl.hs [new file with mode: 0644]
testsuite/tests/th/TH_invalid_add_top_decl.stderr [new file with mode: 0644]
testsuite/tests/th/all.T

index 31695e3..5e50dac 100644 (file)
@@ -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 (file)
index 0000000..b13d873
--- /dev/null
@@ -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 (file)
index 0000000..9124c2d
--- /dev/null
@@ -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
index cd80a6c..0fddd44 100644 (file)
@@ -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, [''])